Skip to content

Commit 6b08b7f

Browse files
committed
fix(client): centralize markdown image source handling
1 parent ab5bb96 commit 6b08b7f

6 files changed

Lines changed: 234 additions & 79 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 32 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Haptics from "expo-haptics";
22
import { KeyboardAwareLegendList } from "@legendapp/list/keyboard";
33
import { type LegendListRef } from "@legendapp/list/react-native";
44
import type { EnvironmentId, MessageId, ThreadId, TurnId } from "@t3tools/contracts";
5+
import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images";
56
import { CHAT_LIST_ANCHOR_OFFSET, resolveChatListAnchoredEndSpace } from "@t3tools/shared/chatList";
67
import { formatElapsed } from "@t3tools/shared/orchestrationTiming";
78
import { SymbolView } from "../../components/AppSymbol";
@@ -103,7 +104,7 @@ import {
103104
} from "./thread-work-log";
104105
import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";
105106
import { useAssetUrl, useAssetUrlState } from "../../state/assets";
106-
import { resolveWorkspaceFilePath, resolveWorkspaceRelativeFilePath } from "../files/filePath";
107+
import { resolveWorkspaceRelativeFilePath } from "../files/filePath";
107108

108109
const WIDE_MARKDOWN_BLOCK_OPTIONS = {
109110
includeOrderedLists: Platform.OS === "android",
@@ -194,58 +195,6 @@ function MessageAttachmentImage(props: {
194195
);
195196
}
196197

197-
const REMOTE_IMAGE_SRC_PATTERN = /^https?:\/\//i;
198-
const NON_FILE_SCHEME_PATTERN = /^[A-Za-z][A-Za-z0-9+.-]*:/;
199-
200-
/**
201-
* Absolute filesystem path for a markdown image src, or null when the src is
202-
* not a workspace file (remote URL, other scheme, or a relative path with no
203-
* workspace root to resolve against).
204-
*/
205-
function markdownImageWorkspacePath(href: string, workspaceRoot: string | null): string | null {
206-
const isFileUri = /^file:\/\//i.test(href);
207-
let path = isFileUri ? href.slice("file://".length) : href;
208-
try {
209-
path = decodeURIComponent(path);
210-
} catch {
211-
// Keep the raw path when it is not valid percent-encoding.
212-
}
213-
if (path.length === 0) {
214-
return null;
215-
}
216-
if (isFileUri) {
217-
// file://localhost/... is the historical spelling of file:///... (WHATWG
218-
// parsers normalize the localhost authority away), not a UNC host.
219-
if (/^localhost\//i.test(path)) {
220-
path = path.slice("localhost".length);
221-
}
222-
if (!path.startsWith("/")) {
223-
// file://host/share/... — the authority is a UNC host on the
224-
// environment machine, not a path segment under the workspace root.
225-
return `\\\\${path.replaceAll("/", "\\")}`;
226-
}
227-
// URL parsers encode Windows drive paths as /C:/... — drop the slash.
228-
if (/^\/[A-Za-z]:[\\/]/.test(path)) {
229-
path = path.slice(1);
230-
}
231-
} else if (path.startsWith("//")) {
232-
// Protocol-relative URL (//cdn.example.com/x.png), not a filesystem path.
233-
return null;
234-
}
235-
const isAbsolute =
236-
path.startsWith("/") || /^[A-Za-z]:[\\/]/.test(path) || path.startsWith("\\\\");
237-
if (!isAbsolute && NON_FILE_SCHEME_PATTERN.test(path)) {
238-
return null;
239-
}
240-
if (isAbsolute) {
241-
return path;
242-
}
243-
if (workspaceRoot === null) {
244-
return null;
245-
}
246-
return resolveWorkspaceFilePath(workspaceRoot, path);
247-
}
248-
249198
/** Markdown image whose src is a workspace file — loads through a signed asset URL. */
250199
function ThreadMarkdownImage(props: {
251200
readonly environmentId: EnvironmentId;
@@ -313,6 +262,31 @@ function ThreadMarkdownImage(props: {
313262
);
314263
}
315264

265+
function ThreadMarkdownImageUnavailable(props: { readonly alt: string | null }) {
266+
const codeBackground = useThemeColor("--color-md-code-bg");
267+
return (
268+
<View style={{ gap: 6 }}>
269+
<View
270+
style={{
271+
width: "100%",
272+
aspectRatio: 16 / 9,
273+
borderRadius: 10,
274+
backgroundColor: codeBackground,
275+
alignItems: "center",
276+
justifyContent: "center",
277+
}}
278+
>
279+
<Text className="text-xs text-foreground-muted">Image unavailable</Text>
280+
</View>
281+
{props.alt ? (
282+
<Text selectable className="text-xs text-foreground-muted">
283+
{props.alt}
284+
</Text>
285+
) : null}
286+
</View>
287+
);
288+
}
289+
316290
const MARKDOWN_MONO_FONT = Platform.select({
317291
ios: "ui-monospace",
318292
android: "monospace",
@@ -1553,19 +1527,18 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
15531527
);
15541528
const renderMarkdownImage = useCallback<MarkdownImageRenderer>(
15551529
(image) => {
1556-
// Remote URIs load fine in a plain RN Image — keep the module fallback.
1557-
if (REMOTE_IMAGE_SRC_PATTERN.test(image.href)) {
1530+
const imageSource = classifyMarkdownImageSource(image.href, props.workspaceRoot ?? null);
1531+
if (imageSource._tag === "Direct") {
15581532
return null;
15591533
}
1560-
const path = markdownImageWorkspacePath(image.href, props.workspaceRoot ?? null);
1561-
if (path === null) {
1562-
return null;
1534+
if (imageSource._tag === "Blocked") {
1535+
return <ThreadMarkdownImageUnavailable alt={image.alt} />;
15631536
}
15641537
return (
15651538
<ThreadMarkdownImage
15661539
environmentId={props.environmentId}
15671540
threadId={props.threadId}
1568-
path={path}
1541+
path={imageSource.path}
15691542
alt={image.alt}
15701543
onPressImage={(uri) => setExpandedImage({ uri })}
15711544
/>

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
squashAtomCommandFailure,
2020
type AtomCommandResult,
2121
} from "@t3tools/client-runtime/state/runtime";
22+
import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images";
2223
import * as Cause from "effect/Cause";
2324
import { AsyncResult } from "effect/unstable/reactivity";
2425
import React, {
@@ -988,9 +989,6 @@ const MarkdownLinkFavicon = memo(function MarkdownLinkFavicon({ host }: { host:
988989
const CHAT_MARKDOWN_IMAGE_CLASS_NAME =
989990
"my-1 block! max-h-96 max-w-full rounded-lg border border-border/40 object-contain";
990991

991-
/** A remote src the browser can fetch on its own — no workspace resolution needed. */
992-
const DIRECTLY_LOADABLE_IMAGE_SRC_PATTERN = /^(?:https?:|data:|blob:|\/\/)/i;
993-
994992
function ChatMarkdownImageFallback(props: { readonly alt: string }) {
995993
return (
996994
<span className="my-1 inline-flex items-center gap-1.5 rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground">
@@ -1816,25 +1814,20 @@ function ChatMarkdown({
18161814
img({ node: _node, title: _title, src, alt, ...props }) {
18171815
const srcString = typeof src === "string" ? normalizeMarkdownLinkDestination(src) : "";
18181816
const altText = alt ?? "";
1819-
if (srcString.length === 0) {
1820-
return <ChatMarkdownImageFallback alt={altText} />;
1817+
const imageSource = classifyMarkdownImageSource(srcString, cwd);
1818+
if (imageSource._tag === "Direct") {
1819+
return <img {...props} src={imageSource.uri} alt={altText} loading="lazy" />;
18211820
}
1822-
if (!DIRECTLY_LOADABLE_IMAGE_SRC_PATTERN.test(srcString)) {
1823-
// Agents reference screenshots they saved into the workspace by path
1824-
// (relative, absolute, or file://); those bytes only exist on the
1825-
// environment host, so they load through a signed asset URL.
1826-
const fileMeta = resolveMarkdownFileLinkMeta(srcString, cwd);
1827-
if (fileMeta && threadRef) {
1828-
return (
1829-
<ChatMarkdownWorkspaceImage
1830-
threadRef={threadRef}
1831-
path={fileMeta.filePath}
1832-
alt={altText}
1833-
/>
1834-
);
1835-
}
1821+
if (imageSource._tag === "WorkspaceFile" && threadRef) {
1822+
return (
1823+
<ChatMarkdownWorkspaceImage
1824+
threadRef={threadRef}
1825+
path={imageSource.path}
1826+
alt={altText}
1827+
/>
1828+
);
18361829
}
1837-
return <img {...props} src={srcString} alt={altText} loading="lazy" />;
1830+
return <ChatMarkdownImageFallback alt={altText} />;
18381831
},
18391832
table({ node: _node, ...props }) {
18401833
return <MarkdownTable {...props} />;

‎apps/web/src/components/ChatMarkdown.workspace-images.test.tsx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ function render(markdown: string): string {
4242
);
4343
}
4444

45+
function renderWithoutThread(markdown: string): string {
46+
return renderToStaticMarkup(<ChatMarkdown cwd={"C:\\Users\\shawn\\project"} text={markdown} />);
47+
}
48+
4549
describe("ChatMarkdown workspace images", () => {
4650
beforeEach(() => {
4751
testState.resources = [];
@@ -98,4 +102,30 @@ describe("ChatMarkdown workspace images", () => {
98102
expect(html).toContain('aria-label="Loading image"');
99103
expect(html).not.toContain("animate-pulse");
100104
});
105+
106+
it("never passes a workspace source to a raw image when thread context is unavailable", () => {
107+
const html = renderWithoutThread(
108+
"![file URL](file:///C:/Users/shawn/project/workspace-image.svg)",
109+
);
110+
111+
expect(testState.resources).toEqual([]);
112+
expect(html).toContain("Image unavailable");
113+
expect(html).not.toContain("file://");
114+
});
115+
116+
it("blocks unsupported image schemes instead of passing them to a raw image", () => {
117+
const html = render("![unsupported](content://media/image/1)");
118+
119+
expect(testState.resources).toEqual([]);
120+
expect(html).toContain("Image unavailable");
121+
expect(html).not.toContain("content://");
122+
});
123+
124+
it("keeps remote images directly loadable", () => {
125+
const html = render("![remote](https://example.com/image.png)");
126+
127+
expect(testState.resources).toEqual([]);
128+
expect(html).toContain('src="https://example.com/image.png"');
129+
expect(html).not.toContain("Image unavailable");
130+
});
101131
});

‎packages/client-runtime/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"types": "./src/environment/index.ts",
1616
"default": "./src/environment/index.ts"
1717
},
18+
"./markdown-images": {
19+
"types": "./src/markdownImages.ts",
20+
"default": "./src/markdownImages.ts"
21+
},
1822
"./errors": {
1923
"types": "./src/errors/index.ts",
2024
"default": "./src/errors/index.ts"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { classifyMarkdownImageSource } from "./markdownImages.js";
4+
5+
describe("classifyMarkdownImageSource", () => {
6+
it.each([
7+
"https://example.com/image.png",
8+
"HTTP://example.com/image.png",
9+
"data:image/png;base64,AAAA",
10+
"blob:https://app.t3.codes/image-id",
11+
"//cdn.example.com/image.png",
12+
])("keeps %s directly loadable", (uri) => {
13+
expect(classifyMarkdownImageSource(uri, "/workspace/project")).toEqual({
14+
_tag: "Direct",
15+
uri,
16+
});
17+
});
18+
19+
it.each([
20+
["images/result.png", "/workspace/project", "/workspace/project/images/result.png"],
21+
["./images/result.png", "/workspace/project", "/workspace/project/./images/result.png"],
22+
[
23+
"images/result.png",
24+
"C:\\Users\\dara\\project",
25+
"C:\\Users\\dara\\project\\images\\result.png",
26+
],
27+
[
28+
"images\\result.png",
29+
"C:\\Users\\dara\\project",
30+
"C:\\Users\\dara\\project\\images\\result.png",
31+
],
32+
["/workspace/project/image.png", null, "/workspace/project/image.png"],
33+
["C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"],
34+
["\\\\server\\share\\image.png", null, "\\\\server\\share\\image.png"],
35+
["file:///workspace/project/image%20one.png", null, "/workspace/project/image one.png"],
36+
["file:///C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"],
37+
["file://localhost/C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"],
38+
["file://server/share/image.png", null, "\\\\server\\share\\image.png"],
39+
])("maps %s to a workspace file", (source, workspaceRoot, path) => {
40+
expect(classifyMarkdownImageSource(source, workspaceRoot)).toEqual({
41+
_tag: "WorkspaceFile",
42+
path,
43+
});
44+
});
45+
46+
it.each([
47+
null,
48+
"",
49+
"#image",
50+
"?image=1",
51+
"image.png",
52+
"~/image.png",
53+
"javascript:alert(1)",
54+
"ftp://example.com/image.png",
55+
"content://media/image/1",
56+
"custom:image.png",
57+
"file://%",
58+
])("blocks unsupported or unresolved source %s", (source) => {
59+
expect(classifyMarkdownImageSource(source)).toEqual({ _tag: "Blocked" });
60+
});
61+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const DIRECT_IMAGE_SOURCE_PATTERN = /^(?:https?:|data:|blob:|\/\/)/i;
2+
const URI_SCHEME_PATTERN = /^[A-Za-z][A-Za-z0-9+.-]*:/;
3+
const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
4+
5+
export type MarkdownImageSource =
6+
| { readonly _tag: "Direct"; readonly uri: string }
7+
| { readonly _tag: "WorkspaceFile"; readonly path: string }
8+
| { readonly _tag: "Blocked" };
9+
10+
function safeDecode(value: string): string {
11+
try {
12+
return decodeURIComponent(value);
13+
} catch {
14+
return value;
15+
}
16+
}
17+
18+
function normalizeSource(value: string): string {
19+
const trimmed = value.trim();
20+
return trimmed.startsWith("<") && trimmed.endsWith(">") ? trimmed.slice(1, -1) : trimmed;
21+
}
22+
23+
function parseFileUrl(value: string): string | null {
24+
try {
25+
const parsed = new URL(value);
26+
if (parsed.protocol.toLowerCase() !== "file:") return null;
27+
28+
if (parsed.hostname.length > 0 && parsed.hostname.toLowerCase() !== "localhost") {
29+
const pathname = safeDecode(parsed.pathname).replaceAll("/", "\\");
30+
return `\\\\${safeDecode(parsed.hostname)}${pathname}`;
31+
}
32+
33+
const pathname = safeDecode(parsed.pathname);
34+
if (pathname.length === 0) return null;
35+
return /^\/[A-Za-z]:[\\/]/.test(pathname) ? pathname.slice(1) : pathname;
36+
} catch {
37+
return null;
38+
}
39+
}
40+
41+
function stripSearchAndHash(value: string): string {
42+
const searchIndex = value.indexOf("?");
43+
const hashIndex = value.indexOf("#");
44+
const end = [searchIndex, hashIndex]
45+
.filter((index) => index >= 0)
46+
.reduce((lowest, index) => Math.min(lowest, index), value.length);
47+
return value.slice(0, end);
48+
}
49+
50+
function joinWorkspacePath(workspaceRoot: string, relativePath: string): string {
51+
const windows =
52+
WINDOWS_DRIVE_PATH_PATTERN.test(workspaceRoot) || workspaceRoot.startsWith("\\\\");
53+
const separator = windows ? "\\" : "/";
54+
const root = workspaceRoot.replace(/[\\/]+$/, "");
55+
const path = relativePath.replace(/[\\/]/g, separator).replace(/^[\\/]+/, "");
56+
return `${root}${separator}${path}`;
57+
}
58+
59+
/**
60+
* Classifies a markdown image source by where its bytes must be loaded from.
61+
* Filesystem paths belong to the environment host and must never reach a
62+
* browser or native image component without first becoming a signed asset URL.
63+
*/
64+
export function classifyMarkdownImageSource(
65+
value: string | null | undefined,
66+
workspaceRoot?: string | null,
67+
): MarkdownImageSource {
68+
if (value === null || value === undefined) return { _tag: "Blocked" };
69+
70+
const source = normalizeSource(value);
71+
if (source.length === 0 || source.startsWith("#") || source.startsWith("?")) {
72+
return { _tag: "Blocked" };
73+
}
74+
if (DIRECT_IMAGE_SOURCE_PATTERN.test(source)) {
75+
return { _tag: "Direct", uri: source };
76+
}
77+
78+
if (/^file:/i.test(source)) {
79+
const path = parseFileUrl(source);
80+
return path === null ? { _tag: "Blocked" } : { _tag: "WorkspaceFile", path };
81+
}
82+
83+
const path = safeDecode(stripSearchAndHash(source));
84+
if (path.length === 0) return { _tag: "Blocked" };
85+
if (path.startsWith("/") || WINDOWS_DRIVE_PATH_PATTERN.test(path) || path.startsWith("\\\\")) {
86+
return { _tag: "WorkspaceFile", path };
87+
}
88+
if (URI_SCHEME_PATTERN.test(path) || path.startsWith("~/") || path.startsWith("~\\")) {
89+
return { _tag: "Blocked" };
90+
}
91+
if (!workspaceRoot) return { _tag: "Blocked" };
92+
93+
return { _tag: "WorkspaceFile", path: joinWorkspacePath(workspaceRoot, path) };
94+
}

0 commit comments

Comments
 (0)