| 1 | import { describe, expect, it } from "bun:test"; |
| 2 | import { renderToStaticMarkup } |
| 3 | |
| 4 | import { Markdown } |
| 5 | |
| 6 | /** The rendered markdown, without the wrapper's class soup. */ |
| 7 | function render( |
| 8 | markdown: string, |
| 9 | resolveUrl?: (url |
| 10 | ) { |
| 11 | const html = |
| 12 | <Markdown resolveUrl={ |
| 13 | ); |
| 14 | return html.slice |
| 15 | } |
| 16 | |
| 17 | describe("Markdown", () |
| 18 | it("keeps the HTML READMEs are actually written with", |
| 19 | expect(render('<h1 align="center">Ghost</h1> |
| 20 | '<h1 align="center">Ghost</h1>', |
| 21 | ); |
| 22 | expect( |
| 23 | render("<details><summary>s</summary>\n\nbody\n\n |
| 24 | ).toContain("<details><summary>s</summary>" |
| 25 | expect( |
| 26 | render('<table><tr><td align="center">x</td></tr></table>'), |
| 27 | ).toContain('<td style="text-align:center">x</td>' |
| 28 | }); |
| 29 | |
| 30 | it("drops script, event handlers, javascript: and inline style", |
| 31 | expect(render("<script>alert(1)</script> |
| 32 | expect(render('<img src="x" onerror="alert(1)"> |
| 33 | expect(render('<a href="javascript:alert(1)">x</a> |
| 34 | expect(render('<iframe src="https://evil.example.com"></iframe> |
| 35 | expect(render('<div style="position:fixed;inset:0">x</div> |
| 36 | "<div>x</div>", |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | /** `align` on a cell becomes `style="text-align:<value>"` downstream of the sanitizer, so anything but the legal values is a CSS injection. */ |
| 41 | it("refuses an align value that would smuggle CSS into style", |
| 42 | expect( |
| 43 | render( |
| 44 | '<table><tr><td align="right;position:fixed;inset:0">x</td></tr></table>', |
| 45 | ), |
| 46 | ).not.toContain( |
| 47 | expect(render("| a | |
| 48 | 'style="text-align:center"', |
| 49 | ); |
| 50 | }); |
| 51 | |
| 52 | it("keeps target and rel, and never opens a tab that keeps the opener", |
| 53 | expect( |
| 54 | render('<a href="https://x.example.com" target="_blank">x</a>'), |
| 55 | ).toContain('rel="noopener noreferrer"' |
| 56 | expect( |
| 57 | render( |
| 58 | '<a href="https://x.example.com" target="_blank" rel="nofollow">x</a>', |
| 59 | ), |
| 60 | ).toContain('rel="nofollow noopener noreferrer"' |
| 61 | expect( |
| 62 | render('<a href="https://x.example.com" target="_top">x</a>'), |
| 63 | ).toContain('target="_top"' |
| 64 | // not a real target: dropped before the component ever sees it |
| 65 | expect( |
| 66 | render('<a href="https://x.example.com" target="evilframe">x</a>'), |
| 67 | ).not.toContain( |
| 68 | }); |
| 69 | |
| 70 | it("gives every link the w-fit class", |
| 71 | expect(render("[x](https://example.com) |
| 72 | }); |
| 73 | |
| 74 | it("rewrites relative URLs, in markdown and in raw HTML alike", |
| 75 | const resolveUrl = |
| 76 | |
| 77 | expect(render(" |
| 78 | 'src="/src/shot.png"', |
| 79 | ); |
| 80 | expect(render('<img src="shot.png"> |
| 81 | 'src="/src/shot.png"', |
| 82 | ); |
| 83 | expect(render("[x](docs/a.md) |
| 84 | 'href="/href/docs/a.md"', |
| 85 | ); |
| 86 | expect(render("[x](https://example.com) |
| 87 | 'href="https://example.com"', |
| 88 | ); |
| 89 | expect(render("[x](#anchor) |
| 90 | }); |
| 91 | |
| 92 | it("links references in issue text, but not in code, links or files", |
| 93 | const html = |
| 94 | <Markdown repository={ |
| 95 | {"Fixes #1 and them/lib#2, cc @bob. `#3` [#4](https://x.io)"} |
| 96 | </Markdown>, |
| 97 | ); |
| 98 | |
| 99 | expect(html).toContain |
| 100 | expect(html).toContain |
| 101 | expect(html).toContain |
| 102 | expect(html).toContain |
| 103 | expect(html).toContain |
| 104 | expect(html).not |
| 105 | expect(render("see #1 |
| 106 | }); |
| 107 | }); |