// Mirrored Markdown is third-party input rendered on this origin. Two locks // keep a script committed upstream from running here: the sanitiser that // strips it at render time, and the CSP hash step that refuses to authorise // any inline script the site's own templates did not produce. import assert from 'node:assert/strict'; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import test from 'node:test'; import { createMarkdownProcessor } from '@astrojs/markdown-remark'; import { isFirstParty, isSafeUrl, rehypeSanitizeMirror } from '../src/lib/rehype-sanitize-mirror.mjs'; import { collect, inlineScripts } from '../scripts/csp-hashes.mjs'; const processor = await createMarkdownProcessor({ rehypePlugins: [[rehypeSanitizeMirror, { contentRoots: ['/repo/src/content'] }]], shikiConfig: { theme: 'rose-pine-moon' }, }); const render = async (markdown, path) => (await processor.render(markdown, { fileURL: new URL(`file:///repo/src/content/${path}`) })).code; const HOSTILE = `# Page Intro Ctrl [js](javascript:alert(1)) [ok](https://example.com/a) [rel](../other#frag)
More **bold** inside

forged unwrapped bad iddigit - [x] task \`\`\`html \`\`\` `; test('scripts, styles, frames, handlers and unsafe URLs are stripped from mirrored pages', async () => { for (const collection of ['payloads/xss/index.md', 'internal/cloud/aws.md', 'hacktricks/pentesting-web/xss.md']) { const html = await render(HOSTILE, collection); // Remove the fenced example before looking for live markup: there the // tags are escaped text, which is the point of a payload reference. const live = html.replace(//g, ''); for (const forbidden of [/'; const dist = site(t, { '404.html': `${layout}`, 'sheets/a/index.html': `${layout}
fine
`, 'payloads/xss/index.html': `${layout}
`, }); const { allowed, unknown, files } = collect(dist); assert.equal(files, 3); assert.deepEqual([...allowed], inlineScripts('').map((s) => s.hash)); assert.equal(unknown.length, 1); assert.equal(unknown[0].page, join('payloads', 'xss', 'index.html')); assert.equal(unknown[0].preview, 'steal()'); assert.ok(!allowed.has(unknown[0].hash)); const clean = site(t, { '404.html': `${layout}`, 'payloads/xss/index.html': `${layout}
<script>steal()</script>
` }); assert.deepEqual(collect(clean).unknown, []); assert.throws(() => collect(site(t, { 'index.html': layout })), /trusted page 404\.html is missing/); });