Skip to content

ui-react: Mermaid SVG serialized as XML is re-parsed as HTML on insertion #2594

Open

Description

@schlessera

Context

The Mermaid pack's screen path works like this:

  1. renderMermaidSvg (packages/ui-react/src/lib/mermaid.ts:87-118) has Mermaid render with securityLevel: "strict" (DOMPurify on labels; same file:28-39).
  2. tokenizedMermaidSvg parses the markup with DOMParser as text/html (parsedSvg, packages/ui-react/src/lib/mermaid-svg.ts:8-15), rewrites paints, and serializes the tree with XMLSerializer (same file:33-108).
  3. 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)

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:

    1. Parse it with DOMParser as image/svg+xml, the same format XMLSerializer wrote.
    2. Reject a parsererror or a root that is not one SVG svg element.
    3. Reject active content: script, iframe, object, embed, link, base, any on* attribute, and a javascript: URL.
    4. importNode the root and attach it with ref.current.replaceChildren(node) in a layout effect.

    A rejected SVG shows the source, as a failed parse does today.

  • 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 dangerouslySetInnerHTML users outside the Mermaid pack.
  • The diagrams module's renderer, which already builds elements from data.
  • Changing Mermaid's securityLevel or 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 dangerouslySetInnerHTML remains in mermaid-block.tsx or mermaid-viewer.tsx.
  • A unit test feeds the inserter SVG strings containing <script>, an onload attribute, a javascript: 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 reach innerHTML.
  • Real-browser proof: the existing Mermaid renderer layout test still passes unchanged in light, dark and print. Add an assertion that the inserted svg is in the SVG namespace and that the zoom viewer shows the same diagram.
  • contentRendererConformance still 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 lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: 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

  1. added
    type: refactor
    area: ui-react
    priority: p3
    agent-ready
    security
    on Oct 22, 2026
  2. schlessera commented on Oct 23, 2026

    @schlessera
    OwnerAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-reactpriority: p3securitytype: refactor

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions