Files
ipal-kit/reference/e2e/closed-doors.ts
T
Radosław SmolińskiandClaude Opus 5.5 c9d1f4b340 2FA: the first set-up is confirmed by e-mail (twoFactor.confirmByEmail, 1.18.0)
Security audit 2026-12, F0: inside an account's setup window its password alone
enrolled an authenticator of the caller's choosing; the owner was locked out.

- /setup-totp for an account without a factor: IAPL checks the code, keeps the
  secret pending (payload-kv), mails a single-use 30-min link to the account and
  ties it to the enrolling browser; the package's handler (and its -totp cookie)
  is never used for a first enrolment
- GET /api/iapl/two-factor/confirm = a page with one button; POST confirms
  (own origin, same browser), ends the account's sessions, signs nobody in
- notice mails on every enrolment, change, removal / reset (any path)
- mail not working: link to the server log, admin confirms on the account page
  (POST /api/iapl/two-factor/confirm-pending), doctor rule 2fa-enrolment-mail
- options and opt-out (confirmByEmail: false), building blocks exported
- tests: HTTP suite on SQLite, MongoDB, Postgres; built-site spec in e2e:built;
  closed doors and judged routes; docs, AGENT.md, skills, changelog

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-01 13:37:39 +01:00

133 lines
11 KiB
TypeScript

