Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/core/auth-js/src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4446,11 +4446,14 @@ export default class GoTrueClient {
(isAuthApiError(err) &&
(err.code === 'refresh_token_not_found' ||
err.code === 'refresh_token_already_used' ||
err.code === 'session_expired'))
err.code === 'session_expired' ||
err.code === 'validation_failed' ||
err.code === 'user_banned'))
) {
// A missing session, a transient/aborted network failure (e.g. a
// superseded page navigation cancelling the in-flight request), or
// a dead refresh token (e.g. stale SSR cookies) is not an
// a dead refresh token (e.g. stale SSR cookies / malformed visitor-
// controlled tokens / banned user leftover cookies) is not an
// application error — warn rather than surface it raw.
console.warn(err)
} else {
Expand Down
70 changes: 68 additions & 2 deletions packages/core/auth-js/test/GoTrueClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4465,14 +4465,18 @@ describe('Refresh-token lifecycle (proactive/reactive, cooldown)', () => {
persistSession: true,
})

const stubInvalidGrant = (client: GoTrueClient, beforeReply?: () => Promise<void>) => {
const stubInvalidGrant = (
client: GoTrueClient,
beforeReply?: () => Promise<void>,
code: string = 'refresh_token_already_used'
) => {
const spy = jest.fn(async () => {
await beforeReply?.()
return {
data: { session: null, user: null },
error: Object.assign(new AuthError('Invalid Refresh Token: Already Used', 400), {
name: 'AuthApiError',
code: 'refresh_token_already_used',
code,
__isAuthError: true,
}),
}
Expand Down Expand Up @@ -5102,6 +5106,68 @@ describe('Refresh-token lifecycle (proactive/reactive, cooldown)', () => {
warnSpy.mockRestore()
})

test('_emitInitialSession downgrades a validation_failed refresh token to console.warn', async () => {
// Auth returns validation_failed for refresh tokens in a format it never
// issues (e.g. visitor-controlled SSR cookies). That must warn, not
// error, so made-up cookies cannot inflate production error rates.
const storage = memoryLocalStorageAdapter()
await plantSession(storage, { secondsUntilExpiry: -60 })

const client = new GoTrueClient({
url: GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON,
storage,
autoRefreshToken: false,
persistSession: true,
skipAutoInitialize: true,
})
stubInvalidGrant(client, undefined, 'validation_failed')

await client.initialize()

const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})

// @ts-expect-error access private for test
await client._emitInitialSession(Symbol('test-validation-failed-init'))

expect(errorSpy).not.toHaveBeenCalled()
expect(warnSpy).toHaveBeenCalled()

errorSpy.mockRestore()
warnSpy.mockRestore()
})

test('_emitInitialSession downgrades a user_banned refresh token to console.warn', async () => {
// Auth returns user_banned for refresh when the account is banned but a
// leftover cookie remains. That must warn, not error, so banned users
// cannot inflate production error rates on every request.
const storage = memoryLocalStorageAdapter()
await plantSession(storage, { secondsUntilExpiry: -60 })

const client = new GoTrueClient({
url: GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON,
storage,
autoRefreshToken: false,
persistSession: true,
skipAutoInitialize: true,
})
stubInvalidGrant(client, undefined, 'user_banned')

await client.initialize()

const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})

// @ts-expect-error access private for test
await client._emitInitialSession(Symbol('test-user-banned-init'))

expect(errorSpy).not.toHaveBeenCalled()
expect(warnSpy).toHaveBeenCalled()

errorSpy.mockRestore()
warnSpy.mockRestore()
})

test('_emitInitialSession does not log a commit-guard-discarded refresh', async () => {
// Concurrent signOut clears storage while a refresh is in flight; the
// commit guard discards the rotated tokens. This is a successful no-op,
Expand Down