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:
@@ -0,0 +1,76 @@
|
||||
/** Unpublished drafts must never be served by the public routing helpers. */
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
import { sqliteAdapter } from '@payloadcms/db-sqlite'
|
||||
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
||||
import os from 'os'
|
||||
import path from 'path'
|
||||
import { buildConfig, getPayload } from 'payload'
|
||||
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
|
||||
|
||||
import { resolveRoute } from '../src/modules/content/resolveRoute.js'
|
||||
import { ipalKit } from '../src/plugin.js'
|
||||
|
||||
let payload: Payload
|
||||
|
||||
beforeAll(async () => {
|
||||
const config = buildConfig({
|
||||
collections: [
|
||||
{
|
||||
slug: 'pages',
|
||||
fields: [
|
||||
{ name: 'title', type: 'text' },
|
||||
{ name: 'slug', type: 'text', localized: true },
|
||||
],
|
||||
versions: { drafts: true },
|
||||
},
|
||||
{ slug: 'posts', fields: [{ name: 'title', type: 'text' }, { name: 'slug', type: 'text', localized: true }], versions: { drafts: true } },
|
||||
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
|
||||
],
|
||||
db: sqliteAdapter({ client: { url: 'file::memory:' }, push: true }),
|
||||
editor: lexicalEditor(),
|
||||
plugins: [
|
||||
ipalKit({
|
||||
access: { authCollection: 'users' },
|
||||
content: { collections: [{ slug: 'posts' }] },
|
||||
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
|
||||
pages: { slug: 'pages' },
|
||||
twoFactor: false,
|
||||
}),
|
||||
],
|
||||
secret: 'drafts-test',
|
||||
telemetry: false,
|
||||
})
|
||||
payload = await getPayload({ config })
|
||||
}, 60_000)
|
||||
|
||||
afterAll(async () => {
|
||||
await payload?.destroy()
|
||||
})
|
||||
|
||||
describe('drafts', () => {
|
||||
test('a never-published draft page is not resolvable', async () => {
|
||||
await payload.create({ collection: 'pages' as never, data: { slug: 'tajne', _status: 'draft', title: 'Tajne' } as never, draft: true })
|
||||
expect(await resolveRoute({ locale: 'pl', payload, segments: ['tajne'] })).toBeNull()
|
||||
})
|
||||
|
||||
test('a published page is resolvable, and a later draft does not replace it', async () => {
|
||||
const doc = await payload.create({ collection: 'pages' as never, data: { slug: 'oferta', _status: 'published', title: 'Oferta' } as never })
|
||||
await payload.update({ id: doc.id, collection: 'pages' as never, data: { title: 'Oferta — szkic zmian' } as never, draft: true })
|
||||
const route = await resolveRoute({ locale: 'pl', payload, segments: ['oferta'] })
|
||||
expect((route?.doc as { title?: string })?.title).toBe('Oferta')
|
||||
})
|
||||
})
|
||||
|
||||
describe('archive listing', () => {
|
||||
test('lists only published entries when the collection has drafts', async () => {
|
||||
const { getArchiveEntries } = await import('../src/modules/content/getArchiveEntries.js')
|
||||
await payload.create({ collection: 'posts' as never, data: { slug: 'jawny', _status: 'published', title: 'Jawny' } as never })
|
||||
await payload.create({ collection: 'posts' as never, data: { slug: 'szkic', _status: 'draft', title: 'Szkic' } as never, draft: true })
|
||||
const list = await getArchiveEntries({ collection: 'posts', locale: 'pl', payload })
|
||||
expect(list.docs.map((d) => (d as { title: string }).title)).toEqual(['Jawny'])
|
||||
expect(list.totalDocs).toBe(1)
|
||||
const preview = await getArchiveEntries({ collection: 'posts', draft: true, locale: 'pl', payload })
|
||||
expect(preview.totalDocs).toBe(2)
|
||||
})
|
||||
})
|
||||
@@ -219,3 +219,26 @@ describe('cookie table', () => {
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('forms access', () => {
|
||||
test('anonymous REST read of a form hides its e-mail recipients; submissions are staff-only', async () => {
|
||||
await payload.update({
|
||||
id: formId,
|
||||
collection: 'forms' as never,
|
||||
data: { emails: [{ emailTo: '[email protected]', subject: 'Nowe zgłoszenie' }] } as never,
|
||||
})
|
||||
const anon = (await payload.findByID({ id: formId, collection: 'forms' as never, overrideAccess: false })) as Record<string, unknown>
|
||||
expect(anon.fields).toBeDefined()
|
||||
expect(JSON.stringify(anon.emails ?? [])).not.toContain('[email protected]')
|
||||
|
||||
const user = (await payload.create({
|
||||
collection: 'users',
|
||||
data: { email: '[email protected]', password: 'Passw0rd!x' } as never,
|
||||
})) as unknown as Record<string, unknown>
|
||||
await expect(
|
||||
payload.find({ collection: 'form-submissions' as never, overrideAccess: false, user: { ...user, collection: 'users' } as never }),
|
||||
).rejects.toThrow()
|
||||
const editor = { ...user, collection: 'users', roles: ['editor'] }
|
||||
expect((await payload.find({ collection: 'form-submissions' as never, overrideAccess: false, user: editor as never })).totalDocs).toBeGreaterThan(0)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -213,3 +213,26 @@ describe('consent storage — version and id', () => {
|
||||
expect(readConsentRecord(raw)).toBeNull() // default version 1
|
||||
})
|
||||
})
|
||||
|
||||
describe('safeHref / resolveLinkHref', () => {
|
||||
test('only safe schemes survive', async () => {
|
||||
const { resolveLinkHref, safeHref } = await import('../src/modules/links/resolveLinkHref.js')
|
||||
for (const ok of ['https://acme.pl', 'mailto:[email protected]', 'tel:+48123', '#kontakt', '/pl/o-nas']) {expect(safeHref(ok)).toBe(ok)}
|
||||
for (const bad of ['javascript:alert(1)', ' JavaScript:alert(1)', 'java\tscript:alert(1)', 'data:text/html,x', '//evil.com', 'vbscript:x', '']) {
|
||||
expect(safeHref(bad)).toBeNull()
|
||||
}
|
||||
expect(resolveLinkHref({ type: 'external', url: 'javascript:alert(1)' }, { config, locale: 'pl' })).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildUploadSecurityHeaders', () => {
|
||||
test('sandboxes files served from upload routes', async () => {
|
||||
const { buildUploadSecurityHeaders } = await import('../src/modules/security/buildSecurityHeaders.js')
|
||||
const rules = buildUploadSecurityHeaders({ collections: ['media', 'documents'] })
|
||||
expect(rules.map((r) => r.source)).toEqual(['/api/media/file/:path*', '/api/documents/file/:path*'])
|
||||
const csp = rules[0].headers.find((h) => h.key === 'Content-Security-Policy')?.value ?? ''
|
||||
expect(csp).toContain('sandbox')
|
||||
expect(csp).toContain("default-src 'none'")
|
||||
expect(csp).not.toContain('script-src')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user