Security review round 2: no exception by name, staff list fails closed, verify limit
R2-1: the sweep skipped every collection whose slug starts with payload-,
so a plugin's payload-... collection added after ipalKit was never guarded.
There is no exception by name or prefix now: every collection and global
is guarded, Payload's own included, and the job queue's run / queue /
cancel rules too.
R2-2: role predicates called without a source fail closed where the staff
collections are not known (browser bundle, middleware, edge): nobody is
staff. They accept req.payload, a config (also the client config) or
{ staffCollections } as the source.
R2-3: the staff list is filed per config / Payload instance; a call without
a source gets only what every config in the process calls staff.
R2-4: payload.config is watched on the instance, so a config swapped in by
a dev reload is guarded before any request or Local API call.
R2-5: verify attempts are limited per account (default 5 in 10 minutes,
twoFactor.verifyLimit), counted in memory, never skipped.
R2-6: a user object without collection owes the second factor; endpoints
the 2FA package may add on collections or globals are guarded too; a
warning names staff collections that 2FA does not cover; docs state what
is not covered, fix the one-argument examples and the doctor example.
HTTP tests for each on payload-2fa beta.7, run on SQLite, Postgres and
MongoDB (test adapter added).
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
32faaa5e30
commit
c56f0d41b8
@@ -0,0 +1,64 @@
|
||||
import type { PayloadRequest } from 'payload'
|
||||
|
||||
import { serverText } from '../../integrations/texts.js'
|
||||
|
||||
/**
|
||||
* A limit on TOTP verify attempts. The 2FA package checks the code and never
|
||||
* counts tries: a six-digit code with a ±1 step window has about three right
|
||||
* answers in a million, so whoever holds the password could simply try.
|
||||
*
|
||||
* Counted per ACCOUNT, not per IP (an attacker changes addresses; the account
|
||||
* is what is being guessed). Every attempt counts, right or wrong — nobody
|
||||
* needs more than a handful of logins in the window. Only a session that
|
||||
* already passed the password reaches the endpoint, so someone without the
|
||||
* password cannot use the limit to lock an account's owner out.
|
||||
*
|
||||
* In memory, per server instance, and always enforced (no "allow when the
|
||||
* store fails" path): with several instances the limit is per instance.
|
||||
*/
|
||||
export type VerifyLimitOptions = {
|
||||
/** Attempts per account in the window. Default 5. */
|
||||
max?: number
|
||||
/** Window in minutes; a refused account waits until it ends. Default 10. */
|
||||
windowMinutes?: number
|
||||
}
|
||||
|
||||
type Bucket = { count: number; resetAt: number }
|
||||
const buckets = new Map<string, Bucket>()
|
||||
|
||||
/** For tests. */
|
||||
export function clearVerifyLimits(): void {
|
||||
buckets.clear()
|
||||
}
|
||||
|
||||
export function buildVerifyLimit(collection: string, options: VerifyLimitOptions = {}) {
|
||||
const max = Number.isFinite(options.max) && options.max! > 0 ? Math.floor(options.max!) : 5
|
||||
const windowMs = (Number.isFinite(options.windowMinutes) && options.windowMinutes! > 0 ? options.windowMinutes! : 10) * 60_000
|
||||
|
||||
return (req: PayloadRequest): null | Response => {
|
||||
// No session: the package refuses by itself, and there is no account to count against.
|
||||
if (!req.user) {return null}
|
||||
// Whatever the id looks like, it is one bucket per value — never "no limit".
|
||||
const raw = (req.user as { id?: unknown }).id
|
||||
const id = typeof raw === 'string' || typeof raw === 'number' ? String(raw) : JSON.stringify(raw ?? null)
|
||||
const now = Date.now()
|
||||
// keep the map small: forget windows that are over
|
||||
if (buckets.size > 5000) {
|
||||
for (const [key, bucket] of buckets) {
|
||||
if (bucket.resetAt <= now) {buckets.delete(key)}
|
||||
}
|
||||
}
|
||||
const key = `${collection}:${id}`
|
||||
const current = buckets.get(key)
|
||||
const bucket = current && current.resetAt > now ? current : { count: 0, resetAt: now + windowMs }
|
||||
bucket.count += 1
|
||||
buckets.set(key, bucket)
|
||||
if (bucket.count <= max) {return null}
|
||||
const retryAfter = Math.max(1, Math.ceil((bucket.resetAt - now) / 1000))
|
||||
req.payload.logger.warn(`[ipal] 2FA: too many verify attempts for ${collection} ${id}; refused for ${retryAfter} s.`)
|
||||
return Response.json(
|
||||
{ message: serverText(req.i18n as never, 'twoFactorTooManyAttempts', { minutes: Math.ceil(retryAfter / 60) }), ok: false },
|
||||
{ headers: { 'Retry-After': String(retryAfter) }, status: 429 },
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user