Security review: drafts no longer public, submissions staff-only, safe links, upload sandbox
- Fix: resolveRoute served never-published drafts by URL (Payload's find returns them with _status 'draft') — also via Markdown, MCP get_page and metadata. Drafts are dropped on every branch; `draft: true` for previews. - Fix: getArchiveEntries listed drafts in archive pages despite its comment; `_status: published` is added automatically when the collection has drafts. - form-submissions readable only by editor+ (form-builder default: any account of the admin collection, incl. plain users from an open sign-up); delete admin. - Forms stay public for rendering, but the Emails tab (recipients) is hidden from anonymous REST reads. - safeHref: CMS links only http(s), mailto, tel, sms, #, / — javascript:, data: and control-character tricks are dropped in resolveLinkHref. - Analytics IDs validated in the panel and before reaching the browser. - buildUploadSecurityHeaders: sandbox CSP for /api/<upload>/file/* so an uploaded SVG can't run script on the site's origin. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,11 +1,38 @@
|
||||
import type { Plugin } from 'payload'
|
||||
import type { Field, Plugin } from 'payload'
|
||||
|
||||
import { formBuilderPlugin } from '@payloadcms/plugin-form-builder'
|
||||
|
||||
import type { FormsOption } from './types.js'
|
||||
|
||||
import { hasMinimumRole, isAdmin } from '../access/predicates.js'
|
||||
|
||||
type AccessArgs = { req: { user?: unknown } }
|
||||
|
||||
/** REST/GraphQL: only logged-in users. Local API (submitForm) is unaffected. */
|
||||
const submissionsCreateAccess = ({ req }: { req: { user?: unknown } }) => Boolean(req.user)
|
||||
const submissionsCreateAccess = ({ req }: AccessArgs) => Boolean(req.user)
|
||||
/**
|
||||
* Submissions are visitors' personal data: editors and admins only. The
|
||||
* form-builder default lets ANY account of the admin collection read them —
|
||||
* including a plain 'user' from an open sign-up.
|
||||
*/
|
||||
const submissionsReadAccess = ({ req }: AccessArgs) => hasMinimumRole(req.user as never, 'editor')
|
||||
const submissionsDeleteAccess = ({ req }: AccessArgs) => isAdmin(req.user as never)
|
||||
|
||||
/**
|
||||
* Forms stay publicly readable (their fields render the form), but the Emails
|
||||
* tab — recipient addresses, templates — is for logged-in staff only, so
|
||||
* GET /api/forms doesn't hand out the company's inboxes to spammers.
|
||||
*/
|
||||
function hideEmails(fields: Field[]): Field[] {
|
||||
return fields.map((field) =>
|
||||
'name' in field && field.name === 'emails'
|
||||
? ({
|
||||
...field,
|
||||
access: { ...(field as { access?: object }).access, read: ({ req }: AccessArgs) => Boolean(req.user) },
|
||||
} as Field)
|
||||
: field,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures @payloadcms/plugin-form-builder from IPAL's FormsOption.
|
||||
@@ -48,12 +75,16 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
|
||||
labels: { plural: 'Forms', singular: 'Form' },
|
||||
...forms.formOverrides,
|
||||
admin: { ...group, ...forms.formOverrides?.admin },
|
||||
fields: ({ defaultFields }) =>
|
||||
hideEmails(forms.formOverrides?.fields ? forms.formOverrides.fields({ defaultFields }) : defaultFields),
|
||||
},
|
||||
formSubmissionOverrides: {
|
||||
labels: { plural: 'Submissions', singular: 'Submission' },
|
||||
...forms.formSubmissionOverrides,
|
||||
access: {
|
||||
create: submissionsCreateAccess,
|
||||
delete: submissionsDeleteAccess,
|
||||
read: submissionsReadAccess,
|
||||
...forms.formSubmissionOverrides?.access,
|
||||
},
|
||||
admin: { ...group, defaultColumns: ['form', 'createdAt'], ...forms.formSubmissionOverrides?.admin },
|
||||
|
||||
Reference in New Issue
Block a user