-
Notifications
You must be signed in to change notification settings - Fork 5.6k
[INFRA-779] fix(security): reject authority-relative next_path redirects #9709
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
mguptahub
wants to merge
4
commits into
preview
Choose a base branch
from
infra-779/next-path-authority-relative-reject
base: preview
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
4 commits
Select commit
Hold shift + click to select a range
8537b6e
[INFRA-779] fix(security): reject authority-relative next_path redirects
mguptahub 8383411
[INFRA-779] strip tab/CR/LF from next_path before the authority-relat…
mguptahub 1241037
[INFRA-779] use str.translate instead of a char-by-char rebuild for t…
mguptahub 43f4dc0
[INFRA-779] delegate isValidURL to the shared isValidNextPath instead…
mguptahub 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,92 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| """Regression test for authority-relative open-redirect via next_path. | ||
|
|
||
| Root cause: urlparse("///example.com/") returns both scheme and netloc | ||
| empty (a quirk of Python's URL parser for exactly-three-or-more leading | ||
| slashes), so validate_next_path's "extract only the path component" branch | ||
| (gated on scheme or netloc being truthy) never fires, and the original, | ||
| unmodified "///example.com/" string passes every remaining check unchanged. | ||
| Browsers still resolve a leading "//" as authority-relative against an | ||
| http(s) base, so the accepted value silently navigates off-domain. | ||
|
|
||
| Fixed by rejecting any next_path starting with "//" outright, after the | ||
| existing "must start with /" check. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from plane.utils.path_validator import validate_next_path | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
|
|
||
| class TestValidateNextPathAuthorityRelative: | ||
| @pytest.mark.parametrize( | ||
| # Exactly three or more leading slashes: urlparse() returns both | ||
| # scheme and netloc empty for these (the actual bug — verified | ||
| # directly against Python's urlparse before writing this fix), so | ||
| # the existing "extract only the path component" branch never fires | ||
| # and the raw, still-dangerous string must be caught by the new | ||
| # explicit "//" check instead. | ||
| "malicious_next_path", | ||
| [ | ||
| "///example.com/", | ||
| "////example.com/", | ||
| "/////example.com/", | ||
| ], | ||
| ) | ||
| def test_rejects_authority_relative_paths_urlparse_misses(self, malicious_next_path): | ||
| assert validate_next_path(malicious_next_path) == "", ( | ||
| f"{malicious_next_path!r} must be rejected — a browser resolves a leading '//' " | ||
| "as authority-relative and navigates off-domain regardless of what urlparse() made of it" | ||
| ) | ||
|
|
||
| def test_exactly_two_slashes_was_already_safely_downgraded(self): | ||
| """Positive control: urlparse() DOES detect a netloc for exactly two | ||
| leading slashes, so the pre-existing branch already strips this down | ||
| to a harmless same-origin path — this case never needed the new | ||
| check and must keep working exactly as before.""" | ||
| assert validate_next_path("//example.com/") == "/" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "safe_next_path", | ||
| [ | ||
| "/workspace/abc", | ||
| "/", | ||
| "/projects/123/issues", | ||
| ], | ||
| ) | ||
| def test_accepts_genuine_relative_paths(self, safe_next_path): | ||
| assert validate_next_path(safe_next_path) == safe_next_path | ||
|
|
||
| def test_still_downgrades_absolute_urls_with_a_scheme_to_a_safe_path(self): | ||
| """Positive control: the pre-existing scheme/netloc branch already | ||
| strips the host from a fully-qualified URL, leaving only a harmless | ||
| same-origin path — this fix must not change that behavior.""" | ||
| assert validate_next_path("https://evil.com/phish") == "/phish" | ||
| assert validate_next_path("http://evil.com/phish") == "/phish" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| # A tab between each slash defeats both urlparse()'s own netloc | ||
| # detection (verified directly: "/\t/\t/evil.com" -> scheme='', | ||
| # netloc='') AND a literal next_path.startswith("//") check, since | ||
| # the second character is a tab, not a slash. Per the WHATWG URL | ||
| # spec, browsers strip every ASCII tab/CR/LF from a URL before | ||
| # parsing it, so what the browser actually navigates on is | ||
| # "///evil.com" — authority-relative, off-origin — even though this | ||
| # function never sees a literal "//" prefix. | ||
| "obfuscated_next_path", | ||
| [ | ||
| "/\t/\t/evil.com", | ||
| "/\r/\r/evil.com", | ||
| "/\n/\n/evil.com", | ||
| ], | ||
| ) | ||
| def test_rejects_tab_cr_lf_obfuscated_authority_relative_paths(self, obfuscated_next_path): | ||
| assert validate_next_path(obfuscated_next_path) == "", ( | ||
| f"{obfuscated_next_path!r} must be rejected — browsers strip tab/CR/LF before parsing, " | ||
| "so this collapses to an authority-relative '///evil.com' navigation" | ||
| ) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.