- Request bodies from strangers are read only up to a cap, declared length or not: mail-service confirm, CSP reports, page views, MCP (maxBytes, 413). readCappedBody moves to shared/ (the consent log keeps exporting it). - searchContent / createSearchRoute / MCP search: queryLimit (default 200 characters, 10 words) before the database; an unknown ?locale is the default locale throughout. - Rich text: autolinks and links marked internal pass safeHref too; lexicalToMarkdown drops unsafe link targets. - truncate() never returns more than its limit (summary / meta autofill after validation). - resolveRoute answers null for a locale the site does not have (xx, all, *): no default-locale page under another code (page route past the proxy, /md/xx, MCP). - A 404 page that is a draft is not shown (resolveNotFoundPage, getNotFoundPage). - readPublished() access rule (exported); Site integrations readVersions: admin. - Plugin switches, buildCsp, buildSecurityHeaders and the seed guard refuse nonsense values at start; an unknown role name is met by nobody, requireRole throws on one. - Docs, AGENT.md, iapl-collection and iapl-audit skills. Co-Authored-By: Claude Opus 5.5 <[email protected]>
84 lines
4.3 KiB
TypeScript
84 lines
4.3 KiB
TypeScript
import { describe, expect, test } from 'vitest'
|
|
|
|
import { sanitizeRichTextLinks } from '../src/modules/richtext/sanitizeLinks.js'
|
|
|
|
const link = (url: string, type = 'link', linkType = 'custom') => ({ type, children: [{ type: 'text', text: 'x' }], fields: { linkType, url } })
|
|
const doc = (...nodes: object[]) => ({ root: { type: 'root', children: [{ type: 'paragraph', children: nodes }] } })
|
|
const urls = (d: ReturnType<typeof doc>) =>
|
|
(d.root.children[0].children as { fields: { url: string } }[]).map((n) => n.fields.url)
|
|
|
|
describe('rich text links', () => {
|
|
test.each(['javascript:alert(1)', ' JaVaScRiPt:alert(1)', 'java\tscript:alert(1)', 'data:text/html,<script>alert(1)</script>', 'vbscript:x'])(
|
|
'%j is neutralised to #',
|
|
(bad) => {
|
|
expect(urls(sanitizeRichTextLinks(doc(link(bad), link(bad, 'autolink'))))).toEqual(['#', '#'])
|
|
},
|
|
)
|
|
|
|
test('safe links are kept and an untouched document is returned as is', () => {
|
|
const d = doc(link('https://acme.pl'), link('/pl/kontakt'), link('mailto:[email protected]'), link('tel:+48123'), link('#faq'))
|
|
expect(sanitizeRichTextLinks(d)).toBe(d)
|
|
})
|
|
|
|
test('only the path to an unsafe link is copied; the input is not mutated', () => {
|
|
const d = doc(link('https://acme.pl'), link('javascript:alert(1)'))
|
|
const out = sanitizeRichTextLinks(d)
|
|
expect(urls(d)).toEqual(['https://acme.pl', 'javascript:alert(1)'])
|
|
expect(urls(out)).toEqual(['https://acme.pl', '#'])
|
|
expect(out.root.children[0].children[0]).toBe(d.root.children[0].children[0])
|
|
})
|
|
})
|
|
|
|
describe('RenderRichText', () => {
|
|
const state = {
|
|
root: {
|
|
type: 'root', children: [{ type: 'paragraph', children: [{ type: 'link', children: [{ type: 'text', detail: 0, format: 0, mode: 'normal', style: '', text: 'klik', version: 1 }], fields: { linkType: 'custom', url: 'data:text/html,<script>alert(1)</script>' }, version: 3 }], version: 1 }],
|
|
direction: null, format: '', indent: 0, version: 1,
|
|
},
|
|
}
|
|
|
|
// React 19 already blocks javascript: hrefs; data: URLs it renders as is.
|
|
test('renders unsafe links as # by default; safeLinks={false} keeps them', async () => {
|
|
const { renderToStaticMarkup } = await import('react-dom/server')
|
|
const { createElement } = await import('react')
|
|
const { RenderRichText } = await import('../src/modules/richtext/RenderRichText.js')
|
|
const html = renderToStaticMarkup(createElement(RenderRichText, { data: state as never }))
|
|
expect(html).toContain('href="#"')
|
|
expect(html).not.toContain('data:')
|
|
const raw = renderToStaticMarkup(createElement(RenderRichText, { data: state as never, safeLinks: false }))
|
|
expect(raw).toContain('href="data:text/html')
|
|
})
|
|
})
|
|
|
|
/**
|
|
* One rule, one function (IAWL idea 19): an `autolink` marked `linkType: 'internal'` is rendered by Payload from
|
|
* `fields.url` all the same, and the Markdown output (llms / `.md` routes) prints link URLs too — both must pass
|
|
* the same safeHref check as <CMSLink> and the plain link.
|
|
*/
|
|
describe('a second way to write the same link', () => {
|
|
const bad = 'data:text/html,<script>alert(1)</script>'
|
|
|
|
test('an autolink marked internal is neutralised like any other', () => {
|
|
expect(urls(sanitizeRichTextLinks(doc(link(bad, 'autolink', 'internal'))))).toEqual(['#'])
|
|
})
|
|
|
|
test('a link marked internal loses an unsafe stored url too (Payload renders its own href)', () => {
|
|
expect(urls(sanitizeRichTextLinks(doc(link(bad, 'link', 'internal'))))).toEqual(['#'])
|
|
})
|
|
|
|
test('RenderRichText never prints the unsafe URL of an internal autolink', async () => {
|
|
const { renderToStaticMarkup } = await import('react-dom/server')
|
|
const { createElement } = await import('react')
|
|
const { RenderRichText } = await import('../src/modules/richtext/RenderRichText.js')
|
|
const html = renderToStaticMarkup(createElement(RenderRichText, { data: doc(link(bad, 'autolink', 'internal')) as never }))
|
|
expect(html).not.toContain('data:')
|
|
})
|
|
|
|
test('the Markdown output drops an unsafe link target and keeps the label', async () => {
|
|
const { lexicalToMarkdown } = await import('../src/modules/richtext/lexicalToMarkdown.js')
|
|
const md = lexicalToMarkdown(doc(link('javascript:alert(1)'), link(bad, 'autolink'), link('https://acme.pl')) as never)
|
|
expect(md).not.toMatch(/javascript:|data:/)
|
|
expect(md).toContain('[x](https://acme.pl)')
|
|
})
|
|
})
|