Skip to content

fix(editor): render formatted Mermaid labels safely - #25554

Merged
nwparker merged 3 commits into
mainfrom
nwparker/editor-mermaid-html-labels
Nov 1, 2026
Merged

nwparker merged 3 commits into
mainfrom
nwparker/editor-mermaid-html-labels

Conversation

@nwparker

@nwparker nwparker commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​141 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​134
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​42 $\color{#cf222e}{\Huge{\mathbf{−}}}$​18 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​24

ELI5

Bold and italic text in Mermaid diagram labels now renders as formatting instead of showing the HTML tags.

What Changed

Enable HTML labels in Markdown preview, rich editing, standalone diagrams, and comments. Keep strict rendering and the final sanitization pass, with explicit SVG, HTML, and MathML profiles plus the required XHTML integration point. SVG filter primitives remain excluded, and form controls cannot turn diagram labels into editable or submittable forms.

Why

The former SVG-only cleanup removed the HTML container used for labels. Enabling labels alone would therefore leave them blank. The shared sanitizer preserves formatted and math labels while removing unsafe markup.

Linked Issue

Fixes #12414.

Visual Proof

Surface Before After
Rich editor Literal HTML tags in diagram labels Formatted labels in the compiled rich editor
Standalone diagram Literal HTML tags in standalone diagram labels Formatted labels in the compiled standalone diagram
HTML form label Strict label with editable input and submit button Label text with form controls removed

Rebased onto main (Oct 31)

  • Main added the diagram expand viewer (feat(markdown): expand Mermaid diagrams into the pan and zoom viewer everywhere #36575) and Copy as PNG (feat(editor): SVG preview + source, and a drag-to-pan image viewer #36576). Rebuilt this PR on top of both. All four call sites drop htmlLabels={false}, and MermaidBlock sanitizes with sanitizeMermaidSvg before main's dark-mode label recolour, which already covers HTML labels (.nodeLabel).
  • Fix found while verifying: Chromium taints a canvas drawn from a blob-URL SVG that contains foreignObject labels, so Copy as PNG would have failed on every flowchart. The diagram viewer now loads the diagram from a data URL instead. Running the real renderImagePngDataUrl in Chromium on an HTML-label diagram: a blob URL throws Tainted canvases may not be exported; a data URL copies correctly.
  • Verified in Chromium (light and dark): inline labels keep <b>/<i>, and the expanded <img> and the copied PNG both show the labels.
  • pnpm tc, plus the MermaidBlock, DiagramViewerModal, mermaid-config and Chromium sanitizer suites, all pass.

Testing

  • Manually tested through hidden Electron CDP on macOS: rich editor, Markdown preview, standalone .mmd, ordinary labels, line breaks, and math labels. Fresh compiled app proof matches the source-backed checks.
  • 14 tests across 4 suites pass with no skips, including real Chromium namespace handling and browser reparsing. Scripts, event handlers, encoded script URLs, nested iframe/object/embed content, and MathML namespace transitions and form controls are covered; SVG and HTML images remain allowed. Generated styles remain; SVG filter primitives are removed.
  • Full typechecks, lint, changed-code quality, whitespace check, and electron-vite build --mode e2e pass. The preserved contributor series and focused follow-ups now rest on reviewed support commit e6a471c8df80e1bc3e789e4a4e1a7ac7d9fed52b. This test-only ancestor refresh leaves all Mermaid source and tests byte-identical, with equal commit patches and 22 combined Mermaid checks passing again.

Review

The production diff received independent source review. This branch includes the reviewed #25540 support ancestor for the lint and test-recorder baseline. No dependency, rendering queue, cancellation, or CSP change is included.

Agent skill upstream boundary

  • Not applicable; no upstream skill-installer material is copied.

Notes

Preserves the two authored commits from Bing.Z (@bbingz) in #12581, including the original Cursor coauthor and cherry-pick references. The follow-ups retain explicit profiles, extend the security controls, and exclude form-control tags after an actual strict-label input/submit reproduction. Both ordinary and math labels and legitimate images remain visible. The original diagnosis is credited to @Fartown.

Cross-checked the exact Mermaid 11.17.2 final sanitization source and DOMPurify 3.4.16 namespace checks. Strict rendering remains enabled; no CSP policy changes are included. The proof does not rely on CSP blocking forms. The sanitizer admits more HTML to support labels, and label sizing or line wrapping can change.

This PR alone retains the existing same-instance invalid-to-valid recovery bug (#18370): saving, closing, and reopening renders the corrected diagram. It is handled in the separate #25575 recovery PR stacked on this change. Native Windows/Linux, live SSH, and the actual comment sidebar were not exercised; this renderer change adds no host or path behavior.

Checklist

  • Small, focused production change: 7 files, 41 additions and 26 deletions.
  • User-visible change, mechanism, and approach explained.
  • Before/after visual proof attached.
  • Self-reviewed for correctness, security, and performance.
  • Cross-platform and SSH impact considered.
  • Required local checks pass.

@coderabbitai

coderabbitai Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3d28dbfe-4b83-49e5-83cc-c1268d225ea6
Commits

Reviewing files that changed from the base of the PR and between c668988 and 36cbb72.

Files selected for processing: 1
  • src/renderer/src/components/editor/mermaid-sanitize.chromium.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

Walkthrough

Mermaid configuration now enables HTML labels by default. MermaidBlock sanitizes rendered SVG with a shared DOMPurify configuration that supports HTML labels. Chromium tests check safe-content preservation and unsafe-content removal. Mermaid call sites and related tests no longer pass the removed htmlLabels option. DiagramViewerModal now uses an encoded SVG data URL instead of a blob URL.

Severity of issue fixed: Medium

Merge Risk: Moderate · up to 36cbb

Crafted Mermaid content may contact attacker-controlled image URLs when opened, exposing network metadata or triggering unintended requests. Resolve this before merging.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly identifies the main change: safely rendering formatted Mermaid labels in the editor.
Description check Passed The description covers the required sections, including the user-facing change, rationale, linked issue, visual proof, testing, review notes, scope considerations, and checklist. It also documents unt…
Linked Issues check Passed Issue #12414 requires formatted Mermaid labels in Markdown preview while retaining strict security. getMermaidConfig sets htmlLabels: true and securityLevel: 'strict'. The Mermaid preview call s…
Out of Scope Changes check Passed The changes remain connected to issue #12414. The shared sanitizer preserves labels after HTML rendering and tests its security boundaries. The data URL in DiagramViewerModal supports PNG copying fo…
Docstring Coverage Passed Docstring coverage is inconclusive: files from earlier reviews still need to be rechecked within the check's analysis limits. Not blocking.
Full details: Description check

Explanation

The description covers the required sections, including the user-facing change, rationale, linked issue, visual proof, testing, review notes, scope considerations, and checklist. It also documents untested platforms and the known recovery limitation.

Full details: Linked Issues check

Explanation

Issue #12414 requires formatted Mermaid labels in Markdown preview while retaining strict security. getMermaidConfig sets htmlLabels: true and securityLevel: 'strict'. The Mermaid preview call sites no longer override HTML labels to false. MermaidBlock applies sanitizeMermaidSvg, which preserves foreignObject label content and supports HTML and MathML profiles. The reported regression tests cover formatted labels and unsafe markup removal.

Full details: Out of Scope Changes check

Explanation

The changes remain connected to issue #12414. The shared sanitizer preserves labels after HTML rendering and tests its security boundaries. The data URL in DiagramViewerModal supports PNG copying for diagrams that contain foreignObject labels. Related Mermaid surfaces receive the same label behavior. No unrelated change is established.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6530a593-dd2d-4be0-a099-d86e25da47e3
📥 Commits

Reviewing files that changed from the base of the PR and between e2da3a1 and 24b9d80505c56a5f4acc620a46e9cf28e9cc17ee.

📒 Files selected for processing (26)
  • mobile/src/terminal/document/cell-geometry.ts
  • mobile/src/terminal/document/fit-scale.ts
  • mobile/src/terminal/document/mouse-report-cell.ts
  • mobile/src/terminal/document/native-document-bundle.test.ts
  • mobile/src/terminal/document/selection-cell-range.ts
  • mobile/src/terminal/document/selection-overlay.ts
  • mobile/src/terminal/document/selection-range.ts
  • mobile/src/terminal/document/viewport-cell.ts
  • mobile/src/terminal/document/viewport-transform.ts
  • mobile/src/transport/client-context.tsx
  • mobile/src/transport/client-context.web.tsx
  • mobile/src/transport/force-reconnect-page-seam.test.tsx
  • mobile/src/transport/host-client-hooks.ts
  • mobile/src/transport/rpc-client-react-context.ts
  • mobile/src/transport/rpc-client-react-context.web.ts
  • src/renderer/src/components/editor/MermaidBlock.tsx
  • src/renderer/src/components/editor/MermaidViewer.tsx
  • src/renderer/src/components/editor/RichMarkdownCodeBlock.tsx
  • src/renderer/src/components/editor/mermaid-config.test.ts
  • src/renderer/src/components/editor/mermaid-config.ts
  • src/renderer/src/components/editor/mermaid-sanitize.chromium.test.ts
  • src/renderer/src/components/editor/mermaid-sanitize.test.ts
  • src/renderer/src/components/editor/mermaid-sanitize.ts
  • src/renderer/src/components/editor/use-markdown-preview-components.tsx
  • src/renderer/src/components/sidebar/CommentMermaidBlock.tsx
  • src/renderer/src/lib/worktree-creation-hook-preparation.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@@ -2,10 +2,11 @@ import type mermaid from 'mermaid'

export function getMermaidConfig(
isDark: boolean,
htmlLabels = false
htmlLabels = true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Security & Privacy | 🛡️ Detected with Advanced Tier | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

git diff --no-ext-diff --unified=50 c2c76498494e6147b63dcda99f77f87aab63156e 24b9d80505c56a5f4acc620a46e9cf28e9cc17ee -- src/renderer/src/components/editor/mermaid-config.ts src/renderer/src/components/editor/MermaidBlock.tsx
printf '\\n--- scoped policy/control search ---\\n'
rg -n -i --glob '!**/node_modules/**' --glob '!**/dist/**' 'Content-Security-Policy|default-src|img-src|webRequest|onBeforeRequest|session\\.webRequest|sanitizeMermaidSvg|mermaid\\.render|htmlLabels' src electron .

Repository: stablyai/orca

Length of output: 43227


🏁 Script executed:

printf '%s\\n' '--- renderer and main entry candidates ---'
rg --files src/renderer src/main | rg '(^|/)(index\\.html|main\\.(ts|tsx)|.*window.*|.*csp.*|.*protocol.*|MermaidBlock\\.tsx|mermaid-sanitize\\.ts)$'
printf '%s\\n' '--- relevant application policy hooks ---'
rg -n 'new BrowserWindow|onHeadersReceived|Content-Security-Policy|webRequest\\.onBeforeRequest|webRequest\\.onHeadersReceived|setWindowOpenHandler|session:' src/main src/renderer --glob '!**/*.test.*'
printf '%s\\n' '--- sanitizer implementation ---'
sed -n '1,180p' src/renderer/src/components/editor/mermaid-sanitize.ts
printf '%s\\n' '--- renderer HTML ---'
if test -f src/renderer/index.html; then sed -n '1,180p' src/renderer/index.html; fi

Repository: stablyai/orca

Length of output: 41561


🏁 Script executed:

printf '%s\\n' '--- build/config files mentioning CSP or injection ---'
rg --files -g '*vite*' -g '*electron*' -g 'package.json' -g '*config*' -g '!**/node_modules/**' | rg '(vite|electron|config|package\\.json)$' | head -100
rg -n -i 'electron-vite injects|content-security-policy|contentSecurityPolicy|img-src|csp|security.?policy' electron.vite.config.* vite.config.* package.json src --glob '!**/*.test.*' --glob '!**/node_modules/**' || test "$?" -eq 1
printf '%s\\n' '--- main window construction ---'
rg -n -F 'new BrowserWindow' src/main/window/createMainWindow.ts
sed -n '1,240p' src/main/window/createMainWindow.ts

Repository: stablyai/orca

Length of output: 22029


🏁 Script executed:

printf '%s\n' '--- build/config files mentioning CSP or injection ---'
rg --files -g '*vite*' -g '*electron*' -g 'package.json' -g '*config*' -g '!**/node_modules/**' | rg '(vite|electron|config|package\.json)$' | head -100
rg -n -i 'electron-vite injects|content-security-policy|contentSecurityPolicy|img-src|csp|security.?policy' electron.vite.config.* vite.config.* package.json src --glob '!**/*.test.*' --glob '!**/node_modules/**' || test "$?" -eq 1
printf '%s\n' '--- main window construction ---'
rg -n -F 'new BrowserWindow' src/main/window/createMainWindow.ts
sed -n '1,240p' src/main/window/createMainWindow.ts

Repository: stablyai/orca

Length of output: 22558


🏁 Script executed:

printf '%s\n' '--- root build configuration candidates ---'
rg --files --hidden -g 'electron.vite.config.*' -g 'vite.config.*' -g 'package.json' -g '!**/node_modules/**' -g '!**/.git/**' | sed -n '1,100p'
printf '%s\n' '--- top-level vite and CSP configuration references ---'
rg -n -i 'content-security-policy|csp|electron-vite|img-src|security.?policy' --glob '*.{ts,js,mjs,cjs,json,jsonc,html}' --glob '!**/node_modules/**' --glob '!**/*.test.*' . | rg 'electron\.vite|vite\.config|package\.json|renderer/index\.html|src/main/window/createMainWindow' | sed -n '1,160p'
printf '%s\n' '--- package scripts and CSP-related dependencies ---'
rg -n -i 'electron-vite|content-security|csp|vite-plugin' package.json

Repository: stablyai/orca

Length of output: 4868


🏁 Script executed:

printf '%s\n' '--- electron.vite.config.ts ---'
sed -n '1,420p' electron.vite.config.ts
printf '%s\n' '--- build wrapper ---'
sed -n '1,240p' config/scripts/run-electron-vite-build.mjs

Repository: stablyai/orca

Length of output: 16613


Information Disclosure

Reachability: External
Exploitability: Moderate
CWE: CWE-200 — Exposure of Sensitive Information to an Unauthorized Actor

Reachability path
● Entry
  src/renderer/src/components/editor/mermaid-config.test.ts:6
  getMermaidConfig
│
▼
● Sink
  src/renderer/src/components/editor/mermaid-config.ts

Block remote resource loads from HTML labels before rendering. MermaidBlock calls mermaid.render before sanitizeMermaidSvg, so the later sanitizer cannot prevent requests made during rendering. The renderer has no visible img-src restriction or request filter. Do not enable htmlLabels for untrusted diagrams unless remote label resources are blocked before mermaid.render.

View in Security blast radius

@nwparker
nwparker force-pushed the nwparker/editor-mermaid-html-labels branch from 24b9d80 to 5d953e2 Compare October 5, 2026 12:21

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/renderer/src/components/editor/mermaid-sanitize.chromium.test.ts (1)

65-65: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Run the sanitizer assertions in blocking Chromium CI.

The unit workflow discovers this file, but probeChromium() returns false when Chromium cannot launch, so describe.skipIf skips every assertion. The required orcad_browser job runs a different test. Run this suite in a blocking job with Playwright Chromium available, and make that job fail if the probe returns false.


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a752ae6a-50c0-4965-8fbb-094c729872a4
📥 Commits

Reviewing files that changed from the base of the PR and between 24b9d80505c56a5f4acc620a46e9cf28e9cc17ee and 91a2f2c.

📒 Files selected for processing (13)
  • mobile/src/test-support/rpc-recording/README.md
  • mobile/src/test-support/rpc-recording/adapter-seam.test.ts
  • mobile/src/test-support/rpc-recording/adapters/agent-history-mount-adapters.ts
  • mobile/src/test-support/rpc-recording/adapters/agent-history-screen-mount-adapters.ts
  • mobile/src/test-support/rpc-recording/adapters/file-explorer-screen-mount-adapters.ts
  • mobile/src/test-support/rpc-recording/adapters/mounted-operation-modules.ts
  • mobile/src/test-support/rpc-recording/adapters/notification-test-screen-mount-adapters.ts
  • mobile/src/test-support/rpc-recording/adapters/source-control-screen-read-mount-adapters.ts
  • mobile/src/test-support/rpc-recording/adapters/tasks-route-screen-mount-adapters.ts
  • mobile/src/test-support/rpc-recording/host-client-context-exposure.ts
  • mobile/web-entry/web-overrides.json
  • src/renderer/src/components/editor/mermaid-sanitize.chromium.test.ts
  • src/renderer/src/components/editor/mermaid-sanitize.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

@nwparker
nwparker force-pushed the nwparker/editor-mermaid-html-labels branch from 91a2f2c to 2ef2b34 Compare October 5, 2026 13:01
@nwparker
nwparker changed the base branch from main to nwparker/editor-mobile-import-cycle-lint October 5, 2026 13:11
@nwparker
nwparker force-pushed the nwparker/editor-mermaid-html-labels branch from 2ef2b34 to 3b9e2d4 Compare October 31, 2026 23:16
@nwparker
nwparker force-pushed the nwparker/editor-mermaid-html-labels branch from 3b9e2d4 to 746d1d8 Compare October 31, 2026 23:29
@nwparker
nwparker changed the base branch from nwparker/editor-mobile-import-cycle-lint to main October 31, 2026 23:30

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Nitpick comments: 1
src/renderer/src/components/editor/DiagramViewerModal.test.tsx: 1

74-74: Trivial · Functional Correctness · Quick win

Assert the encoded SVG payload.

The current assertion checks only the data:image/svg+xml prefix. It would pass if the URL contained no SVG content. Decode the payload and assert that it contains the test SVG content, such as <text>A</text>. (github.com)


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 45deb141-0699-4260-95ea-98474189b4fe
Commits

Reviewing files that changed from the base of the PR and between 91a2f2c and 746d1d8.

Files selected for processing: 10
  • src/renderer/src/components/editor/DiagramViewerModal.test.tsx
  • src/renderer/src/components/editor/DiagramViewerModal.tsx
  • src/renderer/src/components/editor/MermaidBlock.test.tsx
  • src/renderer/src/components/editor/MermaidBlock.tsx
  • src/renderer/src/components/editor/MermaidViewer.tsx
  • src/renderer/src/components/editor/RichMarkdownCodeBlock.tsx
  • src/renderer/src/components/editor/mermaid-config.test.ts
  • src/renderer/src/components/editor/mermaid-config.ts
  • src/renderer/src/components/editor/use-markdown-preview-components.tsx
  • src/renderer/src/components/sidebar/CommentMermaidBlock.tsx
Files with no reviewable changes: 2
  • src/renderer/src/components/editor/use-markdown-preview-components.tsx
  • src/renderer/src/components/editor/MermaidViewer.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Comment on lines +49 to +50
// Strict mode sanitizes label HTML; sanitizeMermaidSvg keeps the foreignObject labels it emits.
htmlLabels: true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

set -eu
printf '%s\n' '--- diff ---'
git diff --find-renames 91c920061384c48145d1be26c5e53e151f7b0759 746d1d8b0da5696415f49b5611d461b2418a65dd -- src/renderer/src/components/editor/mermaid-config.ts
printf '%s\n' '--- current config ---'
nl -ba src/renderer/src/components/editor/mermaid-config.ts
printf '%s\n' '--- config callers and related symbols ---'
rg -n -F --glob '*.{ts,tsx,js,jsx}' -e 'getMermaidConfig' -e 'htmlLabels' -e 'contrast' -e 'labelBackground' -e 'foreignObject' src/ || test "$?" -eq 1
printf '%s\n' '--- Mermaid package declarations and lock entries ---'
rg -n -F -e '"mermaid"' -e 'mermaid@' package.json pnpm-lock.yaml pnpm-workspace.yaml 2>/dev/null || true
printf '%s\n' '--- relevant tests ---'
rg -n -F --glob '*.{test,spec}.{ts,tsx,js,jsx}' -e 'mermaid-config' -e 'getMermaidConfig' -e 'htmlLabels' -e 'foreignObject' src/ tests/ 2>/dev/null || true

Repository: stablyai/orca

Length of output: 35949


Script executed:

set -eu
printf '%s\n' '--- MermaidBlock ---'
nl -ba src/renderer/src/components/editor/MermaidBlock.tsx
printf '%s\n' '--- mermaid-flowchart-contrast ---'
nl -ba src/shared/mermaid-flowchart-contrast.ts
printf '%s\n' '--- contrast tests ---'
nl -ba src/shared/mermaid-flowchart-contrast.test.ts
printf '%s\n' '--- mermaid-config tests ---'
nl -ba src/renderer/src/components/editor/mermaid-config.test.ts
printf '%s\n' '--- sanitize flow and renderer callers ---'
rg -n -F --glob '*.{ts,tsx}' -e 'sanitizeMermaidSvg' -e 'readableMermaidFlowchartSvg' -e 'renderMermaid' -e 'mermaid.render' src/ shared/

Repository: stablyai/orca

Length of output: 25580


Script executed:

set -eu
printf '%s\n' '--- PR diff for helper and Mermaid callers ---'
git diff --find-renames 91c920061384c48145d1be26c5e53e151f7b0759 746d1d8b0da5696415f49b5611d461b2418a65dd -- src/shared/mermaid-flowchart-contrast.ts src/shared/mermaid-flowchart-contrast.test.ts src/renderer/src/components/editor/MermaidBlock.tsx src/renderer/src/components/editor/mermaid-sanitize.ts src/renderer/src/components/sidebar/comment-mermaid-fence.tsx
printf '%s\n' '--- sanitizer ---'
nl -ba src/renderer/src/components/editor/mermaid-sanitize.ts
printf '%s\n' '--- comment Mermaid entrypoint ---'
nl -ba src/renderer/src/components/sidebar/comment-mermaid-fence.tsx
printf '%s\n' '--- Mermaid package locations ---'
find . -path '*/mermaid/package.json' -o -path '*/mermaid/dist/*' -o -path '*/mermaid/src/*' 2>/dev/null | head -80
printf '%s\n' '--- package manager stores mentioning Mermaid 11.17.2 ---'
find . -maxdepth 5 -type f \\( -name 'package.json' -o -name '*mermaid*' \\) 2>/dev/null | head -120

Repository: stablyai/orca

Length of output: 13566


Script executed:

set -eu
MERMAID=node_modules/.pnpm/[email protected]/node_modules/mermaid
printf '%s\n' '--- Mermaid htmlLabels and foreignObject references ---'
rg -n -F --glob '*.js' --glob '*.css' --glob '*.d.ts' -e 'htmlLabels' -e 'foreignObject' -e 'nodeLabel' -e 'label-container' "$MERMAID/dist" | head -240
printf '%s\n' '--- Mermaid package metadata ---'
nl -ba "$MERMAID/package.json" | sed -n '1,90p'
printf '%s\n' '--- Mermaid flowchart renderer files ---'
find "$MERMAID/dist" -type f | rg '/flowchart|flowchart' | head -80
printf '%s\n' '--- local helper diff status ---'
git diff --stat 91c920061384c48145d1be26c5e53e151f7b0759 746d1d8b0da5696415f49b5611d461b2418a65dd -- src/shared/mermaid-flowchart-contrast.ts src/shared/mermaid-flowchart-contrast.test.ts

Repository: stablyai/orca

Length of output: 45665


Script executed:

set -eu
FILE=node_modules/.pnpm/[email protected]/node_modules/mermaid/dist/mermaid.js
printf '%s\n' '--- createText and HTML label styling ---'
nl -ba "$FILE" | sed -n '37320,37620p'
printf '%s\n' '--- flowchart useHtmlLabels and node rendering ---'
nl -ba "$FILE" | sed -n '37770,37840p'
nl -ba "$FILE" | sed -n '44040,44130p'
printf '%s\n' '--- relevant CSS blocks ---'
nl -ba "$FILE" | sed -n '160680,160750p'
nl -ba "$FILE" | sed -n '163000,163115p'
printf '%s\n' '--- effective htmlLabels config ---'
nl -ba "$FILE" | sed -n '6860,6920p'

Repository: stablyai/orca

Length of output: 29908


Script executed:

set -eu
FILE=node_modules/.pnpm/[email protected]/node_modules/mermaid/dist/mermaid.js
printf '%s\n' '--- mark/highlight and label background references ---'
rg -n -F --glob 'mermaid.js' -e '<mark' -e 'mark>' -e 'background-color' -e 'background:' -e 'markdownToHTML' -e 'sanitizeText' "$FILE" | head -180
printf '%s\n' '--- CSS around flowchart label and node styles ---'
nl -ba "$FILE" | sed -n '160710,160790p'
nl -ba "$FILE" | sed -n '192560,192640p'
printf '%s\n' '--- markdown conversion implementation candidates ---'
rg -n -F --glob 'mermaid.js' -e 'function markdownToHTML' -e 'markdownToHTML =' -e 'highlight' -e 'strong' "$FILE" | head -120

Repository: stablyai/orca

Length of output: 23604


Script executed:

set -eu
FILE=node_modules/.pnpm/[email protected]/node_modules/mermaid/dist/mermaid.js
printf '%s\n' '--- Markdown HTML conversion ---'
nl -ba "$FILE" | sed -n '36940,37045p'
printf '%s\n' '--- Mermaid sanitization ---'
nl -ba "$FILE" | sed -n '23095,23145p'
printf '%s\n' '--- Mermaid icon/image syntax references ---'
rg -n -F --glob 'mermaid.js' -e 'img:' -e 'icon:' -e 'node2.img' -e 'node2.icon' "$FILE" | head -100
printf '%s\n' '--- repository Mermaid styles and entrypoints ---'
rg -n -F --glob '*.{css,scss,ts,tsx}' -e 'mermaid-block' -e 'MermaidBlock' -e 'CommentMermaidBlock' -e 'MermaidViewer' src/ | head -180
printf '%s\n' '--- MermaidBlock callers ---'
rg -n -F --glob '*.{ts,tsx}' -e '<MermaidBlock' -e 'MermaidBlock(' -e '<CommentMermaidBlock' src/renderer/src/

Repository: stablyai/orca

Length of output: 22576


Script executed:

set -eu
printf '%s\n' '--- exact comment Mermaid caller ---'
nl -ba src/renderer/src/components/sidebar/CommentMermaidBlock.tsx
printf '%s\n' '--- exact Mermaid preview caller ---'
nl -ba src/renderer/src/components/editor/use-markdown-preview-components.tsx | sed -n '160,185p'
printf '%s\n' '--- repository final sanitizer ---'
nl -ba src/renderer/src/components/editor/mermaid-sanitize.ts
printf '%s\n' '--- DOMPurify style attribute allowlist ---'
rg -n -F --glob '*.mjs' --glob '*.js' -e 'style' node_modules/.pnpm/dompurify@*/node_modules/dompurify/dist | head -80

Repository: stablyai/orca

Length of output: 43867


Account for opaque ancestor backgrounds in Mermaid HTML labels.

With htmlLabels: true, nested formatted elements can be rendered inside foreignObject labels. The contrast helper compares a nested element with the node fill, even when an ancestor such as <mark> or Mermaid’s labelBkg paints the actual background. On a dark node, this can select the light foreground and produce low-contrast text on a light highlight. Walk the HTML ancestor chain before applying the node-fill correction.

Suggested fix
 function contrast(a: number, b: number): number {
   return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05)
 }
 
+function hasOpaqueHtmlBackground(label: Element, stop: Element): boolean {
+  for (
+    let current: Element | null = label;
+    current && current !== stop;
+    current = current.parentElement
+  ) {
+    if (luminance(getComputedStyle(current).backgroundColor) !== null) {
+      return true
+    }
+  }
+  return false
+}
+
 ...
-        if (!svgText && luminance(labelStyle.backgroundColor) !== null) {
+        if (!svgText && hasOpaqueHtmlBackground(label, node)) {
           continue
         }

Enable Mermaid HTML labels everywhere the desktop renders diagrams and
sanitize with foreignObject XHTML kept, form controls and SVG filters removed.
Chromium taints a canvas drawn from a blob-URL SVG that holds foreignObject labels; the viewer now loads the diagram from a data URL.
@nwparker
nwparker force-pushed the nwparker/editor-mermaid-html-labels branch from 746d1d8 to c668988 Compare October 31, 2026 23:45
@nwparker
nwparker merged commit a75b2c7 into main Nov 1, 2026
42 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.

[Bug]: Mermaid node labels show HTML tags literally — htmlLabels can be enabled safely while keeping securityLevel "strict"

1 participant