/**
* The doors of this site that must stay CLOSED, asked from outside on the built site in production mode by
* e2e/closed-doors.spec.ts (`pnpm e2e:built`, both bundlers) as three callers:
* anonymous — no session;
* customer — an account of a visitor collection that signs itself up (`e2e-visitors`, late-staff fixture,
* E2E_LATE_STAFF=1); stands for a shop's or a member zone's customers;
* editor — a staff account with the role `editor`, signed in with the second factor.
* A project adds its own doors here (docs/security.md "Closed doors from outside"). Every door says who it is
* closed for and how a closed answer looks; a caller not named may pass (an editor reads the submissions).
*
* A door that is still OPEN for a caller while the owner decides is listed under `openFor`, with the question: the
* spec asserts that it is open today (`test.fail` — when somebody closes it, the test reports "passed
* unexpectedly" and the entry moves to `closedFor`). A door that writes is asked with a body that cannot change
* anything when the door is open: a document id that does not exist, an empty update, a form id that does not exist.
*/
export type Caller = 'anonymous' | 'customer' | 'editor'
export type DoorAnswer = { body: unknown; location: null | string; status: number }
export type Door = {
/** GraphQL: the query, sent as POST /api/graphql. REST: the path. */
body?: unknown
/** How a closed answer looks; default: status 401, 403 or 404. */
closed?: (answer: DoorAnswer, ctx: { email?: string }) => boolean
closedFor: Caller[]
graphql?: string
/** Extra request headers (e.g. a wrong bearer token). */
headers?: Record<string, string>
method?: 'DELETE' | 'GET' | 'PATCH' | 'POST'
name: string
/** Open for these callers while the owner decides (the finding's question). */
openFor?: { callers: Caller[]; question: string }
path?: string
}
export const ALL: Caller[] = ['anonymous', 'customer', 'editor']
export const OUTSIDERS: Caller[] = ['anonymous', 'customer']
const NO_ID = '000000000000000000000000'
export const refused = ({ status }: DoorAnswer) => [401, 403, 404].includes(status)
const refusedOrNotOk = (answer: DoorAnswer) => refused(answer) || (answer.body as { ok?: unknown } | null)?.ok === false
const forbidden = ({ status }: DoorAnswer) => status === 403
const notFound = ({ status }: DoorAnswer) => status === 404
/** A GraphQL answer that carries no data for the field: an error, or null. */
const noData = (field: string) => ({ body, status }: DoorAnswer) => {
const b = body as { data?: Record<string, unknown> | null; errors?: unknown[] } | null
return status >= 400 || Boolean(b?.errors?.length) || b?.data?.[field] == null
}
export const DOORS: Door[] = [
// ---- secrets and the system's own state ------------------------------------------------------------------
{ closedFor: ALL, name: 'Site integrations (SMTP password, keys) cannot be read', path: '/api/globals/site-integrations' },
{ body: {}, closedFor: ALL, method: 'POST', name: 'Site integrations cannot be saved', path: '/api/globals/site-integrations' },
{ closedFor: ALL, graphql: '{ SiteIntegration { smtpHost } }', closed: noData('SiteIntegration'), name: 'Site integrations over GraphQL' },
{ closedFor: ALL, name: 'Payload\'s key-value store (2FA counts, the mail-service token) cannot be listed', path: '/api/payload-kv' },
{ closedFor: ALL, graphql: '{ PayloadKvs { docs { key } } }', closed: noData('PayloadKvs'), name: 'the key-value store over GraphQL' },
{ closedFor: ALL, name: 'the consent log (admin only)', path: '/api/consent-records' },
{ closedFor: ALL, name: 'the deep doctor status answers 404 without its token', path: '/api/ipal/doctor', closed: notFound },
// ---- jobs, schedules and call-backs (IAWL idea 35) ---------------------------------------------------------
// the fixture's do-nothing task turns the queue on; since 1.17.2 (owner's Q2) only an admin or Bearer CRON_SECRET runs it
// (the "with the secret" case is its own test in closed-doors.spec.ts). Payload answers 401 when refused.
{ closedFor: ALL, name: 'Payload\'s job queue cannot be run (only an admin or the cron secret)', path: '/api/payload-jobs/run' },
{ closedFor: ALL, name: 'Payload\'s job schedules cannot be handled (only an admin or the cron secret)', path: '/api/payload-jobs/handle-schedules' },
{ closedFor: ALL, name: 'a wrong cron secret runs nothing', path: '/api/payload-jobs/run', headers: { authorization: 'Bearer not-the-cron-secret-not-the-cron-secret' } },
{ closedFor: ALL, method: 'POST', name: 'the crawler\'s call-back without its token', path: '/api/ipal/cookie-crawl/results', body: { jobId: 'x', ok: true } },
{ closedFor: OUTSIDERS, method: 'POST', name: 'a crawl cannot be started without the schedule\'s token', path: '/api/ipal/cookie-crawl' },
{ closedFor: OUTSIDERS, name: 'the crawl status', path: '/api/ipal/cookie-crawl' },
{ closedFor: OUTSIDERS, method: 'POST', name: 'a cookie scan cannot be run', path: '/api/ipal/cookie-scan' },
{ closedFor: OUTSIDERS, name: 'the cookie approval list', path: '/api/ipal/cookie-approval' },
{ closedFor: OUTSIDERS, body: { ids: [NO_ID] }, method: 'POST', name: 'cookies cannot be approved', path: '/api/ipal/cookie-approval' },
{ closedFor: OUTSIDERS, name: 'the cookie review entries', path: '/api/cookie-review' },
{ closedFor: OUTSIDERS, name: 'the crawl global', path: '/api/globals/cookie-crawl' },
// ---- admin actions ---------------------------------------------------------------------------------------
{ body: { to: '[email protected]' }, closedFor: ALL, method: 'POST', name: 'a test e-mail cannot be sent', path: '/api/ipal/test-email' },
{ closedFor: ALL, name: 'the mail-service status', path: '/api/iapl/mail-relay/status' },
{ body: { token: 'ims_x' }, closedFor: ALL, method: 'POST', name: 'the mail service cannot be connected', path: '/api/iapl/mail-relay/connect' },
{ closedFor: ALL, method: 'POST', name: 'the mail service cannot be disconnected', path: '/api/iapl/mail-relay/disconnect' },
{ closedFor: ALL, method: 'POST', name: 'the confirm route answers 404 without a nonce of this site', path: '/api/iapl/mail-relay/confirm', body: { nonce: 'n'.repeat(40), state: 'active', token: `ims_${'a'.repeat(43)}` }, closed: notFound },
{ body: { id: NO_ID }, closedFor: ALL, method: 'POST', name: 'another account\'s 2FA cannot be reset', path: '/api/ipal/two-factor/reset' },
{ body: { userId: NO_ID }, closedFor: ALL, method: 'POST', name: 'another account\'s waiting 2FA set-up cannot be confirmed (1.18.0)', path: '/api/iapl/two-factor/confirm-pending' },
{ body: 'u=1&t=' + 'x'.repeat(43), closedFor: ['anonymous'], headers: { 'content-type': 'application/x-www-form-urlencoded' }, method: 'POST', name: 'a made-up 2FA confirmation link turns nothing on (1.18.0)', path: '/api/iapl/two-factor/confirm', closed: ({ status }) => status === 400 || status === 403 },
// payload-2fa refuses with HTTP 200 and `{ ok: false }` (its own convention), so the body is what is judged
{ body: { id: NO_ID }, closedFor: ALL, method: 'POST', name: 'another account\'s 2FA cannot be removed (payload-2fa)', path: '/api/admin/remove-user-totp', closed: refusedOrNotOk },
{ body: { id: NO_ID }, closedFor: ALL, method: 'POST', name: 'another account\'s 2FA cannot be reset (payload-2fa)', path: '/api/admin/reset-user-totp', closed: refusedOrNotOk },
// ---- accounts --------------------------------------------------------------------------------------------
{ body: { email: '[email protected]', password: 'Door-Password-1234', roles: ['admin'] }, closedFor: ALL, method: 'POST', name: 'nobody creates a staff account', path: '/api/users', closed: forbidden },
{ body: { email: '[email protected]', password: 'Door-Password-1234' }, closedFor: ALL, method: 'POST', name: 'the first-user route is closed once an account exists', path: '/api/users/first-register', closed: forbidden },
{ body: { email: '[email protected]' }, closedFor: ALL, method: 'POST', name: 'nobody unlocks an account', path: '/api/users/unlock', closed: forbidden },
{ body: { roles: ['admin'] }, closedFor: ALL, method: 'PATCH', name: 'nobody changes another account', path: `/api/users/${NO_ID}`, closed: forbidden },
{
closedFor: ALL,
// an editor reads its own account only (adminOrSelfIn): one document, its own
closed: (answer, { email }) => refused(answer) || (Array.isArray((answer.body as { docs?: { email?: string }[] })?.docs) && (answer.body as { docs: { email?: string }[] }).docs.every((d) => d.email === email)),
name: 'the staff accounts cannot be listed (an editor sees its own only)',
path: '/api/users?limit=100',
},
{ closedFor: OUTSIDERS, graphql: '{ Users { docs { email } } }', closed: noData('Users'), name: 'the staff accounts over GraphQL' },
// ---- content and visitors' data --------------------------------------------------------------------------
{ closedFor: OUTSIDERS, name: 'form submissions (visitors\' personal data)', path: '/api/form-submissions' },
{ closedFor: OUTSIDERS, graphql: '{ FormSubmissions { docs { id } } }', closed: noData('FormSubmissions'), name: 'form submissions over GraphQL' },
// since 1.17.2 (owner's Q1): a staff account with its second factor only — an editor may (it reaches validation)
{ body: { form: NO_ID, submissionData: [] }, closedFor: OUTSIDERS, method: 'POST', name: 'a submission cannot be posted past submitForm (rate limit, Turnstile, consent, validation)', path: '/api/form-submissions', closed: forbidden },
{ body: {}, closedFor: OUTSIDERS, method: 'POST', name: 'Site settings cannot be saved', path: '/api/globals/site-settings', closed: forbidden },
{ body: {}, closedFor: OUTSIDERS, method: 'POST', name: 'Cookie settings (legal texts) cannot be saved', path: '/api/globals/cookie-settings', closed: forbidden },
// the reference site's content has write rules of its own since 1.17.2 (owner's Q3, answer (b)): editors and admins
{ body: {}, closedFor: OUTSIDERS, method: 'PATCH', name: 'pages cannot be changed', path: `/api/pages/${NO_ID}`, closed: forbidden },
{ body: {}, closedFor: OUTSIDERS, method: 'PATCH', name: 'media cannot be changed', path: `/api/media/${NO_ID}`, closed: forbidden },
{ body: {}, closedFor: OUTSIDERS, method: 'POST', name: 'the header (navigation) cannot be saved', path: '/api/globals/header', closed: forbidden },
{ body: {}, closedFor: OUTSIDERS, method: 'POST', name: 'the company data cannot be saved', path: '/api/globals/company', closed: forbidden },
{
closedFor: ['anonymous'],
name: 'who edits what (Payload\'s document locks, with the editor\'s account)',
// Payload's own collection: the plugin-wide default for it is the owner's Q3 answer (a), recorded for 2.0; the doctor's
// `visitor-writes` rule lists it on a site with a visitor login collection
openFor: { callers: ['customer'], question: 'Q3 (a), 2.0: payload-locked-documents runs on Payload\'s default (any signed-in account)' },
path: '/api/payload-locked-documents',
closed: forbidden,
},
// ---- what differs in production (IAWL idea 33) ------------------------------------------------------------
{ closedFor: ALL, name: 'the GraphQL playground is off in production', path: '/api/graphql-playground', closed: notFound },
{
closedFor: ALL,
graphql: '{ __schema { queryType { name } } }',
closed: ({ body }) => Boolean((body as { errors?: unknown[] })?.errors?.length) && !(body as { data?: { __schema?: unknown } })?.data?.__schema,
name: 'GraphQL introspection is off in production',
},
]