Skip to content

feat(chat): render on-disk sandbox images in chat markdown - #10281

Closed
CelesteHeartsong wants to merge 2 commits into
unslothai:mainfrom
CelesteHeartsong:feat/sandbox-markdown-images
Closed

CelesteHeartsong wants to merge 2 commits into
unslothai:mainfrom
CelesteHeartsong:feat/sandbox-markdown-images

Conversation

@CelesteHeartsong

Copy link
Copy Markdown
Contributor

TL;DR

A model writes ![plot](/api/inference/sandbox/<sid>/plot.png) into its answer. The src carries no scheme, so sanitize keeps it — but keeping it was never the same as being able to fetch it: the route answers on the Authorization header (_authenticate_header_or_query), a bare <img src> 401s, and the answer renders "Image not available". Only tool cards had an authed fetch; markdown was the one path left.

Fix

The src is re-derived from this chat's scope and fetched with auth into an object URL before it reaches the DOM:

  • sandboxFileForSrc rewrites only scheme-less paths (sanitize dropped every scheme-carrying src already) and discards the sid segment the model happened to write; markdownSandboxImageSrc rebuilds it via sandboxSessionIdFor — project-${projectId} for project threads, else this thread's remoteId.
  • One hook, useSandboxImage, does authFetch → createObjectURL for markdown and the Python card, which had its own copy; abort + revokeObjectURL on cleanup, keyed by url so a stale response can never write state for an old file.
  • Registering img replaces Streamdown's default renderer WHOLESALE (its Components map is keyed by tag), so everything that renderer did is restated: wrapper, hidden-when-failed, fallback text, hover download. downloadName follows it exactly: a real extension on the path wins whole; otherwise any extension in alt is stripped and one inferred from the blob's type is appended (alt="plot" downloads plot.png).

Why the scheme-less src survives sanitize (fact, read off the installed packages)

hast-util-sanitize's safeProtocol returns true when the first : precedes no /-precedes-no-colon case — i.e. when colon < 0 || colon > slash, a scheme-less path is never protocol-checked. The bug was never sanitize; it was the missing auth header.

The two extension lists agree, in two languages

SANDBOX_INLINE_IMAGE_EXTS mirrors _SANDBOX_MEDIA_TYPES (frontend test reads inference.py as text and pins it): .avif joins it (a raster codec; nosniff pins the type either way), .svg stays OUT on purpose — the filename is model-chosen, so inline SVG would be same-origin script execution; non-image extensions stay download cards.

What was deliberately NOT ported

The replaced renderer's gating machinery (decoded state, mount-time complete check, onLoad/onError forwarding, loaded||sized gates): a sandbox image is hidden until the authed fetch lands and fetch state already drives visibility, so nothing here needs a decode gate. What WAS kept — string-level lines with no machinery, pinned by name in tests: data-streamdown="image", the hover overlay div, the download-name regex.

Verification / Tests

  • tsc -b and tsc -p tsconfig.test.json: 0 errors. Suite: 6830 pass / 0 fail.
  • Pinned by tests: rewrite-before-DOM and authed-fetch/revoke (markdown-sandbox-image.test.ts); %23 round-trips while a raw # is a fragment delimiter → null; project scope beats threadId (search-images.test.ts).

Follow-up (not blocking)

Upstream, this is one line: Streamdown shipping its own img swap point. If that lands, the wholesale restatement shrinks back to just the resolve-and-fetch step.

Streamdown sanitizes before hardening, and its default sanitize schema lists only http(s)
under protocols.src. A data:image/... src is therefore stripped before the harden stage
(which itself sets allowDataImages: true), so assistant messages with base64 images rendered
as "[Image blocked: ...]". Pass an explicit rehypePlugins pipeline derived from Streamdown's
exported defaults, extended with 'data' on image source protocols; derive it rather than
hand-roll one so the default-pipeline merge (tagNames/attributes for allowedTags) is carried
unchanged. Tests included.
A model that writes `![plot](/api/inference/sandbox/<sid>/plot.png)` got "[Image blocked]"
before, and "Image not available" after unslothai#10269: the src carries no scheme, so sanitize keeps it
and safeMarkdownUrl returns it unchanged -- but the route answers on the Authorization header
(_authenticate_header_or_query), so a bare <img src> hits it unauthenticated and 401s. Only
tool cards did the authed fetch (tool-ui-python.tsx), so markdown was the one path left.

Resolve the src through sandboxFilePath(sandboxSessionIdFor(threadId, projectId)) and fetch it
into an object URL before it reaches the DOM: the session segment the model happened to write is
discarded and rebuilt from this chat's scope, so a project thread reads the project's workspace.
One hook, useSandboxImage, now does that for markdown AND for the Python card, which had its own
copy; abort + revokeObjectURL on cleanup in both.

Registering `img` replaces Streamdown's default renderer wholesale (its Components map is keyed by
tag), so everything that renderer did for a data: image is restated: wrapper, hidden-when-failed,
fallback text, hover download. The two features are load-bearing on each other rather than merely
compatible: unslothai#10269's early return for `data:`/`blob:` is exactly why the rewritten src survives the
way back out, and it returns those unchanged, so scheme-carrying sources never reach this path.

.avif joins _SANDBOX_MEDIA_TYPES (a raster codec, and nosniff pins the type either way); .svg stays
out on purpose -- the filename is model-chosen, so inline SVG would be same-origin script execution,
and a non-image extension stays the download card the file list already renders. Tests included.
@CelesteHeartsong

Copy link
Copy Markdown
Contributor Author

Closed in favor of the stacked review: CelesteHeartsong#1 targets fix/markdown-data-images (the still-open #10269) as its base, so its diff is exactly this feature (8 files, +512/-73) rather than both commits. Will be rebased onto main and retargeted upstream once #10269 lands.

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