Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

34.1 KB
Raw Blame
import { startAuthentication } from '@simplewebauthn/browser'
import { router } from 'kea-router'
import { expectLogic, testUtilsPlugin } from 'kea-test-utils'
import posthog from 'posthog-js'

import { removeProjectIdIfPresent } from 'lib/utils/kea-router'
import { handleLoginRedirect, redirectAfterLogin } from 'lib/utils/loginRedirect'
import { passkeyLogic } from 'scenes/authentication/shared/passkeyLogic'

import { initKea } from '~/initKea'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'

import { loginLogic } from './loginLogic'

jest.mock('@simplewebauthn/browser', () => ({ startAuthentication: jest.fn() }))
jest.mock('posthog-js')

// isWebKitBrowser() reads navigator.vendor: "Apple Computer, Inc." on WebKit, "Google Inc." on Chromium.
const WEBKIT_VENDOR = 'Apple Computer, Inc.'
const CHROMIUM_VENDOR = 'Google Inc.'

function setVendor(vendor: string): void {
    Object.defineProperty(window.navigator, 'vendor', { value: vendor, configurable: true })
}

describe('loginLogic', () => {
    describe('redirect vulnerability', () => {
        beforeEach(() => {
            // Note, initKeaTests() is not called here because that uses a memory history, which doesn't throw on origin redirect
            initKea({ beforePlugins: [testUtilsPlugin] })
        })
        it('should ignore redirect attempt to a different origin', () => {
            router.actions.push(`${origin}/login?next=//google.com`)
            handleLoginRedirect()
            expect(router.values.location.pathname).toEqual('/')
        })
    })

    describe('wasSignedOutForSessionRisk', () => {
        let logic: ReturnType<typeof loginLogic.build>

        beforeEach(() => {
            initKeaTests()
            logic = loginLogic()
            logic.mount()
        })

        const cases: [string, boolean][] = [
            ['/login?reason=session_risk', true],
            ['/login?reason=something_else', false],
            ['/login', false],
        ]

        for (const [url, expected] of cases) {
            it(`for "${url}" it returns ${expected}`, () => {
                router.actions.push(url)
                expect(logic.values.wasSignedOutForSessionRisk).toEqual(expected)
            })
        }
    })

    describe('parseLoginRedirectURL', () => {
        let logic: ReturnType<typeof loginLogic.build>

        beforeEach(() => {
            initKeaTests()
            logic = loginLogic()
            logic.mount()
        })

        const origin = `http://localhost`
        const matches = [
            [null, '/'],
            ['/', '/'],
            ['asdf', '/'],
            ['?next=javascript:something', '/'],
            ['javascript:something', '/'],
            ['/bla', '/bla'],
            [`${origin}/bla`, '/bla'],
            [`http://some-other.origin/bla`, '/'],
            ['//foo.bar', '/'],
            ['/bla?haha', '/bla?haha'],
            ['/bla?haha#hoho', '/bla?haha#hoho'],
            // Percent-encoded chars nested in next's own query params must survive the redirect
            // (e.g. docs "Run in PostHog" links carrying %0A newlines in open_query); the router
            // round-trip normalizes form-encoded "+" spaces to "%20", which decodes the same
            [
                '/sql?open_query=SELECT%0A++properties.%24mcp+AS+tool',
                '/sql?open_query=SELECT%0A%20%20properties.%24mcp%20AS%20tool',
            ],
        ]

        for (const [next, result] of matches) {
            it(`for next param "${next}" it returns "${result}"`, () => {
                if (next) {
                    const [nextPath, nextHash] = next.split('#')
                    // The hash is the only part of the URL that isn't sent to the server
                    router.actions.push(
                        `${origin}/?next=${encodeURIComponent(nextPath)}${nextHash ? `#` + nextHash : ''}`
                    )
                } else {
                    router.actions.push(origin)
                }
                handleLoginRedirect()
                const newPath =
                    router.values.location.pathname + router.values.location.search + router.values.location.hash
                expect(removeProjectIdIfPresent(newPath)).toEqual(result)
            })
        }
    })

    describe('passkey auto-trigger after precheck', () => {
        let logic: ReturnType<typeof loginLogic.build>
        let beginHandler: jest.Mock
        const originalVendor = window.navigator.vendor

        beforeEach(() => {
            setVendor(CHROMIUM_VENDOR)
            // Treat the passkey prompt as a user cancellation so it resolves without a page reload.
            ;(startAuthentication as jest.Mock).mockRejectedValue(
                Object.assign(new Error('cancelled'), { name: 'AbortError' })
            )
            beginHandler = jest.fn(() => [
                200,
                {
                    challenge: 'abc',
                    timeout: 60000,
                    rpId: 'localhost',
                    allowCredentials: [],
                    userVerification: 'preferred',
                },
            ])
            useMocks({
                get: { '/api/users/@me/': () => [200, {}] },
                post: {
                    '/api/login/precheck': () => [
                        200,
                        { saml_available: false, webauthn_credentials: [{ id: 'cred-1', type: 'public-key' }] },
                    ],
                    '/api/webauthn/login/begin/': beginHandler,
                },
            })
            initKeaTests()
            router.actions.push('/login')
            logic = loginLogic()
            logic.mount()
            passkeyLogic().mount()
        })

        afterEach(() => {
            passkeyLogic().unmount()
            logic.unmount()
            setVendor(originalVendor)
            jest.clearAllMocks()
        })

        it('auto-triggers the passkey prompt once per email on non-WebKit browsers', async () => {
            logic.actions.precheck({ email: '[email protected]' })
            // Drain the whole passkey flow (begin request included) so nothing leaks into the next test.
            await expectLogic(passkeyLogic)
                .toDispatchActions(['beginPasskeyLogin', 'startPasskeyAuthenticationSuccess'])
                .toFinishAllListeners()
            expect(beginHandler).toHaveBeenCalledTimes(1)

            logic.actions.precheck({ email: '[email protected]', autoAttempt: true })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
            expect(beginHandler).toHaveBeenCalledTimes(1)
        })

        it('does not auto-trigger the passkey modal on WebKit (Safari)', async () => {
            setVendor(WEBKIT_VENDOR)
            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
            expect(beginHandler).not.toHaveBeenCalled()
        })
    })

    describe('code-based verification', () => {
        let logic: ReturnType<typeof loginLogic.build>
        const originalVendor = window.navigator.vendor

        beforeEach(() => {
            setVendor(WEBKIT_VENDOR) // skip passkey auto-trigger
            useMocks({
                get: { '/api/users/@me/': () => [200, {}] },
                post: {
                    '/api/login/precheck': () => [200, { saml_available: false }],
                    '/api/login': () => [401, { code: 'code_based_verification_required', detail: '[email protected]' }],
                    '/api/login/code-based-verification': () => [200, { success: true }],
                },
            })
            initKeaTests()
            router.actions.push('/login')
            logic = loginLogic()
            logic.mount()
        })

        afterEach(() => {
            logic.unmount()
            setVendor(originalVendor)
            jest.clearAllMocks()
        })

        it('enters code-entry mode when login requires a code, and exits on demand', async () => {
            logic.actions.setLoginValues({ email: '[email protected]', password: 'a-password' })
            logic.actions.submitLogin()
            await expectLogic(logic).toDispatchActions(['setCodeVerificationRequired', 'submitLoginFailure'])

            expect(logic.values.codeVerificationRequired).toBe(true)
            expect(logic.values.codeVerificationEmail).toBe('[email protected]')
            expect(logic.values.generalError).toBe(null)

            logic.actions.exitCodeVerification()
            expect(logic.values.codeVerificationRequired).toBe(false)
            expect(logic.values.codeVerificationEmail).toBe(null)
        })

        it('shows an undeliverable address on resend as a login error, and clears it once a resend goes out', async () => {
            const detail = "We can't send email to this address."
            useMocks({
                post: {
                    '/api/login/code-based-verification/resend': () => [
                        400,
                        { type: 'server_error', code: 'login_code_email_undeliverable', detail },
                    ],
                },
            })

            logic.actions.resendCodeBasedVerification(null)
            await expectLogic(logic).toDispatchActions(['resendCodeBasedVerificationSuccess'])

            expect(logic.values.resendResponse).toBe(null)
            expect(logic.values.generalError).toEqual({ code: 'login_code_email_undeliverable', detail })

            const sent = { success: true, message: 'sent' }
            useMocks({ post: { '/api/login/code-based-verification/resend': () => [200, sent] } })

            logic.actions.resendCodeBasedVerification(null)
            await expectLogic(logic).toDispatchActions(['resendCodeBasedVerificationSuccess'])

            expect(logic.values.resendResponse).toEqual(sent)
            expect(logic.values.generalError).toBe(null)
        })
    })

    describe('opaque login failure', () => {
        let logic: ReturnType<typeof loginLogic.build>
        const originalVendor = window.navigator.vendor

        beforeEach(() => {
            setVendor(WEBKIT_VENDOR) // skip passkey auto-trigger
            useMocks({
                get: { '/api/users/@me/': () => [200, {}] },
                post: {
                    '/api/login/precheck': () => [200, { saml_available: false }],
                    // A response with no JSON body (a 5xx from the edge, a dropped connection) leaves
                    // `code`/`detail` null on the resulting ApiError.
                    '/api/login': () => [502],
                },
            })
            initKeaTests()
            router.actions.push('/login')
            logic = loginLogic()
            logic.mount()
        })

        afterEach(() => {
            logic.unmount()
            setVendor(originalVendor)
            jest.clearAllMocks()
        })

        it('reports the underlying status when code and detail are both null', async () => {
            logic.actions.setLoginValues({ email: '[email protected]', password: 'a-password' })
            logic.actions.submitLogin()
            await expectLogic(logic).toDispatchActions(['submitLoginFailure'])

            expect(logic.values.generalError?.code).toBeFalsy()
            expect(posthog.captureException).toHaveBeenCalledWith(
                expect.anything(),
                expect.objectContaining({ extra: expect.objectContaining({ status: 502 }) })
            )
        })
    })

    describe('precheck dedupe', () => {
        let logic: ReturnType<typeof loginLogic.build>
        let precheckHandler: jest.Mock
        const originalVendor = window.navigator.vendor

        beforeEach(() => {
            setVendor(WEBKIT_VENDOR) // skip passkey auto-trigger, isolate precheck
            precheckHandler = jest.fn(() => [200, { saml_available: false }])
            useMocks({ post: { '/api/login/precheck': precheckHandler } })
            initKeaTests()
            router.actions.push('/login')
            logic = loginLogic()
            logic.mount()
        })

        afterEach(() => {
            logic.unmount()
            setVendor(originalVendor)
            jest.clearAllMocks()
        })

        it('skips a redundant precheck for an already-resolved or in-flight email but re-runs for a new one', async () => {
            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic).toDispatchActions(['precheckSuccess'])
            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic).toDispatchActions(['precheckSuccess'])
            expect(precheckHandler).toHaveBeenCalledTimes(1)

            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic).toDispatchActions(['precheckSuccess'])
            expect(precheckHandler).toHaveBeenCalledTimes(2)

            logic.actions.precheck({ email: '[email protected]' })
            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic)
                .toDispatchActions(['precheckSuccess'])
                .toMatchValues({
                    precheckResponse: expect.objectContaining({ email: '[email protected]', status: 'completed' }),
                })
            expect(precheckHandler).toHaveBeenCalledTimes(3)
        })

        it('prechecks the latest email only after typing pauses for 3 seconds', async () => {
            jest.useFakeTimers()
            try {
                logic.actions.setLoginValue('email', '[email protected]')
                await jest.advanceTimersByTimeAsync(2999)
                logic.actions.setLoginValue('email', '[email protected]')
                await jest.advanceTimersByTimeAsync(2999)
                expect(precheckHandler).not.toHaveBeenCalled()

                await jest.advanceTimersByTimeAsync(1)
                expect(precheckHandler).toHaveBeenCalledTimes(1)
                await jest.advanceTimersByTimeAsync(0)
                expect(logic.values.precheckResponse).toMatchObject({ status: 'completed', email: '[email protected]' })
            } finally {
                jest.useRealTimers()
            }
        })

        it('retries a failed precheck instead of caching its fallback for the page session', async () => {
            precheckHandler.mockImplementationOnce(() => [429, { detail: 'Request was throttled.' }])

            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic).toDispatchActions(['precheckSuccess'])
            // The fallback keeps the form usable, but doesn't know about this account's real options.
            expect(logic.values.precheckResponse).toMatchObject({ status: 'completed', precheckFailed: true })
            expect(logic.values.precheckResponse.sso_enforcement).toBeUndefined()

            precheckHandler.mockImplementationOnce(() => [
                200,
                { saml_available: false, sso_enforcement: 'google-oauth2' },
            ])
            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic).toDispatchActions(['precheckSuccess'])
            expect(precheckHandler).toHaveBeenCalledTimes(2)
            expect(logic.values.precheckResponse.sso_enforcement).toEqual('google-oauth2')
        })
    })

    describe('available login methods', () => {
        let logic: ReturnType<typeof loginLogic.build>
        let precheckResponse: Record<string, any>
        let resetHandler: jest.Mock
        const originalVendor = window.navigator.vendor

        beforeEach(() => {
            setVendor(WEBKIT_VENDOR) // skip the passkey auto-trigger, isolate the precheck response
            precheckResponse = { saml_available: false }
            resetHandler = jest.fn(() => [200, { success: true }])
            useMocks({ post: { '/api/login/precheck': () => [200, precheckResponse], '/api/reset/': resetHandler } })
            initKeaTests()
            router.actions.push('/login')
            logic = loginLogic()
            logic.mount()
        })

        afterEach(() => {
            logic.unmount()
            setVendor(originalVendor)
            jest.clearAllMocks()
        })

        async function precheck(response: Record<string, any>, email = '[email protected]'): Promise<void> {
            precheckResponse = response
            logic.actions.setLoginValues({ email })
            logic.actions.precheck({ email })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
        }

        it('keeps password login available before precheck resolves', () => {
            expect(logic.values.isPasswordLoginUnavailable).toBe(false)
            expect(logic.values.availableLoginMethods).toEqual([])
            expect(logic.values.restrictToProviders).toBe(null)
        })

        it('keeps password login available for a response without the field', async () => {
            // A server that predates this field must not make the password box disappear.
            await precheck({ saml_available: false })
            expect(logic.values.isPasswordLoginUnavailable).toBe(false)
            expect(logic.values.availableLoginMethods).toEqual(['password'])
            expect(logic.values.hasNoConfiguredLoginMethod).toBe(false)
            expect(logic.values.restrictToProviders).toBe(null)
        })

        it('keeps password login available for an unknown email', async () => {
            await precheck({ saml_available: false, password_login_available: true, social_providers: [] })
            expect(logic.values.isPasswordLoginUnavailable).toBe(false)
            expect(logic.values.availableLoginMethods).toEqual(['password'])
        })

        it('offers only the linked provider for a passwordless account', async () => {
            await precheck({
                saml_available: false,
                password_login_available: false,
                social_providers: ['google-oauth2'],
            })
            expect(logic.values.isPasswordLoginUnavailable).toBe(true)
            expect(logic.values.availableLoginMethods).toEqual(['google-oauth2'])
            expect(logic.values.hasNoConfiguredLoginMethod).toBe(false)
            expect(logic.values.restrictToProviders).toEqual(['google-oauth2'])
        })

        it('offers only the passkey for a passwordless account with one', async () => {
            await precheck({
                saml_available: false,
                password_login_available: false,
                social_providers: [],
                webauthn_credentials: [{ id: 'cred-1', type: 'public-key' }],
            })
            expect(logic.values.availableLoginMethods).toEqual(['passkey'])
            expect(logic.values.hasNoConfiguredLoginMethod).toBe(false)
            expect(logic.values.restrictToProviders).toBe(null)
        })

        it('offers SAML for a passwordless account on a SAML domain', async () => {
            await precheck({ saml_available: true, password_login_available: false, social_providers: [] })
            expect(logic.values.availableLoginMethods).toEqual(['saml'])
            expect(logic.values.hasNoConfiguredLoginMethod).toBe(false)
        })

        it('offers OIDC for a passwordless account on an OIDC domain', async () => {
            await precheck({
                saml_available: false,
                oidc_available: true,
                password_login_available: false,
                social_providers: [],
            })
            expect(logic.values.availableLoginMethods).toEqual(['oidc'])
            expect(logic.values.hasNoConfiguredLoginMethod).toBe(false)
        })

        it('offers every provider and a set-password email to a passwordless account with nothing linked', async () => {
            await precheck({ saml_available: false, password_login_available: false, social_providers: [] })
            expect(logic.values.isPasswordLoginUnavailable).toBe(true)
            expect(logic.values.availableLoginMethods).toEqual([])
            expect(logic.values.hasNoConfiguredLoginMethod).toBe(true)
            expect(logic.values.restrictToProviders).toBe(null)

            logic.actions.sendSetPasswordLink('[email protected]')
            await expectLogic(logic).toDispatchActions(['sendSetPasswordLinkSuccess'])

            expect(resetHandler).toHaveBeenCalledTimes(1)
            expect(logic.values.setPasswordLinkSentTo).toBe('[email protected]')
        })

        it('ignores a passwordless precheck once the email in the field changes', async () => {
            await precheck({ saml_available: false, password_login_available: false, social_providers: [] })

            logic.actions.setLoginValues({ email: '[email protected]' })

            expect(logic.values.isPasswordLoginUnavailable).toBe(false)
            expect(logic.values.hasNoConfiguredLoginMethod).toBe(false)
            expect(logic.values.restrictToProviders).toBe(null)
        })

        it('defers entirely to enforced SSO', async () => {
            await precheck({
                saml_available: false,
                sso_enforcement: 'google-oauth2',
                password_login_available: false,
                social_providers: [],
            })
            expect(logic.values.availableLoginMethods).toEqual([])
            expect(logic.values.hasNoConfiguredLoginMethod).toBe(false)
        })

        it('falls back to password login when precheck fails, so a 429 cannot lock the form', async () => {
            useMocks({ post: { '/api/login/precheck': () => [429, { detail: 'Request was throttled.' }] } })
            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()

            expect(logic.values.precheckResponse.status).toBe('completed')
            expect(logic.values.isPasswordLoginUnavailable).toBe(false)
            expect(logic.values.availableLoginMethods).toEqual(['password'])
        })
    })

    describe('auto-redirect to a single login method', () => {
        let logic: ReturnType<typeof loginLogic.build>
        let precheckResponse: Record<string, any>
        let assignMock: jest.Mock
        const originalVendor = window.navigator.vendor

        beforeEach(() => {
            setVendor(WEBKIT_VENDOR) // skip the passkey auto-trigger, isolate the redirect
            precheckResponse = {
                saml_available: false,
                password_login_available: false,
                social_providers: ['google-oauth2'],
            }
            useMocks({ post: { '/api/login/precheck': () => [200, precheckResponse] } })
            initKeaTests()
            router.actions.push('/login')
            logic = loginLogic()
            logic.mount()
            assignMock = jest.fn()
            Object.defineProperty(window, 'location', {
                value: { ...window.location, assign: assignMock },
                configurable: true,
            })
        })

        afterEach(() => {
            logic.unmount()
            setVendor(originalVendor)
            jest.clearAllMocks()
        })

        it('redirects when the account has exactly one method and the user explicitly submitted the email', async () => {
            logic.actions.setLoginValue('email', '[email protected]')
            logic.actions.precheck({ email: '[email protected]', autoAttempt: true })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()

            expect(logic.values.autoRedirectingToProvider).toBe('google-oauth2')
            expect(assignMock).toHaveBeenCalledWith('/login/google-oauth2/?email=user%40example.com')
        })

        it('does not redirect without autoAttempt, so autofill and ?email= links stay put', async () => {
            logic.actions.setLoginValue('email', '[email protected]')
            logic.actions.precheck({ email: '[email protected]' })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()

            expect(logic.values.autoRedirectingToProvider).toBe(null)
            expect(assignMock).not.toHaveBeenCalled()
        })

        it('does not redirect when the account has more than one method', async () => {
            precheckResponse = {
                saml_available: false,
                password_login_available: false,
                social_providers: ['google-oauth2'],
                webauthn_credentials: [{ id: 'cred-1', type: 'public-key' }],
            }
            logic.actions.setLoginValue('email', '[email protected]')
            logic.actions.precheck({ email: '[email protected]', autoAttempt: true })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()

            expect(assignMock).not.toHaveBeenCalled()
        })

        it('does not redirect when the account can use a password', async () => {
            precheckResponse = { saml_available: false, password_login_available: true, social_providers: [] }
            logic.actions.setLoginValue('email', '[email protected]')
            logic.actions.precheck({ email: '[email protected]', autoAttempt: true })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()

            expect(assignMock).not.toHaveBeenCalled()
        })

        it('redirects at most once per email', async () => {
            logic.actions.setLoginValue('email', '[email protected]')
            logic.actions.precheck({ email: '[email protected]', autoAttempt: true })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
            expect(assignMock).toHaveBeenCalledTimes(1)

            logic.actions.precheck({ email: '[email protected]', autoAttempt: true })
            await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
            expect(assignMock).toHaveBeenCalledTimes(1)
        })
    })

    describe('cross-project redirect after login', () => {
        const originalContext = window.POSTHOG_APP_CONTEXT
        const originalLocation = window.location
        let hrefSpy: jest.Mock
        let replaceSpy: jest.Mock

        beforeEach(() => {
            initKeaTests()
            // getCurrentTeamIdOrNone() reads the current project from the app context.
            window.POSTHOG_APP_CONTEXT = { current_team: { id: 5 } } as any
            window.sessionStorage.clear()
            hrefSpy = jest.fn()
            replaceSpy = jest.fn()
            Object.defineProperty(window, 'location', {
                value: {
                    origin: 'http://localhost',
                    pathname: '/login',
                    search: '',
                    hash: '',
                    set href(url: string) {
                        hrefSpy(url)
                    },
                    replace: replaceSpy,
                },
                configurable: true,
            })
        })

        afterEach(() => {
            window.POSTHOG_APP_CONTEXT = originalContext
            Object.defineProperty(window, 'location', { value: originalLocation, configurable: true })
        })

        it('does a full navigation to a project other than the current one', () => {
            router.actions.push(`/login?next=${encodeURIComponent('/project/999/pipeline/destinations/hog-abc')}`)
            handleLoginRedirect()
            // A full load lets AutoProjectMiddleware switch the active project before the scene mounts.
            expect(hrefSpy).toHaveBeenCalledWith('/project/999/pipeline/destinations/hog-abc')
        })

        it('stays client-side when the target is already the current project', () => {
            router.actions.push(`/login?next=${encodeURIComponent('/project/5/pipeline/destinations/hog-abc')}`)
            handleLoginRedirect()
            expect(hrefSpy).not.toHaveBeenCalled()
        })

        it('does a full navigation from a page rendered for a signed-out visitor', () => {
            // That page holds only the signed-out part of the stable chunk import map, so a client-side
            // route to an app scene fails to import its chunk.
            window.POSTHOG_APP_CONTEXT = { anonymous: true, current_team: { id: 5 } } as any
            router.actions.push(`/login?next=${encodeURIComponent('/project/5/dashboard/1')}`)
            handleLoginRedirect()
            // A replace (not an href assignment) so the stale signed-out document leaves history.
            expect(replaceSpy).toHaveBeenCalledWith('/project/5/dashboard/1')
        })

        it('stays client-side when the full navigation came straight back signed out', () => {
            // A server that keeps rendering the session signed out sends the new document back to /login.
            window.POSTHOG_APP_CONTEXT = { anonymous: true, current_team: { id: 5 } } as any
            router.actions.push(`/login?next=${encodeURIComponent('/project/5/dashboard/1')}`)
            handleLoginRedirect()
            router.actions.push(`/login?next=${encodeURIComponent('/project/5/dashboard/1')}`)
            handleLoginRedirect()

            expect(replaceSpy).toHaveBeenCalledTimes(1)
            expect(posthog.capture).toHaveBeenCalledWith('signed-out page redirect repeated')
        })

        it('stays client-side when sessionStorage is blocked, so it cannot reload forever', () => {
            window.POSTHOG_APP_CONTEXT = { anonymous: true, current_team: { id: 5 } } as any
            const getItem = jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
                throw new Error('blocked')
            })
            try {
                router.actions.push(`/login?next=${encodeURIComponent('/project/5/dashboard/1')}`)
                handleLoginRedirect()
            } finally {
                getItem.mockRestore()
            }

            expect(replaceSpy).not.toHaveBeenCalled()
            expect(posthog.capture).toHaveBeenCalledWith('signed-out page redirect skipped', {
                reason: 'storage_unavailable',
            })
        })

        it.each(['/.//evil.example/path', '/a/..//evil.example/path', '/../\\evil.example/path'])(
            'keeps the signed-out full load on our origin for next=%s',
            (next) => {
                window.POSTHOG_APP_CONTEXT = { anonymous: true, current_team: { id: 5 } } as any
                router.actions.push(`/login?next=${encodeURIComponent(next)}`)
                handleLoginRedirect()
                expect(replaceSpy).toHaveBeenCalledWith('/')
            }
        )

        it('does a full navigation for a project token the middleware resolves server-side', () => {
            // A `phc_` key or a legacy allowlisted api_token can't be compared to the numeric team id,
            // so it must reach AutoProjectMiddleware for resolution — client-side routing wouldn't switch.
            router.actions.push(`/login?next=${encodeURIComponent('/project/phc_ABC123/pipeline/destinations')}`)
            handleLoginRedirect()
            expect(hrefSpy).toHaveBeenCalledWith('/project/phc_ABC123/pipeline/destinations')
        })
    })

    describe('redirectAfterLogin', () => {
        const originalLocation = window.location
        let replaceSpy: jest.Mock
        let assignSpy: jest.Mock
        let reloadSpy: jest.Mock

        beforeEach(() => {
            initKeaTests()
            replaceSpy = jest.fn()
            assignSpy = jest.fn()
            reloadSpy = jest.fn()
            Object.defineProperty(window, 'location', {
                value: {
                    origin: 'http://localhost',
                    pathname: '/login',
                    search: '',
                    hash: '',
                    replace: replaceSpy,
                    assign: assignSpy,
                    reload: reloadSpy,
                },
                configurable: true,
            })
        })

        afterEach(() => {
            Object.defineProperty(window, 'location', { value: originalLocation, configurable: true })
        })

        it('lands on the app root in a single document load', () => {
            router.actions.push('/login')
            redirectAfterLogin()
            expect(replaceSpy).toHaveBeenCalledTimes(1)
            expect(replaceSpy).toHaveBeenCalledWith('/')
            // A second navigation would cancel every chunk the first boot had started fetching
            expect(reloadSpy).not.toHaveBeenCalled()
        })

        it('takes the place of the login entry, so Back does not return to it', () => {
            router.actions.push('/login')
            redirectAfterLogin()
            expect(assignSpy).not.toHaveBeenCalled()
        })

        it('lands on the next path, keeping its query and hash', () => {
            router.actions.push(`/login?next=${encodeURIComponent('/project/5/insights?foo=bar')}#tab=raw`)
            redirectAfterLogin()
            expect(replaceSpy).toHaveBeenCalledTimes(1)
            expect(replaceSpy).toHaveBeenCalledWith('/project/5/insights?foo=bar#tab=raw')
        })

        it('ignores a next path pointing at another origin', () => {
            router.actions.push('/login?next=//google.com')
            redirectAfterLogin()
            expect(replaceSpy).toHaveBeenCalledWith('/')
        })

        it('prefers an explicit destination over the next path', () => {
            router.actions.push(`/login?next=${encodeURIComponent('/project/5/insights')}`)
            redirectAfterLogin('/project/5/replay/home')
            expect(replaceSpy).toHaveBeenCalledTimes(1)
            expect(replaceSpy).toHaveBeenCalledWith('/project/5/replay/home')
        })

        it('rejects an explicit cross-origin destination', () => {
            router.actions.push('/login')
            redirectAfterLogin('https://evil.example/steal')
            expect(replaceSpy).toHaveBeenCalledWith('/')
        })

        it('rejects a protocol-relative cross-origin destination', () => {
            router.actions.push('/login')
            redirectAfterLogin('//evil.example/steal')
            expect(replaceSpy).toHaveBeenCalledWith('/')
        })

        it('accepts an absolute destination on our own origin, as its path', () => {
            // The guard must not over-reject: a same-origin absolute URL is a legitimate
            // destination, and it arrives as a path.
            router.actions.push('/login')
            redirectAfterLogin('http://localhost/project/5/insights?foo=bar#tab=raw')
            expect(replaceSpy).toHaveBeenCalledWith('/project/5/insights?foo=bar#tab=raw')
        })
    })
})