Skip to content

make-pdf: quotes in alt text, titles and raw HTML can no longer add script handlers (GST-3641) - #4761

Merged
capy-ai[bot] merged 3 commits into
mainfrom
make-pdf-quote-entities-stay-in-attributes
Nov 24, 2026
Merged

capy-ai[bot] merged 3 commits into
mainfrom
make-pdf-quote-entities-stay-in-attributes

Conversation

@garrytan

@garrytan garrytan commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

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).

What changed
  • Quote entities are read only in text. render() used to turn ", ', ' and ' back into straight quotes across the whole page after the sanitizer had run, so an image caption such as x"/onerror="alert(1) printed as a live handler. That pass is gone. Smartypants now reads those entities as quotes inside its text runs only, and every quote in a text run comes out curly, so an entity can never become attribute or tag syntax. Tags, code and URLs keep their entities, which the browser shows as the same characters.
  • Handlers and javascript: links after / or a closing quote. The browser starts an attribute after whitespace, a / or a closing quote. The sanitizer only looked after whitespace, so inside a raw HTML block <img src="x"/onerror="…">, <img src="x"onerror="…">, <svg/onload=…> and <a title="t"href="javascript:…"> passed straight through on main. The handler and javascript: rules now match after all four, and the handler's name is always removed, whatever its value starts with.
  • No attribute or element left open. An unquoted handler value now stops at a quote or <, so removing handler text from inside a title, or from a code span such as a/onset=2, no longer eats the closing quote or the </code> tag.
  • Quotes in alt text and titles now render whole instead of cutting the text at the first quote.
Security notes

Does the print browser run page scripts? Yes, on both engines. Both open the staged HTML over a loopback server at http://127.0.0.1:<port>/<nonce>/document.html with JavaScript on: the Aside path injects its error hook with Page.addScriptToEvaluateOnNewDocument and both paths poll page expressions, so script is required there, and nothing sets a Content Security Policy or intercepts requests. Checked on this branch's base (a256d15) with gstack's own browser (headless Chromium 158): a raw-HTML <svg/onload=fetch(…)> and an image caption whose encoded quotes add onload each sent a request to a local listener during make-pdf generate, the first with the page URL (including the per-render nonce) and the document's first 30 characters. On this branch the same two files sent nothing. Aside was not run (macOS only); it runs the same page in a tab of the user's Aside browser.

What a script could reach there:

  • The document itself and anything in the staging directory (only document.html; images are inlined as data URIs).
  • The network, whatever --allow-network says: make-pdf's offline posture is enforced by the sanitizer and the image inliner, not the browser. A script can send the document anywhere, and can send requests to other loopback services on the machine.
  • Local files: no. The page's origin is http://127.0.0.1, so Chromium refuses file:// reads, and the loopback server serves only the staging directory, checked on the resolved path.
  • The user's sessions: requests only, not reads. The page is cross-origin to every site, so it can't read another site's cookies or responses. It can still send requests that include the browser's cookies for sites that allow cross-site cookies, and it can rewrite what gets printed.

