Files
faunosteo/packages/supabase/tests/no-redirect-fetch.test.ts
bchanot d3dfd03213 test(supabase): avatar e2e clients refuse HTTP redirects
The suite's supabase-js clients followed redirects by default, so a 3xx
from the sandbox gateway would have carried the apikey header to another
origin. `noRedirectFetch` (`redirect: 'error'`) is passed as `global.fetch`
of both client factories; a regression test with two loopback servers
proves REST, auth and Storage calls stop at the 302. Live proof j and a
second 5/5 run are in the audit log; no production client changes.
2026-11-02 11:22:51 +01:00

96 lines
2.8 KiB
TypeScript

import { createServer, type IncomingMessage, type Server } from 'node:http'
import type { AddressInfo } from 'node:net'
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { createClient } from '@supabase/supabase-js'
import type { Database } from '../src'
import { noRedirectFetch } from './e2e/noRedirectFetch'
type Seen = { url?: string; apikey?: string | string[] }
const targetSeen: Seen[] = []
const originSeen: Seen[] = []
let target: Server
let origin: Server
let originUrl = ''
function listen(
seen: Seen[],
answer: (res: import('node:http').ServerResponse) => void,
): Promise<Server> {
const server = createServer((req: IncomingMessage, res) => {
seen.push({ url: req.url, apikey: req.headers.apikey })
answer(res)
})
return new Promise((resolve) => {
server.listen(0, '127.0.0.1', () => resolve(server))
})
}
const portOf = (server: Server) => (server.address() as AddressInfo).port
function client() {
return createClient<Database>(originUrl, 'test-key', {
auth: { persistSession: false, autoRefreshToken: false },
global: { fetch: noRedirectFetch },
})
}
function expectNotFollowed() {
expect(targetSeen).toHaveLength(0)
// supabase-js may retry a failed GET: count the origin hits, not their number
expect(originSeen.length).toBeGreaterThan(0)
expect(originSeen.every((r) => r.apikey === 'test-key')).toBe(true)
}
beforeAll(async () => {
target = await listen(targetSeen, (res) => {
res.writeHead(200, { 'content-type': 'application/json' }).end('{}')
})
origin = await listen(originSeen, (res) => {
const landing = `http://127.0.0.1:${portOf(target)}/landed`
res.writeHead(302, { location: landing }).end()
})
originUrl = `http://127.0.0.1:${portOf(origin)}`
})
afterAll(async () => {
await Promise.all(
[target, origin].map((s) => new Promise((done) => s.close(done))),
)
})
beforeEach(() => {
targetSeen.length = 0
originSeen.length = 0
})
describe('noRedirectFetch', () => {
it('does not follow a redirect on a REST call', async () => {
const { error } = await client().from('assets').select('id')
expect(error).not.toBeNull()
expectNotFollowed()
})
it('does not follow a redirect on an auth call', async () => {
const { error } = await client().auth.signInWithPassword({
email: 'a@b.c',
password: 'x',
})
expect(error).not.toBeNull()
expectNotFollowed()
})
it('does not follow a redirect on a storage call', async () => {
const { error } = await client().storage.from('avatars').download('k')
expect(error).not.toBeNull()
expectNotFollowed()
})
it('control: the default fetch follows the same redirect', async () => {
const res = await fetch(`${originUrl}/control`)
expect(res.status).toBe(200)
expect(targetSeen).toHaveLength(1)
expect(targetSeen[0].url).toBe('/landed')
})
})