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
+37 -2
View File
@@ -10,7 +10,7 @@ import type { I18nConfig } from '../src/modules/i18n/types.js'
import { adminOrSelfIn } from '../src/modules/access/access.js'
import { isAdmin, staffCollectionSlugs, userMeetsRole } from '../src/modules/access/predicates.js'
import { requestFromOwnOrigin } from '../src/modules/security/totp/sameOrigin.js'
import { clearReportedOrigins, reportRefusedOrigin, requestFromOwnOrigin } from '../src/modules/security/totp/sameOrigin.js'
import {
ensurePayloadGuarded,
guardPayloadAccess,
@@ -422,6 +422,14 @@ describe('ipalKit and the 2FA plugin', () => {
['a sibling subdomain', { host: 'site.example', origin: 'https://blog.site.example' }, { serverURL: 'https://site.example' }, false],
['another site', { host: 'site.example', origin: 'https://evil.example' }, {}, false],
['the same host on another port', { host: 'site.example', origin: 'https://site.example:8443' }, {}, false],
// a proxy that writes the default port out
['the forwarded host with :443 for an https origin', { host: 'internal:3000', origin: 'https://site.example', 'x-forwarded-host': 'site.example:443' }, {}, true],
['the host with :80 for an http origin', { host: 'site.example:80', origin: 'http://site.example' }, {}, true],
['Origin with the default port written out', { host: 'site.example', origin: 'https://site.example:443' }, {}, true],
[':443 is not the default of http', { host: 'site.example:443', origin: 'http://site.example' }, {}, false],
[':80 is not the default of https', { host: 'site.example:80', origin: 'https://site.example' }, {}, false],
// nginx's plain proxy_pass: Host rewritten, no X-Forwarded-Host, no serverURL — the site's own page is refused (and reported, below)
['a proxy that hides the address, nothing configured', { host: 'upstream:3000', origin: 'https://site.example' }, {}, false],
['Origin: null (a sandboxed page)', { host: 'site.example', origin: 'null' }, {}, false],
['Sec-Fetch-Site: same-origin without Origin', { 'sec-fetch-site': 'same-origin' }, {}, true],
['Sec-Fetch-Site: same-site (a sibling subdomain) without Origin', { 'sec-fetch-site': 'same-site' }, {}, false],
@@ -430,6 +438,29 @@ describe('ipalKit and the 2FA plugin', () => {
expect(requestFromOwnOrigin(req(headers as Record<string, string>, config))).toBe(expected)
})
test('a refusal is never silent: one line per origin, with the ways to allow it', () => {
clearReportedOrigins()
const warn = vi.fn()
const hidden = { headers: new Headers({ host: 'upstream:3000', origin: 'https://site.example' }), payload: { config: {}, logger: { warn } }, url: 'http://upstream:3000/api/verify-totp' }
expect(reportRefusedOrigin(hidden)).toBe(true)
expect(reportRefusedOrigin(hidden)).toBe(false) // once per origin
expect(warn).toHaveBeenCalledTimes(1)
const line = warn.mock.calls[0][0] as string
expect(line).toContain('origin "https://site.example"')
expect(line).toContain('host upstream:3000')
expect(line).toMatch(/serverURL/)
expect(line).toMatch(/sameOrigin: \{ origins: \['https:\/\/site\.example'\] \}/)
expect(line).toMatch(/X-Forwarded-Host/)
// another origin gets its own line; a request without Origin is named by what the browser said
expect(reportRefusedOrigin({ ...hidden, headers: new Headers({ origin: 'https://evil.example' }) })).toBe(true)
expect(reportRefusedOrigin({ ...hidden, headers: new Headers({ 'sec-fetch-site': 'cross-site' }) })).toBe(true)
expect(warn.mock.calls[2][0]).toContain('sec-fetch-site: cross-site')
// the log cannot be flooded with made-up origins
for (let i = 0; i < 200; i++) {reportRefusedOrigin({ ...hidden, headers: new Headers({ origin: `https://n${i}.example` }) })}
expect(warn.mock.calls.length).toBeLessThanOrEqual(50)
clearReportedOrigins()
})
test('twoFactor.sameOrigin.origins adds an origin', () => {
const other = req({ host: 'site.example', origin: 'https://panel.example.org' })
expect(requestFromOwnOrigin(other)).toBe(false)
@@ -438,13 +469,15 @@ describe('ipalKit 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 warn = vi.fn()
clearReportedOrigins()
const withEndpoints = (o: { collection: string }) => (c: Config) => ({
...withTotpParts(c, o.collection),
endpoints: [...(c.endpoints ?? []), ...['/verify-totp', '/setup-totp', '/remove-totp'].map((path) => ({ handler, method: 'post' as const, path }))],
})
const build = (twoFactor: Record<string, unknown> = {}) => ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: withEndpoints as never, ...twoFactor } as never })({ collections: [], endpoints: [], secret: 'x' } as unknown as Config)
const call = async (config: Config, path: string, headers: Record<string, string>, user: Record<string, unknown>) =>
(await config.endpoints!.find((e) => e.path === path)!.handler({ headers: new Headers(headers), i18n: undefined, payload: { config, logger: { warn: () => {} } }, url: 'https://site.example/api' + path, user: { id: 5, collection: 'users', ...user } } as never)).status
(await config.endpoints!.find((e) => e.path === path)!.handler({ headers: new Headers(headers), i18n: undefined, payload: { config, logger: { warn } }, url: 'https://site.example/api' + path, user: { id: 5, collection: 'users', ...user } } as never)).status
const elsewhere = { host: 'site.example', origin: 'https://blog.site.example' }
const own = { host: 'site.example', origin: 'https://site.example' }
const owes = { _strategy: 'local-jwt', hasTotp: true }
@@ -455,6 +488,8 @@ describe('ipalKit and the 2FA plugin', () => {
// twenty attempts from elsewhere: all refused, none counted
for (let i = 0; i < 20; i++) {expect(await call(config, '/verify-totp', elsewhere, owes)).toBe(403)}
expect(handler).not.toHaveBeenCalled()
// …and said in the log, once, with the origin and how to allow it
expect(warn.mock.calls.filter(([line]) => String(line).includes('blog.site.example'))).toHaveLength(1)
for (let i = 0; i < 5; i++) {expect(await call(config, '/verify-totp', own, owes)).toBe(200)} // the owner still has all five
expect(await call(config, '/verify-totp', own, owes)).toBe(429)
expect(await call(config, '/setup-totp', elsewhere, fresh)).toBe(403)