Repository navigation
Studio mermaid guard: a quoted @{ cannot hide a later remote image - #14907
Conversation
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.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| if (source[open] === '"') inString = !inString; | ||
| if (inString || source[open] !== "@" || source[open + 1] !== "{") continue; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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".
| if (close < 0) { | ||
| from = open + 2; | ||
| continue; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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]; |
There was a problem hiding this comment.
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 👍 / 👎.
| } | ||
| if (close < 0) return blocks; | ||
| blocks.push({ start: open, end: close + 1, body: source.slice(open + 2, close) }); | ||
| from = close + 1; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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]
|
@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.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Problem
stripRemoteDiagramImages(#14427) drops a remoteimgfrom 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: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:@{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);#64;{, mermaid's entity for@, which shows the same text and opens nothing;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.<image>elementsshowshow @{ img: @{ img: x }