|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { Marked } from "marked"; |
| 3 | +import { createSafeRenderer, escapeHtml, isSafeUrl } from "@/lib/markdownSafety"; |
| 4 | + |
| 5 | +/** Renders markdown exactly as lib/docs.ts does. */ |
| 6 | +function render(markdown: string): string { |
| 7 | + const marked = new Marked({ renderer: createSafeRenderer() }); |
| 8 | + return marked.parse(markdown, { gfm: true, async: false }) as string; |
| 9 | +} |
| 10 | + |
| 11 | +describe("escapeHtml", () => { |
| 12 | + it("escapes the characters that break out of markup", () => { |
| 13 | + expect(escapeHtml(`<script>"&"</script>`)).toBe( |
| 14 | + "<script>"&"</script>", |
| 15 | + ); |
| 16 | + }); |
| 17 | + |
| 18 | + it("escapes ampersands before the entities it introduces", () => { |
| 19 | + expect(escapeHtml("<")).toBe("&lt;"); |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe("isSafeUrl", () => { |
| 24 | + it("allows http, https, mailto, relative and anchor URLs", () => { |
| 25 | + for (const url of [ |
| 26 | + "https://example.com", |
| 27 | + "http://example.com", |
| 28 | + "mailto:a@b.c", |
| 29 | + "/docs/guides/webhooks", |
| 30 | + "./sibling.md", |
| 31 | + "#section", |
| 32 | + ]) { |
| 33 | + expect(isSafeUrl(url), url).toBe(true); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + it("blocks script-bearing schemes", () => { |
| 38 | + for (const url of [ |
| 39 | + "javascript:alert(1)", |
| 40 | + "JavaScript:alert(1)", |
| 41 | + "data:text/html;base64,PHNjcmlwdD4=", |
| 42 | + "vbscript:msgbox(1)", |
| 43 | + ]) { |
| 44 | + expect(isSafeUrl(url), url).toBe(false); |
| 45 | + } |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("safe renderer (MEDIUM-5 regression)", () => { |
| 50 | + // Docs content is repo markdown, but this repo merges contributor docs PRs |
| 51 | + // at volume and docs diffs get the least scrutiny. The output here goes |
| 52 | + // straight into dangerouslySetInnerHTML. |
| 53 | + it("escapes an inline <script> instead of emitting it", () => { |
| 54 | + const html = render(`# Title\n\n<script>alert(1)</script>\n`); |
| 55 | + expect(html).not.toContain("<script>"); |
| 56 | + expect(html).toContain("<script>"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("escapes an onerror image payload", () => { |
| 60 | + const html = render(`<img src=x onerror="alert(1)">`); |
| 61 | + expect(html).not.toMatch(/<img[^>]*onerror/i); |
| 62 | + expect(html).toContain("<img"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("strips a javascript: link but keeps its text", () => { |
| 66 | + const html = render(`[click me](javascript:alert(1))`); |
| 67 | + expect(html).not.toContain("javascript:"); |
| 68 | + expect(html).toContain("click me"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("strips a data: image but keeps its alt text", () => { |
| 72 | + const html = render(``); |
| 73 | + expect(html).not.toContain("data:text/html"); |
| 74 | + expect(html).toContain("alt text"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("escapes raw HTML embedded mid-paragraph", () => { |
| 78 | + const html = render(`Normal text <iframe src="https://evil.example"></iframe> more text.`); |
| 79 | + expect(html).not.toContain("<iframe"); |
| 80 | + expect(html).toContain("<iframe"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("still renders ordinary markdown", () => { |
| 84 | + const html = render( |
| 85 | + ["# Heading", "", "Some **bold** text and `code`.", "", "- item one", "- item two"].join( |
| 86 | + "\n", |
| 87 | + ), |
| 88 | + ); |
| 89 | + expect(html).toContain("<h1>Heading</h1>"); |
| 90 | + expect(html).toContain("<strong>bold</strong>"); |
| 91 | + expect(html).toContain("<code>code</code>"); |
| 92 | + expect(html).toContain("<li>item one</li>"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("keeps safe links and images working", () => { |
| 96 | + expect(render(`[docs](/docs/guides/webhooks)`)).toContain( |
| 97 | + `<a href="/docs/guides/webhooks">docs</a>`, |
| 98 | + ); |
| 99 | + expect(render(``)).toContain( |
| 100 | + `<img src="https://example.com/logo.png" alt="logo">`, |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it("applies a caller-supplied link resolver only to safe URLs", () => { |
| 105 | + const marked = new Marked({ |
| 106 | + renderer: createSafeRenderer((href) => `/reference/${href}`), |
| 107 | + }); |
| 108 | + const html = marked.parse(`[a](sibling.md) [b](javascript:alert(1))`, { |
| 109 | + gfm: true, |
| 110 | + async: false, |
| 111 | + }) as string; |
| 112 | + |
| 113 | + expect(html).toContain(`href="/reference/sibling.md"`); |
| 114 | + expect(html).not.toContain("javascript:"); |
| 115 | + }); |
| 116 | +}); |
0 commit comments