@@ -0,0 +1,336 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package markdown |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// TestRender_HostileInputs is the XSS-vector cheatsheet. Every |
| 12 | +// fixture is a markdown body that *attempts* to inject executable |
| 13 | +// JS through a different vector. The pass condition: the rendered |
| 14 | +// HTML contains no `<script` tag, no `javascript:` URL, no event |
| 15 | +// handler attribute (`on*`), and no `data:` URI. |
| 16 | +// |
| 17 | +// Add new vectors here when a CVE / advisory lands in goldmark or |
| 18 | +// bluemonday — they're cheap to keep. |
| 19 | +func TestRender_HostileInputs(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + vectors := []string{ |
| 22 | + // Direct script tag. |
| 23 | + `<script>alert(1)</script>`, |
| 24 | + `<SCRIPT>alert(1)</SCRIPT>`, |
| 25 | + `<script src="//evil.com/x.js"></script>`, |
| 26 | + // Inline event handlers. |
| 27 | + `<img src="x" onerror="alert(1)">`, |
| 28 | + `<img src=x onerror=alert(1)>`, |
| 29 | + `<a onmouseover="alert(1)">x</a>`, |
| 30 | + `<body onload="alert(1)">`, |
| 31 | + // Style with expressions. |
| 32 | + `<style>body{background:url("javascript:alert(1)")}</style>`, |
| 33 | + `<div style="background:url(javascript:alert(1))">x</div>`, |
| 34 | + // javascript: links. |
| 35 | + `[click](javascript:alert(1))`, |
| 36 | + `<a href="javascript:alert(1)">x</a>`, |
| 37 | + `<a href="JaVaScRiPt:alert(1)">x</a>`, |
| 38 | + `[click](JAVASCRIPT:alert(1))`, |
| 39 | + // data: URIs (we disallow even data:image). |
| 40 | + `<img src="data:image/svg+xml;base64,PHN2Zz4=">`, |
| 41 | + `[x](data:text/html,<script>alert(1)</script>)`, |
| 42 | + // vbscript:. |
| 43 | + `<a href="vbscript:msgbox(1)">x</a>`, |
| 44 | + // SVG-embedded scripts. |
| 45 | + `<svg><script>alert(1)</script></svg>`, |
| 46 | + `<svg onload="alert(1)"></svg>`, |
| 47 | + // iframes. |
| 48 | + `<iframe src="//evil.com"></iframe>`, |
| 49 | + `<iframe srcdoc="<script>alert(1)</script>"></iframe>`, |
| 50 | + // HTML in markdown link text doesn't escape sanitizer. |
| 51 | + `[<script>alert(1)</script>](https://example.com)`, |
| 52 | + // Mutation XSS via mismatched quotes. |
| 53 | + `<a href="x"onmouseover="alert(1)">x</a>`, |
| 54 | + // Encoded payloads. |
| 55 | + `<a href="javascript:alert(1)">x</a>`, |
| 56 | + `<a href="javascript:alert(1)">x</a>`, |
| 57 | + // Backticked code-like content shouldn't escape. |
| 58 | + "`<script>alert(1)</script>`", |
| 59 | + // Embedded in autolinks. |
| 60 | + `<javascript:alert(1)>`, |
| 61 | + // Object/embed. |
| 62 | + `<object data="x.swf"></object>`, |
| 63 | + `<embed src="x.swf">`, |
| 64 | + // Form/button with formaction. |
| 65 | + `<form><button formaction="javascript:alert(1)">x</button></form>`, |
| 66 | + // Meta refresh. |
| 67 | + `<meta http-equiv="refresh" content="0; url=javascript:alert(1)">`, |
| 68 | + // Base href hijack. |
| 69 | + `<base href="javascript:">`, |
| 70 | + // MathML / annotation. |
| 71 | + `<math><annotation-xml encoding="text/html"><script>alert(1)</script></annotation-xml></math>`, |
| 72 | + // CSS expression (legacy IE). |
| 73 | + `<div style="width: expression(alert(1))">x</div>`, |
| 74 | + // Nested fenced code with a script. |
| 75 | + "```\n<script>alert(1)</script>\n```", |
| 76 | + // Markdown link href with newlines. |
| 77 | + "[x](\njavascript:alert(1))", |
| 78 | + // Image with javascript:. |
| 79 | + `)`, |
| 80 | + // HTML entities in URI. |
| 81 | + `[x](javascript:alert(1))`, |
| 82 | + // Hex / decimal entities in href attribute. |
| 83 | + `<a href="javascript:alert(1)">x</a>`, |
| 84 | + // Tab/newline obfuscation. |
| 85 | + "<a href=\"java\tscript:alert(1)\">x</a>", |
| 86 | + "<a href=\"java\nscript:alert(1)\">x</a>", |
| 87 | + // Polyglot HTML+SVG. |
| 88 | + `<svg/onload=alert(1)>`, |
| 89 | + // Anchor with target=_blank but no rel (we want rel auto-set). |
| 90 | + `<a href="https://evil.com" target="_blank">x</a>`, |
| 91 | + } |
| 92 | + for i, src := range vectors { |
| 93 | + out, _, _, err := Render(context.Background(), []byte(src), Options{}) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("vector %d render error: %v", i, err) |
| 96 | + } |
| 97 | + // Lower-case for case-insensitive substring search. We |
| 98 | + // distinguish "executable surface" from "harmless text". |
| 99 | + // Plain-text "javascript:" in prose is safe; "javascript:" |
| 100 | + // inside href/src is an XSS — guard the latter shape only. |
| 101 | + s := strings.ToLower(string(out)) |
| 102 | + for _, bad := range []string{ |
| 103 | + "<script", "</script>", |
| 104 | + `href="javascript:`, `href='javascript:`, |
| 105 | + `src="javascript:`, `src='javascript:`, |
| 106 | + `href="vbscript:`, `src="vbscript:`, |
| 107 | + `href="data:`, `src="data:text`, `src="data:image`, |
| 108 | + " onerror=", " onload=", " onclick=", " onmouseover=", |
| 109 | + "<iframe", "<object", "<embed", |
| 110 | + "<style", "<base ", "<meta ", |
| 111 | + "<annotation-xml", "expression(", |
| 112 | + } { |
| 113 | + if strings.Contains(s, bad) { |
| 114 | + t.Errorf("vector %d (%q): rendered HTML contains %q\nout=%q", i, src, bad, out) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// TestRender_AllowsSafeHTML ensures the strict policy doesn't strip |
| 121 | +// `<details>`, `<summary>`, `<kbd>`, `<sup>`, `<sub>`, task-list |
| 122 | +// checkboxes, language-* class on code blocks, or auto-heading IDs. |
| 123 | +func TestRender_AllowsSafeHTML(t *testing.T) { |
| 124 | + t.Parallel() |
| 125 | + cases := []struct { |
| 126 | + name string |
| 127 | + src string |
| 128 | + mustContain []string |
| 129 | + }{ |
| 130 | + { |
| 131 | + "details + summary", |
| 132 | + "<details><summary>click</summary>secret</details>", |
| 133 | + []string{"<details>", "<summary>", "click", "secret"}, |
| 134 | + }, |
| 135 | + { |
| 136 | + "kbd", |
| 137 | + "press <kbd>Ctrl</kbd>+<kbd>C</kbd>", |
| 138 | + []string{"<kbd>Ctrl</kbd>", "<kbd>C</kbd>"}, |
| 139 | + }, |
| 140 | + { |
| 141 | + "sup/sub", |
| 142 | + "x<sup>2</sup> + y<sub>i</sub>", |
| 143 | + []string{"<sup>2</sup>", "<sub>i</sub>"}, |
| 144 | + }, |
| 145 | + { |
| 146 | + "task list", |
| 147 | + "- [x] done\n- [ ] not yet\n", |
| 148 | + []string{"<input", "checkbox", "disabled"}, |
| 149 | + }, |
| 150 | + { |
| 151 | + "fenced code with language", |
| 152 | + "```go\nfmt.Println(\"hi\")\n```", |
| 153 | + []string{`class="language-go"`}, |
| 154 | + }, |
| 155 | + { |
| 156 | + "heading anchor id", |
| 157 | + "# Hello world", |
| 158 | + []string{`id="hello-world"`}, |
| 159 | + }, |
| 160 | + { |
| 161 | + "GFM table", |
| 162 | + "| a | b |\n|---|---|\n| 1 | 2 |\n", |
| 163 | + []string{"<table>", "<th>a</th>", "<td>1</td>"}, |
| 164 | + }, |
| 165 | + { |
| 166 | + "strikethrough", |
| 167 | + "~~obsolete~~", |
| 168 | + []string{"<del>obsolete</del>"}, |
| 169 | + }, |
| 170 | + { |
| 171 | + "autolink", |
| 172 | + "https://example.com", |
| 173 | + []string{`href="https://example.com"`}, |
| 174 | + }, |
| 175 | + } |
| 176 | + for _, c := range cases { |
| 177 | + c := c |
| 178 | + t.Run(c.name, func(t *testing.T) { |
| 179 | + t.Parallel() |
| 180 | + out, _, _, err := Render(context.Background(), []byte(c.src), Options{}) |
| 181 | + if err != nil { |
| 182 | + t.Fatalf("render: %v", err) |
| 183 | + } |
| 184 | + s := string(out) |
| 185 | + for _, want := range c.mustContain { |
| 186 | + if !strings.Contains(s, want) { |
| 187 | + t.Errorf("expected %q in output, got %q", want, s) |
| 188 | + } |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +// TestRender_MentionResolution checks that @user resolves when the |
| 195 | +// resolver returns ok and stays plain text otherwise. |
| 196 | +func TestRender_MentionResolution(t *testing.T) { |
| 197 | + t.Parallel() |
| 198 | + resolver := func(_ context.Context, name string) (string, bool) { |
| 199 | + if name == "alice" { |
| 200 | + return "/alice", true |
| 201 | + } |
| 202 | + return "", false |
| 203 | + } |
| 204 | + out, _, mentions, err := Render(context.Background(), []byte("hi @alice and @bob"), Options{ |
| 205 | + Resolvers: Resolvers{User: resolver}, |
| 206 | + }) |
| 207 | + if err != nil { |
| 208 | + t.Fatalf("render: %v", err) |
| 209 | + } |
| 210 | + s := string(out) |
| 211 | + if !strings.Contains(s, `href="/alice"`) { |
| 212 | + t.Errorf("expected @alice link, got %q", s) |
| 213 | + } |
| 214 | + if strings.Contains(s, `href="/bob"`) { |
| 215 | + t.Errorf("@bob should not link, got %q", s) |
| 216 | + } |
| 217 | + if len(mentions) != 1 || mentions[0].Username != "alice" { |
| 218 | + t.Errorf("expected 1 mention (alice), got %v", mentions) |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +// TestRender_IssueRefResolution checks both same-repo and cross-repo |
| 223 | +// refs, and that an unresolvable ref renders as plain text (no link). |
| 224 | +func TestRender_IssueRefResolution(t *testing.T) { |
| 225 | + t.Parallel() |
| 226 | + resolver := func(_ context.Context, owner, name string, num int64, _ int64) (string, bool) { |
| 227 | + // Same-repo refs leave owner+name empty. |
| 228 | + if owner == "" && name == "" && num == 7 { |
| 229 | + return "/o/r/issues/7", true |
| 230 | + } |
| 231 | + if owner == "alice" && name == "proj" && num == 3 { |
| 232 | + return "/alice/proj/issues/3", true |
| 233 | + } |
| 234 | + return "", false |
| 235 | + } |
| 236 | + out, refs, _, err := Render(context.Background(), []byte("see #7 and alice/proj#3, but not bob/x#9"), Options{ |
| 237 | + Resolvers: Resolvers{Issue: resolver}, |
| 238 | + }) |
| 239 | + if err != nil { |
| 240 | + t.Fatalf("render: %v", err) |
| 241 | + } |
| 242 | + s := string(out) |
| 243 | + if !strings.Contains(s, `href="/o/r/issues/7"`) { |
| 244 | + t.Errorf("expected #7 link, got %q", s) |
| 245 | + } |
| 246 | + if !strings.Contains(s, `href="/alice/proj/issues/3"`) { |
| 247 | + t.Errorf("expected alice/proj#3 link, got %q", s) |
| 248 | + } |
| 249 | + if strings.Contains(s, `href="/bob/x/issues/9"`) { |
| 250 | + t.Errorf("bob/x#9 should not link, got %q", s) |
| 251 | + } |
| 252 | + if len(refs) != 2 { |
| 253 | + t.Errorf("expected 2 refs, got %v", refs) |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +// TestRender_RefsInsideCodeAreInert confirms that #N inside inline |
| 258 | +// code or fenced code stays as text. |
| 259 | +func TestRender_RefsInsideCodeAreInert(t *testing.T) { |
| 260 | + t.Parallel() |
| 261 | + resolver := func(_ context.Context, owner, name string, num int64, _ int64) (string, bool) { |
| 262 | + return "/should/not/appear", true |
| 263 | + } |
| 264 | + src := "Inline `#7` and:\n\n```\nblock #7 here\n```" |
| 265 | + out, refs, _, err := Render(context.Background(), []byte(src), Options{ |
| 266 | + Resolvers: Resolvers{Issue: resolver}, |
| 267 | + }) |
| 268 | + if err != nil { |
| 269 | + t.Fatalf("render: %v", err) |
| 270 | + } |
| 271 | + if strings.Contains(string(out), "/should/not/appear") { |
| 272 | + t.Errorf("ref leaked into code block: %q", out) |
| 273 | + } |
| 274 | + if len(refs) != 0 { |
| 275 | + t.Errorf("expected 0 refs inside code, got %v", refs) |
| 276 | + } |
| 277 | +} |
| 278 | + |
| 279 | +// TestRender_EmojiShortcodes checks the curated set works. |
| 280 | +func TestRender_EmojiShortcodes(t *testing.T) { |
| 281 | + t.Parallel() |
| 282 | + out, _, _, err := Render(context.Background(), []byte("ship it :rocket: :+1: :notrealemoji:"), Options{}) |
| 283 | + if err != nil { |
| 284 | + t.Fatalf("render: %v", err) |
| 285 | + } |
| 286 | + s := string(out) |
| 287 | + if !strings.Contains(s, "🚀") { |
| 288 | + t.Errorf("expected rocket emoji in output, got %q", s) |
| 289 | + } |
| 290 | + if !strings.Contains(s, "👍") { |
| 291 | + t.Errorf("expected +1 emoji in output, got %q", s) |
| 292 | + } |
| 293 | + if !strings.Contains(s, ":notrealemoji:") { |
| 294 | + t.Errorf("unknown shortcode should pass through, got %q", s) |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +// TestRender_InputTooLarge enforces the renderer's defensive cap. |
| 299 | +func TestRender_InputTooLarge(t *testing.T) { |
| 300 | + t.Parallel() |
| 301 | + big := make([]byte, MaxRenderInputBytes+1) |
| 302 | + for i := range big { |
| 303 | + big[i] = 'x' |
| 304 | + } |
| 305 | + if _, _, _, err := Render(context.Background(), big, Options{}); err == nil { |
| 306 | + t.Errorf("expected ErrInputTooLarge") |
| 307 | + } |
| 308 | +} |
| 309 | + |
| 310 | +// TestRender_SoftBreakAsBR controls the comment-vs-readme newline |
| 311 | +// handling. |
| 312 | +func TestRender_SoftBreakAsBR(t *testing.T) { |
| 313 | + t.Parallel() |
| 314 | + src := "line one\nline two\n" |
| 315 | + br, _, _, _ := Render(context.Background(), []byte(src), Options{SoftBreakAsBR: true}) |
| 316 | + noBR, _, _, _ := Render(context.Background(), []byte(src), Options{SoftBreakAsBR: false}) |
| 317 | + if !strings.Contains(string(br), "<br") { |
| 318 | + t.Errorf("SoftBreakAsBR=true: expected <br>, got %q", br) |
| 319 | + } |
| 320 | + if strings.Contains(string(noBR), "<br") { |
| 321 | + t.Errorf("SoftBreakAsBR=false: should not contain <br>, got %q", noBR) |
| 322 | + } |
| 323 | +} |
| 324 | + |
| 325 | +// TestRender_BackCompatRenderHTML keeps the old shim working so the |
| 326 | +// interim S17/S21/S22 callers don't need rewrite during S25. |
| 327 | +func TestRender_BackCompatRenderHTML(t *testing.T) { |
| 328 | + t.Parallel() |
| 329 | + html, err := RenderHTML([]byte("**bold** text")) |
| 330 | + if err != nil { |
| 331 | + t.Fatalf("RenderHTML: %v", err) |
| 332 | + } |
| 333 | + if !strings.Contains(html, "<strong>bold</strong>") { |
| 334 | + t.Errorf("expected bold, got %q", html) |
| 335 | + } |
| 336 | +} |