// Markdown breakout, the /v1 remote policy, viewer dot-segments, and security headers. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { parseFragment, type DefaultTreeAdapterMap } from 'parse5'; import { createGuardedFetch, decideRemote, isAllowedRemote, redirectOptions, remoteAllowUrl, type AllowedRemote } from '../src/remote.ts'; import { createTransport, forgetQuirks, lsRefs, Tracer } from 'ply'; import { unwrap } from '../src/ply-result.ts'; import { classifyHref, renderMarkdown } from '../src/viewer/markdown.ts'; import { handleViewer } from '../src/viewer/handle.ts'; import { renderLoaded } from '../src/viewer/html.ts'; import { parseViewerRequest, routeOptsFromEnv } from '../src/viewer/route.ts'; import { viewerHref } from '../src/viewer/url.ts'; import type { Loaded, TraceModel } from '../src/viewer/model.ts'; /** Null and a missing value take the fallback. An empty string and 0 stay. */ function fill(value: unknown, fallback: T): T { return value === null || value === void 0 ? fallback : value as T; } /** One property. A null receiver and a missing key are holes. A stored null stays null. */ function slot(receiver: unknown, key: string): unknown { if (receiver === null || receiver === void 0) return void 0; if (typeof receiver !== 'object' && typeof receiver !== 'function') return void 0; return (receiver as Record)[key]; } type Node = DefaultTreeAdapterMap['node']; type Element = DefaultTreeAdapterMap['element']; const URL_ATTRS = new Set(['href', 'src', 'action', 'formaction', 'poster', 'data', 'xlink:href', 'srcset']); const MARKDOWN_TAGS = new Set(['p', 'a', 'code', 'strong', 'em', 'del', 'ul', 'ol', 'li', 'pre', 'blockquote', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'div', 'span', 'br', 'hr', 'h2', 'h3', 'h4', 'h5', 'h6']); // Markdown output may carry only these attributes, with these values: the renderer never copies source text into one, // except href (allowlisted, checked below), an integer `start`, and a heading id slug. const MARKDOWN_CLASSES = new Set(['md-code', 'md-code-lang', 'md-table', 'task', 'task done', 'task-item', 'al-l', 'al-c', 'al-r', 'md-img', 'md-frontmatter']); function markdownAttrOk(name: string, value: string): boolean { if (name === 'href') return true; if (name === 'rel') return value === 'noopener noreferrer nofollow'; if (name === 'class') return MARKDOWN_CLASSES.has(value); if (name === 'start') return /^\d{1,9}$/.test(value); if (name === 'id') return /^[a-z0-9][a-z0-9-]{0,79}$/.test(value); return false; } function isElement(node: Node): node is Element { return node.nodeName !== '#text' && node.nodeName !== '#comment' && node.nodeName !== '#document-fragment' && node.nodeName !== '#document' && 'attrs' in node; } function walk(node: Node, tags: string[], attrs: { name: string; value: string }[]): void { if (isElement(node)) { tags.push(node.nodeName.toLowerCase()); for (const attr of node.attrs) attrs.push({ name: attr.name.toLowerCase(), value: attr.value }); for (const child of node.childNodes) walk(child, tags, attrs); return; } if ('childNodes' in node) for (const child of node.childNodes) walk(child, tags, attrs); } function scan(html: string): { tags: string[]; attrs: { name: string; value: string }[] } { const tags: string[] = []; const attrs: { name: string; value: string }[] = []; walk(parseFragment(html), tags, attrs); return { tags, attrs }; } function assertHtmlSafe(html: string, where: string, markdown = false): void { const { tags, attrs } = scan(html); for (const tag of tags) { assert.equal(['script', 'style', 'iframe', 'object', 'embed', 'frame'].includes(tag), false, `${where} tag ${tag}`); if (markdown) assert.equal(MARKDOWN_TAGS.has(tag), true, `${where} unexpected ${tag}`); } for (const attr of attrs) { if (markdown) assert.equal(markdownAttrOk(attr.name, attr.value), true, `${where} markdown attribute ${attr.name}=${attr.value}`); assert.equal(attr.name.startsWith('on'), false, `${where} handler ${attr.name}=${attr.value}`); assert.equal(/^[-a-z0-9:]+$/.test(attr.name), true, `${where} attribute name ${attr.name}`); if (!URL_ATTRS.has(attr.name)) continue; let compact = ''; for (const char of attr.value) { const code = fill(char.codePointAt(0), 0); if (code <= 0x1f || code === 0x7f || code === 0x20) continue; compact += char; } compact = compact.toLowerCase(); assert.equal(/^(javascript|data|vbscript):/.test(compact), false, `${where} ${attr.name}=${attr.value}`); } } function mustAllow(raw: string, allow: readonly string[] = []): AllowedRemote { const decision = decideRemote(raw, allow); assert.equal(decision.kind, 'allowed', `${raw} -> ${decision.kind === 'rejected' ? decision.reason : ''}`); if (decision.kind !== 'allowed') throw new Error('unreachable'); assert.equal(isAllowedRemote(decision.remote), true); return decision.remote; } function mustReject(raw: string, reason: string, allow: readonly string[] = []): void { const decision = decideRemote(raw, allow); assert.equal(decision.kind, 'rejected', raw); if (decision.kind === 'rejected') assert.equal(decision.reason, reason, raw); } test('markdown hrefs are escaped, scheme-allowlisted, and carry rel', () => { const cases = [ 'See [docs](https://example.com/" onfocus="alert(1)" autofocus="x) for more.', 'x [y](https://e.com/"onclick=alert(1) x=") z', 'x [y](https://e.com/"/>alert(1))', '[x](vbscript:msgbox(1))', '[x](//evil.example/a)', '[x](/\\evil.example)', ]; for (const src of cases) { const html = String(renderMarkdown(src)); assertHtmlSafe(html, src, true); const { attrs } = scan(html); assert.equal(attrs.some(attr => attr.name === 'onfocus' || attr.name === 'onerror' || attr.name === 'onclick'), false, src); assert.equal(/href\s*=\s*["']?\s*javascript:/i.test(html), false, src); } const ok = String(renderMarkdown('[ok](https://github.com/octocat/Hello-World) and [mail](mailto:a@b.example) and [rel](/view)')); assert.match(ok, /href="https:\/\/github.com\/octocat\/Hello-World" rel="noopener noreferrer nofollow"/); assert.match(ok, /href="mailto:a@b\.example" rel="noopener noreferrer nofollow"/); assert.match(ok, /href="\/view" rel="noopener noreferrer nofollow"/); assert.equal(classifyHref('https://example.com/a?b=1&c=2'), 'https://example.com/a?b=1&c=2'); const escaped = String(renderMarkdown('[q](https://example.com/a?b=1&c=2)')); assert.match(escaped, /href="https:\/\/example.com\/a\?b=1&c=2"/); assertHtmlSafe(escaped, 'query', true); }); test('hostile markdown, filenames, commits, refs, and authors do not break out of HTML', () => { const payloads = [ '', '', '