Watch
1
0
Fork
You've already forked packages
0
packages/markdown/tests/markdown.test.weft
Weft packages 306b143104 packages/ at markup-kit dd1e6eb5270e
A snapshot of packages/ in the Weft repository; the official packages are published from these commits.

Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
2026-11-19 20:43:26 +00:00

220 lines
8.3 KiB
Text

// `weft test`: the package's API and its safety. The spec's examples are in tests/spec.weft, hostile input in
// tests/hostile.weft (both are programs: they run past a test's deadline).
import { assert, assertEq } from "std/testing"
import { parse, render, sanitize, toHtml, toText, visit } from "../markdown"
// ---- safe by default ----
test "raw HTML is shown as text" {
assertEq(toHtml("<script>alert(1)</script>"), "<p>&lt;script&gt;alert(1)&lt;/script&gt;</p>\n")
assertEq(
toHtml("a <b onclick=\"x()\">b</b>"),
"<p>a &lt;b onclick=&quot;x()&quot;&gt;b&lt;/b&gt;</p>\n",
)
}
test "unsafe link schemes render as their text" {
let attacks = [
"[x](javascript:alert(1))",
"[x](JaVaScRiPt:alert(1))",
"[x](vbscript:msgbox(1))",
"[x](data:text/html;base64,PHNjcmlwdD4=)",
"[x](&#106;avascript:alert(1))",
"[x](javascript&colon;alert(1))",
"[x](file:///etc/passwd)",
]
for a of attacks {
assertEq(toHtml(a), "<p>x</p>\n")
}
assertEq(toHtml("<javascript:alert(1)>"), "<p>javascript:alert(1)</p>\n")
}
test "unsafe images render as their alt text" {
assertEq(toHtml("![cat](javascript:alert(1))"), "<p>cat</p>\n")
assertEq(toHtml("![cat](data:image/svg+xml;base64,PHN2Zz4=)"), "<p>cat</p>\n")
assertEq(
toHtml("![cat](data:image/png;base64,AAAA)"),
"<p><img src=\"data:image/png;base64,AAAA\" alt=\"cat\" /></p>\n",
)
}
test "safe URLs are kept" {
assertEq(
toHtml("[a](https://x.dev/?q=1&r=2)"),
"<p><a href=\"https://x.dev/?q=1&amp;r=2\">a</a></p>\n",
)
assertEq(toHtml("[a](/docs/x:y)"), "<p><a href=\"/docs/x:y\">a</a></p>\n")
assertEq(toHtml("[a](#top)"), "<p><a href=\"#top\">a</a></p>\n")
assertEq(toHtml("[a](mailto:me@x.dev)"), "<p><a href=\"mailto:me@x.dev\">a</a></p>\n")
assertEq(toHtml("[a](tel:+38640123456)"), "<p><a href=\"tel:+38640123456\">a</a></p>\n")
}
test "titles, code and attributes are escaped" {
assertEq(
toHtml("[a](/x \"\\\"><script>\")"),
"<p><a href=\"/x\" title=\"&quot;&gt;&lt;script&gt;\">a</a></p>\n",
)
assertEq(toHtml("`<b>`"), "<p><code>&lt;b&gt;</code></p>\n")
assertEq(
toHtml("```\"><script>\nx\n```"),
"<pre><code class=\"language-&quot;&gt;&lt;script&gt;\">x\n</code></pre>\n",
)
}
// ---- the sanitiser ----
test "sanitize removes scripts, handlers and unsafe URLs" {
assertEq(sanitize("<p onclick=\"x()\">hi<script>bad()</script></p>"), "<p>hi</p>")
assertEq(sanitize("<a href=\"javascript:x()\" title=\"t\">hi</a>"), "<a title=\"t\">hi</a>")
assertEq(sanitize("<a href=\"java&#115;cript:x()\">hi</a>"), "<a>hi</a>")
assertEq(sanitize("<a href=\"java&#9;script:x()\">hi</a>"), "<a>hi</a>")
assertEq(sanitize("<a href=\" &#14; javascript:x()\">hi</a>"), "<a>hi</a>")
assertEq(sanitize("<img src=x onerror=alert(1)>"), "<img src=\"x\" />")
assertEq(sanitize("<IMG SRC=\"JAVASCRIPT:x\">"), "<img />")
assertEq(sanitize("<svg><script>x</script></svg>ok"), "ok")
assertEq(sanitize("<math><mi>x</mi></math>ok"), "ok")
assertEq(sanitize("<style>*{display:none}</style>ok"), "ok")
assertEq(sanitize("<iframe src=\"https://evil\"></iframe>ok"), "ok")
assertEq(sanitize("<ScRiPt >x</sCrIpT >ok"), "ok")
assertEq(sanitize("<script>never closed"), "")
assertEq(sanitize("<!-- <script>x</script> -->ok"), "ok")
assertEq(sanitize("<form action=\"/x\"><button>go</button></form>"), "go")
}
test "sanitize keeps only a disabled checkbox" {
assertEq(sanitize("<input type=\"text\" value=\"pw\">"), "")
assertEq(sanitize("<input>"), "")
assertEq(
sanitize("<input type=\"checkbox\" checked>"),
"<input type=\"checkbox\" checked=\"\" disabled=\"\" />",
)
}
test "sanitize gives well-formed output" {
assertEq(sanitize("<b><i>x</b>y"), "<b><i>x</i></b>y")
assertEq(sanitize("</div>x"), "x")
assertEq(sanitize("<p>a"), "<p>a</p>")
assertEq(sanitize("1 < 2 & 3 > 2 &amp; ok"), "1 &lt; 2 &amp; 3 &gt; 2 &amp; ok")
assertEq(sanitize("<a title=\"a&quot;b\">x</a>"), "<a title=\"a&quot;b\">x</a>")
assertEq(
sanitize("<a target=\"_blank\" href=\"/x\">x</a>"),
"<a target=\"_blank\" href=\"/x\" rel=\"noopener noreferrer\">x</a>",
)
assertEq(sanitize("<a target=\"evil\" href=\"/x\">x</a>"), "<a href=\"/x\">x</a>")
}
test "a policy narrows what is kept" {
assertEq(sanitize("<b>x</b><i>y</i>", { tags: ["b"] }), "<b>x</b>y")
assertEq(sanitize("<a href=\"ftp://x\">y</a>", { schemes: ["ftp"] }), "<a href=\"ftp://x\">y</a>")
assertEq(sanitize("<a href=\"https://x\">y</a>", { schemes: ["ftp"] }), "<a>y</a>")
let attributes = Map<string, string[]>([["b", ["title"]]])
assertEq(
sanitize("<b title=\"t\" id=\"i\">x</b>", { tags: ["b"], attributes }),
"<b title=\"t\">x</b>",
)
}
test "html: sanitize keeps safe raw HTML in Markdown" {
assertEq(
toHtml("<div onclick=\"x()\">\n*hi*\n</div>", { html: "sanitize" }),
"<div>\n*hi*\n</div>\n",
)
assertEq(
toHtml("a <kbd>b</kbd> <script>c</script>", { html: "sanitize" }),
"<p>a <kbd>b</kbd> </p>\n",
)
assertEq(toHtml("<script>\nbad()\n\n</script>\n\nok", { html: "sanitize" }), "\n<p>ok</p>\n")
assertEq(
toHtml("- [x] done", { html: "sanitize" }),
"<ul>\n<li><input checked=\"\" disabled=\"\" type=\"checkbox\" /> done</li>\n</ul>\n",
)
}
test "html: trust passes raw HTML through" {
assertEq(
toHtml("<div onclick=\"x()\">a</div>", { html: "trust" }),
"<div onclick=\"x()\">a</div>\n",
)
}
// ---- GFM ----
test "tables" {
let html = toHtml("| a | b |\n|:--|--:|\n| 1 | `x\\|y` |")
assertEq(
html,
"<table>\n<thead>\n<tr>\n<th align=\"left\">a</th>\n<th align=\"right\">b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">1</td>\n<td align=\"right\"><code>x|y</code></td>\n</tr>\n</tbody>\n</table>\n",
)
}
test "task lists" {
let doc = parse("- [ ] todo\n- [x] done\n- plain")
match doc.children[0] {
BulletList { items } => {
let checked: boolean?[] = items.map(i => i.checked)
let expected: boolean?[] = [false, true, null]
assertEq(checked, expected)
},
_ => assert(false, "a list"),
}
}
test "strikethrough and bare links" {
assertEq(toHtml("~~gone~~"), "<p><del>gone</del></p>\n")
assertEq(
toHtml("see https://x.dev/a_b."),
"<p>see <a href=\"https://x.dev/a_b\">https://x.dev/a_b</a>.</p>\n",
)
assertEq(toHtml("www.x.dev"), "<p><a href=\"http://www.x.dev\">www.x.dev</a></p>\n")
assertEq(toHtml("me@x.dev"), "<p><a href=\"mailto:me@x.dev\">me@x.dev</a></p>\n")
}
test "gfm: false is plain CommonMark" {
assertEq(toHtml("~~a~~ https://x.dev", { gfm: false }), "<p>~~a~~ https://x.dev</p>\n")
assertEq(toHtml("| a |\n|---|", { gfm: false }), "<p>| a |\n|---|</p>\n")
}
// ---- what Vidni's briefs need ----
test "heading offset, link target and URL rewriting" {
assertEq(toHtml("# T", { headingOffset: 1 }), "<h2>T</h2>\n")
assertEq(toHtml("###### T", { headingOffset: 3 }), "<h6>T</h6>\n")
assertEq(
toHtml("[a](https://x.dev)", { linkTarget: "_blank" }),
"<p><a href=\"https://x.dev\" target=\"_blank\" rel=\"noopener noreferrer\">a</a></p>\n",
)
let resolve = (url: string, kind: "link" | "image") => url.startsWith("upload:")
? `https://cdn.x.dev/${url.slice(7)}`
: null
assertEq(
toHtml("![c](upload:abc)", { rewriteUrl: resolve }),
"<p><img src=\"https://cdn.x.dev/abc\" alt=\"c\" /></p>\n",
)
// Not resolved: `upload:` isn't an allowed scheme, so the caption stays.
assertEq(toHtml("![c](upload:abc)"), "<p>c</p>\n")
}
test "toText for previews and emails" {
assertEq(
toText(parse("# Hi\n\nRead **this** [guide](https://x.dev).\n\n![cat](a.png)\n\n- one\n- two")),
"Hi\n\nRead this guide (https://x.dev).\n\n[image: cat]\n\n- one\n\n- two",
)
}
test "visit finds the images of a document" {
let mut urls: string[] = []
visit(parse("![a](upload:1)\n\n> *![b](upload:2)*\n\n| ![c](upload:3) |\n|---|"), n => {
if let Picture { url } = n {
urls.push(url)
}
})
assertEq(urls, ["upload:1", "upload:2", "upload:3"])
}
test "render uses a tree that was changed" {
let doc = parse("hello")
assertEq(render({ children: [...doc.children, ...doc.children] }), "<p>hello</p>\n<p>hello</p>\n")
}
test "strange characters survive" {
assertEq(toHtml("a\u{0}b"), "<p>a\u{fffd}b</p>\n")
assertEq(toHtml("line\r\nnext\rlast"), "<p>line\nnext\nlast</p>\n")
assertEq(toHtml("😀 *é*"), "<p>😀 <em>é</em></p>\n")
assertEq(toHtml(""), "")
}