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.
This commit is contained in:
bchanot committed 2026-11-02 11:22:51 +01:00
1 parent e8da0d96d9
commit d3dfd03213
7 files changed
+147 -16

No files matched your search

@@ -15,6 +15,7 @@ import { createHash, randomBytes } from 'node:crypto'
import { execFileSync } from 'node:child_process'
import { deflateSync } from 'node:zlib'
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
import { noRedirectFetch } from './noRedirectFetch'
import {
beginAvatarUpload,
confirmAssetUpload,
@@ -52,6 +53,7 @@ type Client = SupabaseClient<Database>
function anonClient(): Client {
return createClient<Database>(SANDBOX_URL, ANON ?? '', {
auth: { persistSession: false, autoRefreshToken: false },
global: { fetch: noRedirectFetch },
})
}
@@ -65,6 +67,7 @@ async function signedIn(email: string): Promise<Client> {
function serviceClient(): Client {
return createClient<Database>(SANDBOX_URL, SERVICE ?? '', {
auth: { persistSession: false, autoRefreshToken: false },
global: { fetch: noRedirectFetch },
})
}
@@ -0,0 +1,8 @@
/**
* The suite's fetch: a 3xx answer is an error, never a request to another
* origin (owner decision 2026-11-02). supabase-js hands `global.fetch` to its
* auth, REST and Storage calls and spreads the caller's init before ours runs,
* so `redirect` set here is final: nothing restores `follow`.
*/
export const noRedirectFetch: typeof fetch = (input, init) =>
fetch(input, { ...init, redirect: 'error' })
@@ -0,0 +1,95 @@
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')
})
})