Skip to content
refs
History
20 lines · 986 BRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { renderMarkdown } from "../markdown.ts";
4
5test("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, /&#60;script&#62;/);
9});
10
11test("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
16test("routes relative links and images through the resolver", () => {
17 const html = renderMarkdown("[doc](docs/a.md) ![img](logo.png)", (href, kind) => `/${kind}/${href}`);
18 assert.match(html, /href="\/blob\/docs\/a.md"/);
19 assert.match(html, /src="\/raw\/logo.png"/);
20});