2FA verify limit: a right code gives its place back; exempt by an admin-only field, not e-mail (docs)

The verify limit took a place for every attempt and kept it, so the sixth
correct login of one account within 10 minutes was refused (IAWL idea 27).
Now an attempt takes its place before the compare (a parallel burst still
cannot slip under the limit), a right code gives it back, an attempt refused
without a compare gives it back at once, and a new attempt held out only by
attempts still being checked gets 429 + Retry-After: 2 "try again in a
moment". buildVerifyLimit returns limit.settle(req, correct); the plugin's
guard calls it with the package's answer. No schema change: payload-kv keeps
{ count, resetAt }.

Docs: the exempt example keyed on e-mail, which every account may change
itself (shown over REST and GraphQL): now an admin-only checkbox, with a test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 20:15:51 +01:00
co-authored by Claude Opus 5.5
parent e821d52d19
commit 91770ec17f
15 changed files with 384 additions and 25 deletions
+9 -4
View File
@@ -316,8 +316,12 @@ describe('iapl and the 2FA plugin', () => {
test('verify attempts are limited per account, and the limit can be changed or switched off', async () => {
clearVerifyLimits()
const req = (id: unknown) => ({ i18n: undefined, payload: { logger: { warn: () => {} } }, user: { id, collection: 'users' } }) as never
const limit = buildVerifyLimit('users') // 5 in 10 minutes
for (let i = 0; i < 5; i++) {expect(await limit(req(1))).toBeNull()}
const limit = buildVerifyLimit('users') // 5 wrong codes in 10 minutes
for (let i = 0; i < 5; i++) {
const attempt = req(1)
expect(await limit(attempt)).toBeNull()
await limit.settle(attempt, false) // a wrong code (1.17.1: a right one gives its place back)
}
const refused = (await limit(req(1)))!
expect(refused.status).toBe(429)
expect(Number(refused.headers.get('retry-after'))).toBeGreaterThan(0)
@@ -360,7 +364,8 @@ describe('iapl and the 2FA plugin', () => {
expect(await limit(req(kv))).toBeNull() // 4th
expect(await limit(req(kv))).toBeNull() // 5th
expect((await limit(req(kv)))!.status).toBe(429)
expect(kv.data.get(verifyLimitKey('users', '1'))).toMatchObject({ count: 6 })
// five attempts; the refused sixth compared nothing and gave its place back (1.17.1)
expect(kv.data.get(verifyLimitKey('users', '1'))).toMatchObject({ count: 5 })
// and a restart while refused keeps refusing, with the stored window's Retry-After
clearVerifyLimits()
const again = (await limit(req(kv)))!
@@ -468,7 +473,7 @@ describe('iapl and the 2FA plugin', () => {
})
test('wired by default on verify and setup, before the limit; not on other endpoints; sameOrigin: false turns it off', async () => {
const handler = vi.fn(() => Response.json({ ok: true }))
const handler = vi.fn(() => Response.json({ ok: false })) // wrong codes: they stay counted (a right one gives its place back)
const warn = vi.fn()
clearReportedOrigins()
const withEndpoints = (o: { collection: string }) => (c: Config) => ({