fix: prevent open redirect in OAuth callback (CWE-601)#15
Closed
fix: prevent open redirect in OAuth callback (CWE-601)#15
Conversation
`handleCallback` wrote the OAuth state's `returnPathname` straight into a `Location` header. The state is attacker-influenceable, so crafted values (`https://evil.com`, `//evil.com`, `/\evil.com`) let an attacker redirect victims off-origin after a successful sign-in — a classic credential-phishing primitive. Port the authkit-nextjs mitigation: parse `returnPathname` against a throwaway origin so the WHATWG URL parser strips host/scheme/backslash tricks, then emit a RELATIVE Location (`pathname + search + hash`) with a leading-slash normalization that kills the `//evil.com` protocol- relative edge case. Relative (not absolute) keeps us safe behind proxies that don't reconstruct `event.url`'s public origin, and the hash fragment is preserved so `/dashboard#billing` still lands correctly. Adds 10 tests covering attack payloads, query/hash preservation, and a proxied-origin scenario.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
handleCallbackwas writing the OAuth state'sreturnPathnamedirectly into theLocationheader. Becausestateround-trips through the IdP and is attacker-influenceable, crafted values (https://evil.com,//evil.com,/\evil.com) redirected victims off-origin after a successful sign-in — a credential-phishing primitive (CWE-601).returnPathnameagainst a throwaway origin so the WHATWG URL parser strips any host, scheme, or backslash trick, then emit a relativeLocationbuilt frompathname + search + hashwith a leading-slash normalization that defuses the//evil.comprotocol-relative case.Locationavoids a deployment footgun: behind proxies/TLS terminators that don't rewriteevent.url's origin, an absolute Location would leak an internal backend host. The browser resolves the relative path against the public callback URL instead./dashboard#billingstill lands on the right anchor / client-side route.