Security: staff checks only for staff collections; 2FA covers what the plugin adds

Staff check (userMeetsRole): the 'no roles field = every account passes'
rule applied to the account's own collection, so an account of a second
auth collection (customers) passed every staff check. It now applies only
to staff collections: the panel's login collection plus the new
access.staffCollections. The injected auth collection uses the new
adminOrSelfIn(slug), so an account of another collection with the same
numeric id is not 'self'.

2FA: @clocklimited/payload-2fa wraps the collections and globals present
when it runs; ipalKit's own globals and collections were added later and
stayed unwrapped, so a password without the TOTP code opened them over
REST and GraphQL. ipal-kit now has its own second-factor check
(secondFactorPassed / requireSecondFactor): applied to every collection
and global at the end of ipalKit, again in onInit for entities added by
plugins listed after it, and inside userMeetsRole and the endpoints that
take a project access function. It fails closed: an unrecognised user
shape is refused, and startup stops when the injected 2FA plugin lacks
the parts the check reads (twoFactor.guard: false opts out).

Tests through real HTTP requests (REST and GraphQL) against Payload's
route handlers, plus the reference smoke test. Docs, agent guide, skills
and the reference skills copy updated; changelog entry without a version.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-24 00:31:55 +01:00
co-authored by Claude Fable 5.2
parent bc1045a5b7
commit 0c61ac231b
40 changed files with 1224 additions and 63 deletions
+79 -3
View File
@@ -344,8 +344,9 @@ addition to real safeguards, not a replacement for them.
The plugin enforces two-factor authentication (TOTP) for ALL panel users —
with no way to turn it off per user. Every project gets it automatically.
It uses the proven `@clocklimited/payload-2fa` (it wraps access control — TOTP is
checked before access to DATA, not only the panel UI).
It uses `@clocklimited/payload-2fa`, which wraps access control: TOTP is checked before access to DATA
(REST, GraphQL), not only in the panel UI. On top of the package ipal-kit runs its own check, so the second
factor also covers what the package cannot see — see "What the second factor covers" below.
### Dependency
@@ -368,6 +369,7 @@ ipalKit({
plugin: payloadTotp, // ← INJECT — see below why
// collectionSlug: 'users', // default: the panel login collection (admin.user, else the first auth collection)
// allowCollectionMismatch: true, // deliberate only: 2FA on a collection other than admin.user
// guard: false, // NOT recommended: ipal-kit's own check off, the package's wrapper only
},
})
```
@@ -390,6 +392,67 @@ gets the injected plugin.
The plugin sets `forceSetup: true` — every user MUST set up TOTP after
logging in. There is no per-user "on/off" option.
### What the second factor covers
The package wraps the `access` functions of the collections and globals that are in the config **at the moment
it runs**. ipalKit runs it early, so the plugin's own globals and collections (Site settings, Site integrations,
Cookie banner, Notifications, forms, form submissions, consent records, cookie review, stats) and anything a
plugin listed after `ipalKit` adds were not wrapped. **Up to 1.16.3 a staff password without the TOTP code
therefore read and changed them over the API.** Since the release after 1.16.3 ipal-kit closes that itself:
| Where | How |
|---|---|
| every collection and global in the config when ipalKit finishes | `access` wrapped with `requireSecondFactor` |
| collections and globals added by plugins listed **after** ipalKit | wrapped in ipalKit's `onInit`; the log line `[ipal] 2FA also enforced on entities added after ipalKit: …` names them |
| the plugin's endpoints (`/api/ipal/test-email`, `cookie-scan`, `cookie-approval`, `cookie-crawl`, the `access` option of `/api/ipal/doctor`) | `userMeetsRole` and `secondFactorPassed` |
The order of the project's `plugins` array does not matter. Payload's own collections (`payload-preferences`,
`payload-locked-documents`, …) and `access.admin` (may this account open the panel) are left alone: the code is
entered inside the panel.
An account of the 2FA collection passes when it logged in with **password and code**, or uses an **API key**
(`auth.useAPIKey`), or has **not set TOTP up yet** (the setup screen needs that; the package behaves the same).
Everything else is refused, including a user object the check does not recognise. Accounts of other auth
collections, anonymous visitors, bearer-token routes (`/api/ipal/doctor`, the cookie-crawl callback, MCP) and
the Local API with `overrideAccess: true` (the default) are not affected.
Two things follow for project code:
- `payload.find({ …, user, overrideAccess: false })` on a plugin collection needs a `user` that came from
`payload.auth({ headers })` (it carries how the account logged in). A user fetched with `findByID` and passed
by hand is refused when it has TOTP set up.
- Your own endpoints do not go through `access`. Guard them:
```ts
import { requireSecondFactor, secondFactorPassed, userMeetsRole } from '@intecion/ipal-kit'
// an endpoint
handler: async (req) => {
if (!req.user || !secondFactorPassed(req)) return Response.json({ ok: false }, { status: 403 })
// or: if (!userMeetsRole(req, 'editor')) … — it includes the second-factor check
}
// an access function (collections and globals in the config are wrapped for you; this is for the rest)
access: { read: requireSecondFactor(({ req }) => isAdmin(req.user)) }
```
**Fails closed.** At startup ipalKit checks that the injected 2FA plugin added what the check reads (the
`totpSecret` and `hasTotp` fields and the `totp` auth strategy on the 2FA collection). When it did not — a
package version with another shape — startup stops with `[ipal] twoFactor: the injected 2FA plugin did not
add …`. Supported: `@clocklimited/payload-2fa` 3.0.0-beta.1 to beta.7. `twoFactor.guard: false` switches
ipal-kit's check off and leaves the package's wrapper alone; the plugin's own data is then open to a password
without the code again, so use it only to get a site started while the package is being sorted out.
**Check** (replace the address and the account):
```bash
# password only, no code → 403 on all three
TOKEN=$(curl -s https://example.com/api/users/login -H 'content-type: application/json' \
-d '{"email":"[email protected]","password":"…"}' | node -pe 'JSON.parse(require("fs").readFileSync(0)).token')
for p in globals/site-integrations form-submissions users; do
curl -s -o /dev/null -w "$p %{http_code}\n" -H "Authorization: JWT $TOKEN" https://example.com/api/$p
done
```
### Import map (required)
The 2FA screens and the redirect to them are admin components. After adding `twoFactor`, and after every
@@ -469,7 +532,8 @@ An admin can reset another user's 2FA (when they lose their phone) — through
Chosen because it wraps ACCESS CONTROL (TOTP before data access) + forceSetup
(enforced for everyone). Other 2FA plugins for Payload only gate /admin
navigation — a user with a password can bypass them through REST/GraphQL/Bearer. @clocklimited protects
data access, not just the UI.
data access, not just the UI — for what is in the config when it runs; ipal-kit's own check covers the rest
("What the second factor covers").
## 1.9.1 — security review results
@@ -483,6 +547,16 @@ data access, not just the UI.
- Uploads: `buildUploadSecurityHeaders()` in `next.config.ts` (an SVG cannot run a script on the site's domain).
- `users`: the plugin injects an auth collection when the project has none (roles and 2FA always attach).
## The release after 1.16.3 — two access fixes
- **Staff checks and a second auth collection.** `userMeetsRole` treated an account of any auth collection
without a `roles` field as staff — a `customers` account passed every check of the plugin, admin ones
included. Now only accounts of a staff collection can pass: the panel's login collection plus
`access.staffCollections` ([access.md](./access.md#who-is-staff)). The injected auth collection uses
`adminOrSelfIn(slug)`, so an account of another collection with the same numeric id no longer counts as "self".
- **2FA and the plugin's own data.** See "What the second factor covers" above.
- After upgrading: [PROGRAMISTA.md](./dev/PROGRAMISTA.md) upgrade table, and the CHANGELOG entry of that release.
## Form files (`forms.attachments`)
Files attached to a form are **never stored and cannot be downloaded**: they are checked in memory, attached to
@@ -517,6 +591,8 @@ size while reading, content signature, container and macro checks, an optional `
## Turn off, override, extend
- **2FA check:** `twoFactor.guard: false` turns ipal-kit's own second-factor check off (not recommended);
`requireSecondFactor` / `secondFactorPassed` are exported for your own access functions and endpoints.
- **Turn off:** headers — do not call `buildSecurityHeaders`, or `null` / `false` per header (`frameOptions`,
`referrerPolicy`, `permissionsPolicy`, `coop`), `hsts: false`; CSP — leave out `csp` (the default);
X-Powered-By hiding — `withSecurityConfig(config, { hidePoweredBy: false })` or do not call it;