From b2fed3bf902a237266d892cf2095071ac0956c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Smoli=C5=84ski?= Date: Thu, 26 Nov 2026 20:22:21 +0100 Subject: [PATCH] Turnstile docs: limit before asking Cloudflare; test and lint fixes Co-Authored-By: Claude Opus 5.5 --- docs/turnstile.md | 2 +- src/modules/security/totp/secondFactor.ts | 10 +++++----- test/exemptByField.test.ts | 4 ++-- test/twoFactorResetOriginSuite.ts | 1 - 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/docs/turnstile.md b/docs/turnstile.md index c7a65fd..4ad905f 100644 --- a/docs/turnstile.md +++ b/docs/turnstile.md @@ -262,5 +262,5 @@ full control or have an unusual case. The provider is a convenience layer over i - **Access to the keys:** Site integrations is admin-only (read and update); change it with `iapl({ globalOverrides: { 'site-integrations': { access: … } } })` — keep `read` restricted, the secret lives there. - **Look:** `theme` on `` / ``, `turnstileTheme` in `RenderForm` / `BuilderForm`. -- **Your own integration:** `checkTurnstile` (full `pass` / `reject` / `degraded` classification), +- **Your own integration:** limit first, then ask Cloudflare — `consumeRateLimit({ key: clientIp(req), scope: 'signup' })` before `checkTurnstile`, so a flood of posts does not become a flood of requests to Cloudflare (the plugin's forms do it in this order: `passRateLimit`, then `passTurnstile`). `checkTurnstile` (full `pass` / `reject` / `degraded` classification), `verifyTurnstile`, `getTurnstileConfig`, ``, `useTurnstile` / `useOptionalTurnstile`. diff --git a/src/modules/security/totp/secondFactor.ts b/src/modules/security/totp/secondFactor.ts index 83bc03f..694da60 100644 --- a/src/modules/security/totp/secondFactor.ts +++ b/src/modules/security/totp/secondFactor.ts @@ -382,11 +382,6 @@ export type TotpEndpointGuards = { * it. Return a Response to refuse (too many attempts), null to let it through. */ limitVerify?: (req: never) => null | Promise | Response - /** - * Called after a verify attempt that `limitVerify` let through: `correct` is true when the package - * answered `{ ok: true }`. The plugin's limit gives a right code's place back (buildVerifyLimit `settle`). - */ - settleVerify?: (req: never, correct: boolean) => Promise | void /** The answer for an account whose setup window has passed (in the caller's language). Default: a plain 403. */ onSetupClosed?: (req: never) => Response /** @@ -394,6 +389,11 @@ export type TotpEndpointGuards = { * (sameOrigin.ts). False = refused, and NOT counted against the verify limit. */ ownOrigin?: (req: never) => boolean + /** + * Called after a verify attempt that `limitVerify` let through: `correct` is true when the package + * answered `{ ok: true }`. The plugin's limit gives a right code's place back (buildVerifyLimit `settle`). + */ + settleVerify?: (req: never, correct: boolean) => Promise | void } export function guardTotpEndpoint(endpoint: T, collection: string, guards: TotpEndpointGuards = {}): T { diff --git a/test/exemptByField.test.ts b/test/exemptByField.test.ts index f593ab7..e092aed 100644 --- a/test/exemptByField.test.ts +++ b/test/exemptByField.test.ts @@ -72,14 +72,14 @@ describe('exempt from 2FA setup: by an admin-only field, not by e-mail', () => { const rest = await call(`/users/${editorId}`, { body: JSON.stringify({ serviceAccount: true }), method: 'PATCH' }) expect(rest.status).toBe(200) await call('/graphql', { body: JSON.stringify({ query: `mutation { updateUser(id: ${JSON.stringify(editorId)}, data: { serviceAccount: true }) { id } }` }), method: 'POST' }) - const stored = (await payload.findByID({ id: editorId, collection: 'users' as never, overrideAccess: true })) as { serviceAccount?: boolean } + const stored = (await payload.findByID({ id: editorId, collection: 'users' as never, overrideAccess: true })) as unknown as { serviceAccount?: boolean } expect(stored.serviceAccount).toBe(false) }) test('…while the same editor can change its own e-mail to anything: an e-mail list is no boundary', async () => { const res = await call(`/users/${editorId}`, { body: JSON.stringify({ email: 'it@example.com' }), method: 'PATCH' }) expect(res.status).toBe(200) - const stored = (await payload.findByID({ id: editorId, collection: 'users' as never, overrideAccess: true })) as { email: string } + const stored = (await payload.findByID({ id: editorId, collection: 'users' as never, overrideAccess: true })) as unknown as { email: string } expect(stored.email).toBe('it@example.com') }) }) diff --git a/test/twoFactorResetOriginSuite.ts b/test/twoFactorResetOriginSuite.ts index 8ed6d07..3779acd 100644 --- a/test/twoFactorResetOriginSuite.ts +++ b/test/twoFactorResetOriginSuite.ts @@ -13,7 +13,6 @@ import { lexicalEditor } from '@payloadcms/richtext-lexical' import { getPayload } from 'payload' import { afterAll } from 'vitest' -import { clearReportedOrigins } from '../src/modules/security/totp/sameOrigin.js' import { iapl } from '../src/plugin.js' import { buildTestConfig } from './helpers.js' import { testDatabase } from './http/db.js'