Skip to content

Commit 50a510c

Browse files
committed
Use safeDecodeURIComponent
1 parent bbd1ad0 commit 50a510c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

library/vulnerabilities/path-traversal/checkUrlPathForPathTraversal.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ t.test("it does not detect", async (t) => {
5151
).found,
5252
false
5353
);
54+
t.equal(
55+
checkUrlPathForPathTraversal("https://example.com/path/to/resource/%C3%A4")
56+
.found,
57+
false
58+
);
59+
60+
// Invalid url encoded characters
61+
t.equal(
62+
checkUrlPathForPathTraversal("https://example.com/path/to/resource/%a")
63+
.found,
64+
false
65+
);
5466
});
5567

5668
t.test("only detect in path segments", async (t) => {

library/vulnerabilities/path-traversal/checkUrlPathForPathTraversal.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getRawUrlPath } from "../../helpers/getRawUrlPath";
2+
import { safeDecodeURIComponent } from "../../helpers/safeDecodeURIComponent";
23
import { normalizeLikeURLConstructor } from "./normalizeLikeURLConstructor";
34

45
const forbiddenPattern = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
@@ -43,9 +44,15 @@ export function checkUrlPathForPathTraversal(url: string | undefined): {
4344
}
4445

4546
// Also check encoded paths
46-
const decodedPath = normalizeLikeURLConstructor(decodeURIComponent(rawPath));
47+
const decodedPath = safeDecodeURIComponent(rawPath);
48+
if (!decodedPath) {
49+
return {
50+
found: false,
51+
};
52+
}
4753

48-
if (forbiddenPattern.test(decodedPath)) {
54+
const normalizedDecodedPath = normalizeLikeURLConstructor(decodedPath);
55+
if (forbiddenPattern.test(normalizedDecodedPath)) {
4956
return {
5057
found: true,
5158
payload: rawPath,

0 commit comments

Comments
 (0)