From d7303ebc2acbcf83c97df799c8e2ec16abfaf50c Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Wed, 11 Nov 2026 05:34:42 +0000 Subject: [PATCH] fix(bitbucket): accept a /user 403 only when Bitbucket sent it --- .../bitbucket/credential-connection.test.ts | 19 +++++++++++++--- src/main/bitbucket/user-request.ts | 22 ++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/bitbucket/credential-connection.test.ts b/src/main/bitbucket/credential-connection.test.ts index aefc254793e..2bf06a5587a 100644 --- a/src/main/bitbucket/credential-connection.test.ts +++ b/src/main/bitbucket/credential-connection.test.ts @@ -96,11 +96,24 @@ describe('Bitbucket credential connection', () => { it('saves an access token Bitbucket accepts without account access (STA-14576)', async () => { const conn = await loadModule() - // Bitbucket answers /user with 403 for every repository, project or workspace access token. - globalThis.fetch = vi.fn(async () => new Response(null, { status: 403 })) + globalThis.fetch = vi.fn( + async () => new Response('Forbidden', { status: 403 }) + ) + const proxied = await conn.connectBitbucket({ authMode: 'token', accessToken: 'repo-token' }) + expect(proxied.ok).toBe(false) + expect(conn.getBitbucketConnectionStatus().source).toBe('none') + // Bitbucket's /user answer for every repository, project or workspace access token. + globalThis.fetch = vi.fn(async () => + Response.json( + { + type: 'error', + error: { message: 'Token is invalid or not supported for this endpoint.' } + }, + { status: 403 } + ) + ) const result = await conn.connectBitbucket({ authMode: 'token', accessToken: 'repo-token' }) - expect(result).toEqual({ ok: true, account: null }) expect(conn.getBitbucketConnectionStatus().source).toBe('stored') }) diff --git a/src/main/bitbucket/user-request.ts b/src/main/bitbucket/user-request.ts index a11c6c4b369..e990f7fb6b3 100644 --- a/src/main/bitbucket/user-request.ts +++ b/src/main/bitbucket/user-request.ts @@ -35,13 +35,15 @@ export async function fetchBitbucketUserResult( } catch { return { ok: false, reason: 'unreachable' } } + // Bitbucket answers 401 to bad credentials; its 403 means it accepted them without account access + // (every access token, and API tokens without read:user:bitbucket). + if (response.status === 403) { + return (await isBitbucketErrorBody(response)) + ? { ok: true, user: null } + : { ok: false, reason: 'unreachable' } + } if (!response.ok) { await cancelUnreadResponseBody(response) - // Bitbucket answers 401 to bad credentials; 403 means it accepted them without account access - // (every access token, and API tokens without read:user:bitbucket). - if (response.status === 403) { - return { ok: true, user: null } - } return { ok: false, reason: response.status === 401 ? 'rejected' : 'unreachable' } } try { @@ -50,3 +52,13 @@ export async function fetchBitbucketUserResult( return { ok: false, reason: 'unreachable' } } } + +// A proxy or non-Bitbucket base URL can answer 403 without checking the credential. +async function isBitbucketErrorBody(response: Response): Promise { + try { + const body: unknown = await response.json() + return typeof body === 'object' && body !== null && 'type' in body && body.type === 'error' + } catch { + return false + } +}