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:
2026-10-16 03:06:26 +02:00
co-authored by Claude Opus 5.5
parent ebf70d49bd
commit 9e49856611
14 changed files with 284 additions and 20 deletions
+14 -4
View File
@@ -15,6 +15,8 @@ type GetArchiveEntriesArgs = {
collection: string
/** Relationship depth. Defaults to 1 — enough for a cover image. */
depth?: number
/** Preview (draftMode): include unpublished entries and latest drafts. Default false. */
draft?: boolean
locale: string
/** 1-based. Values below 1 are clamped. */
page?: number
@@ -30,9 +32,10 @@ type GetArchiveEntriesArgs = {
/**
* Fetches one page of a collection's entries for an archive listing.
*
* Only published documents are returned when the collection has drafts enabled;
* Payload's `where` on `_status` is left to the caller, since a collection
* without drafts has no such field.
* Only published documents are returned when the collection has drafts enabled
* (the `_status` filter is added automatically — only then, since a collection
* without drafts has no such field and querying it would throw). Pass
* `draft: true` for a preview.
*
* Returns pagination facts rather than markup — the listing itself belongs to
* the client (as a block on the archive page, most likely), because how a list
@@ -41,6 +44,7 @@ type GetArchiveEntriesArgs = {
export async function getArchiveEntries<T = Record<string, unknown>>({
collection,
depth = 1,
draft = false,
locale,
page = 1,
payload,
@@ -48,14 +52,20 @@ export async function getArchiveEntries<T = Record<string, unknown>>({
sort = '-createdAt',
where,
}: GetArchiveEntriesArgs): Promise<ArchiveEntries<T>> {
const versions = payload.collections?.[collection as never]?.config?.versions as { drafts?: unknown } | false | undefined
const hasDrafts = Boolean(versions && versions.drafts)
const published = !draft && hasDrafts ? { _status: { equals: 'published' } } : undefined
const fullWhere = published && where ? { and: [published, where] } : (published ?? where)
const result = await payload.find({
collection: collection as never,
depth,
draft,
limit: perPage,
locale: locale as never,
page: Math.max(1, page),
sort,
...(where ? { where: where as never } : {}),
...(fullWhere ? { where: fullWhere as never } : {}),
})
return {
+25 -3
View File
@@ -31,6 +31,12 @@ export type ResolvedRoute =
type ResolveRouteArgs = {
content?: ContentOption
/**
* Preview (Next draftMode): return the latest draft instead of the published
* version, and resolve never-published pages. Default false — public
* requests only ever see published content.
*/
draft?: boolean
locale: string
/** Page number from the query string (?page=2). Defaults to 1. */
page?: number
@@ -72,8 +78,20 @@ type ResolveRouteArgs = {
* make canonical and hreflang ambiguous (the same entry reachable under several
* URLs).
*/
/**
* Payload's `find` returns never-published documents from the main collection
* (with `_status: 'draft'`) unless asked otherwise — public routing must drop
* them, or an unpublished page is reachable by its URL.
*/
function isPublic(doc: unknown, draft: boolean): boolean {
if (draft || !doc || typeof doc !== 'object') {return Boolean(doc)}
const status = (doc as { _status?: unknown })._status
return status === undefined || status === null || status === 'published'
}
export async function resolveRoute({
content,
draft = false,
locale,
page = 1,
pagesSlug = 'pages',
@@ -107,10 +125,11 @@ export async function resolveRoute({
id: homepageId,
collection: pagesSlug,
depth: 2,
draft,
locale: locale as never,
})
return doc ? { type: 'home', doc: doc as Record<string, unknown> } : null
return doc && isPublic(doc, draft) ? { type: 'home', doc: doc as Record<string, unknown> } : null
}
const [first, ...rest] = segments
@@ -122,6 +141,7 @@ export async function resolveRoute({
const archiveDoc = archive as ArchivePage
if (archiveDoc.slug !== first) {continue}
if (!isPublic(archiveDoc, draft)) {return null}
// /pl/artykuly → the archive page itself.
if (rest.length === 0) {
@@ -139,12 +159,13 @@ export async function resolveRoute({
const found = await payload.find({
collection: collection.slug as never,
depth: 2,
draft,
limit: 1,
locale: locale as never,
where: { slug: { equals: rest[0] } },
})
const entry = found.docs[0]
if (!entry) {return null}
if (!entry || !isPublic(entry, draft)) {return null}
return {
type: 'entry',
@@ -162,12 +183,13 @@ export async function resolveRoute({
const found = await payload.find({
collection: pagesSlug as never,
depth: 2,
draft,
limit: 1,
locale: locale as never,
where: { slug: { equals: segments.join('/') } },
})
const doc = found.docs[0]
if (!doc) {return null}
if (!doc || !isPublic(doc, draft)) {return null}
// The page assigned as 404 in System Pages must not be reachable as a normal
// 200 URL (/pl/strona-nie-znaleziona) — that's a soft-404 / duplicate. Treat