Repository navigation
Allow inline SVG as a static-graphics subset - #11
Merged
Merged
Conversation
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>
Deploying with
|
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 markuplands in your own origin's DOM.
It doesn't here:
preview is an
<iframe>over ablob:PDF. There is nodangerouslySetInnerHTMLanywhere insrc/.page.setJavaScriptEnabled(false)),so SVG script and event handlers are inert even on a sanitiser miss.
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
isAllowedElementnow dispatches onnamespaceURIand consults a separateelement 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 anddifferent 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
titlefrom being treated as the HTML one.Allowed:
svg g defs symbol use title desc, the shapes,text/tspan,gradients,
clipPath/mask/pattern, andimage.Excluded, each for its own reason:
script,animate/setforeignObjecta,styleTwo details that would be easy to get wrong:
spec's case adjustment during tree construction, and
viewBoxis notviewbox.xlink:hrefandhrefare one entry. parse5 reportsxlink:hrefwithname: "href"and an xlink prefix, so both spellings hit the same allowlistentry and the same URL check.
data:image/svg+xmlstays rejected. Inline SVG is parsed into the tree andchecked 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:
foreignObjectto 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.
bun run checkis clean.src/sanitize.test.tsandsrc/server/mcpServer.test.tspass (78 tests).
survives a document nested far past anything legitimatetimes 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
instructionsstring, and the plugin'swriting-documentsskill 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