|
| 1 | +import { describe, it, expect, afterEach, vi } from 'vitest' |
| 2 | +import { authenticateWithSupabase } from '../../supabase/managementAuth' |
| 3 | + |
| 4 | +const MGMT_API_BASE = 'https://api.supabase.com' |
| 5 | +const PROJECT_REF = 'lgiudgtkdioaxbczoqhq' |
| 6 | +const VALID_TOKEN = 'sbp_valid_token' |
| 7 | + |
| 8 | +// Stubs global fetch so response.ok is driven by the requested URL. |
| 9 | +function mockFetchByUrl(isOk: (url: string) => boolean) { |
| 10 | + const mockFetch = vi.fn().mockImplementation((url: string) => Promise.resolve({ ok: isOk(url) })) |
| 11 | + global.fetch = mockFetch |
| 12 | + return mockFetch |
| 13 | +} |
| 14 | + |
| 15 | +describe('authenticateWithSupabase', () => { |
| 16 | + afterEach(() => { |
| 17 | + vi.restoreAllMocks() |
| 18 | + }) |
| 19 | + |
| 20 | + it('rejects a missing token without calling the management API', async () => { |
| 21 | + const mockFetch = mockFetchByUrl(() => true) |
| 22 | + |
| 23 | + const error = await authenticateWithSupabase(MGMT_API_BASE, PROJECT_REF, null) |
| 24 | + |
| 25 | + expect(error).toBe('Unauthorized: Invalid credentials') |
| 26 | + expect(mockFetch).not.toHaveBeenCalled() |
| 27 | + }) |
| 28 | + |
| 29 | + it('rejects a fabricated token (the reported attack)', async () => { |
| 30 | + // A bogus token gets a 401 from the management API, so response.ok is false. |
| 31 | + const mockFetch = mockFetchByUrl(() => false) |
| 32 | + |
| 33 | + const error = await authenticateWithSupabase(MGMT_API_BASE, PROJECT_REF, 'fake_attacker_token') |
| 34 | + |
| 35 | + expect(error).toBe('Unauthorized: Invalid credentials') |
| 36 | + // Both the project and branch checks are attempted before rejecting. |
| 37 | + expect(mockFetch).toHaveBeenCalledTimes(2) |
| 38 | + }) |
| 39 | + |
| 40 | + it('accepts a token that can access the project', async () => { |
| 41 | + mockFetchByUrl((url) => url.endsWith(`/v1/projects/${PROJECT_REF}`)) |
| 42 | + |
| 43 | + const error = await authenticateWithSupabase(MGMT_API_BASE, PROJECT_REF, VALID_TOKEN) |
| 44 | + |
| 45 | + expect(error).toBeNull() |
| 46 | + }) |
| 47 | + |
| 48 | + it('accepts a branch ref reachable via /v1/branches as a fallback', async () => { |
| 49 | + const mockFetch = mockFetchByUrl((url) => url.includes('/v1/branches/')) |
| 50 | + |
| 51 | + const error = await authenticateWithSupabase(MGMT_API_BASE, PROJECT_REF, VALID_TOKEN) |
| 52 | + |
| 53 | + expect(error).toBeNull() |
| 54 | + // Direct project check ran first and failed, then the branch check passed. |
| 55 | + expect(mockFetch).toHaveBeenCalledTimes(2) |
| 56 | + expect(mockFetch).toHaveBeenLastCalledWith( |
| 57 | + `${MGMT_API_BASE}/v1/branches/${PROJECT_REF}`, |
| 58 | + expect.objectContaining({ |
| 59 | + method: 'GET', |
| 60 | + headers: expect.objectContaining({ Authorization: `Bearer ${VALID_TOKEN}` }), |
| 61 | + }) |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + it('queries the exact project ref with the bearer token', async () => { |
| 66 | + const mockFetch = mockFetchByUrl(() => true) |
| 67 | + |
| 68 | + await authenticateWithSupabase(MGMT_API_BASE, PROJECT_REF, VALID_TOKEN) |
| 69 | + |
| 70 | + expect(mockFetch).toHaveBeenCalledWith( |
| 71 | + `${MGMT_API_BASE}/v1/projects/${PROJECT_REF}`, |
| 72 | + expect.objectContaining({ |
| 73 | + method: 'GET', |
| 74 | + headers: expect.objectContaining({ Authorization: `Bearer ${VALID_TOKEN}` }), |
| 75 | + }) |
| 76 | + ) |
| 77 | + }) |
| 78 | +}) |
0 commit comments