Skip to content
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

fix: open redirect issue with trailing slash detection #2313

Merged
merged 5 commits into from
Feb 14, 2024

Conversation

marvinhagemeister
Copy link
Collaborator

@marvinhagemeister marvinhagemeister commented Feb 14, 2024

Passing a path like /foo/bar as the location header is interpreted as a relative path. But when it contains two leading slashes like //evil.com/ it is interpreted as a port relative url. This has the consequence that the origin of a URL can essentially be replaced:

new URL("//evil.com", "https://example.com/");
// -> 'https://evil.com/'

Our trailing slash detection logic didn't guard against this case. By normalizing the pathname of the URL and stripping sibling forward slashes we sidestep this problem.

Copy link
Member

@crowlKats crowlKats left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@marvinhagemeister marvinhagemeister merged commit aeca15e into main Feb 14, 2024
12 checks passed
@marvinhagemeister marvinhagemeister deleted the url-redirect branch February 14, 2024 14:25
Comment on lines +190 to +194
// Syntactically having double slashes in the pathname is valid per
// spec, but there is no behavior defined for that. Practically all
// servers normalize the pathname of a URL to not include double
// forward slashes.
url.pathname = url.pathname.replaceAll(/\/+/g, "/");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also check further down whether the original pathname matches the changed pathname? Because if it doesnt (there were double slashes), we should redirect to the normalized variant. Otherwise there will be a weird edge case where this logic matches as if there are no double slashes, but when a user does new URL(req.url), they still get the "old" non-normalized URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants