|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseHTML } from "linkedom"; |
| 3 | +import { isRichTextFormattingTag, sanitizeRichTextChildren } from "./richTextSanitize"; |
| 4 | + |
| 5 | +function parseWithLinkedom(html: string): Element { |
| 6 | + const { document: doc } = parseHTML( |
| 7 | + `<!DOCTYPE html><html><body><div id="test-host">${html}</div></body></html>`, |
| 8 | + ); |
| 9 | + const host = doc.getElementById("test-host"); |
| 10 | + if (!host) throw new Error("test host was not parsed"); |
| 11 | + return host as unknown as Element; |
| 12 | +} |
| 13 | + |
| 14 | +// Both DOM implementations, every case. The contract must not depend on which |
| 15 | +// parser constructs the inert tree at a trust boundary. |
| 16 | +const PARSERS: Array<[string, (html: string) => Element]> = [ |
| 17 | + [ |
| 18 | + "jsdom", |
| 19 | + (html) => { |
| 20 | + const host = document.createElement("div"); |
| 21 | + host.innerHTML = html; |
| 22 | + return host; |
| 23 | + }, |
| 24 | + ], |
| 25 | + ["linkedom", parseWithLinkedom], |
| 26 | +]; |
| 27 | + |
| 28 | +function clean(html: string, parse: (html: string) => Element): string { |
| 29 | + const host = parse(html); |
| 30 | + sanitizeRichTextChildren(host); |
| 31 | + return host.innerHTML; |
| 32 | +} |
| 33 | + |
| 34 | +describe.each(PARSERS)("sanitizeRichTextChildren (%s)", (_name, parse) => { |
| 35 | + it("keeps a styled span, which is the whole point", () => { |
| 36 | + expect(clean('<span style="color: red">hi</span>', parse)).toBe( |
| 37 | + '<span style="color: red">hi</span>', |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it("keeps plain text untouched", () => { |
| 42 | + expect(clean("just words", parse)).toBe("just words"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("keeps nested formatting and its nesting", () => { |
| 46 | + expect(clean('<b><span style="color: red">x</span></b>', parse)).toBe( |
| 47 | + '<b><span style="color: red">x</span></b>', |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it("keeps a line break", () => { |
| 52 | + expect(clean("a<br>b", parse)).toContain("<br>"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("removes a script and does not leave its source as visible text", () => { |
| 56 | + const out = clean("<script>alert(1)</script>keep", parse); |
| 57 | + expect(out).not.toContain("script"); |
| 58 | + expect(out).not.toContain("alert"); |
| 59 | + expect(out).toContain("keep"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("strips an event handler from a tag it otherwise keeps", () => { |
| 63 | + const out = clean('<span onclick="steal()" style="color: red">x</span>', parse); |
| 64 | + expect(out).not.toContain("onclick"); |
| 65 | + expect(out).toContain("color: red"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("strips every attribute that is neither style nor an identity", () => { |
| 69 | + const out = clean('<span id="a" class="b" data-x="c" style="color: red">x</span>', parse); |
| 70 | + expect(out).not.toContain("id="); |
| 71 | + expect(out).not.toContain("class="); |
| 72 | + expect(out).not.toContain("data-x"); |
| 73 | + expect(out).toContain("color: red"); |
| 74 | + }); |
| 75 | + |
| 76 | + // The design panel tracks each text layer by this. Stripping it left the |
| 77 | + // panel unable to match a layer to its source after any inline style edit. |
| 78 | + it("keeps the attributes a text layer is tracked by", () => { |
| 79 | + const out = clean( |
| 80 | + '<span data-hf-text-key="child:1" data-hf-id="hf-abc" style="color: red">x</span>', |
| 81 | + parse, |
| 82 | + ); |
| 83 | + expect(out).toContain('data-hf-text-key="child:1"'); |
| 84 | + expect(out).toContain('data-hf-id="hf-abc"'); |
| 85 | + }); |
| 86 | + |
| 87 | + it("drops an identity attribute whose value is not a bare token", () => { |
| 88 | + const out = clean(`<span data-hf-text-key='a" onload="alert(1)'>x</span>`, parse); |
| 89 | + expect(out).not.toContain("onload"); |
| 90 | + expect(out).not.toContain("data-hf-text-key"); |
| 91 | + }); |
| 92 | + |
| 93 | + // These are what the design panel writes onto those same spans. Sanitizing |
| 94 | + // them away did not stop a text edit changing layout, it deleted the layout |
| 95 | + // the user had already set: colouring one word dropped a sibling's size. |
| 96 | + it("keeps the typography the design panel authors on a text layer", () => { |
| 97 | + const out = clean( |
| 98 | + '<span style="font-family: Inter; font-size: 48px; letter-spacing: -1px; line-height: 1.2">x</span>', |
| 99 | + parse, |
| 100 | + ); |
| 101 | + expect(out).toContain("font-family: Inter"); |
| 102 | + expect(out).toContain("font-size: 48px"); |
| 103 | + expect(out).toContain("letter-spacing: -1px"); |
| 104 | + expect(out).toContain("line-height: 1.2"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("still refuses a value that reaches outside the stylesheet", () => { |
| 108 | + const out = clean(`<span style="font-family: url(http://x/f.woff)">x</span>`, parse); |
| 109 | + expect(out).not.toContain("url("); |
| 110 | + }); |
| 111 | + |
| 112 | + it("unwraps a tag that is not formatting, keeping its words in place", () => { |
| 113 | + expect(clean("before<div>middle</div>after", parse)).toBe("beforemiddleafter"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("unwraps deeply and keeps the formatting found inside", () => { |
| 117 | + const out = clean('<div><p><span style="color: red">deep</span></p></div>', parse); |
| 118 | + expect(out).toBe('<span style="color: red">deep</span>'); |
| 119 | + }); |
| 120 | + |
| 121 | + it("keeps only the allowlisted style properties", () => { |
| 122 | + const out = clean('<span style="color: red; position: fixed; z-index: 99">x</span>', parse); |
| 123 | + expect(out).toContain("color: red"); |
| 124 | + expect(out).not.toContain("position"); |
| 125 | + expect(out).not.toContain("z-index"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("keeps every property the allowlist names", () => { |
| 129 | + const style = |
| 130 | + "color: red; background-color: blue; font-weight: 700; font-style: italic; text-decoration-line: underline"; |
| 131 | + const out = clean(`<span style="${style}">x</span>`, parse); |
| 132 | + for (const property of [ |
| 133 | + "color", |
| 134 | + "background-color", |
| 135 | + "font-weight", |
| 136 | + "font-style", |
| 137 | + "text-decoration-line", |
| 138 | + ]) { |
| 139 | + expect(out).toContain(property); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + it("rejects a value that smuggles a url or a script in", () => { |
| 144 | + const out = clean( |
| 145 | + '<span style="background-color: url(javascript:alert(1)); color: red">x</span>', |
| 146 | + parse, |
| 147 | + ); |
| 148 | + expect(out).not.toContain("javascript"); |
| 149 | + expect(out).not.toContain("url("); |
| 150 | + expect(out).toContain("color: red"); |
| 151 | + }); |
| 152 | + |
| 153 | + it.each([ |
| 154 | + ["an image event handler", "<img src=x onerror=alert(1)>safe", "safe"], |
| 155 | + ["a script URL", '<a href="javascript:alert(1)">safe</a>', "safe"], |
| 156 | + ["an SVG script", "<svg><script>alert(1)</script></svg>safe", "safe"], |
| 157 | + [ |
| 158 | + "a legacy CSS expression", |
| 159 | + '<span style="color: expression(alert(1))">safe</span>', |
| 160 | + "<span>safe</span>", |
| 161 | + ], |
| 162 | + [ |
| 163 | + "an entity-encoded script scheme", |
| 164 | + '<span style="color: javascript:alert(1)">safe</span>', |
| 165 | + "<span>safe</span>", |
| 166 | + ], |
| 167 | + [ |
| 168 | + "a case-folded script scheme", |
| 169 | + '<span style="color: JAVASCRIPT:alert(1)">safe</span>', |
| 170 | + "<span>safe</span>", |
| 171 | + ], |
| 172 | + ])("rejects %s", (_case, html, expected) => { |
| 173 | + expect(clean(html, parse)).toBe(expected); |
| 174 | + }); |
| 175 | + |
| 176 | + it("drops the style attribute entirely when nothing in it survives", () => { |
| 177 | + expect(clean('<span style="position: fixed">x</span>', parse)).toBe("<span>x</span>"); |
| 178 | + }); |
| 179 | + |
| 180 | + it("keeps a value carrying a function with its own separators", () => { |
| 181 | + const out = clean('<span style="color: rgb(1, 2, 3); font-style: italic">x</span>', parse); |
| 182 | + expect(out).toContain("rgb(1, 2, 3)"); |
| 183 | + expect(out).toContain("font-style: italic"); |
| 184 | + }); |
| 185 | + |
| 186 | + it("keeps a quoted semicolon inside a style value", () => { |
| 187 | + const out = clean(`<span style='font-family: "Roboto Mono; a"; color: red'>x</span>`, parse); |
| 188 | + expect(out).toContain("Roboto Mono; a"); |
| 189 | + expect(out).toContain("color: red"); |
| 190 | + }); |
| 191 | + |
| 192 | + it("removes a comment, which is neither text nor formatting", () => { |
| 193 | + expect(clean("a<!-- note -->b", parse)).toBe("ab"); |
| 194 | + }); |
| 195 | + |
| 196 | + it("leaves an empty element alone", () => { |
| 197 | + expect(clean("", parse)).toBe(""); |
| 198 | + }); |
| 199 | + |
| 200 | + it("does not produce unbalanced markup from an unclosed tag", () => { |
| 201 | + const out = clean('<span style="color: red">open', parse); |
| 202 | + expect(out).toBe('<span style="color: red">open</span>'); |
| 203 | + }); |
| 204 | + |
| 205 | + it("is a fixed point", () => { |
| 206 | + const host = parse('<div><span onclick="steal()" style="color: red">x</span></div>'); |
| 207 | + sanitizeRichTextChildren(host); |
| 208 | + const once = host.innerHTML; |
| 209 | + sanitizeRichTextChildren(host); |
| 210 | + expect(host.innerHTML).toBe(once); |
| 211 | + }); |
| 212 | +}); |
| 213 | + |
| 214 | +it("sanitizes adversarially deep markup without recursive stack growth", () => { |
| 215 | + const depth = 15_000; |
| 216 | + const host = parseWithLinkedom(`${"<b>".repeat(depth)}x${"</b>".repeat(depth)}`); |
| 217 | + |
| 218 | + expect(() => sanitizeRichTextChildren(host)).not.toThrow(); |
| 219 | +}); |
| 220 | + |
| 221 | +describe("isRichTextFormattingTag", () => { |
| 222 | + it("names the tags an inline edit may contain", () => { |
| 223 | + for (const tag of ["SPAN", "B", "STRONG", "I", "EM", "U", "BR"]) { |
| 224 | + expect(isRichTextFormattingTag(tag)).toBe(true); |
| 225 | + } |
| 226 | + }); |
| 227 | + |
| 228 | + it("is case-insensitive, since the two parsers disagree about case", () => { |
| 229 | + expect(isRichTextFormattingTag("span")).toBe(true); |
| 230 | + }); |
| 231 | + |
| 232 | + it("says no to anything structural", () => { |
| 233 | + for (const tag of ["DIV", "P", "H1", "IMG", "SCRIPT", "A"]) { |
| 234 | + expect(isRichTextFormattingTag(tag)).toBe(false); |
| 235 | + } |
| 236 | + }); |
| 237 | +}); |
0 commit comments