Files
accounted/tests/pg/oauth-used-codes-lockdown.pg.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

55 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { getClient } from '@/tests/pg/setup'
/**
* Migration 20260602145425_lock_down_oauth_used_codes.sql enables RLS on
* public.oauth_used_codes and REVOKEs all privileges from the browser-facing
* `anon` and `authenticated` roles. The OAuth token endpoint reaches this
* replay-tracking table only through a service-role client (which bypasses
* both RLS and the revoke), so the API roles must be denied. These tests lock
* that contract in so a future migration cannot silently re-expose the table.
*/
describe('oauth_used_codes lockdown (pg)', () => {
async function expectDenied(role: 'anon' | 'authenticated', sql: string) {
const client = await getClient()
try {
await client.query('BEGIN')
await client.query(`SET LOCAL ROLE ${role}`)
await expect(client.query(sql)).rejects.toThrow(/permission denied/i)
} finally {
await client.query('ROLLBACK').catch(() => {})
client.release()
}
}
it('denies SELECT to the anon role', async () => {
await expectDenied('anon', 'SELECT * FROM public.oauth_used_codes LIMIT 1')
})
it('denies SELECT to the authenticated role', async () => {
await expectDenied('authenticated', 'SELECT * FROM public.oauth_used_codes LIMIT 1')
})
it('denies INSERT to the authenticated role', async () => {
await expectDenied(
'authenticated',
`INSERT INTO public.oauth_used_codes (code_hash) VALUES ('lockdown-test')`,
)
})
it('still allows the privileged (service-role / owner) connection to read', async () => {
// The app reaches this table through a service-role client, which bypasses
// RLS and the anon/authenticated revoke. Model that with the default
// privileged pg connection (table owner) the pg-real harness uses.
const client = await getClient()
try {
const res = await client.query<{ n: number }>(
'SELECT count(*)::int AS n FROM public.oauth_used_codes',
)
expect(res.rows[0]!.n).toBeGreaterThanOrEqual(0)
} finally {
client.release()
}
})
})