Security review fixes: staff collection first everywhere, 2FA package endpoints guarded, per-entity opt-out, hot reload
F1: roles count only for accounts of a staff collection, in every exported predicate and access builder (isAdmin, isEditor, hasMinimumRole, adminOnly, adminOrEditor, requireRole, adminOrSelf, adminOrSelfIn, the Field variants, the roles field) and first in userMeetsRole. An account of another auth collection with its own roles: ['admin'] gets nothing. Staff collections: admin.user, access.authCollection, access.staffCollections. Predicates take req.payload as an optional last argument and otherwise use the staff collections of the config ipalKit built in this process. F2: every endpoint @clocklimited/payload-2fa registers answers accounts of the 2FA collection only (setup-totp wrote the secret into the 2FA collection under the caller's id, whatever collection the caller came from); remove and the admin reset/remove endpoints also need the second factor. F3: per-entity opt-out honoured, operation by operation: the package's custom.totp.disableAccessWrapper and ipal-kit's custom.ipal.secondFactor (passed on to the package); the package's global disableAccessWrapper too. F4: a no-user auth strategy on the 2FA collection guards the live config at the start of each request, so entities from plugins listed after ipalKit stay guarded after a dev hot reload and with disableOnInit. Tests through HTTP for all four; the 2FA tests now run on payload-2fa 3.0.0-beta.7 (what client sites use), a second file on beta.1 (alias), and the HTTP files can run against Postgres (IPAL_TEST_POSTGRES_URI). Docs, agent guide, skills and changelog updated; the not-yet-enrolled limitation is stated plainly. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
97ee1289aa
commit
32faaa5e30
+44
-25
@@ -67,35 +67,49 @@ keeps Payload's default rules — without roles all accounts are equal.
|
||||
|
||||
## Who is staff
|
||||
|
||||
**Staff collections** are the auth collections whose accounts work in the panel:
|
||||
|
||||
- the one the admin panel logs in with (`admin.user`; Payload's default is the **first** auth collection in the
|
||||
list — set `admin: { user: 'users' }` yourself as soon as the project has two);
|
||||
- the one that carries the plugin's roles (`access.authCollection`);
|
||||
- the ones named in `access.staffCollections`.
|
||||
|
||||
An account of **any other auth collection** — `customers`, `members`, a client zone — is a visitor to the
|
||||
plugin. **Nothing in the plugin counts a role for it**, also when its own collection has a `roles` field with
|
||||
the value `admin`: not `userMeetsRole`, not `isAdmin` / `isEditor` / `hasMinimumRole`, not `adminOnly`,
|
||||
`adminOrEditor`, `requireRole`, `adminOrSelf`, `adminOrSelfIn` or the `…Field` variants, not the `roles` field
|
||||
itself. The staff-collection check always comes first.
|
||||
|
||||
The plugin's own staff features — Site integrations, the settings globals, form submissions, consent records,
|
||||
stats, the cookie scanner, `/api/ipal/test-email` — check with `userMeetsRole(req, minimum)`. It passes when:
|
||||
stats, the cookie scanner, `/api/ipal/test-email` — check with `userMeetsRole(req, minimum)`, in this order:
|
||||
|
||||
1. the account holds at least the role `minimum`; or
|
||||
2. the account belongs to a **staff collection** that has **no `roles` field**. Then every account of that
|
||||
collection passes, as in Payload's default (without roles all accounts are equal).
|
||||
1. the account belongs to a staff collection;
|
||||
2. with 2FA on: an account that has TOTP set up has entered the code in this session
|
||||
([security.md](./security.md) → "What the second factor covers");
|
||||
3. it holds at least the role `minimum` — **or** its collection has no `roles` field at all. Then every account
|
||||
of that staff collection passes, as in Payload's default (without roles all accounts are equal).
|
||||
|
||||
**Staff collections** are the auth collection the admin panel logs in with (`admin.user`, by default the first
|
||||
auth collection) and the ones named in `access.staffCollections`. An account of **any other auth collection** —
|
||||
`customers`, `members`, a client zone — is a visitor to the plugin and never passes rule 2, with or without
|
||||
the `access` option.
|
||||
What follows:
|
||||
|
||||
With 2FA on, an account that has TOTP set up and has not entered the code in this session passes neither rule
|
||||
([security.md](./security.md) → 2FA).
|
||||
|
||||
- **Without `access`** there is no `roles` field, so `isAdmin` always returns `false` and rule 2 keeps the
|
||||
- **Without `access`** there is no `roles` field, so `isAdmin` always returns `false` and rule 3 keeps the
|
||||
staff features reachable: every account of the panel collection is admin to the plugin. To narrow that
|
||||
without roles: `globalOverrides` for the globals, `forms.formSubmissionOverrides` for submissions, or add
|
||||
`access` to get roles.
|
||||
- **A second staff collection** (e.g. `partners` who edit content): name it in
|
||||
`access.staffCollections`. Without a `roles` field every partner then passes every staff check, admin ones
|
||||
included. To tell them apart, add the field: `fields: [buildRolesField('editor')]` in that collection.
|
||||
`access.staffCollections`. That is the one switch that makes its roles count. Without a `roles` field every
|
||||
partner passes every staff check, admin ones included; to tell them apart add the field:
|
||||
`fields: [buildRolesField('editor')]` in that collection.
|
||||
- **A visitor collection that should reach one thing** (e.g. customers read their own form submissions):
|
||||
do not make it staff. Write that one rule: `forms.formSubmissionOverrides.access.read`, `globalOverrides`,
|
||||
`collectionOverrides` of the module.
|
||||
do not make it staff. Write that one rule yourself — `forms.formSubmissionOverrides.access.read`,
|
||||
`globalOverrides`, `collectionOverrides` of the module — and check `req.user.collection` in it.
|
||||
- **A user object you build yourself** (Local API with `overrideAccess: false`, a custom auth strategy) must
|
||||
carry `collection`: `{ ...doc, collection: 'users' }`. Payload's own strategies and `payload.auth()` set it. An
|
||||
account without it is not staff.
|
||||
|
||||
> **Changed in the release after 1.16.3.** Before, rule 2 applied to the account's own collection, whatever it
|
||||
> was: an account of a second auth collection without a `roles` field passed every staff check. A project
|
||||
> with one auth collection sees no difference. A project that relied on a second collection being staff sets
|
||||
> **Changed in the release after 1.16.3.** Before, a role or the "no roles field" rule counted for an account
|
||||
> of any auth collection: an account of a second auth collection passed staff checks — every one when its
|
||||
> collection had no `roles` field, and the matching ones when it had its own `roles` field. A project with one
|
||||
> auth collection sees no difference. A project that relied on a second collection being staff sets
|
||||
> `access.staffCollections`.
|
||||
|
||||
## Predicates (front end / hooks / access)
|
||||
@@ -103,14 +117,19 @@ With 2FA on, an account that has TOTP set up and has not entered the code in thi
|
||||
```ts
|
||||
import { isAdmin, isEditor, hasMinimumRole, staffCollectionSlugs, userMeetsRole } from '@intecion/ipal-kit'
|
||||
|
||||
isAdmin(user) // admin?
|
||||
isEditor(user) // editor or admin (hierarchy)
|
||||
hasMinimumRole(user, 'editor') // at least editor
|
||||
userMeetsRole(req, 'admin') // admin — or any account of a staff collection that has no roles field ("Who is staff")
|
||||
staffCollectionSlugs(req.payload) // ['users', …]: admin.user + access.staffCollections
|
||||
isAdmin(req.user, req.payload) // a staff account with the admin role
|
||||
isEditor(req.user, req.payload) // editor or admin (hierarchy)
|
||||
hasMinimumRole(req.user, 'editor', req.payload) // at least editor
|
||||
userMeetsRole(req, 'admin') // the plugin's staff check: staff collection, second factor, role ("Who is staff")
|
||||
staffCollectionSlugs(req.payload) // ['users', …]
|
||||
```
|
||||
|
||||
They take a loosely typed user (like `req.user`) and read `roles` defensively.
|
||||
They take a loosely typed user (like `req.user`) and read `roles` defensively. The last argument (`req.payload`)
|
||||
tells them which collections are staff. Without it — `isAdmin(req.user)` — they use the staff collections of
|
||||
the config `ipalKit` built in this process, so existing calls in hooks and endpoints are covered too. Only in a
|
||||
browser bundle, where there is no config, the roles are taken as they are: fine for showing or hiding a
|
||||
button, never a security check. `isAdmin` and the others do **not** check the second factor; `userMeetsRole`
|
||||
and every `access` function in the config do.
|
||||
|
||||
## Ready access functions
|
||||
|
||||
|
||||
Reference in New Issue
Block a user