mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Context Bit of an odd bug that I happened to come across which I can't remember how to reproduce from scratch but its a valid issue. If i try to log in on the sign-in route with `method` and `error_description` query params in the URL, the `error_description` param gets carried forward to the next page, which if my account has MFA, it'll then bounce back to `/sign-in` `/sign-in?error_description=Unable+to+exchange+external+code%3A+53f8&method=github` Am opting to strip transient params when building the sign-in redirects ## To test - [ ] Ensure that you've got an account with MFA - [ ] Try logging in via this URL `/sign-in?error_description=Unable+to+exchange+external+code%3A+53f8&method=github` - [ ] You should be able to land on the sign-in-mfa page with no issue + no error toasts
222 lines
7.3 KiB
TypeScript
222 lines
7.3 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
buildPathWithParams,
|
|
buildSignUpReturnPath,
|
|
DEFAULT_FALLBACK_PATH,
|
|
DEFAULT_SIGNUP_RETURN_PATH,
|
|
getReturnToPath,
|
|
getSignInQuery,
|
|
getSignUpReturnTo,
|
|
validateReturnTo,
|
|
} from './gotrue'
|
|
|
|
describe('getSignInQuery', () => {
|
|
it('drops returnTo when it is exactly /new and keeps other params', () => {
|
|
expect(getSignInQuery({ returnTo: '/new', destination: 'cli' })).toEqual({ destination: 'cli' })
|
|
expect(getSignInQuery({ returnTo: ['/new', '/other'] })).toEqual({})
|
|
})
|
|
|
|
it('keeps /new with a query string', () => {
|
|
expect(getSignInQuery({ returnTo: '/new?foo=bar' })).toEqual({ returnTo: '/new?foo=bar' })
|
|
})
|
|
|
|
it('keeps other destinations and params', () => {
|
|
const query = { returnTo: '/project/ref', destination: 'cli' }
|
|
expect(getSignInQuery(query)).toEqual(query)
|
|
})
|
|
|
|
it('returns the query unchanged when there is no returnTo', () => {
|
|
expect(getSignInQuery({})).toEqual({})
|
|
expect(getSignInQuery({ method: 'github' })).toEqual({ method: 'github' })
|
|
})
|
|
})
|
|
|
|
describe('getSignUpReturnTo', () => {
|
|
it('defaults to /new when returnTo is missing', () => {
|
|
expect(getSignUpReturnTo(undefined)).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
expect(getSignUpReturnTo('')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
})
|
|
|
|
it('rewrites /org and /organizations to /new', () => {
|
|
expect(getSignUpReturnTo('/org')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
expect(getSignUpReturnTo('/organizations')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
})
|
|
|
|
it('rewrites org list paths while preserving query params', () => {
|
|
expect(getSignUpReturnTo('/org?foo=bar')).toBe('/new?foo=bar')
|
|
expect(getSignUpReturnTo('/organizations?foo=bar')).toBe('/new?foo=bar')
|
|
})
|
|
|
|
it('preserves explicit destinations including invite paths', () => {
|
|
expect(getSignUpReturnTo('/join?token=abc')).toBe('/join?token=abc')
|
|
expect(getSignUpReturnTo('/project/ref')).toBe('/project/ref')
|
|
})
|
|
|
|
it('uses the first value when returnTo is an array', () => {
|
|
expect(getSignUpReturnTo(['/join?token=abc', '/other'])).toBe('/join?token=abc')
|
|
expect(getSignUpReturnTo(['/org'])).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
expect(getSignUpReturnTo(['/organizations'])).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
})
|
|
})
|
|
|
|
describe('buildSignUpReturnPath', () => {
|
|
it('defaults to /new when returnTo is missing', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '' }
|
|
|
|
expect(buildSignUpReturnPath(undefined)).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
})
|
|
|
|
it('rewrites /org and /organizations to /new', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '' }
|
|
|
|
expect(buildSignUpReturnPath('/org')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
expect(buildSignUpReturnPath('/organizations')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
})
|
|
|
|
it('rewrites org list paths while preserving query params', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '' }
|
|
|
|
expect(buildSignUpReturnPath('/org?foo=bar')).toBe('/new?foo=bar')
|
|
expect(buildSignUpReturnPath('/organizations?foo=bar')).toBe('/new?foo=bar')
|
|
})
|
|
|
|
it('preserves invite returnTo paths', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '' }
|
|
|
|
expect(buildSignUpReturnPath('/join?token=abc')).toBe('/join?token=abc')
|
|
})
|
|
|
|
it('falls back to /new for invalid returnTo values', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '' }
|
|
|
|
expect(buildSignUpReturnPath('https://evil.com')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
expect(buildSignUpReturnPath('//evil.com')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
|
|
})
|
|
|
|
it('strips returnTo from the current location search params', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '?returnTo=/org&foo=bar' }
|
|
|
|
expect(buildSignUpReturnPath('/new')).toBe('/new?foo=bar')
|
|
})
|
|
|
|
it('uses the first value when returnTo is an array', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '' }
|
|
|
|
expect(buildSignUpReturnPath(['/join?token=abc', '/other'])).toBe('/join?token=abc')
|
|
})
|
|
})
|
|
|
|
describe('getReturnToPath', () => {
|
|
it('rebuilds a nested returnTo path with the params that rode alongside it', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '?ref=abc&name=OPENAI_API_KEY&returnTo=%2Fmcp%2Fsecrets' }
|
|
|
|
expect(getReturnToPath()).toBe('/mcp/secrets?ref=abc&name=OPENAI_API_KEY')
|
|
})
|
|
|
|
it('accepts a multi-segment returnTo rather than falling back', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search: '?returnTo=%2Fmcp%2Fsecrets' }
|
|
|
|
expect(getReturnToPath()).toBe('/mcp/secrets')
|
|
})
|
|
})
|
|
|
|
describe('transient auth params', () => {
|
|
const search =
|
|
'?error_description=Unable+to+exchange+external+code%3A+53f8&error=server_error&error_code=unexpected_failure&returnTo=%2Fnew'
|
|
|
|
it('buildPathWithParams does not carry them into the new path', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search }
|
|
|
|
expect(buildPathWithParams('/sign-in-mfa')).toBe('/sign-in-mfa?returnTo=%2Fnew')
|
|
})
|
|
|
|
it('getReturnToPath does not append them to the destination', () => {
|
|
// @ts-ignore
|
|
delete window.location
|
|
// @ts-ignore
|
|
window.location = { search }
|
|
|
|
expect(getReturnToPath()).toBe('/new')
|
|
})
|
|
})
|
|
|
|
describe('validateReturnTo', () => {
|
|
const fallback = DEFAULT_FALLBACK_PATH
|
|
|
|
it('should return the path if it is a valid internal path', () => {
|
|
expect(validateReturnTo('/dashboard')).toBe('/dashboard')
|
|
expect(validateReturnTo('/settings/profile')).toBe('/settings/profile')
|
|
expect(validateReturnTo('/projects?id=123')).toBe('/projects?id=123')
|
|
})
|
|
|
|
it('should return fallback if given an external URL', () => {
|
|
expect(validateReturnTo('https://example.com')).toBe(fallback)
|
|
expect(validateReturnTo('http://malicious-site.com')).toBe(fallback)
|
|
expect(validateReturnTo('//evil.com')).toBe(fallback)
|
|
})
|
|
|
|
it('should return fallback for potentially malicious paths', () => {
|
|
expect(validateReturnTo('/%2e%2e/etc/passwd')).toBe(fallback)
|
|
expect(validateReturnTo('/..')).toBe(fallback)
|
|
expect(validateReturnTo('/@evil/path')).toBe(fallback)
|
|
expect(validateReturnTo('/$malicious')).toBe(fallback)
|
|
})
|
|
|
|
it('should use custom fallback when provided', () => {
|
|
const customFallback = '/custom-fallback'
|
|
expect(validateReturnTo('https://example.com', customFallback)).toBe(customFallback)
|
|
expect(validateReturnTo('/%2e%2e/etc/passwd', customFallback)).toBe(customFallback)
|
|
})
|
|
|
|
it('should handle paths with query parameters correctly', () => {
|
|
expect(validateReturnTo('/dashboard?param1=value1¶m2=value2')).toBe(
|
|
'/dashboard?param1=value1¶m2=value2'
|
|
)
|
|
})
|
|
|
|
it('should handle nextjs dynamic path js', () => {
|
|
expect(
|
|
validateReturnTo(
|
|
'%2F%5B%5Bx%5D%5Djavascript%3Aalert(%22H4CK3D%22)%2F%5By%5D%2F%5B%5Bx%5D%5D%2F%5By%5D%3Fx%26y'
|
|
)
|
|
).toBe(fallback)
|
|
expect(
|
|
validateReturnTo(
|
|
'/%2F%5B%5Bx%5D%5Djavascript%3Aalert(%22H4CK3D%22)%2F%5By%5D%2F%5B%5Bx%5D%5D%2F%5By%5D%3Fx%26y'
|
|
)
|
|
).toBe(fallback)
|
|
})
|
|
})
|