From 2b10471b0ad71084491ca5c5db82837c6d397096 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 4 Nov 2026 18:55:47 +0100 Subject: [PATCH] test(supabase): TOTP step guard in the sandbox verify helper --- apps/app/e2e/support/admin.ts | 23 +++++-------------- apps/app/e2e/support/editor.ts | 7 ++++-- packages/supabase/tests/sandbox/mfa.ts | 30 ++++++++++++++----------- packages/supabase/tests/sandbox/totp.ts | 13 ++++++++++- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/apps/app/e2e/support/admin.ts b/apps/app/e2e/support/admin.ts index a664e409..be182064 100644 --- a/apps/app/e2e/support/admin.ts +++ b/apps/app/e2e/support/admin.ts @@ -10,13 +10,11 @@ import type { Dict } from '@/i18n/types' import type { Locator, Page } from '@playwright/test' import { enrolTotp, + refusal, verifyTotp, } from '../../../../packages/supabase/tests/sandbox/mfa' import { sql, stack } from '../../../../packages/supabase/tests/sandbox/stack' -import { - totpCode, - TOTP_STEP_SECONDS, -} from '../../../../packages/supabase/tests/sandbox/totp' +import { currentCode } from '../../../../packages/supabase/tests/sandbox/totp' import { expect } from './browser' import type { SandboxAccount } from './sandbox' @@ -69,25 +67,14 @@ export async function enrolVerifiedFactor( ): Promise { const token = await passwordToken(account) const enrolment = await enrolTotp(token, name) - if (!enrolment.factor) - throw new Error(`sandbox enrol: HTTP ${enrolment.status}`) + if (!enrolment.factor) throw new Error(`sandbox enrol: ${refusal(enrolment)}`) const verified = await verifyTotp(token, enrolment.factor) if (verified.status !== 200) - throw new Error(`sandbox verify: HTTP ${verified.status}`) + throw new Error(`sandbox verify: ${refusal(verified)}`) return enrolment.factor.secret } -/** - * A code the authenticator would show now, never one about to change: a code - * typed in its last seconds may reach GoTrue in the next step. - */ -export async function currentCode(secret: string): Promise { - const step = TOTP_STEP_SECONDS * 1000 - const left = step - (Date.now() % step) - if (left < 3000) - await new Promise((resolve) => setTimeout(resolve, left + 200)) - return totpCode(secret) -} +export { currentCode } /** Types a code from `secret` into the TOTP form inside `scope` and sends it. */ export async function enterCode( diff --git a/apps/app/e2e/support/editor.ts b/apps/app/e2e/support/editor.ts index 13f11e28..2d942dcb 100644 --- a/apps/app/e2e/support/editor.ts +++ b/apps/app/e2e/support/editor.ts @@ -12,6 +12,7 @@ import type { Dict } from '@/i18n/types' import type { Browser, BrowserContext, Page } from '@playwright/test' import { enrolTotp, + refusal, verifyTotp, } from '../../../../packages/supabase/tests/sandbox/mfa' import { @@ -50,10 +51,12 @@ export async function makeActor( for (const kind of grants) await seedGrant(account, kind) const token = await passwordToken(account) const enrolment = await enrolTotp(token, 'Téléphone') - if (!enrolment.factor) throw new Error('editor fixture: enrol refused') + if (!enrolment.factor) + throw new Error(`editor fixture: enrol ${refusal(enrolment)}`) const verified = await verifyTotp(token, enrolment.factor) const aal2 = verified.body.access_token - if (typeof aal2 !== 'string') throw new Error('editor fixture: no aal2') + if (typeof aal2 !== 'string') + throw new Error(`editor fixture: no aal2 ${refusal(verified)}`) return { account, secret: enrolment.factor.secret, aal2 } } diff --git a/packages/supabase/tests/sandbox/mfa.ts b/packages/supabase/tests/sandbox/mfa.ts index e60cce40..ae619895 100644 --- a/packages/supabase/tests/sandbox/mfa.ts +++ b/packages/supabase/tests/sandbox/mfa.ts @@ -5,7 +5,7 @@ * in memory and are never printed. */ import { stack, type Account } from './stack' -import { totpCode } from './totp' +import { currentCode } from './totp' export interface Factor { id: string @@ -73,21 +73,26 @@ export async function verifyTotp( if (challenge.status !== 200) return challenge return authRequest('POST', `/factors/${factor.id}/verify`, token, { challenge_id: challenge.body.id, - code: totpCode(factor.secret), + code: await currentCode(factor.secret), }) } +/** The HTTP status and GoTrue's error code of an answer: never a body, a token or a secret. */ +export function refusal(result: { + status: number + body: Record +}): string { + const code = result.body.error_code ?? result.body.code + return `HTTP ${result.status}${typeof code === 'string' ? ` ${code}` : ''}` +} + /** Enrols and verifies a TOTP factor from `token`, returning the new aal2 access token. */ -export async function stepUp( - token: string, - name = 'device', -): Promise { +export async function stepUp(token: string, name = 'device'): Promise { const enrol = await enrolTotp(token, name) - if (!enrol.factor) - throw new Error(`enrol failed: ${JSON.stringify(enrol.body)}`) + if (!enrol.factor) throw new Error(`enrol failed: ${refusal(enrol)}`) const verified = await verifyTotp(token, enrol.factor) const aal2 = verified.body.access_token as string - if (!aal2) throw new Error(`verify failed: ${JSON.stringify(verified.body)}`) + if (!aal2) throw new Error(`verify failed: ${refusal(verified)}`) return aal2 } @@ -103,8 +108,7 @@ export async function steppedUp(account: Account): Promise { /** The claims of an access token (payload only; the signature is not checked here). */ export function claimsOf(token: string): Record { const payload = token.split('.')[1] ?? '' - return JSON.parse(Buffer.from(payload, 'base64url').toString('utf8')) as Record< - string, - unknown - > + return JSON.parse( + Buffer.from(payload, 'base64url').toString('utf8'), + ) as Record } diff --git a/packages/supabase/tests/sandbox/totp.ts b/packages/supabase/tests/sandbox/totp.ts index 5284a55e..f67d9779 100644 --- a/packages/supabase/tests/sandbox/totp.ts +++ b/packages/supabase/tests/sandbox/totp.ts @@ -28,6 +28,18 @@ function base32Decode(text: string): Buffer { return Buffer.from(bytes) } +/** + * A code the authenticator would show now, never one about to change: a code + * sent in its last seconds may reach GoTrue in the next step. + */ +export async function currentCode(secret: string): Promise { + const step = TOTP_STEP_SECONDS * 1000 + const left = step - (Date.now() % step) + if (left < 3000) + await new Promise((resolve) => setTimeout(resolve, left + 200)) + return totpCode(secret) +} + /** The 6-digit code of `secret` for the step holding `at` (ms since epoch). */ export function totpCode(secret: string, at = Date.now()): string { const counter = Buffer.alloc(8) @@ -37,4 +49,3 @@ export function totpCode(secret: string, at = Date.now()): string { const value = (mac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000 return value.toString().padStart(6, '0') } -