Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA
TypeScript
make-pdf: a NUL character can no longer hide script from the sanitizer (#4762)
Garry Tanf7402213a04d
diff --git a/changelog.d/make-pdf-nul-cannot-revive-handlers.md b/changelog.d/make-pdf-nul-cannot-revive-handlers.mdnew file mode 100644index 000000000..7b21bf770--- /dev/null+++ b/changelog.d/make-pdf-nul-cannot-revive-handlers.md@@ -0,0 +1,4 @@+---+important: security+---+**make-pdf no longer lets a NUL character hide an `onerror` handler, a `javascript:` link or a `<script>` tag from its sanitizer.** The typography pass drops U+0000 after the sanitizer has run, so `on\u0000error=` became a live `onerror` in the print browser; the sanitizer now drops NUL first. Removing a handler could also join the two halves of a tag name, so `<scr onx="a"ipt>` came out as a live `<script>`; the sanitizer now repeats until its output stops changing. Removing a handler or a `javascript:` link also keeps the page's structure. Handler-looking text inside a quoted attribute no longer deletes the elements after it, and a `javascript:` value loses only its scheme instead of gaining quotes, and the scheme is also caught after leading spaces or with its colon written as an entity.diff --git a/make-pdf/src/render.ts b/make-pdf/src/render.tsindex 19be20bed..e696cfa1b 100644--- a/make-pdf/src/render.ts+++ b/make-pdf/src/render.ts@@ -193,6 +193,21 @@ export function render(opts: RenderOptions): RenderResult {* (@import, url(), image-set() with remote string candidates).*/export function sanitizeUntrustedHtml(html: string): string {+ // smartypants drops U+0000 after this runs, which would join on\u0000error+ // or java\u0000script: back together, so check the string without it.+ // A removal can also join what was around it (<scr onx="a"ipt> becomes+ // <script>), so repeat the pass until nothing changes, and print the page+ // as text if it never settles.+ let s = html.replace(/\u0000/g, "");+ for (let i = 0; i < 8; i++) {+ const next = sanitizePass(s);+ if (next === s) return s;+ s = next;+ }+ return escapeHtml(s);+}++function sanitizePass(html: string): string {let s = html;// Elements to remove entirely (including content).@@ -224,15 +239,21 @@ export function sanitizeUntrustedHtml(html: string): string {// handler's name is always removed, even before a value that starts with// "<" (onerror=<!-- … runs its second line). Matching runs over the// whole page, text included, so "/on…=" inside a URL or code goes too.- s = s.replace(/(?:\s+|(?<=[/"']))on[a-zA-Z]+\s*=\s*"[^"]*"/gi, "");- s = s.replace(/(?:\s+|(?<=[/"']))on[a-zA-Z]+\s*=\s*'[^']*'/gi, "");+ // A quoted value goes with its name only when it holds no "<" or ">":+ // otherwise the quote may belong to a later element, so the last rule+ // removes just the name and the rest stays where it was.+ s = s.replace(/(?:\s+|(?<=[/"']))on[a-zA-Z]+\s*=\s*"[^"<>]*"/gi, "");+ s = s.replace(/(?:\s+|(?<=[/"']))on[a-zA-Z]+\s*=\s*'[^'<>]*'/gi, "");s = s.replace(/(?:\s+|(?<=[/"']))on[a-zA-Z]+\s*=\s*[^\s>"'`<]*/gi, "");// javascript: URLs in href/src/action/formaction (same attribute starts).s = s.replace(- /([\s/"']\s*(?:href|src|action|formaction|xlink:href)\s*=\s*)(?:"javascript:[^"]*"|'javascript:[^']*'|javascript:[^\s>]+)/gi,+ /([\s/"']\s*(?:href|src|action|formaction|xlink:href)\s*=\s*)(?:"javascript:[^"<>]*"|'javascript:[^'<>]*')/gi,'$1"#"',);+ // Any other javascript: value (unquoted, or quoted around "<" or ">") loses+ // only its scheme, so no quote is added or removed.+ s = s.replace(/([\s/"']\s*(?:href|src|action|formaction|xlink:href)\s*=\s*["']?[\s\x01-\x20]*)javascript(?::|:|�*58;?|�*3a;?)/gi, "$1#");// srcdoc attribute (iframe escape hatch — already stripped via iframe above,// but defense-in-depth).diff --git a/make-pdf/test/render.test.ts b/make-pdf/test/render.test.tsindex e95de526e..8c3436c35 100644--- a/make-pdf/test/render.test.ts+++ b/make-pdf/test/render.test.ts@@ -480,6 +480,98 @@ describe("render() — quote entities never open attributes or tags", () => {});});+describe("render() — characters dropped after sanitizing cannot revive script", () => {+ // The typography pass drops U+0000 after the sanitizer has run, so the+ // sanitizer has to see the page without it.+ test("a NUL inside a handler name, a javascript: scheme or a tag name is no way around the sanitizer", () => {+ const md = [+ "<div>",+ "<img src=pic.png on\u0000error=alert(1)>",+ "<img src=\"pic.png\" o\u0000nload=\"alert(1)\">",+ "<a href=\"java\u0000script:alert(1)\">j</a>",+ "<a href=java\u0000script:alert(1)>k</a>",+ "<scr\u0000ipt>alert(1)</script>",+ "</div>",+ "",+ ].join("\n");+ const html = render({ markdown: md }).bodyHtml;+ expect(liveScript(html)).toEqual([]);+ expect(html).not.toMatch(/<script/i);+ expect(html).not.toContain("\u0000");+ expect(html).toContain(">j</a>");+ expect(html).toContain(">k</a>");+ });++ test("sanitizeUntrustedHtml drops NUL itself, for callers that skip the typography pass", () => {+ const out = sanitizeUntrustedHtml("<svg on\u0000load=alert(1)><circle r=\"2\"/></svg>");+ expect(out).not.toContain("\u0000");+ expect(liveScript(out)).toEqual([]);+ });+});++describe("render() — removing script keeps the page's structure", () => {+ test("handler-looking text inside a quoted attribute never eats the elements after it", () => {+ for (const md of [+ `<div title="docs/onload='example">Keep me</div><p title='other'>Next</p>\n`,+ `<div title='docs/onload="example'>Keep me</div><p title="other">Next</p>\n`,+ ]) {+ const html = render({ markdown: md }).bodyHtml;+ expect(liveScript(html)).toEqual([]);+ expect(html).toContain(">Keep me</div>");+ expect(html).toContain(">Next</p>");+ expect(attrsOf(html, "div")).toEqual(["title"]);+ expect(attrsOf(html, "p")).toEqual(["title"]);+ }+ });++ test("a handler value holding < or > loses its handler without spilling into the page", () => {+ const html = render({ markdown: `<div>\n<img src="pic.png" onerror="a>b<img src=x onload=alert(1)>">\n</div>\n` }).bodyHtml;+ expect(liveScript(html)).toEqual([]);+ expect(html).not.toMatch(/onerror|onload/i);+ });++ test("a javascript: href holding < or > is still neutralized", () => {+ const html = render({ markdown: `<div>\n<a href="javascript:a>b">j</a>\n</div>\n` }).bodyHtml;+ expect(liveScript(html)).toEqual([]);+ });++ test("an ordinary link whose path contains /href=javascript: keeps its attributes and text", () => {+ const html = render({ markdown: "[Reference](https://example.com/docs/href=javascript:overview) after\n" }).bodyHtml;+ expect(liveScript(html)).toEqual([]);+ expect(attrsOf(html, "a")).toEqual(["href"]);+ expect(html).toContain(">Reference</a> after");+ expect(browserAttrs(html).find((a) => a.name === "href")!.value).toMatch(/^https:\/\/example\.com\/docs\//);+ });++ test("removing an attribute cannot join the halves of a dangerous tag name", () => {+ for (const x of [+ `<scr onx="a"ipt>alert(1)</script>`,+ `<scr onx='a'ipt>alert(1)</script>`,+ `<scr srcdoc="a"ipt>alert(1)</script>`,+ `<ifr onx="a"ame src="pic.png"></iframe>`,+ `<ob onx="a"ject data="pic.png"></object>`,+ ]) {+ const html = render({ markdown: `<div>\n${x}\n</div>\n` }).bodyHtml;+ expect(html).not.toMatch(/<(?:script|iframe|object)\b/i);+ expect(liveScript(html)).toEqual([]);+ }+ });++ test("a javascript: value after leading spaces or with an encoded colon is neutralized", () => {+ for (const a of [`href=" javascript:alert(1)"`, `href="\tjavascript:alert(1)"`, "href=javascript:alert(1)", `href="javascript:alert(1)"`, "href=javascript:alert(1)"]) {+ const html = render({ markdown: `<div>\n<a ${a}>j</a>\n</div>\n` }).bodyHtml;+ expect(liveScript(html)).toEqual([]);+ expect(html).toContain(">j</a>");+ }+ });++ test("an unquoted javascript: value is replaced without adding quotes", () => {+ const out = sanitizeUntrustedHtml("<a href=javascript:alert(1)>j</a>");+ expect(liveScript(out)).toEqual([]);+ expect(out).not.toContain('"');+ });+});+// ─── print-css ──────────────────────────────────────────────describe("printCss", () => {