SPB Git

Repository Public

markdown.test.ts 2.8 KB 44 lines typescript

1import { createElement } from "react";2import { renderToStaticMarkup } from "react-dom/server";3import { describe, expect, it } from "vitest";4import { Markdown } from "@/components/ui/Markdown";56const render = (source: string) => renderToStaticMarkup(createElement(Markdown, { source }));78describe("Markdown rendering (public pages and studio preview)", () => {9  it("never lets markup, scripts, handlers or active links through", () => {10    const html = render([11      "<script>alert(1)</script>", "<img src=x onerror=alert(2)>", '<iframe src="https://evil.example"></iframe>', "<svg onload=alert(3)></svg>", '<a href="javascript:alert(4)">x</a>',12      "[a](javascript:alert(5))", "[b](data:text/html,<script>alert(6)</script>)", "[c](vbscript:x)", "[d](//evil.example)", "[e](/chemin/interne)", "![img](https://example.org/x.png)",13      "<style>body{display:none}</style>", "Texte <b onmouseover=alert(7)>gras</b> après.", "[f](https://example.org/ok)",14    ].join("\n\n"));15    for (const bad of ["<script", "<img", "<iframe", "<svg", "<style", "onerror", "onload", "onmouseover", 'href="javascript', 'href="data:', 'href="vbscript', 'href="//', "evil.example"]) expect(html, bad).not.toContain(bad);16    expect(html.match(/<a /g)).toHaveLength(1); // only the https link is a link17    expect(html).toContain('href="https://example.org/ok" target="_blank" rel="noopener noreferrer"');18    expect(html).toContain("après");19  });2021  it("typesets LaTeX from double dollars only, with KaTeX's unsafe commands off", () => {22    const html = render("Prix : 5 $ pour l'un, 6 $ pour l'autre.\n\nÉnergie : $$E = mc^2$$\n\n$$\n\\href{javascript:alert(1)}{clic} \\includegraphics{x} \\int_0^1 x\\,dx\n$$");23    expect(html).toContain("5 $ pour l&#x27;un, 6 $ pour l&#x27;autre"); // single dollars are left alone24    expect(html.match(/class="katex"/g)!.length).toBe(2);25    expect(html).not.toContain("<a ");26    expect(html).not.toContain("<img");27  });2829  it("highlights code by class names and wraps tables in a focusable, labelled region", () => {30    const html = render("```python\ndef f(x):\n    return x  # commentaire\n```\n\n```inconnu\n<script>x</script>\n```\n\n| a | b |\n|---|---|\n| 1 | 2 |\n");31    expect(html).toContain('<span class="hljs-keyword">def</span>');32    expect(html).toContain("&lt;script&gt;x&lt;/script&gt;"); // an unknown language is shown as text, escaped33    expect(html).toContain('<pre tabindex="0">');34    expect(html).toContain('role="group" aria-label="Tableau : faites défiler horizontalement si nécessaire"');35    expect(html).toContain("<th>a</th>");36  });3738  it("shifts headings so that imported text cannot outrank the page's own headings", () => {39    const html = renderToStaticMarkup(createElement(Markdown, { source: "# Un\n\n## Deux", headingOffset: 2 }));40    expect(html).toContain("<h3>Un</h3>");41    expect(html).toContain("<h4>Deux</h4>");42  });43});44
Files main
Menu

Theme