Skip to content

Commit b119f9d

Browse files
authored
fix: prevent path traversal for API endpoint URL (#1950)
* fix: prevent path traversal for API endpoint URL * fix: switch to check for existence of slashes
1 parent a9048bc commit b119f9d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/services/PageService.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { isSafePath } = require("../utils/misc")
2+
13
export class PageService {
24
constructor({ apiClient }) {
35
this.apiClient = apiClient
@@ -12,6 +14,22 @@ export class PageService {
1214
resourceCategoryName,
1315
fileName,
1416
}) {
17+
// Check the input parameters to ensure the paths are safe
18+
const paramsToCheck = [
19+
siteName,
20+
collectionName,
21+
subCollectionName,
22+
resourceRoomName,
23+
resourceCategoryName,
24+
fileName,
25+
]
26+
27+
paramsToCheck.forEach((param) => {
28+
if (param && !isSafePath(param)) {
29+
throw new Error(`Unsafe path detected in parameter: ${param}`)
30+
}
31+
})
32+
1533
let endpoint = `/sites/${siteName}`
1634
if (collectionName) {
1735
endpoint += `/collections/${collectionName}`

src/utils/misc.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@ export const isLinkInternal = (url: string) => {
77
tempLink.href = url
88
return tempLink.hostname === window.location.hostname
99
}
10+
11+
// Util method to check if a URL path is safe
12+
export const isSafePath = (path: string): boolean => {
13+
if (path.indexOf("\\") !== -1) {
14+
return false
15+
}
16+
17+
return true
18+
}

0 commit comments

Comments
 (0)