-
Notifications
You must be signed in to change notification settings - Fork 21
Block request if URL path contains path traversal patterns #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
timokoessler
wants to merge
8
commits into
main
Choose a base branch
from
url-path-traversal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f320cd8
Improve reliability of path traversal detection
timokoessler 9d3e7dc
Fix wrong payload path
timokoessler 12a85bc
Add normalizeLikeURLConstructor
timokoessler bbd1ad0
Handle tab chars in url
timokoessler 50a510c
Use safeDecodeURIComponent
timokoessler 585bde8
Merge branch 'main' into url-path-traversal
timokoessler dffa405
Merge branch 'main' into url-path-traversal
timokoessler b5357b4
Fix code quality comment
timokoessler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import * as t from "tap"; | ||
| import { getRawUrlPath } from "./getRawUrlPath"; | ||
|
|
||
| t.test("it returns the raw URL path", async (t) => { | ||
| t.equal(getRawUrlPath(""), "/"); | ||
| t.equal(getRawUrlPath("/"), "/"); | ||
| t.equal(getRawUrlPath("/?test=abc"), "/"); | ||
| t.equal(getRawUrlPath("#"), "/"); | ||
| t.equal(getRawUrlPath("https://example.com"), "/"); | ||
|
|
||
| t.equal( | ||
| getRawUrlPath("https://example.com/path/to/resource"), | ||
| "/path/to/resource" | ||
| ); | ||
| t.equal( | ||
| getRawUrlPath("http://example.com/path/to/resource/"), | ||
| "/path/to/resource/" | ||
| ); | ||
| t.equal( | ||
| getRawUrlPath("https://example.com/path/to/resource/123"), | ||
| "/path/to/resource/123" | ||
| ); | ||
| t.equal( | ||
| getRawUrlPath("https://example.com/path/to/resource/123/456"), | ||
| "/path/to/resource/123/456" | ||
| ); | ||
| t.equal( | ||
| getRawUrlPath("https://example.com/path/to/resource/123/456/789"), | ||
| "/path/to/resource/123/456/789" | ||
| ); | ||
| t.equal( | ||
| getRawUrlPath( | ||
| "https://example.com/path/to/resource/123/456/789?query=string" | ||
| ), | ||
| "/path/to/resource/123/456/789" | ||
| ); | ||
| t.equal( | ||
| getRawUrlPath("https://example.com/path/to/resource/123/456/789#fragment"), | ||
| "/path/to/resource/123/456/789" | ||
| ); | ||
| t.equal( | ||
| getRawUrlPath( | ||
| "https://example.com/path/to/resource/123/456/789?query=string#fragment" | ||
| ), | ||
| "/path/to/resource/123/456/789" | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export function getRawUrlPath(url: string): string { | ||
| let partialUrl = url; | ||
|
|
||
| // Remove protocol (http://, https://, etc.) | ||
| const pathStart = partialUrl.indexOf("://"); | ||
| if (pathStart !== -1) partialUrl = partialUrl.slice(pathStart + 3); | ||
|
|
||
| // Remove hostname and port | ||
| const slashIndex = partialUrl.indexOf("/"); | ||
| if (slashIndex === -1) return "/"; // only hostname given | ||
| partialUrl = partialUrl.slice(slashIndex); | ||
|
|
||
| // Remove query and fragment | ||
| const queryIndex = partialUrl.indexOf("?"); | ||
| const hashIndex = partialUrl.indexOf("#"); | ||
|
|
||
| let endIndex = partialUrl.length; | ||
| if (queryIndex !== -1) endIndex = Math.min(endIndex, queryIndex); | ||
| if (hashIndex !== -1) endIndex = Math.min(endIndex, hashIndex); | ||
|
|
||
| return partialUrl.slice(0, endIndex) || "/"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
library/vulnerabilities/path-traversal/checkUrlPathForPathTraversal.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import * as t from "tap"; | ||
| import { checkUrlPathForPathTraversal } from "./checkUrlPathForPathTraversal"; | ||
|
|
||
| t.test("it does not detect", async (t) => { | ||
| t.equal(checkUrlPathForPathTraversal("").found, false); | ||
| t.equal(checkUrlPathForPathTraversal("").payload, undefined); | ||
| t.equal(checkUrlPathForPathTraversal("abc").found, false); | ||
| t.equal(checkUrlPathForPathTraversal("/").found, false); | ||
| t.equal(checkUrlPathForPathTraversal("/abc").found, false); | ||
| t.equal(checkUrlPathForPathTraversal("/a?test=123").found, false); | ||
|
|
||
| t.equal( | ||
| checkUrlPathForPathTraversal("https://example.com/path/to/resource").found, | ||
| false | ||
| ); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal("https://example.com/path/to/resource/").found, | ||
| false | ||
| ); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal("https://example.com/path/to/resource/123") | ||
| .found, | ||
| false | ||
| ); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal("https://example.com/path/to/resource/123/456") | ||
| .found, | ||
| false | ||
| ); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal( | ||
| "https://example.com/path/to/resource/123/456/789" | ||
| ).found, | ||
| false | ||
| ); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal( | ||
| "https://example.com/path/to/resource/123/456/789?query=string" | ||
| ).found, | ||
| false | ||
| ); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal( | ||
| "https://example.com/path/to/resource/123/456/789#fragment" | ||
| ).found, | ||
| false | ||
| ); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal( | ||
| "https://example.com/path/to/resource/123/456/789?query=string#fragment" | ||
| ).found, | ||
| false | ||
| ); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal("https://example.com/path/to/resource/%C3%A4") | ||
| .found, | ||
| false | ||
| ); | ||
|
|
||
| // Invalid url encoded characters | ||
| t.equal( | ||
| checkUrlPathForPathTraversal("https://example.com/path/to/resource/%a") | ||
| .found, | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| t.test("only detect in path segments", async (t) => { | ||
| t.equal(checkUrlPathForPathTraversal("/test#/../").found, false); | ||
| t.equal(checkUrlPathForPathTraversal("/test?param=/../etc").found, false); // Query parameters are checked differently | ||
| }); | ||
|
|
||
| t.test("it detects", async (t) => { | ||
| t.equal(checkUrlPathForPathTraversal("/..").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/../").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/..").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/..").payload, "/abc/.."); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/../def").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/def/..").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/def/../ghi").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/def/../../ghi").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/def/../../../ghi").found, true); | ||
|
|
||
| // With backslashes | ||
| t.equal(checkUrlPathForPathTraversal("/..\\").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/..\\\\").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/..\\").found, true); | ||
|
|
||
| // With URL encoding | ||
| t.equal(checkUrlPathForPathTraversal("/%2E%2E").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/%2E%2E/").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/%2E%2E").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/%2E%2E").payload, "/abc/%2E%2E"); | ||
|
|
||
| // With space characters | ||
| t.equal(checkUrlPathForPathTraversal("/.%09./").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/.%0a./def").found, true); | ||
| t.equal(checkUrlPathForPathTraversal("/abc/.%0D./def").found, true); | ||
| t.equal( | ||
| checkUrlPathForPathTraversal("/abc/.%0D./def").payload, | ||
| "/abc/.%0D./def" | ||
| ); | ||
| }); |
65 changes: 65 additions & 0 deletions
65
library/vulnerabilities/path-traversal/checkUrlPathForPathTraversal.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { getRawUrlPath } from "../../helpers/getRawUrlPath"; | ||
| import { safeDecodeURIComponent } from "../../helpers/safeDecodeURIComponent"; | ||
| import { normalizeLikeURLConstructor } from "./normalizeLikeURLConstructor"; | ||
|
|
||
| const forbiddenPattern = /(?:^|[\\/])\.\.(?:[\\/]|$)/; | ||
|
|
||
| /** | ||
| * Check if the URL path contains a path traversal attack | ||
| * A legitimate URL path should not contain ".." or "/../" or "\..\" or "\..\" | ||
| * https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4 | ||
| */ | ||
| export function checkUrlPathForPathTraversal(url: string | undefined): { | ||
| found: boolean; | ||
| payload?: string; | ||
| } { | ||
| if (!url || url.length < 3) { | ||
| // Performance optimization, url most include at least 3 characters (/..) | ||
| return { | ||
| found: false, | ||
| }; | ||
| } | ||
|
|
||
| const rawPath = getRawUrlPath(url); | ||
| if (rawPath.length < 3) { | ||
| // Performance optimization, we don't need to check for path traversal if the path is less than 3 characters | ||
| // Can happen if the URL has a query string | ||
| return { | ||
| found: false, | ||
| }; | ||
| } | ||
|
|
||
| if (forbiddenPattern.test(rawPath)) { | ||
| return { | ||
| found: true, | ||
| payload: rawPath, | ||
| }; | ||
| } | ||
|
|
||
| if (!url.includes("%")) { | ||
hansott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Performance optimization, if the URL does not contain any encoded characters, we don't need to decode it | ||
| return { | ||
| found: false, | ||
| }; | ||
| } | ||
|
|
||
| // Also check encoded paths | ||
timokoessler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const decodedPath = safeDecodeURIComponent(rawPath); | ||
| if (!decodedPath) { | ||
| return { | ||
| found: false, | ||
| }; | ||
| } | ||
|
|
||
| const normalizedDecodedPath = normalizeLikeURLConstructor(decodedPath); | ||
| if (forbiddenPattern.test(normalizedDecodedPath)) { | ||
| return { | ||
| found: true, | ||
| payload: rawPath, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| found: false, | ||
| }; | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
library/vulnerabilities/path-traversal/normalizeLikeURLConstructor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * This function is used for urls, because they can contain a TAB, carriage return or line feed that is silently removed by the URL constructor. | ||
| * | ||
| * The WHATWG URL spec defines the following: | ||
| * - Remove all ASCII tab or newline from input. | ||
| * - An ASCII tab or newline is U+0009 TAB, U+000A LF, or U+000D CR. | ||
| * | ||
| * Also, backslashes are converted to forward slashes by the URL constructor. | ||
| * | ||
| * See https://url.spec.whatwg.org/#url-parsing | ||
| */ | ||
| export function normalizeLikeURLConstructor(url: string): string { | ||
| return url.replace(/[\t\n\r]/g, "").replace(/\\/g, "/"); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function handles multiple unrelated security concerns (IP blocking, user agent blocking, path traversal) in 185 lines.
Feedback
Post a comment with the following structure to provide feedback on this finding:
Aikido will process this feedback into learnings to give better review comments in the future.
More info