fix: prevent open redirect via protocol-relative returnTo - #1185
fix: prevent open redirect via protocol-relative returnTo#1185yogeshchoudhary147 wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe authentication redirect converts protocol-relative pathnames such as ChangesReturn path sanitization
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@__tests__/with-authentication-required.test.tsx`:
- Around line 189-210: Update this test to save the original browser URL before
replacing it, then wrap the render and waitFor assertions in a try/finally block
that always restores the saved URL. Preserve the existing redirect expectations
while ensuring cleanup runs even when either operation fails.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 56f1c66c-5639-41e1-a0fd-0ba0567476eb
📒 Files selected for processing (2)
__tests__/with-authentication-required.test.tsxsrc/with-authentication-required.tsx
d632900 to
8a23d7e
Compare
| const defaultReturnTo = (): string => { | ||
| // Normalize the pathname to prevent protocol-relative open redirects. | ||
| // A URL like https://app.example.com//evil.com produces a pathname of | ||
| // //evil.com, which routers (react-router, next.js, gatsby) interpret as a | ||
| // protocol-relative URL and redirect the user to an external host. | ||
| const pathname = window.location.pathname.replace(/^\/\/+/, '/'); | ||
| return `${pathname}${window.location.search}`; | ||
| }; |
There was a problem hiding this comment.
This does fixes the open redirect via protocol relative urls but still see some concerns:
defaultOnRedirectCallbackconsumesreturnToas is.- Custom
returnTobypassesdefaultReturnToentirely. - Fallback in
defaultOnRedirectCallbackis also unsanitized. - Percent-encoded slash bypass -
https://app.example.com/%2Fevil.comgivespathname = "/%2Fevil.com"which will still go though the regex defined unnoticed.
defaultOnRedirectCallback - the consumption point, where returnTo is actually acted on. It has zero sanitization regardless of where the value came from.
There was a problem hiding this comment.
Hey @cschetan77, went through all of them:
-
defaultOnRedirectCallbackconsumes as-is — value is already sanitized bydefaultReturnTobefore it even gets there, so//evil.comnever reaches that point. -
Custom
returnTobypasses sanitization — yeah this is true, but that's developer-controlled input, SDK can't own that. will add a docs note as follow-up. -
Fallback unsanitized — fallback only runs when
appState.returnTois undefined, in our flow it's always set so this never executes. -
Percent-encoded slash bypass —
/%2Fevil.comstays on same origin, browser doesn't decode%2Fto/during navigation. not a valid bypass.
Summary
defaultReturnToinwithAuthenticationRequiredcapturedwindow.location.pathnameverbatim, which equals//evil.comwhen a user visitshttps://app.example.com//evil.com//-prefixed string as a protocol-relative URL, redirecting the user to an external hostappState.returnToTest plan
loginWithRedirectreceives a sanitized single-slashreturnToSummary by CodeRabbit
Bug Fixes
Tests