From e4944f91c9cb18906c7022e55419242a2d462578 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Aamir <93091750+umeraamir69@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:56:56 -0500 Subject: [PATCH 1/2] fix(auth): warn on validation_failed dead refresh tokens in INITIAL_SESSION Malformed visitor-controlled SSR cookies can make Auth return validation_failed on refresh; treat that like other dead-token codes so INITIAL_SESSION logs console.warn instead of console.error. --- packages/core/auth-js/src/GoTrueClient.ts | 8 ++-- .../core/auth-js/test/GoTrueClient.test.ts | 39 ++++++++++++++++++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/packages/core/auth-js/src/GoTrueClient.ts b/packages/core/auth-js/src/GoTrueClient.ts index eea8ef58e6..dcf400ef3c 100644 --- a/packages/core/auth-js/src/GoTrueClient.ts +++ b/packages/core/auth-js/src/GoTrueClient.ts @@ -4446,12 +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')) ) { // 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 - // application error — warn rather than surface it raw. + // a dead refresh token (e.g. stale SSR cookies / malformed visitor- + // controlled tokens) is not an application error — warn rather than + // surface it raw. console.warn(err) } else { console.error(err) diff --git a/packages/core/auth-js/test/GoTrueClient.test.ts b/packages/core/auth-js/test/GoTrueClient.test.ts index b1cc51964f..472513a03c 100644 --- a/packages/core/auth-js/test/GoTrueClient.test.ts +++ b/packages/core/auth-js/test/GoTrueClient.test.ts @@ -4465,14 +4465,18 @@ describe('Refresh-token lifecycle (proactive/reactive, cooldown)', () => { persistSession: true, }) - const stubInvalidGrant = (client: GoTrueClient, beforeReply?: () => Promise) => { + const stubInvalidGrant = ( + client: GoTrueClient, + beforeReply?: () => Promise, + 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, }), } @@ -5102,6 +5106,37 @@ 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 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, From d903b88297f61b1bdb5abe8a39efba09a8753046 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Aamir <93091750+umeraamir69@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:52:43 -0500 Subject: [PATCH 2/2] fix(auth): also warn on user_banned in INITIAL_SESSION Banned accounts with leftover cookies return user_banned on refresh; treat that like the other dead-token codes so INITIAL_SESSION logs console.warn instead of console.error. --- packages/core/auth-js/src/GoTrueClient.ts | 7 +++-- .../core/auth-js/test/GoTrueClient.test.ts | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/core/auth-js/src/GoTrueClient.ts b/packages/core/auth-js/src/GoTrueClient.ts index dcf400ef3c..15975485ac 100644 --- a/packages/core/auth-js/src/GoTrueClient.ts +++ b/packages/core/auth-js/src/GoTrueClient.ts @@ -4447,13 +4447,14 @@ export default class GoTrueClient { (err.code === 'refresh_token_not_found' || err.code === 'refresh_token_already_used' || err.code === 'session_expired' || - err.code === 'validation_failed')) + 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 / malformed visitor- - // controlled tokens) is not an application error — warn rather than - // surface it raw. + // controlled tokens / banned user leftover cookies) is not an + // application error — warn rather than surface it raw. console.warn(err) } else { console.error(err) diff --git a/packages/core/auth-js/test/GoTrueClient.test.ts b/packages/core/auth-js/test/GoTrueClient.test.ts index 472513a03c..28befda505 100644 --- a/packages/core/auth-js/test/GoTrueClient.test.ts +++ b/packages/core/auth-js/test/GoTrueClient.test.ts @@ -5137,6 +5137,37 @@ describe('Refresh-token lifecycle (proactive/reactive, cooldown)', () => { 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,