Staff sign-in rules: failures per IP and address, own origin, current password for own changes (1.18.0)

Q2 (IAWL idea 16): Payload locks a staff account after 5 wrong passwords
from anybody, so a stranger kept the owner out without a password. Failed
sign-ins are now counted per client IP and typed address (folded: NFKC,
lower case, trim): 5 in 15 min, then 429 for that IP and address only;
only failures count (a place taken before the compare, given back on
success or refusal; "try again in a moment" while others are being
checked). Payload's own lock stays as the net at 50 failures for an hour
where the project set none. The mail service's account-mail limit now
counts per address per visitor (5) with 20 per address in all, so one
stranger no longer blocks the owner's own reset mail.
Q3 (idea 7): staff login, forgot and reset password refuse a request a
browser marks as coming from another origin (403 before anything is
counted or sent); visitor collections untouched.
Q5 (idea 29): an account that changes its own e-mail, username or password
must send its current password (panel field on the account page via the
plugin's MaskedField, no import-map change; REST and GraphQL). Admins
changing others, server code and API keys are not asked; wrong ones count
like failed sign-ins.
Options accounts.signInLimit / signInSameOrigin / confirmOwnChanges, all
validated at start. Tests: 15 HTTP tests (2FA on/off, all off), fail
before on 1.17.1; built-site spec own-account.spec.ts in e2e:built.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 11:12:40 +01:00
co-authored by Claude Opus 5.5
parent fc70a34f3e
commit 52710021ef
28 changed files with 839 additions and 21 deletions
+22
View File
@@ -166,6 +166,28 @@ export async function consumeRateLimit({
return { ok: bucket.count <= max, retryAfter }
}
/**
* Gives back one place that `consumeRateLimit` took (`cost` 1) — for a limiter that counts only failures: it takes
* a place before it compares (a burst cannot slip under the limit) and gives it back when the attempt was right.
* Never below zero in memory; with a shared store, `hit(id, -cost)` (a store that cannot take a negative cost
* simply keeps the place: fewer attempts, never more).
*/
export async function releaseRateLimit({ cost = 1, key: rawKey, max: rawMax = 5, scope = 'form', windowMs = 60_000 }: CheckArgs): Promise<void> {
const resolved = resolveKey(rawKey, rawMax)
if (!resolved) {return}
const id = `${scope}:${resolved.key}`
if (store) {
try {
await store.hit(id, -cost, windowMs)
} catch {
// the place stays taken until the window ends
}
return
}
const bucket = buckets.get(id)
if (bucket && bucket.resetAt > Date.now()) {bucket.count = Math.max(0, bucket.count - cost)}
}
/** Seconds until the key's IN-MEMORY window resets — for a Retry-After header. */
export function retryAfterSeconds(key: string, scope = 'form'): number {
const bucket = buckets.get(`${scope}:${key}`)
+23
View File
@@ -50,6 +50,28 @@ export function assertPluginSwitches(options: Record<string, unknown>): void {
// `accounts: 0` / `''` dropped every account rule (staff-only reads, admin-only writes, the panel role,
// the password rule, the closed first-user route).
assertSwitchOrObject('accounts', options.accounts, { allowTrue: false })
// 1.18.0: the sign-in rules. `signInLimit: 0` / `'false'` must not read as off; a limit of 0 never refuses.
const accounts = bag(options.accounts)
if (accounts) {
assertBooleanSwitch('accounts.confirmOwnChanges', accounts.confirmOwnChanges)
assertSwitchOrObject('accounts.signInLimit', accounts.signInLimit, { allowTrue: false })
const limit = bag(accounts.signInLimit)
if (limit) {
assertNumberSwitch('accounts.signInLimit.perAddress', limit.perAddress, { min: 1 })
assertNumberSwitch('accounts.signInLimit.windowMinutes', limit.windowMinutes, { exclusive: true, min: 0 })
assertSwitchOrObject('accounts.signInLimit.accountLock', limit.accountLock, { allowTrue: false })
const lock = bag(limit.accountLock)
if (lock) {
assertNumberSwitch('accounts.signInLimit.accountLock.maxLoginAttempts', lock.maxLoginAttempts, { min: 1 })
assertNumberSwitch('accounts.signInLimit.accountLock.lockTime', lock.lockTime, { exclusive: true, min: 0 })
}
}
assertSwitchOrObject('accounts.signInSameOrigin', accounts.signInSameOrigin, { allowTrue: false })
const same = bag(accounts.signInSameOrigin)
if (same && same.origins !== undefined && !(Array.isArray(same.origins) && same.origins.every((o) => typeof o === 'string'))) {
throw new Error(`${where('accounts.signInSameOrigin.origins')} must be a list of origins such as ['https://panel.example.com'] (received ${JSON.stringify(same.origins)}).`)
}
}
// Opt-in features that add public endpoints or outbound calls: `'false'` switched them ON.
for (const name of ['cookieScanner', 'stats', 'consentLog', 'adminMeta', 'whiteLabel'] as const) {
@@ -106,6 +128,7 @@ export function assertPluginSwitches(options: Record<string, unknown>): void {
if (!limits) {throw new Error(`${where('mail.relay.accountMail')} must be false or an object (received ${JSON.stringify(accountMail)}).`)}
assertNumberSwitch('mail.relay.accountMail.windowMinutes', limits.windowMinutes, { exclusive: true, min: 0 })
assertNumberSwitch('mail.relay.accountMail.perAddress', limits.perAddress, { min: 1 })
assertNumberSwitch('mail.relay.accountMail.perAddressTotal', limits.perAddressTotal, { min: 1 })
assertNumberSwitch('mail.relay.accountMail.perVisitor', limits.perVisitor, { min: 1 })
}
const confirm = bag(relay.confirmRateLimit)