Skip to content

Allow inline SVG as a static-graphics subset - #11

Merged
afternoon merged 1 commit into
mainfrom
worktree-allow-svg
Sep 1, 2026
Merged

afternoon merged 1 commit into
mainfrom
worktree-allow-svg

Conversation

@afternoon

Copy link
Copy Markdown
Owner

Documents could not carry vector graphics at all, so icons — social profile
marks, logos — had to be raster images or CSS drawings. This allows inline
<svg> as a narrow static-graphics subset.

Why the blanket ban was broader than the threat

SVG is normally banned because it can carry <script>, event handlers, and —
through <foreignObject> — arbitrary HTML. That matters when sanitised markup
lands in your own origin's DOM.

It doesn't here:

  • Document HTML never reaches the app's DOM. The editor is a textarea; the
    preview is an <iframe> over a blob: PDF. There is no
    dangerouslySetInnerHTML anywhere in src/.
  • JavaScript is disabled in the render page (page.setJavaScriptEnabled(false)),
    so SVG script and event handlers are inert even on a sanitiser miss.
  • Each render gets a fresh incognito browser context with no credentials, so
    script that did run would have nothing to steal.

So the question isn't whether SVG can carry script — it's whether an
allowlist can name a subset that can't. It can.

The change

isAllowedElement now dispatches on namespaceURI and consults a separate
element and attribute set per namespace.

The namespace is load-bearing, not a formality. Several names exist in both
HTML and SVG — title, a, style, image — with different parsing rules and
different attributes. The previous HTML-namespace guard was documented as
defence against a shape parse5 doesn't actually produce; it now does real work,
keeping SVG's title from being treated as the HTML one.

Allowed: svg g defs symbol use title desc, the shapes, text/tspan,
gradients, clipPath/mask/pattern, and image.

Excluded, each for its own reason:

Excluded Why
script, animate/set carry event attributes
foreignObject the integration point where HTML parsing resumes mid-SVG — admitting it lets HTML re-enter through a subtree the HTML allowlist never sees
SVG a, style distinct elements from their HTML namesakes; no use in a document icon

Two details that would be easy to get wrong:

  • SVG attribute matching is case-sensitive, unlike HTML. parse5 applies the
    spec's case adjustment during tree construction, and viewBox is not
    viewbox.
  • xlink:href and href are one entry. parse5 reports xlink:href with
    name: "href" and an xlink prefix, so both spellings hit the same allowlist
    entry and the same URL check.

data:image/svg+xml stays rejected. Inline SVG is parsed into the tree and
checked element by element; an SVG inside a base64 URL is opaque to the
sanitiser, and admitting it would mean decoding and re-sanitising a nested
document. Authors write SVG inline, where it can be checked.

Testing

13 new tests, and the two existing tests that asserted SVG was dropped
wholesale now assert the same security intent against a root that survives —
the payloads must be removed on their own merits rather than by the root going.

Both directions were checked by mutation rather than trusted because they pass:

  • Adding foreignObject to the allowlist fails the foreign-content test —
    and the failure output shows an HTML <a> surviving inside the SVG subtree,
    confirming it is a genuine re-entry point.
  • Lowercasing the SVG attribute lookup fails three tests.

bun run check is clean. src/sanitize.test.ts and src/server/mcpServer.test.ts
pass (78 tests).

⚠️ One pre-existing, unrelated failure: survives a document nested far past anything legitimate times out at 5s under full-suite parallel load (it takes
~4.5s alone). I confirmed it fails identically on unmodified code via git stash,
so it is not from this change and I left it alone.

Docs

README, the MCP server instructions string, and the plugin's
writing-documents skill all stated the ban as a rule authors would trip over,
so all three are updated — including telling agents to paste icon SVGs inline
rather than as data URLs.

🤖 Generated with Claude Code

Documents could not carry vector graphics at all, which made icons — social
profile marks, logos — reach for raster images or CSS drawing. SVG was banned
wholesale because it is a document format that can carry script, event
handlers, and, through foreignObject, arbitrary HTML.

The ban was broader than the threat. Document HTML never reaches the app's own
DOM: the editor is a textarea and the preview is an iframe over a blob: PDF.
The render page runs with JavaScript disabled, in a fresh incognito browser
context with no credentials. So the question is not whether SVG can carry
script, but whether an allowlist can name a subset that cannot.

It can, provided it is keyed on namespace rather than tag name. Several names
exist in both HTML and SVG — title, a, style, image — with different parsing
rules and different attributes, so isAllowedElement now dispatches on
namespaceURI and consults a separate set per namespace. What was previously a
single HTML-namespace guard, documented as defence against a shape parse5 does
not actually produce, is now load-bearing: it is what keeps SVG's title from
being treated as the HTML one.

Excluded from the SVG set, each for its own reason: script and the animate/set
family carry event attributes; foreignObject is the integration point where
HTML parsing resumes mid-SVG, and admitting it would let HTML re-enter through
a subtree the HTML allowlist never sees; SVG's own a and style are distinct
elements from their HTML namesakes with no use in a document icon.

SVG attribute matching is case-sensitive, unlike HTML, because parse5 applies
the spec's case adjustment during tree construction and viewBox is not
viewbox. Both spellings of the reference attribute are covered by one entry:
parse5 reports xlink:href with name "href" and an xlink prefix.

data:image/svg+xml stays rejected. Inline SVG is parsed into the tree and
checked element by element; an SVG inside a base64 URL is opaque to the
sanitiser, and admitting it would mean decoding and re-sanitising a nested
document.

The two existing tests that asserted SVG was dropped wholesale now assert the
same security intent against a root that survives — the payloads must be
removed on their own merits rather than by the root going. Both were checked
by mutation: allowing foreignObject fails the foreign-content test (and shows
an HTML <a> surviving inside the SVG subtree), and lowercasing the SVG
attribute lookup fails three.

README, the MCP server instructions and the plugin's writing-documents skill
all stated the ban as a rule authors would trip over, so all three are updated.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
htmlcsspdf bf78219 Commit Preview URL

Branch Preview URL
Sep 01 2026, 09:15 PM

@afternoon
afternoon merged commit 0856797 into main Sep 1, 2026
1 of 2 checks passed
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