Skip to content

Commit c0fa311

Browse files
fix(files): sandbox inline HTML assets and anchor links to the rendered file
Co-Authored-By: Claude Code <[email protected]>
1 parent c1d8c40 commit c0fa311

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

‎apps/server/src/http.test.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,13 @@ describe("assetResponseHeaders", () => {
324324
"X-Content-Type-Options": "nosniff",
325325
});
326326
});
327-
it("declares utf-8 for HTML assets so non-ASCII content renders correctly", () => {
328-
expect(assetResponseHeaders("/workspace/page.html")).toHaveProperty(
329-
"Content-Type",
330-
"text/html; charset=utf-8",
331-
);
332-
expect(assetResponseHeaders("/workspace/PAGE.HTM")).toHaveProperty(
333-
"Content-Type",
334-
"text/html; charset=utf-8",
335-
);
327+
it("serves HTML assets as utf-8 inside a sandboxed origin", () => {
328+
for (const path of ["/workspace/page.html", "/workspace/PAGE.HTM", "/tmp/report.html"]) {
329+
expect(assetResponseHeaders(path)).toMatchObject({
330+
"Content-Type": "text/html; charset=utf-8",
331+
"Content-Security-Policy": "sandbox allow-scripts allow-forms allow-popups allow-modals",
332+
});
333+
}
336334
});
337335

338336
it("downloads uploaded documents without executing their content", () => {

‎apps/server/src/http.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces";
5151
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
5252
const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"];
5353
const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
54+
// HTML previews are agent output, not the app. The sandbox gives the document an
55+
// opaque origin: scripts run, but same-origin cookies, storage, and API calls are
56+
// out of reach. Relative sibling assets still load through their signed URLs.
57+
const HTML_CONTENT_SECURITY_POLICY = "sandbox allow-scripts allow-forms allow-popups allow-modals";
5458

5559
// Types a browser may render as a document if a proxy strips the disposition
5660
// header. Downloads of these fall back to octet-stream.
@@ -105,7 +109,10 @@ export function assetResponseHeaders(
105109
: inlineVideoMimeType !== undefined && isSafeInlineVideoMimeType(inlineVideoMimeType)
106110
? { "Content-Type": inlineVideoMimeType }
107111
: lowerPath.endsWith(".html") || lowerPath.endsWith(".htm")
108-
? { "Content-Type": "text/html; charset=utf-8" }
112+
? {
113+
"Content-Type": "text/html; charset=utf-8",
114+
"Content-Security-Policy": HTML_CONTENT_SECURITY_POLICY,
115+
}
109116
: {}),
110117
...(!options?.download && lowerPath.endsWith(".svg")
111118
? { "Content-Security-Policy": SVG_CONTENT_SECURITY_POLICY }

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ interface ChatMarkdownProps {
188188
parseRawHtml?: boolean;
189189
/** Append a prompt that invokes a newly created artifact-template skill. */
190190
onUseArtifactTemplate?: ((template: CodexArtifactTemplate) => void) | undefined;
191+
/** Directory that anchors relative links and images; defaults to `cwd`. Set
192+
to the file's own directory when rendering a markdown file. */
191193
imageBaseDir?: string | undefined;
192194
onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
193195
extraRemarkPlugins?: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
@@ -2100,24 +2102,24 @@ function ChatMarkdown({
21002102
for (const href of extractMarkdownLinkHrefs(renderCodexFileCitationsAsMarkdown(text))) {
21012103
const normalizedHref = normalizeMarkdownLinkHrefKey(href);
21022104
if (metaByHref.has(normalizedHref)) continue;
2103-
const meta = resolveMarkdownFileLinkMeta(normalizedHref, cwd);
2105+
const meta = resolveMarkdownFileLinkMeta(normalizedHref, cwd, imageBaseDir ?? cwd);
21042106
if (meta) {
21052107
metaByHref.set(normalizedHref, meta);
21062108
}
21072109
}
21082110
return metaByHref;
2109-
}, [cwd, text]);
2111+
}, [cwd, imageBaseDir, text]);
21102112
const inlineCodeFileLinkMetaByText = useMemo(() => {
21112113
const metaByText = new Map<string, MarkdownFileLinkMeta>();
21122114
for (const span of extractInlineCodeSpans(text)) {
21132115
if (metaByText.has(span)) continue;
2114-
const meta = resolveInlineCodeFileLinkMeta(span, cwd);
2116+
const meta = resolveInlineCodeFileLinkMeta(span, cwd, imageBaseDir ?? cwd);
21152117
if (meta) {
21162118
metaByText.set(span, meta);
21172119
}
21182120
}
21192121
return metaByText;
2120-
}, [cwd, text]);
2122+
}, [cwd, imageBaseDir, text]);
21212123
const fileLinkParentSuffixByPath = useMemo(() => {
21222124
const filePaths = [
21232125
...[...markdownFileLinkMetaByHref.values()].map((meta) => meta.filePath),
@@ -2442,7 +2444,7 @@ function ChatMarkdown({
24422444
const normalizedHref = href ? normalizeMarkdownLinkHrefKey(href) : "";
24432445
const fileLinkMeta = normalizedHref
24442446
? (markdownFileLinkMetaByHref.get(normalizedHref) ??
2445-
resolveMarkdownFileLinkMeta(normalizedHref, cwd))
2447+
resolveMarkdownFileLinkMeta(normalizedHref, cwd, imageBaseDir ?? cwd))
24462448
: null;
24472449
if (!fileLinkMeta) {
24482450
const faviconHost = resolveExternalWebLinkHost(href);
@@ -2588,7 +2590,7 @@ function ChatMarkdown({
25882590
const codeText = nodeToPlainText(children);
25892591
const fileLinkMeta =
25902592
inlineCodeFileLinkMetaByText.get(codeText.trim()) ??
2591-
resolveInlineCodeFileLinkMeta(codeText, cwd);
2593+
resolveInlineCodeFileLinkMeta(codeText, cwd, imageBaseDir ?? cwd);
25922594
if (fileLinkMeta) {
25932595
return fileLinkChip(
25942596
fileLinkMeta,

‎apps/web/src/markdown-links.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,22 @@ describe("resolveMarkdownFileLinkTarget", () => {
275275
});
276276
});
277277

278+
describe("relative links inside a rendered host file", () => {
279+
it("anchor to the file's directory while workspace membership follows cwd", () => {
280+
const meta = resolveMarkdownFileLinkMeta("appendix.md", "/repo", "/tmp/report");
281+
expect(meta).toMatchObject({
282+
filePath: "/tmp/report/appendix.md",
283+
workspaceRelativePath: null,
284+
});
285+
const inline = resolveInlineCodeFileLinkMeta("Makefile:12", "/repo", "/tmp/report");
286+
expect(inline).toMatchObject({ filePath: "/tmp/report/Makefile", line: 12 });
287+
expect(resolveMarkdownFileLinkMeta("src/main.ts", "/repo", "/repo/docs")).toMatchObject({
288+
filePath: "/repo/docs/src/main.ts",
289+
workspaceRelativePath: "docs/src/main.ts",
290+
});
291+
});
292+
});
293+
278294
describe("resolveInlineCodeFileLinkMeta", () => {
279295
it("links relative paths with file extensions", () => {
280296
expect(

‎apps/web/src/markdown-links.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,15 @@ function hasExternalScheme(path: string): boolean {
189189
return !POSITION_ONLY_PATTERN.test(rest);
190190
}
191191

192+
/**
193+
* `baseDir` anchors relative links; it defaults to the workspace root and is the
194+
* file's own directory when rendering a markdown file. `cwd` stays the workspace
195+
* root so the result still knows whether the target is inside it.
196+
*/
192197
export function resolveMarkdownFileLinkTarget(
193198
href: string | undefined,
194199
cwd?: string,
200+
baseDir: string | undefined = cwd,
195201
): string | null {
196202
if (!href) return null;
197203
const rawHref = normalizeMarkdownLinkDestination(href);
@@ -222,8 +228,8 @@ export function resolveMarkdownFileLinkTarget(
222228
return pathWithPosition;
223229
}
224230

225-
if (!cwd) return null;
226-
return resolvePathLinkTarget(pathWithPosition, cwd);
231+
if (!baseDir) return null;
232+
return resolvePathLinkTarget(pathWithPosition, baseDir);
227233
}
228234

229235
/**
@@ -235,18 +241,19 @@ export function resolveMarkdownFileLinkTarget(
235241
export function resolveInlineCodeFileLinkMeta(
236242
codeText: string,
237243
cwd?: string,
244+
baseDir: string | undefined = cwd,
238245
): MarkdownFileLinkMeta | null {
239246
const candidate = inlineCodeFilePathCandidate(codeText);
240247
if (candidate === null) return null;
241248

242-
const resolved = resolveMarkdownFileLinkMeta(candidate, cwd);
249+
const resolved = resolveMarkdownFileLinkMeta(candidate, cwd, baseDir);
243250
if (resolved) return resolved;
244251

245252
// `Makefile:12` — conventional extensionless names fail the generic
246253
// markdown-link candidate patterns, but here the :line suffix already
247254
// marked the span as a file reference.
248-
if (cwd && isConventionalFilePosition(candidate)) {
249-
return buildFileLinkMetaFromTarget(resolvePathLinkTarget(candidate, cwd), cwd);
255+
if (baseDir && isConventionalFilePosition(candidate)) {
256+
return buildFileLinkMetaFromTarget(resolvePathLinkTarget(candidate, baseDir), cwd);
250257
}
251258
return null;
252259
}
@@ -276,8 +283,9 @@ function workspaceRelativePath(path: string, workspaceRoot: string | undefined):
276283
export function resolveMarkdownFileLinkMeta(
277284
href: string | undefined,
278285
cwd?: string,
286+
baseDir: string | undefined = cwd,
279287
): MarkdownFileLinkMeta | null {
280-
const targetPath = resolveMarkdownFileLinkTarget(href, cwd);
288+
const targetPath = resolveMarkdownFileLinkTarget(href, cwd, baseDir);
281289
if (!targetPath) return null;
282290
return buildFileLinkMetaFromTarget(targetPath, cwd);
283291
}

0 commit comments

Comments
 (0)