20 lines · 986 BRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { renderMarkdown } from "../markdown.ts"; |
| 4 | |
| 5 | test("escapes raw HTML", () => { |
| 6 | const html = renderMarkdown("<script>alert(1)</script>\n\n<img src=x onerror=alert(2)>"); |
| 7 | assert.doesNotMatch(html, /<script|<img/); |
| 8 | assert.match(html, /<script>/); |
| 9 | }); |
| 10 | |
| 11 | test("neutralises dangerous link schemes and keeps safe ones", () => { |
| 12 | const html = renderMarkdown("[a](javascript:alert(1)) [b](data:text/html,x) [c](//evil.example) [d](https://ok.example) [e](mailto:x@y.z) [f](#top)"); |
| 13 | assert.deepEqual([...html.matchAll(/href="([^"]*)"/g)].map((m) => m[1]), ["#", "#", "#", "https://ok.example", "mailto:x@y.z", "#top"]); |
| 14 | }); |
| 15 | |
| 16 | test("routes relative links and images through the resolver", () => { |
| 17 | const html = renderMarkdown("[doc](docs/a.md) ", (href, kind) => `/${kind}/${href}`); |
| 18 | assert.match(html, /href="\/blob\/docs\/a.md"/); |
| 19 | assert.match(html, /src="\/raw\/logo.png"/); |
| 20 | }); |