Still open, older than this PR:

  • NUL characters (script runs). Smartypants deletes U+0000 from the whole page after the sanitizer has run, so a handler name or javascript: scheme with a NUL inside it (on<NUL>error=) passes the sanitizer and comes out live. This exists on main today (since browse --session before the command, Gemini AQ. keys, /verify trust across worktrees, and two more fixes from outside reports #4684) and this PR neither causes nor worsens it. The fix (strip those characters before sanitizing, and make the handler and javascript: rules edit only the attribute name, never quotes) is the next PR, opened now from main.
  • A javascript: link written with numeric entities (&#106;avascript:) is not rewritten; it only runs if someone clicks it.
  • The offline-posture rules for style, srcset, SVG href and media src still look only after whitespace, so raw HTML glued with / or a quote can fetch a remote URL at print time (no script).
  • A Content Security Policy for the print page (an existing P1 item in TODOS.md) would stop this whole class at the browser.
Tests
  • bun test make-pdf/test/: 317 tests ran, 0 failed (final check at e1f0632). typecheck clean; typecheck:test 812 known, none new.
  • Render diff of all 609 tracked Markdown files, main vs this branch: 0 differ.
Why

make-pdf prints Markdown that often comes from somewhere else: a README, a report an agent fetched, a document a user was sent. The sanitizer is what stands between that text and a browser that runs scripts, and both bypasses let script run with access to the document being printed and to the network.

Blast radius

make-pdf/src/render.ts (sanitizer and render pipeline) and make-pdf/src/smartypants.ts. Rendered all 609 tracked Markdown files with main and with this branch, reading quote entities as characters: 0 differ. Handler-looking text such as /onload=x is now removed wherever it appears, URLs and code included; the same was already true after whitespace.

Test Coverage

New tests in make-pdf/test/render.test.ts check what the browser's tokenizer would see, not exact strings: an onerror from an image alt, an onload from an image title (which must not swallow the next paragraph), a second href from a link title, a javascript: href from a raw-HTML title, single-quote entities ending a raw attribute, handlers or javascript: links after / or a closing quote, handler values starting with < or a quote (including an HTML comment opener followed by an encoded newline), handler text in a code span that must not eat </code>, and a positive control. Two smartypants tests pin the contract. On base, with only the tests applied, 7 of the first 10 fail; the image-title test passes on base and guards a regression this change would otherwise cause.

Pre-Landing Review

Pre-Landing Review: 10 issues — 3 auto-fixed, 0 asked, 1 deferred, 4 noted, 2 open as ruled notes.

Deferred findings
  • Deferred: CRITICAL, in code this PR did not change: the NUL-character bypass described under Security notes. It predates this PR and goes in the next PR.
Review notes
  • Informational: the offline-posture rules still need whitespace before the attribute (see Security notes).
  • Informational: quoted handler values can still eat an enclosing attribute's closing quote on crafted input; /on…= in URLs and code is removed; the javascript: rewrite can flip quote parity on crafted input. None runs script; all are covered by the structure-preserving rewrite in the next PR.
Exploratory QA

Functional, make-pdf generate through gstack's own browser on owned fixtures. Status: pass. An ordinary document with quotes printed (exit 0, 16,699-byte PDF), and the encoded-quote onload caption plus a raw <svg/onload> sent 0 requests (main: 2). Aside not run.

Documentation

Status: current. No documentation changes were needed for this branch.

Scope: the committed diff a256d15..e1f0632 (make-pdf/src/render.ts, make-pdf/src/smartypants.ts, make-pdf/test/render.test.ts, changelog.d/make-pdf-quote-entities-stay-in-attributes.md). There were no staged, unstaged or new files. The change is an internal security fix: quote entities stay inside attributes and the sanitizer is stricter. No flag, command or output format changed.

Documentation health:

  • make-pdf/SKILL.md.tmpl and make-pdf/SKILL.md: current. Neither mentions sanitizer or quote handling.
  • docs/skills.md (/make-pdf section): current.
  • README.md (/make-pdf rows): current.
  • docs/howto-diagrams-and-formats.md: current.
  • changelog.d/make-pdf-quote-entities-stay-in-attributes.md: matches the diff (read only).

Coverage debt: no user doc describes how make-pdf sanitizes raw HTML. This audit leaves that gap as it is and does not fill it.
Diagram drift: none.

Agent notes

Found in GEVA-584's Opus 5.5 runs on main b5eb401 (control agent gstack-c); its blind verifier spotted the onerror bypass. The handoff patch kept entities in tags by splitting on <[^>]*>, which still decodes them wherever that split disagrees with the browser, so this PR moves the decode into smartypants' curly-only text pass instead. The opening-quote and SVG stroke-width fixes from the same handoff touch other functions and follow in a separate PR. gstack-issue-links read &#10; and &#39; in commit messages as issue references #10 and #39; they are not, so no issue links are listed. Lane GSTA-832 for GStack Minder.

Approvals

Approved: yes (record r-e53fc3f88688). The full approval record stays private.
Plan: exempt: bug fix (R65)
Plan check: exempt bug fix

REVIEW_LOOP: passes=3 blocking_found=4 deferred=1 noted=5 fixed=3 scope=frozen open=2 dispatches=12
Gate edits: none detected (gate scan tagged 0 hunks at e1f0632)
Ruled note: make-pdf/src/render.ts:228:outside-voice at e1f0632 ruled by GStack Minder (GSTA-36) (relayed from GSTA-36:2026-11-23T23:45:55Z) at 2026-11-23T23:45:55.468Z: over-match on contrived input; no script runs; 0 of 609 tracked Markdown files change; structure-preserving fix follows in the NUL hotfix PR (approval a-9de1cacca5e4)
Ruled note: make-pdf/src/render.ts:233:outside-voice at e1f0632 ruled by GStack Minder (GSTA-36) (relayed from GSTA-36:2026-11-23T23:45:55Z) at 2026-11-23T23:45:55.587Z: over-match on contrived input; no script runs; 0 of 609 tracked Markdown files change; structure-preserving fix follows in the NUL hotfix PR (approval a-2d25d95c29fa)

{
  "schema_version": 1,
  "pr": 4761,
  "head": "e1f0632429560c891cf81a81395bd5c9c583be6e",
  "version": "1.103.0.0",
  "base": "main",
  "gate": {
    "make-pdf": {
      "pass": 317,
      "fail": 0,
      "skip": 13
    }
  },
  "calls": 20,
  "session_kind": "unattended",
  "run": "/home/user/.capy/work/gsta832/ship",
  "head_tree": "d2ac976277cd8306ac8ec706fe2c37f02a8894ab",
  "product_patch_id": "371d51dc518a51c4d4b915cf8e76efd8daf249b3",
  "gstack_version": "1.103.0.0",
  "review_depth": {
    "depth": "light",
    "class": "code",
    "runtime_lines": 64,
    "rules": "33831f8248a814fd",
    "base": "aab606e6510c4f3f5c3490cc47f5017c9210fcde",
    "registry": "aab606e6510c4f3f5c3490cc47f5017c9210fcde",
    "gate_scan": {
      "tagged": 0,
      "head": "e1f0632429560c891cf81a81395bd5c9c583be6e"
    },
    "reasons": [
      "runtime=64"
    ],
    "risk": [],
    "mode": "panel"
  },
  "pregate": "pass @d2ac976277cd (8 checks, current for e1f063242956)",
  "issues": {
    "fixes": [],
    "refs": [],
    "flakes": [],
    "related": []
  },
  "docs": {
    "status": "current",
    "scope": "make-pdf/SKILL.md.tmpl, docs/, README.md (make-pdf sections)"
  },
  "review_loop": {
    "passes": 3,
    "blocking_found": 4,
    "deferred": 1,
    "noted": 5,
    "fixed": 3,
    "scope": "frozen",
    "rounds": 3,
    "dispatches": {
      "specialist": 9,
      "red-team": 3
    }
  },
  "ruled_notes": [
    {
      "finding": "make-pdf/src/render.ts:228:outside-voice",
      "head": "e1f0632429560c891cf81a81395bd5c9c583be6e",
      "by": "GStack Minder (GSTA-36)",
      "reason": "over-match on contrived input; no script runs; 0 of 609 tracked Markdown files change; structure-preserving fix follows in the NUL hotfix PR",
      "at": "2026-11-23T23:45:55.468Z",
      "approval": "a-9de1cacca5e4",
      "relayed": "GSTA-36:2026-11-23T23:45:55Z"
    },
    {
      "finding": "make-pdf/src/render.ts:233:outside-voice",
      "head": "e1f0632429560c891cf81a81395bd5c9c583be6e",
      "by": "GStack Minder (GSTA-36)",
      "reason": "over-match on contrived input; no script runs; 0 of 609 tracked Markdown files change; structure-preserving fix follows in the NUL hotfix PR",
      "at": "2026-11-23T23:45:55.587Z",
      "approval": "a-2d25d95c29fa",
      "relayed": "GSTA-36:2026-11-23T23:45:55Z"
    }
  ],
  "gate_dispositions": []
}
{"head":"38ff446c1b9723ae49b06d546fe85dc4aef47ac4","run":"ship","status":"started","review_loop":{"passes":1,"blocking_found":0,"deferred":0,"noted":1,"fixed":2,"scope":"frozen","rounds":1,"dispatches":{"specialist":3,"red-team":1}}}
{"head":"e1f0632429560c891cf81a81395bd5c9c583be6e","run":"ship","mark":"stop","tree":"d2ac976277cd8306ac8ec706fe2c37f02a8894ab","at":"2026-11-23T23:44:22.164Z"}

Open in Capy

…ript handlers

make-pdf decoded &quot; and &#39; after sanitizing, so an image alt, link title or raw HTML attribute could end early and grow an onerror, onload or javascript: href. Quote entities are now read only in text, where they become curly quotes, and the sanitizer also catches handlers and javascript: links that start after a / or a closing quote. Found by GEVA-584.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@trunk-io

trunk-io Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

😎 This pull request was merged.

garrytan and others added 2 commits November 23, 2026 23:26
Handler removal no longer eats a following tag, the stale comment names smartypants, and the changelog entry avoids a sample that the sanitizer strips.

Review-Finding: p1/specialist-batched#1 reader=security
Review-Finding: p1/red-team#2 reader=red-team
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Handler removal always drops the handler's name, even when its unquoted value starts with < (onerror=<!--&#10;code).

Review-Finding: p2/red-team#1 reader=red-team
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai
capy-ai Bot merged commit 40986b7 into main Nov 24, 2026
55 checks passed
@garrytan garrytan mentioned this pull request Nov 24, 2026
capy-ai Bot added a commit that referenced this pull request Nov 24, 2026
- `/ship` now tells you what to change when GitHub refuses a push for email privacy. (#4748)
- The release gate's /cso check accepts the low-confidence hypotheses /cso now reports. (#4753)
- /cso reports every security finding the code supports instead of dropping most of them. (#4739)
- /cso can search the whole repository in one call, and a daily report now lists its low-confidence hypotheses. (#4742)
- A first version's plan and try-it message now open with the owner's own words, and the build is tried the same way he will try it before he is asked. (#4759)
- A first version's public export now keeps out the owner's short quotes and his reasons to leave something out, and a phone first-use check installs a build the cloud phone can actually run. (#4772)
- Three intermittent test failures are fixed. (#4745)
- Free Tests runs its shards on 8-core runners again. (#4749)
- A land that waits on an earlier CI run now decides on what that run actually did. (#4763)
- Landing a PR no longer starts a new CI run when one for the same merged code is already running or has passed. (#4760)
- /ship no longer misses a file-mode change from main on files whose names start with `:`. (#4774)
- 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. (#4761)
- Posting a PR body that lists earlier /ship runs no longer stops for a phone number that isn't there. (#4747)
- A first version's public export now catches a short owner quote whose padding or quote marks are encoded. (#4778)
- A first version's public export now also keeps out a short owner quote written with spaces just inside its quote marks. (#4776)
- Posting a PR body that lists earlier /ship runs no longer stops for a card number that isn't there. (#4751)
- /ship no longer asks for an extra reader when you merge the base branch next to code that was already reviewed. (#4754)
- /ship no longer plans narrow re-checks after a fix. (#4741)
- /ship can't quietly gain new refusals. (#4740)
- `gstack-upgrade` no longer refuses to run after you disable skills. (#4743)
- The free test suite splits its shards from this week's recorded test times. (#4744)
- gstack now writes up a failed or shelved attempt before closing it. (#4735)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
capy-ai Bot added a commit that referenced this pull request Nov 24, 2026
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant