M1 review: account rules before 2FA, the SMTP password stays with its server, the rest of the findings

M1-1 the first-user route is the plugin's own endpoint in front of Payload's; nothing is read from the path.
M1-2 the account rules are put on the collections before the 2FA package wraps them.
M1-6 the admin rule only where the roles are the plugin's; other staff collections: an account of that collection.
     update / delete get the matching defaults (accounts.adminOnlyWrite); doctor rule staff-accounts-open.
M1-5 access.firstUserAdmin: false with a role below the panel role: the default panel role steps aside, an explicit one is refused.
M1-3 a save that changes the SMTP server, port, user or encryption must send the password again (secrets.reenterOnChange);
     new panel field smtpRequireTls.
M1-4 a restore of a version masks the secrets (maskSecretsOnRestore).
M1-7 a refused origin is logged once with the ways to allow it; default ports are normalised.
Minors: m1 day boundary, m3 e-mail check on reset and bulk update, m4 text of 'all', m5 the mask is never kept.
The account suite runs with 2FA off and on.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 00:39:23 +01:00
co-authored by Claude Fable 5.2
parent 81cee02e78
commit 1141c749de
28 changed files with 1557 additions and 457 deletions
+52 -2
View File
@@ -13,8 +13,25 @@
* `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.
*
* A reverse proxy that rewrites `Host` and sends no `X-Forwarded-Host` (nginx's plain
* `proxy_pass`) hides the site's address from the server: without `serverURL` the site's own
* pages are then refused. That must not happen in silence — the first refusal of every origin
* is written to the log with the ways to allow it (`reportRefusedOrigin`).
*/
type RequestLike = { headers?: { get(name: string): null | string }; payload?: { config?: { cors?: unknown; csrf?: unknown; serverURL?: unknown } }; url?: null | string }
type RequestLike = {
headers?: { get(name: string): null | string }
payload?: { config?: { cors?: unknown; csrf?: unknown; serverURL?: unknown }; logger?: { warn(message: string): void } }
url?: null | string
}
/** `site.example:443` is `site.example` for https, `:80` for http — a proxy may write the port out. */
const withoutDefaultPort = (host: string, protocol: string): string => {
const lower = host.toLowerCase()
if (protocol === 'https:' && lower.endsWith(':443')) {return lower.slice(0, -4)}
if (protocol === 'http:' && lower.endsWith(':80')) {return lower.slice(0, -3)}
return lower
}
const originOf = (value: unknown): null | string => {
if (typeof value !== 'string' || !value) {return null}
@@ -54,9 +71,42 @@ export function requestFromOwnOrigin(req: RequestLike, extra: string[] = []): bo
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())
if (origins.has(parsed)) {return true}
// `URL.origin` has already dropped the default port; a `Host` / `X-Forwarded-Host` may carry it
const { host, protocol } = new URL(parsed)
return [...hosts].some((own) => withoutDefaultPort(own, protocol) === host.toLowerCase())
}
const site = req.headers?.get('sec-fetch-site')
if (site) {return site === 'same-origin' || site === 'none'}
return true
}
const reported = new Set<string>()
const MAX_REPORTED = 50
/**
* Says in the log, once per origin and process, that a 2FA attempt was refused and how to allow
* that origin. Without it a site behind a proxy that hides its address has a panel nobody can
* enter and a silent log. Returns true when a line was written.
*/
export function reportRefusedOrigin(req: RequestLike, extra: string[] = []): boolean {
const origin = req.headers?.get('origin')
const key = origin ? (originOf(origin) ?? 'null') : `sec-fetch-site: ${req.headers?.get('sec-fetch-site') ?? 'none'}`
if (reported.has(key) || reported.size >= MAX_REPORTED) {return false}
reported.add(key)
const { hosts, origins } = ownOrigins(req, extra)
const known = [...origins, ...[...hosts].map((h) => `host ${h}`)].join(', ') || 'none'
const how = originOf(origin)
? `If "${originOf(origin)}" is this site's own address (a reverse proxy that rewrites Host and sends no X-Forwarded-Host), set serverURL to it in the Payload config, ` +
`or pass twoFactor: { sameOrigin: { origins: ['${originOf(origin)}'] } }, or make the proxy send X-Forwarded-Host. `
: 'The browser did not name an origin this site knows. '
req.payload?.logger?.warn(
`[ipal] 2FA: a code from ${origin ? `origin "${origin}"` : `a ${key} request`} was refused and not counted — it is not one of this site's own origins (${known}). ` +
how +
'If it is a foreign page, this is the protection working. twoFactor: { sameOrigin: false } turns the check off (docs/security.md "Verify and setup attempts only from the site\'s own origin"). Said once per origin.',
)
return true
}
/** For tests: forget which origins were reported. */
export const clearReportedOrigins = (): void => reported.clear()