Skip to content

Commit c74d82c

Browse files
authored
fix: validate path is safe for creates (#1469)
1 parent 6881802 commit c74d82c

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/services/directoryServices/ResourceDirectoryService.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const {
77
} = require("@utils/markdown-utils")
88
const { slugifyCollectionName } = require("@utils/utils")
99

10+
const { isSafePath } = require("@root/validators/validators")
11+
1012
const INDEX_FILE_NAME = "index.html"
1113

1214
class ResourceDirectoryService {
@@ -79,7 +81,13 @@ class ResourceDirectoryService {
7981
sessionData,
8082
{ resourceRoomName, resourceCategoryName }
8183
) {
82-
if (/[^a-zA-Z0-9- ]/g.test(resourceCategoryName)) {
84+
if (
85+
/[^a-zA-Z0-9- ]/g.test(resourceCategoryName) ||
86+
!isSafePath(
87+
`/${resourceRoomName}/${resourceCategoryName}`,
88+
`/${resourceRoomName}`
89+
)
90+
) {
8391
// Contains non-allowed characters
8492
throw new BadRequestError(
8593
"Special characters not allowed in resource category name"
@@ -111,7 +119,13 @@ class ResourceDirectoryService {
111119
githubSessionData,
112120
{ resourceRoomName, resourceCategoryName, newDirectoryName }
113121
) {
114-
if (/[^a-zA-Z0-9- ]/g.test(newDirectoryName)) {
122+
if (
123+
/[^a-zA-Z0-9- ]/g.test(newDirectoryName) ||
124+
!isSafePath(
125+
`/${resourceRoomName}/${newDirectoryName}`,
126+
`/${resourceRoomName}`
127+
)
128+
) {
115129
// Contains non-allowed characters
116130
throw new BadRequestError(
117131
"Special characters not allowed in resource category name"

src/services/fileServices/MdPageServices/ResourcePageService.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const {
55
convertDataToMarkdown,
66
} = require("@utils/markdown-utils")
77

8-
const { hasSpecialCharInTitle, isDateValid } = require("@validators/validators")
8+
const {
9+
hasSpecialCharInTitle,
10+
isDateValid,
11+
isSafePath,
12+
} = require("@validators/validators")
913

1014
class ResourcePageService {
1115
constructor({ gitHubService }) {
@@ -16,7 +20,7 @@ class ResourcePageService {
1620
const fileNameArray = fileName.split(".md")[0]
1721
const tokenArray = fileNameArray.split("-")
1822
const date = tokenArray.slice(0, 3).join("-")
19-
if (!isDateValid(date))
23+
if (!isDateValid(date) || !isSafePath(`/${fileName}`, "/"))
2024
throw new BadRequestError("Special characters not allowed in file name")
2125

2226
const type = ["file", "post", "link"].includes(tokenArray[3])

src/services/fileServices/MdPageServices/SubcollectionPageService.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {
66
} = require("@utils/markdown-utils")
77
const { deslugifyCollectionName } = require("@utils/utils")
88

9-
const { hasSpecialCharInTitle } = require("@validators/validators")
9+
const { hasSpecialCharInTitle, isSafePath } = require("@validators/validators")
1010

1111
class SubcollectionPageService {
1212
constructor({ gitHubService, collectionYmlService }) {
@@ -27,7 +27,11 @@ class SubcollectionPageService {
2727
) {
2828
if (
2929
!shouldIgnoreCheck &&
30-
hasSpecialCharInTitle({ title: fileName, isFile: true })
30+
(hasSpecialCharInTitle({ title: fileName, isFile: true }) ||
31+
!isSafePath(
32+
`/${collectionName}/${subcollectionName}/${fileName}`,
33+
`/${collectionName}`
34+
))
3135
)
3236
throw new BadRequestError(
3337
`Special characters not allowed when creating files. Given name: ${fileName}`
@@ -117,7 +121,13 @@ class SubcollectionPageService {
117121
sha,
118122
}
119123
) {
120-
if (hasSpecialCharInTitle({ title: newFileName, isFile: true }))
124+
if (
125+
hasSpecialCharInTitle({ title: newFileName, isFile: true }) ||
126+
!isSafePath(
127+
`/${collectionName}/${subcollectionName}/${newFileName}`,
128+
`/${collectionName}`
129+
)
130+
)
121131
throw new BadRequestError(
122132
`Special characters not allowed when renaming files. Given name: ${newFileName}`
123133
)

0 commit comments

Comments
 (0)