Files
ipal-kit/src/modules/content/getArchiveEntries.ts
T
radoslaw.smolinskiandClaude Opus 5.5 9e49856611 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]>
2026-10-16 03:06:26 +02:00

80 lines
2.5 KiB
TypeScript

import type { BasePayload } from 'payload'
export type ArchiveEntries<T = Record<string, unknown>> = {
docs: T[]
hasNextPage: boolean
hasPrevPage: boolean
/** 1-based. */
page: number
totalDocs: number
totalPages: number
}
type GetArchiveEntriesArgs = {
/** Collection to list, e.g. 'posts'. */
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
payload: BasePayload
/** Defaults to 10. */
perPage?: number
/** Payload sort string. Defaults to newest first. */
sort?: string
/** Extra constraints merged into the query, e.g. { category: { equals: id } }. */
where?: Record<string, unknown>
}
/**
* Fetches one page of a collection's entries for an archive listing.
*
* 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
* of posts should look isn't something a plugin can decide.
*/
export async function getArchiveEntries<T = Record<string, unknown>>({
collection,
depth = 1,
draft = false,
locale,
page = 1,
payload,
perPage = 10,
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,
...(fullWhere ? { where: fullWhere as never } : {}),
})
return {
docs: result.docs as T[],
hasNextPage: result.hasNextPage,
hasPrevPage: result.hasPrevPage,
page: result.page ?? 1,
totalDocs: result.totalDocs,
totalPages: result.totalPages,
}
}