M1 review round 3: late staff collections without guessing Payload's default; rules survive a dev reload

R3-1 nothing is decided by a function's name any more (a production build renames it): on a collection that
     arrives after ipalKit the plugin's rule is put in FRONT of the rule that is there and both must allow
     (bothAllow). unlock follows the same table. The log says exactly what was put.
R3-2 the adoption walks the live config (payload.config), which Payload swaps first on a dev reload.
R3-3 readVersions gets the read rule on staff collections.
R3-5 a secret that is not text is refused.
Also: the async check of requireSetup.exempt compares the constructor, not its name.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 00:39:24 +01:00
co-authored by Claude Fable 5.2
parent 3e44863c98
commit 6e0ad26b2c
13 changed files with 288 additions and 72 deletions
+10 -4
View File
@@ -197,12 +197,18 @@ export const ipalKit = (options: IpalOptions): Plugin => {
// 2FA guard below wraps their access (it needs the rule inside its wrapper).
if (accounts) {
const rule = resolvePasswordRule(accounts.password)
const lateAccounts = adoptLateAuthCollections(payload as never, staffCollectionSlugs(payload), {
// `payload.config`, not `payload.collections`: on a dev reload Payload swaps the config first, and
// `payload.collections` still holds the old one at this moment.
const lateAccounts = adoptLateAuthCollections(payload.config as never, staffCollectionSlugs(payload), {
access: { ...accountAccess, pluginRoles: [] },
beforeOperation: rule ? buildPasswordRuleHook(rule) : undefined,
firstUserRoute: accounts.firstUserRoute,
})
if (lateAccounts.length) {payload.logger.info(`[ipal] Account rules also put on auth collections added after ipalKit: ${lateAccounts.join(', ')}.`)}
// said exactly: what was put, on which collection
for (const late of lateAccounts) {
const parts = [late.applied.length ? `access rules for ${late.applied.join(', ')}` : '', late.password ? 'the password rule' : '', late.firstUser ? 'the first-user endpoint' : ''].filter(Boolean)
payload.logger.info(`[ipal] "${late.slug}" was added after ipalKit: put on it — ${parts.join('; ')}.`)
}
}
if (!secondFactorGuard) {return}
const late = ensurePayloadGuarded(payload)
@@ -894,11 +900,11 @@ export const ipalKit = (options: IpalOptions): Plugin => {
if (incomingOnInit) {await incomingOnInit(payload)}
// A staff collection that does not exist protects nothing and hides a typo: its accounts would not be staff,
// or — worse — a collection of that name added later would be, without anyone having decided it.
const unknownStaff = unknownStaffCollections(payload as never, [...new Set([options.access?.authCollection, ...(options.access?.staffCollections ?? [])].filter((s): s is string => Boolean(s)))])
const unknownStaff = unknownStaffCollections(payload.config as never, [...new Set([options.access?.authCollection, ...(options.access?.staffCollections ?? [])].filter((s): s is string => Boolean(s)))])
if (unknownStaff.length) {
throw new Error(
`[ipal] access names ${unknownStaff.map((s) => `"${s}"`).join(', ')} as a staff collection, but this config has no auth collection with that slug ` +
`(auth collections: ${Object.values(payload.collections).filter((c) => c.config.auth).map((c) => c.config.slug).join(', ') || 'none'}). ` +
`(auth collections: ${payload.config.collections.filter((c) => c.auth).map((c) => c.slug).join(', ') || 'none'}). ` +
'Fix the slug in access.authCollection / access.staffCollections, or add the collection (docs/access.md "Who is staff").',
)
}