Skip to content

Studio mermaid guard: a quoted @{ cannot hide a later remote image - #14907

Merged
danielhanchen merged 6 commits into
mainfrom
fix-mermaid-quoted-shape-data
Oct 27, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
fix-mermaid-quoted-shape-data

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Problem

stripRemoteDiagramImages (#14427) drops a remote img from mermaid shape data blocks (id@{ img: "..." }) before mermaid renders a diagram from chat markdown, since mermaid fetches the image while it measures the node. The block scan took the first @{ anywhere in the source as an opener, including one inside a quoted label. The label's closing " then flipped the scan into string mode, the real block's } was never found, and the scan returned no blocks at all, so the diagram reached mermaid unchanged:

flowchart LR
  A["@{"] --> B@{ img: "https://attacker.example/label-confusion", w: 10, h: 10 }

came back byte-identical on main, while the same block without the label is stripped. Model output (including prompt-injected text from a fetched page) controls the diagram, and the Studio page CSP allows https: images, so this reopens the image beacon channel #14427 closed.

Change

studio/frontend/src/components/assistant-ui/mermaid-guard.ts: whether a given @{ is shape data or text (a quoted label, accessibility text, a sequence message) depends on mermaid's lexer state, which the guard cannot reproduce, so it no longer picks one reading:

  • every @{ is read on its own, as a block up to its first } outside a string (precomputed in one pass, so any number of markers costs linear time);
  • each reading that names a remote image gets the image taken out, as before;
  • when two such readings overlap, both openers become #64;{, mermaid's entity for @, which shows the same text and opens nothing;
  • the result is vetted again until it is stable.

Diagrams without a remote image are unchanged, including an unterminated @{ in a sequence message.

Tests

studio/frontend/tests/mermaid-remote-images.test.ts: a @{ in a quoted label; a @{ and a } in later labels; a } quoted inside the real block's label; accessibility text with a lone quote; each strips the remote image and is stable on a second pass. An unterminated @{ in a sequence message passes through unchanged, two overlapping readings become inert, and 16,000 unclosed markers scan in under a second. Main fails 2 of these; this branch passes all 49 in the file.

Full frontend suite (node --test "tests/**/*.test.ts", TAP): the same 569 failing test IDs on main and on this branch (they need the full frontend install), no new failures.

Before / after

The same streamed reply (two mermaid diagrams) rendered by a Studio built from the merge base and one built from this branch. https://attacker.example/** was answered with a red image so a fetch is visible.

Before: the quoted @{ hides B's shape data, so the remote image loads (red box) and C's label is cut to "show". After: no remote fetch, B and D keep their labels, C keeps its full text.

Before After
Requests to the remote host 1 0
Remote <image> elements 1 0
C node label show show @{ img: @{ img: x }

A @{ inside a quoted label started a shape data block whose closing
quote flipped the string state, so the real block's } was never found
and every later block, remote img included, reached mermaid unvetted.
Openers inside strings are now skipped, and an unterminated block runs
to the end and is still vetted.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T13:59:14.794097Z 4c94af5 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

2 similar comments
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 566e1335cf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +65 to +66
if (source[open] === '"') inString = !inString;
if (inString || source[open] !== "@" || source[open + 1] !== "{") continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Prevent accessibility text quotes from hiding image blocks

Mermaid does not treat every " as a diagram-wide string delimiter: for example, accTitle: A " title is valid because the flowchart lexer consumes the remainder of that line as accessibility text. With that line before B@{ img: "https://attacker.example/x", w: 1, h: 1 }, this persistent inString flag remains true and skips the real shape block, so stripRemoteDiagramImages returns the remote URL unchanged and restores the image-beacon path this guard is meant to close. Quote tracking needs to respect lexer context or reset for constructs where quotes are plain text.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in eab462a: the diagram-wide quote state is gone. Every @{ gets its own close scan, and one that never closes is skipped as text with the scan resuming right after it, so accessibility text with a lone quote no longer hides a later block. Added the accTitle case as a test.

if (source[open] === '"') inString = !inString;
if (inString || source[open] !== "@" || source[open + 1] !== "{") continue;
let quoted = false;
let close = source.length;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Do not truncate valid messages at an unterminated marker

An unterminated @{ is not necessarily a malformed shape block in other Mermaid grammars. For example, sequenceDiagram\n Alice->>Bob: show @{ img: foo is a valid sequence diagram whose message is rendered literally, but defaulting close to EOF makes this guard parse the remainder as shape data and return only sequenceDiagram\n Alice->>Bob: show . This regresses the stated pass-through behavior for diagrams without remote images; the EOF fallback should only apply where @{ is actually recognized as shape-data syntax.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in eab462a: an unterminated @{ no longer runs to the end of the source; it is treated as text and the diagram passes through unchanged. Added the sequenceDiagram message case as a test.

Tracking quotes across the whole diagram misread accessibility text,
and taking an unterminated @{ to the end truncated valid sequence
messages. Each @{ now gets its own close scan; one that never closes is
text, and the scan resumes right after it, so a fake opener in a label
can no longer swallow the real block.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eab462a469

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +77 to +79
if (close < 0) {
from = open + 2;
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid rescanning every unmatched opener to EOF

When model-generated diagram text contains many @{ tokens but no closing }—for example, literal text in sequence messages—the inner loop scans the entire remaining suffix for each token, then this branch advances by only two characters and repeats. This makes stripRemoteDiagramImages quadratic: a roughly 48 KB input containing 16,000 markers takes about two seconds in this function alone, enough to stall the browser's main thread during markdown rendering; the recovery should continue searching without rescanning every previously examined suffix.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 15fd213: the first } outside a string is now precomputed from the end of the source for both quote states, so each @{ looks up its close in constant time and any number of unclosed markers costs one pass. Added a test with 16,000 unclosed markers in a sequence message (passes through unchanged, under a second); the previous head fails it.

Unclosed @{ markers each rescanned the rest of the source. The first
} outside a string is now precomputed from the end for both quote
states, so any number of markers costs one pass.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 15fd21345c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

close = index;
break;
}
const close = closeFrom[open + 2];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep unrelated @{ tokens from sharing a closer

When a sequence message contains an unclosed literal @{ and a later message contains closed text such as @{ img: foo }, this lookup pairs the first marker with the later }. The resulting multi-line body fails YAML parsing, matches the img fallback, and truncates both messages, even though Mermaid 11.17.2 accepts the original diagram and treats both markers as message text. Unlike the now-covered single-marker case, the later brace makes the first marker appear closed, so candidates need to be scoped to lexer-recognized shape data rather than paired across unrelated text.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 7415a1b: when two readings that name a remote image overlap, both openers become #64;{ (mermaid's entity for @), so the messages keep their text and nothing is truncated or opened. Added that sequenceDiagram case as a test.

}
if (close < 0) return blocks;
blocks.push({ start: open, end: close + 1, body: source.slice(open + 2, close) });
from = close + 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Do not let a label brace skip the real image block

Fresh evidence beyond the fixed accessibility-title case is flowchart LR\n A["@{"] --> B@{ label: "}", img: "https://attacker.example/x" }: Mermaid treats the first marker as label text and the quoted } as part of B's shape data, but this scanner treats that brace as the false marker's close. withoutRemoteImage then sees no img before that point and makes no replacement, while advancing here skips over B's real @{ opener, so the remote URL is returned byte-identically and can still be fetched.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 7415a1b: every @{ is now read on its own instead of the scan jumping past a closed reading, so B's opener is vetted even when the label's @{ closes on the quoted }. Your example is a test; the remote img is removed and B keeps its label.

… inert

A label's @{ could close on a } quoted inside the real block, so the
scan skipped the real opener. Each @{ is now read independently; any
reading that names a remote image is rewritten, and when two such
readings overlap both openers become mermaid's #64;{ entity, which
shows the same text and opens nothing. The result is re-vetted until
stable.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

2 similar comments
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

…able

Many @{ sharing one close made every block body a suffix of the source,
so each was parsed to the end and a 160 KB message took seconds. A body
holding neither img nor a backslash cannot name an image and is now
skipped before parsing (checked in O(1) from a precomputed table), and
the text parsed per diagram is capped; past the cap every @{ is made
inert, as when the passes run out.

Overlapping readings now make only the openers in their own run inert.
A block that overlaps none is rewritten in place as before, so a node
next to an ambiguous label no longer turns into B#64;{ ... }, which
mermaid rejects.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 4c94af5ecc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 27, 2026
@danielhanchen
danielhanchen merged commit ca59a0d into main Oct 27, 2026
37 of 40 checks passed
@danielhanchen
danielhanchen deleted the fix-mermaid-quoted-shape-data branch October 27, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant