From f7402213a04d720eed5c50557c0ba528a69af4bd Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Tue, 24 Nov 2026 06:12:46 -0800 Subject: [PATCH] make-pdf: a NUL character can no longer hide script from the sanitizer (#4762) make-pdf drops NUL characters before its sanitizer runs, and the sanitizer now repeats until its output stops changing, so neither a NUL nor a removed attribute can turn a Markdown file's raw HTML into a live handler, `javascript:` link or `` tag. Removing a handler or a `javascript:` link also keeps the rest of the page. This follows #4761 and is stacked on it until it lands. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=security Review-Finding: p1/red-team#2 reader=red-team Review-Finding: p2/red-team#1 reader=red-team Review-Finding: p1/red-team#1 reader=red-team --- .../make-pdf-nul-cannot-revive-handlers.md | 4 + make-pdf/src/render.ts | 27 +++++- make-pdf/test/render.test.ts | 92 +++++++++++++++++++ 3 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 changelog.d/make-pdf-nul-cannot-revive-handlers.md diff --git a/changelog.d/make-pdf-nul-cannot-revive-handlers.md b/changelog.d/make-pdf-nul-cannot-revive-handlers.md new file mode 100644 index 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 `", + "", + "", + ].join("\n"); + const html = render({ markdown: md }).bodyHtml; + expect(liveScript(html)).toEqual([]); + expect(html).not.toMatch(/`, + `alert(1)`, + `alert(1)`, + ``, + ``, + ]) { + const html = render({ markdown: `
\n${x}\n
\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: `
\nj\n
\n` }).bodyHtml; + expect(liveScript(html)).toEqual([]); + expect(html).toContain(">j"); + } + }); + + test("an unquoted javascript: value is replaced without adding quotes", () => { + const out = sanitizeUntrustedHtml("j"); + expect(liveScript(out)).toEqual([]); + expect(out).not.toContain('"'); + }); +}); + // ─── print-css ────────────────────────────────────────────── describe("printCss", () => {