Repository navigation
feat(chat): render on-disk sandbox images in chat markdown - #10281
Closed
CelesteHeartsong wants to merge 2 commits into
Closed
CelesteHeartsong wants to merge 2 commits into
CelesteHeartsong wants to merge 2 commits into
Conversation
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 `` 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
requested review from
Imagineer99,
danielhanchen and
oobabooga
as code owners
September 4, 2026 03:58
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
A model writes
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 theAuthorizationheader (_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:
sandboxFileForSrcrewrites only scheme-less paths (sanitize dropped every scheme-carrying src already) and discards the sid segment the model happened to write;markdownSandboxImageSrcrebuilds it viasandboxSessionIdFor—project-${projectId}for project threads, else this thread'sremoteId.useSandboxImage, doesauthFetch→createObjectURLfor markdown and the Python card, which had its own copy; abort +revokeObjectURLon cleanup, keyed by url so a stale response can never write state for an old file.imgreplaces 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.downloadNamefollows 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"downloadsplot.png).Why the scheme-less src survives sanitize (fact, read off the installed packages)
hast-util-sanitize'ssafeProtocolreturns true when the first:precedes no/-precedes-no-colon case — i.e. whencolon < 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_EXTSmirrors_SANDBOX_MEDIA_TYPES(frontend test readsinference.pyas text and pins it):.avifjoins it (a raster codec;nosniffpins the type either way),.svgstays 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
completecheck,onLoad/onErrorforwarding, 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 -bandtsc -p tsconfig.test.json: 0 errors. Suite: 6830 pass / 0 fail.markdown-sandbox-image.test.ts);%23round-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
imgswap point. If that lands, the wholesale restatement shrinks back to just the resolve-and-fetch step.