fix(bitbucket): accept a /user 403 only when Bitbucket sent it

This commit is contained in:
Kelvin Amoaba
2026-11-11 05:34:51 +00:00
parent 952af1b81a
commit d7303ebc2a
2 changed files with 33 additions and 8 deletions
@@ -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<typeof fetch>(async () => new Response(null, { status: 403 }))
globalThis.fetch = vi.fn<typeof fetch>(
async () => new Response('<html>Forbidden</html>', { 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<typeof fetch>(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')
})
+17 -5
View File
@@ -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<boolean> {
try {
const body: unknown = await response.json()
return typeof body === 'object' && body !== null && 'type' in body && body.type === 'error'
} catch {
return false
}
}