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
+2
View File
@@ -1,5 +1,6 @@
import type { GlobalConfig } from 'payload'
import { adminOrEditor } from '../../modules/access/index.js'
import { cookieSettingsFields } from './fields.js'
/**
@@ -11,6 +12,7 @@ export function buildCookieSettings(): GlobalConfig {
slug: 'cookie-settings',
access: {
read: () => true,
update: adminOrEditor, // consent texts are legal copy — not for plain 'user' accounts
},
admin: {
group: 'Settings',
+2
View File
@@ -1,5 +1,6 @@
import type { GlobalConfig } from 'payload'
import { adminOrEditor } from '../../modules/access/index.js'
import { notificationsFields } from './fields.js'
/**
@@ -12,6 +13,7 @@ export function buildNotifications(): GlobalConfig {
slug: 'notifications',
access: {
read: () => true, // texts are public-facing (shown to end users)
update: adminOrEditor,
},
admin: { group: 'Settings' },
fields: notificationsFields,
+4
View File
@@ -1,5 +1,6 @@
import type { Field, GlobalConfig } from 'payload'
import { adminOrEditor } from '../../modules/access/index.js'
import { generalFields } from './fields/general.js'
import { themeFields } from './fields/theme.js'
@@ -42,6 +43,9 @@ export function buildSiteSettings({
slug: 'site-settings',
access: {
read: () => true,
// Editors and admins only — a plain 'user' account (open sign-up) must
// not rewrite the brand, homepage or construction mode.
update: adminOrEditor,
},
admin: {
group: 'Settings',
+14 -2
View File
@@ -26,10 +26,15 @@ const submissionsCreateAccess = ({ req }: AccessArgs) => Boolean(req.user)
*/
const submissionsReadAccess = ({ req }: AccessArgs) => hasMinimumRole(req.user as never, 'editor')
const submissionsDeleteAccess = ({ req }: AccessArgs) => isAdmin(req.user as never)
/**
* Forms carry recipient addresses (Emails → To/BCC): a plain 'user' account
* editing a form could BCC every submission to itself. Editors and admins only.
*/
const staffAccess = ({ req }: AccessArgs) => hasMinimumRole(req.user as never, 'editor')
/**
* Forms stay publicly readable (their fields render the form), but the Emails
* tab — recipient addresses, templates — is for logged-in staff only, so
* tab — recipient addresses, templates — is for editors and admins only, so
* GET /api/forms doesn't hand out the company's inboxes to spammers.
*/
function hideEmails(fields: Field[]): Field[] {
@@ -37,7 +42,7 @@ function hideEmails(fields: Field[]): Field[] {
'name' in field && field.name === 'emails'
? ({
...field,
access: { ...(field as { access?: object }).access, read: ({ req }: AccessArgs) => Boolean(req.user) },
access: { ...(field as { access?: object }).access, read: staffAccess },
} as Field)
: field,
)
@@ -104,6 +109,13 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
formOverrides: {
labels: { plural: 'Forms', singular: 'Form' },
...forms.formOverrides,
access: {
create: staffAccess,
delete: staffAccess,
read: () => true,
update: staffAccess,
...forms.formOverrides?.access,
},
admin: { ...group, ...forms.formOverrides?.admin },
fields: ({ defaultFields }) =>
hideEmails(forms.formOverrides?.fields ? forms.formOverrides.fields({ defaultFields }) : defaultFields),
+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)