/** * 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 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 | 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) --------------------------------------------------------- { closedFor: ALL, name: 'Payload\'s job queue has no run route (no jobs on this site)', path: '/api/payload-jobs/run', closed: notFound }, { 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: 'nobody@example.com' }, 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' }, // 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: 'door@example.com', password: 'Door-Password-1234', roles: ['admin'] }, closedFor: ALL, method: 'POST', name: 'nobody creates a staff account', path: '/api/users', closed: forbidden }, { body: { email: 'first@example.com', 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: 'nobody@example.com' }, 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' }, { body: { form: NO_ID, submissionData: [] }, closedFor: ['anonymous'], method: 'POST', name: 'a submission cannot be posted past submitForm (rate limit, Turnstile, consent, validation)', openFor: { callers: ['customer'], question: 'Q1 (iawl-ideas-c-findings.md): any signed-in account may POST /api/form-submissions' }, 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 }, { body: {}, closedFor: ['anonymous'], method: 'PATCH', name: 'pages cannot be changed', openFor: { callers: ['customer'], question: 'Q3: collections without their own write rules run on Payload\'s default (any signed-in account)' }, path: `/api/pages/${NO_ID}`, closed: forbidden, }, { closedFor: ['anonymous'], name: 'who edits what (Payload\'s document locks, with the editor\'s account)', openFor: { callers: ['customer'], question: 'Q3: 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', }, ]