From 40986b7c76ff51bd85267547fabd996cdb77cb88 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Mon, 23 Nov 2026 17:07:12 -0800 Subject: [PATCH] make-pdf: quotes in alt text, titles and raw HTML can no longer add script handlers (GST-3641) (#4761) Quotes in image alt text, link titles and raw HTML attributes now stay inside their attribute, so a Markdown file can no longer add an `onerror`, `onload` or `javascript:` link that runs in make-pdf's print browser. Why: the print browser runs page scripts, and on main a crafted image caption made it send the document's own text to another address. Nothing to do: ordinary Markdown prints the same (609 of gstack's own Markdown files render identically). 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 --- ...e-pdf-quote-entities-stay-in-attributes.md | 4 + make-pdf/src/render.ts | 53 +++--- make-pdf/src/smartypants.ts | 9 +- make-pdf/test/render.test.ts | 159 ++++++++++++++++++ 4 files changed, 194 insertions(+), 31 deletions(-) create mode 100644 changelog.d/make-pdf-quote-entities-stay-in-attributes.md diff --git a/changelog.d/make-pdf-quote-entities-stay-in-attributes.md b/changelog.d/make-pdf-quote-entities-stay-in-attributes.md new file mode 100644 index 000000000..e3ee54bf6 --- /dev/null +++ b/changelog.d/make-pdf-quote-entities-stay-in-attributes.md @@ -0,0 +1,4 @@ +--- +important: security +--- +**make-pdf keeps quotes in alt text, link titles and raw HTML attributes inside the attribute, so a Markdown file can no longer add an `onerror`, `onload` or `javascript:` link that runs in the print browser.** make-pdf used to turn `"` back into `"` after its sanitizer had run, so an image caption holding a quote followed by an `onerror` handler printed as a live handler; quote entities are now read as quotes only in text, where they become curly quotes. The sanitizer also catches a handler or `javascript:` link written right after a `/` or a closing quote inside a raw HTML block, and no longer leaves an attribute or element open when it removes handler text. Handler-looking text such as `/onload=` is still removed wherever it appears, URLs and code included. Quotes in alt text and titles also render correctly now instead of cutting the text short. diff --git a/make-pdf/src/render.ts b/make-pdf/src/render.ts index ac43415ae..19be20bed 100644 --- a/make-pdf/src/render.ts +++ b/make-pdf/src/render.ts @@ -79,15 +79,11 @@ export function render(opts: RenderOptions): RenderResult { // 2. Sanitize const cleanHtml = sanitizeUntrustedHtml(directedHtml); - // 3. Decode common entities so smartypants can match raw " and '. - // marked HTML-encodes quotes in text ("hello" → "hello"); - // without decoding, smartypants' regex never fires. These get re-encoded - // implicitly by the browser's HTML parser downstream, and for the ones - // that should stay as curly-quote Unicode, that IS the final form. - const decoded = decodeTypographicEntities(cleanHtml); - - // 4. Smartypants (code-safe) - const typographicHtml = smartypants(decoded); + // 3. Smartypants (code-safe). It reads marked's quote entities + // (", ') in text as quotes and curls them; tags, code and URLs + // keep theirs, so a decoded quote can never end an attribute value that + // the sanitizer above has already checked. + const typographicHtml = smartypants(cleanHtml); // 4. Derive metadata (title from first H1 if not provided) const derivedTitle = opts.title ?? extractFirstHeading(typographicHtml) ?? "Document"; @@ -174,19 +170,6 @@ export function render(opts: RenderOptions): RenderResult { }; } -/** - * Decode the HTML entities that marked emits for text-node quotes/apostrophes. - * Only the four that matter for smartypants — leaves & alone because it - * can be legitimately doubled (&amp;) and we don't want to double-decode. - */ -function decodeTypographicEntities(html: string): string { - return html - .replace(/"/g, "\"") - .replace(/'/g, "'") - .replace(/'/g, "'") - .replace(/'/g, "'"); -} - // ─── Sanitizer ──────────────────────────────────────────────────────── /** @@ -230,14 +213,24 @@ export function sanitizeUntrustedHtml(html: string): string { return `${body.replace(//gi, "")}`; }); - // Event handler attributes (on* in any case). - 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, ""); + // Event handler attributes (on* in any case). The browser starts an + // attribute name after whitespace, a "/" or a closing quote + // (, ), so all four count. + // An unquoted value stops at a quote or "<": marked writes a quote inside + // an attribute as ", so the next straight quote closes the attribute + // this handler text sits in, and the next "<" starts a tag (text such as + // `a/onset=2` in a code span sits right before ). Eating either + // would leave an attribute or element open. The value may be empty, so the + // handler's name is always removed, even before a value that starts with + // "<" (onerror=