Repository navigation
Description
Context
The Mermaid pack's screen path works like this:
renderMermaidSvg(packages/ui-react/src/lib/mermaid.ts:87-118) has Mermaid render withsecurityLevel: "strict"(DOMPurify on labels; same file:28-39).tokenizedMermaidSvgparses the markup withDOMParserastext/html(parsedSvg,packages/ui-react/src/lib/mermaid-svg.ts:8-15), rewrites paints, and serializes the tree withXMLSerializer(same file:33-108).- The block hands that XML string to React's
dangerouslySetInnerHTML, which re-parses it with the HTML parser. This happens three times:- the screen SVG (
packages/ui-react/src/components/chat/mermaid-block.tsx:83-89) - the print SVG (same file:95-98)
- the zoom viewer (
packages/ui-react/src/components/chat/mermaid-viewer.tsx:31-40)
- the screen SVG (
Parsing in one mode, serializing and re-parsing in another is the shape mutation XSS (mXSS) exploits. Diagram sources come from the model and from repository files. No exploit is known; this hardening closes the class.
Guide security/sanitize-untrusted-html: "Move the resulting nodes into the page rather than serializing them to a string and re-parsing it with innerHTML, which can reintroduce mutation XSS (mXSS)." Guide security/trusted-types: innerHTML is a sink that Trusted Types would refuse a raw string for. Removing these three sinks is a step towards that.
How this relates to #2079. #2079 delivered the content-renderer seam that Mermaid now renders through (MermaidContent, packages/ui-react/src/content-renderers/mermaid-content.tsx:5-10). Its constraints include "No renderer receives raw content markup to inject; renderers build elements from data". This task brings the Mermaid pack's on-page insertion in line with that constraint. It changes no seam type, manifest or conformance rule, and neither it nor #2079's open work waits on the other.
Scope
-
Add one internal component that inserts a cached Mermaid SVG string as nodes:
- Parse it with
DOMParserasimage/svg+xml, the same formatXMLSerializerwrote. - Reject a
parsererroror a root that is not one SVGsvgelement. - Reject active content:
script,iframe,object,embed,link,base, anyon*attribute, and ajavascript:URL. importNodethe root and attach it withref.current.replaceChildren(node)in a layout effect.
A rejected SVG shows the source, as a failed parse does today.
- Parse it with
-
Use it for the screen SVG and the print SVG in
MermaidBlock, and for the zoom viewer. -
Keep the string cache and every export and share path (
mermaid-share.ts,content-renderers/mermaid.tsx,staticMermaidSvg) as they are. Those strings leave the page.
Out of scope
- Enabling Trusted Types or changing any CSP. Those are host-owned.
- Other
dangerouslySetInnerHTMLusers outside the Mermaid pack. - The diagrams module's renderer, which already builds elements from data.
- Changing Mermaid's
securityLevelor the token rewrite. - Deferring the print render (ui-react: Mermaid renders throwaway print SVGs while a fence streams #2593). That edits the same file, so land the two one after the other.
Acceptance criteria
- No
dangerouslySetInnerHTMLremains inmermaid-block.tsxormermaid-viewer.tsx. - A unit test feeds the inserter SVG strings containing
<script>, anonloadattribute, ajavascript:href, and markup that is not SVG. Each is rejected and shows the source. The test fails on the tree before the change, where the strings reachinnerHTML. - Real-browser proof: the existing Mermaid renderer layout test still passes unchanged in light, dark and print. Add an assertion that the inserted
svgis in the SVG namespace and that the zoom viewer shows the same diagram. -
contentRendererConformancestill passes for the Mermaid pack.
Verification
bun run test packages/ui-react/tests
node scripts/visual.mjs --project=ui-react-layout --filter=mermaid-content-renderer
node scripts/visual.mjs --project=ui-react-layout --filter=content-renderers
bun run typecheck
bun run lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT:commit prefix, and the doc updated in the same commit. - This changes a published package's behaviour, so it needs a changeset.
Rendering is unchanged for valid diagrams, and an SVG carrying active content now falls back to the source. Patch changeset for @schlessera/brain-ui-react.
Activity
schlessera commentedon Oct 23, 2026
2026-10-24 restructuring: moved from #2587 to #2553 as a direct security-hardening task beside #2607. Its outcome is closing an XML/HTML reparse class and removing injection sinks, not responsiveness. It still references #2079 and #2593.