-
Notifications
You must be signed in to change notification settings - Fork 31
fix(auth): stop the /login redirect loop left by a dead Auth0 session #2906
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
Astach
wants to merge
1
commit into
staging
Choose a base branch
from
fix/login-redirect-loop-on-stale-session
base: staging
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
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
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
42 changes: 42 additions & 0 deletions
42
libs/shared/routes/src/lib/sub-router/login.router.spec.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,42 @@ | ||
| import { SESSION_EXPIRED_REASON, getSafeRedirect, isLoginPath, shouldRedirectAuthenticatedUser } from './login.router' | ||
|
|
||
| describe('login router', () => { | ||
| describe('isLoginPath', () => { | ||
| it.each(['/login', '/login/auth0-callback'])('should match %s', (pathname) => { | ||
| expect(isLoginPath(pathname)).toBe(true) | ||
| }) | ||
|
|
||
| it.each(['/', '/organization/123', '/loginish'])('should not match %s', (pathname) => { | ||
| expect(isLoginPath(pathname)).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getSafeRedirect', () => { | ||
| it('should fall back to the root when there is no redirect', () => { | ||
| expect(getSafeRedirect(undefined)).toBe('/') | ||
| expect(getSafeRedirect('')).toBe('/') | ||
| }) | ||
|
|
||
| it('should refuse a redirect that points back at the login page', () => { | ||
| expect(getSafeRedirect('/login?redirect=%2F')).toBe('/') | ||
| }) | ||
|
|
||
| it('should keep an in-app redirect', () => { | ||
| expect(getSafeRedirect('/organization/123/overview')).toBe('/organization/123/overview') | ||
| }) | ||
| }) | ||
|
|
||
| describe('shouldRedirectAuthenticatedUser', () => { | ||
| it('should send an authenticated visitor back into the app', () => { | ||
| expect(shouldRedirectAuthenticatedUser({ isAuthenticated: true })).toBe(true) | ||
| }) | ||
|
|
||
| it('should keep an anonymous visitor on the login page', () => { | ||
| expect(shouldRedirectAuthenticatedUser({ isAuthenticated: false })).toBe(false) | ||
| }) | ||
|
|
||
| it('should keep a visitor whose session expired on the login page even though Auth0 still reports them as authenticated', () => { | ||
| expect(shouldRedirectAuthenticatedUser({ isAuthenticated: true, reason: SESSION_EXPIRED_REASON })).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -1,3 +1,31 @@ | ||
| export const LOGIN_URL = '/login' | ||
| export const LOGIN_AUTH_REDIRECT_URL = '/auth0-callback' | ||
| export const LOGOUT_URL = '/logout' | ||
|
|
||
| // Marks a redirect to /login that was forced by an unusable session. The login page must not | ||
| // bounce such a visit back into the app: Auth0 caches the user in localStorage without an expiry | ||
| // check, so `isAuthenticated` can stay true for a session that can no longer mint a token, and | ||
| // the two redirects chase each other forever. | ||
| export const SESSION_EXPIRED_REASON = 'session-expired' | ||
|
|
||
| export function isLoginPath(pathname: string) { | ||
| return pathname === LOGIN_URL || pathname.startsWith(`${LOGIN_URL}/`) | ||
| } | ||
|
|
||
| export function getSafeRedirect(redirectPath?: string) { | ||
| if (!redirectPath || redirectPath.startsWith(LOGIN_URL)) { | ||
| return '/' | ||
| } | ||
|
|
||
| return redirectPath | ||
| } | ||
|
|
||
| export function shouldRedirectAuthenticatedUser({ | ||
| isAuthenticated, | ||
| reason, | ||
| }: { | ||
| isAuthenticated: boolean | ||
| reason?: string | ||
| }) { | ||
| return isAuthenticated && reason !== SESSION_EXPIRED_REASON | ||
| } | ||
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.
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.
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.
P2: When a redirect targets a valid path beginning with
/loginbut not the login route, this condition sends it to/because it tests a raw prefix. Use the same segment-aware check asisLoginPathafter stripping the query and hash.Prompt for AI agents