M1 (8): 2FA attempts only from the own origin; the verify count is stored

R3-2. (b) POST /api/verify-totp and /api/setup-totp refuse a request that a
browser marks as coming from another origin (Origin, Sec-Fetch-Site) — before it
is counted, so another page (a sibling subdomain included) cannot burn an
account's attempts and lock its owner out. Own origins: the request's host,
serverURL, Payload's csrf / cors lists, twoFactor.sameOrigin.origins; requests
without those headers pass. twoFactor.sameOrigin: false turns it off.
(a) The verify count is kept in memory (exact for parallel bursts) and in
Payload's key-value store (payload.kv, no new collection), so a restart or a
second instance does not reset it; the higher count decides, and a failing store
never opens the gate. verifyLimit.store: 'memory' = this instance only.
Doctor: csrf-empty (static, warning) when neither serverURL nor csrf is set.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 00:39:12 +01:00
co-authored by Claude Fable 5.2
parent 0b4c4e8295
commit f7296edc71
10 changed files with 318 additions and 13 deletions
+22
View File
@@ -454,6 +454,28 @@ describe('the code cannot be guessed: verify attempts are limited per account',
expect((await fetch(`${server.url}/api/pages`, { headers: { cookie } })).status).toBe(403)
})
// M1 (R3-2). Order matters: this runs after the account above is already refused.
test('attempts from another origin are refused without being counted, and the count survives a restart', async () => {
await payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['editor'], totpSecret: GUESS_SECRET } as never })
const mine = (await login('users', '[email protected]')).cookies.join('; ')
// a page on a sibling subdomain posts wrong codes with the owner's cookie: refused, twenty times
for (let i = 0; i < 20; i++) {
const res = await verify('000000', { cookie: mine, origin: 'https://blog.example.com' })
expect(res.status).toBe(403)
}
expect((await verify('000000', { cookie: mine, 'sec-fetch-site': 'cross-site' })).status).toBe(403)
// …and the owner still has every attempt: three wrong ones are answered, not refused
for (let i = 0; i < 3; i++) {expect((await verify('000000', { cookie: mine, origin: server.url })).status).toBe(200)}
// a restart / another instance: the memory is empty, the database is not
const { clearVerifyLimits } = await import('../src/modules/security/totp/verifyLimit.js')
clearVerifyLimits()
expect((await verify('000000', { cookie: mine })).status).toBe(200) // 4th
expect((await verify('000000', { cookie: mine })).status).toBe(200) // 5th
expect((await verify(totp(GUESS_SECRET), { cookie: mine })).status).toBe(429) // 6th: refused, also the right code
clearVerifyLimits()
expect((await verify(totp(GUESS_SECRET), { cookie: mine })).status).toBe(429)
})
test('the limit is the account\'s alone: nobody without its password can use it, other accounts are not affected', async () => {
// someone without the password cannot count against the account: no session, or a session of another collection
for (let i = 0; i < 8; i++) {