// Raw HTML in untrusted bodies: and friends become Markdown nodes, and // nothing else gets through. import assert from "node:assert/strict"; import { test } from "node:test"; import { htmlBlock, inlineChildren, proxiedImage, safeUrl, type MdNode } from "../web/src/ui/markdownHtml.ts"; const ALLOWED = new Set(["paragraph", "text", "image", "link", "break", "strong", "emphasis", "inlineCode"]); const IMG = "https://github.com/user-attachments/assets/52cc3f06-06ec-404f-94b5-74a9af1e1c7e"; function all(nodes: MdNode[]): MdNode[] { return nodes.flatMap((n) => [n, ...all(n.children ?? [])]); } const of = (nodes: MdNode[], type: string) => all(nodes).filter((n) => n.type === type); test("an in a block becomes a proxied image", () => { const out = htmlBlock(`

Screen

`); const [img] = of(out, "image"); assert.deepEqual(img, { type: "image", url: IMG, alt: "Screen", title: null }); assert.equal(proxiedImage(safeUrl(img!.url!)), `/img?${new URLSearchParams({ u: IMG })}`); assert.ok(all(out).every((n) => ALLOWED.has(n.type))); assert.ok(!JSON.stringify(out).includes("onerror")); }); test("no other HTML gets through", () => { const out = htmlBlock(`
More & lesskept text
<b>
`); assert.ok(all(out).every((n) => ALLOWED.has(n.type)), JSON.stringify(out)); const text = JSON.stringify(out); for (const bad of ["alert", "p{}", "evil", "onclick", "style"]) assert.ok(!text.includes(bad), bad); assert.ok(all(out).every((n) => Object.keys(n).every((k) => ["type", "value", "url", "alt", "title", "children"].includes(k)))); assert.equal(of(out, "strong")[0]!.children![0]!.value, "More & less"); assert.ok(of(out, "text").some((n) => n.value === "kept text")); assert.equal(of(out, "inlineCode")[0]!.value, ""); }); test("a javascript: or foreign src stays a chip or link, never a proxied image", () => { for (const src of ["javascript:alert(1)", "https://evil.example/a.png", "http://github.com/user-attachments/assets/x", "data:image/png;base64,AAAA"]) { const [img] = of(htmlBlock(`x`), "image"); assert.equal(proxiedImage(safeUrl(img!.url!)), null, src); } const [link] = of(htmlBlock(`click`), "link"); assert.equal(safeUrl(link!.url!), ""); }); test("inline tags inside a paragraph", () => { const kids = inlineChildren([ { type: "text", value: "before " }, { type: "html", value: `a` }, { type: "html", value: "
" }, { type: "html", value: '' }, { type: "text", value: "link text" }, { type: "html", value: "" }, { type: "html", value: "" }, { type: "text", value: " after" }, { type: "html", value: "" }, ]); assert.deepEqual(kids.map((k) => k.type), ["text", "image", "break", "link", "text"]); assert.equal(kids[3]!.children![0]!.value, "link text"); });