Lock down injected users, 2FA slug, forms and globals writes

- Injected auth collection with roles: admins manage all accounts, others
  only themselves (Payload's default let any account edit or delete any other)
- 2FA defaults to the resolved auth collection instead of 'users'
- SiteSettings, CookieSettings, Notifications, Forms: write needs editor+;
  form Emails tab readable by editor+ only

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-22 16:58:00 +02:00
co-authored by Claude Opus 5.5
parent 12fbd424a5
commit 10da62ea4c
10 changed files with 194 additions and 7 deletions
+20 -3
View File
@@ -7,7 +7,7 @@ import { migrateCookieSettingsSaveButton } from './globals/CookieSettings/migrat
import { buildNotifications } from './globals/Notifications/index.js'
import { buildSiteIntegrations } from './globals/SiteIntegrations/index.js'
import { buildSiteSettings } from './globals/SiteSettings/index.js'
import { injectRoles } from './modules/access/index.js'
import { adminOnly, adminOrSelf, injectRoles } from './modules/access/index.js'
import { buildConsentLogCollection, buildConsentLogEndpoint } from './modules/consent/consentLog.js'
import { buildConstructionFields } from './modules/construction/fields.js'
import { buildArchiveFields } from './modules/content/index.js'
@@ -65,10 +65,23 @@ export const ipalKit = (options: IpalOptions): Plugin => {
(options.twoFactor ? options.twoFactor.collectionSlug : undefined) ??
config.admin?.user ??
'users'
// With roles on it (access.authCollection), the injected collection is
// locked down: Payload's default lets ANY logged-in account read, edit
// (email, password) or delete every other account — a plain 'user' could
// take over an admin. Admins manage everyone; others only themselves.
if (!(config.collections ?? []).some((c) => c.slug === authSlug)) {
const hasRoles = options.access?.authCollection === authSlug
config.collections = [
...(config.collections ?? []),
{ slug: authSlug, admin: { useAsTitle: 'email' }, auth: true, fields: [] },
{
slug: authSlug,
...(hasRoles
? { access: { create: adminOnly, delete: adminOnly, read: adminOrSelf, update: adminOrSelf } }
: {}),
admin: { useAsTitle: 'email' },
auth: true,
fields: [],
},
]
}
@@ -108,8 +121,12 @@ export const ipalKit = (options: IpalOptions): Plugin => {
)
}
// Default to the resolved auth collection (access.authCollection /
// admin.user), not a literal 'users': wrapping a slug nobody logs in with
// leaves the real collection without TOTP fields, and the TOTP access
// wrapper lets users without those fields through — 2FA silently off.
config = await totp({
collection: tf.collectionSlug ?? 'users',
collection: tf.collectionSlug ?? authSlug,
forceSetup: true, // ENFORCED — every user must set up TOTP; no opt-out
totp: { issuer: tf.issuer },
})(config)