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:
co-authored by
Claude Fable 5.2
parent
0b4c4e8295
commit
f7296edc71
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 2FA verify and setup attempts only from the site's own origin.
|
||||
*
|
||||
* The verify limit counts every attempt of an account. A page on another origin that the
|
||||
* browser still sends the login cookie to — a sibling subdomain is "same site", so
|
||||
* `SameSite=Lax` does not stop it, and Payload's own `csrf` list is empty unless `serverURL`
|
||||
* or `csrf` is set — could post wrong codes in the owner's name: no password needed, the
|
||||
* owner's five attempts burnt, the owner locked out for the window. So a request that a
|
||||
* browser marks as coming from elsewhere is refused BEFORE it is counted.
|
||||
*
|
||||
* What counts as the site's own origin: the host the request came in on (`Host`,
|
||||
* `X-Forwarded-Host`, the request URL), `serverURL`, Payload's `csrf` and `cors` lists, and
|
||||
* `twoFactor.sameOrigin.origins`. A request with neither `Origin` nor `Sec-Fetch-Site` is not
|
||||
* a browser's cross-site request (a script, `curl`, a server) and passes: it cannot ride on
|
||||
* somebody's cookie.
|
||||
*/
|
||||
type RequestLike = { headers?: { get(name: string): null | string }; payload?: { config?: { cors?: unknown; csrf?: unknown; serverURL?: unknown } }; url?: null | string }
|
||||
|
||||
const originOf = (value: unknown): null | string => {
|
||||
if (typeof value !== 'string' || !value) {return null}
|
||||
try {
|
||||
return new URL(value).origin
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** Origins and hosts this request may come from. */
|
||||
export function ownOrigins(req: RequestLike, extra: string[] = []): { hosts: Set<string>; origins: Set<string> } {
|
||||
const config = req.payload?.config
|
||||
const cors = config?.cors
|
||||
const listed = [
|
||||
config?.serverURL,
|
||||
...(Array.isArray(config?.csrf) ? (config.csrf as unknown[]) : []),
|
||||
...(Array.isArray(cors) ? (cors as unknown[]) : Array.isArray((cors as { origins?: unknown } | undefined)?.origins) ? ((cors as { origins: unknown[] }).origins) : []),
|
||||
...extra,
|
||||
]
|
||||
const origins = new Set(listed.map(originOf).filter((o): o is string => o !== null))
|
||||
const hosts = new Set<string>()
|
||||
for (const name of ['host', 'x-forwarded-host']) {
|
||||
// a proxy may list several; the first is the one the browser asked for
|
||||
const value = req.headers?.get(name)?.split(',')[0]?.trim().toLowerCase()
|
||||
if (value) {hosts.add(value)}
|
||||
}
|
||||
const fromUrl = originOf(req.url)
|
||||
if (fromUrl) {hosts.add(new URL(fromUrl).host)}
|
||||
return { hosts, origins }
|
||||
}
|
||||
|
||||
/** False when a browser says the request comes from another origin (see the top of the file). */
|
||||
export function requestFromOwnOrigin(req: RequestLike, extra: string[] = []): boolean {
|
||||
const origin = req.headers?.get('origin')
|
||||
if (origin) {
|
||||
const parsed = originOf(origin)
|
||||
if (!parsed) {return false} // "null" (a sandboxed page, a redirect chain) or garbage
|
||||
const { hosts, origins } = ownOrigins(req, extra)
|
||||
return origins.has(parsed) || hosts.has(new URL(parsed).host.toLowerCase())
|
||||
}
|
||||
const site = req.headers?.get('sec-fetch-site')
|
||||
if (site) {return site === 'same-origin' || site === 'none'}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user