@devries-cameron20 / t3code-1 / commits / b7b53d404e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): preview downloads no longer echo data: URL contents (#22156)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 20, 2026 at 01:10 UTC b7b53d404e72df21854bf7861dfc2f3106e3294d
5 files changed +154 -12
apps/server/src/preview/ServerBrowser.test.ts
+25 -2
@@ -3,6 +3,7 @@ import * as NodeEvents from "node:events";
3 import * as NodeFSP from "node:fs/promises";
4
5 import * as NodeServices from "@effect/platform-node/NodeServices";
6 +import * as HostProcess from "@t3tools/shared/HostProcess";
7 import { it } from "@effect/vitest";
8 import {
9 EnvironmentId,
@@ -19,6 +20,7 @@ import * as Fiber from "effect/Fiber";
20 import * as FileSystem from "effect/FileSystem";
21 import * as Layer from "effect/Layer";
22 import * as Option from "effect/Option";
23 +import * as Path from "effect/Path";
24 import * as PubSub from "effect/PubSub";
25 import * as Queue from "effect/Queue";
26 import * as Scope from "effect/Scope";
@@ -1644,7 +1646,7 @@ it.live.each([false, true])(
1646 },
1647 saveAs: (path: string) => NodeFSP.copyFile(original, path),
1648 suggestedFilename: () => "report.csv",
1647 - url: () => "blob:http://localhost:5173/1",
1649 + url: () => 'data:text/csv;charset="a,b",a b\'c',
1650 });
1651 let offered = yield* Queue.take(viewer.output);
1652 while (offered._tag !== "download") offered = yield* Queue.take(viewer.output);
@@ -1658,6 +1660,9 @@ it.live.each([false, true])(
1660 });
1661 expect(Option.isSome(file) && file.value.fileName).toBe("report.csv");
1662 expect(yield* fs.readFileString(Option.getOrThrow(file).path)).toBe("a,b");
1663 + if ((yield* HostProcess.Platform) !== "win32") {
1664 + expect((yield* fs.stat(Option.getOrThrow(file).path)).mode & 0o777).toBe(0o600);
1665 + }
1666 const status = yield* broker.invoke<PreviewAutomationStatus>({
1667 scope,
1668 tabId,
@@ -1665,7 +1670,11 @@ it.live.each([false, true])(
1670 input: {},
1671 });
1672 expect(status.downloads).toEqual([
1668 - expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }),
1673 + expect.objectContaining({
1674 + fileName: "report.csv",
1675 + sizeBytes: 3,
1676 + url: "data:text/csv,…",
1677 + }),
1678 ]);
1679 expect(
1680 Option.isNone(
@@ -1676,6 +1685,20 @@ it.live.each([false, true])(
1685 }),
1686 ),
1687 ).toBe(true);
1688 + if ((yield* HostProcess.Platform) !== "win32") {
1689 + const folder = (yield* Path.Path).dirname(Option.getOrThrow(file).path);
1690 + yield* fs.chmod(folder, 0o755);
1691 + page.emit("download", {
1692 + failure: async () => null,
1693 + delete: async () => {},
1694 + saveAs: (path: string) => NodeFSP.writeFile(path, "c"),
1695 + suggestedFilename: () => "again.csv",
1696 + url: () => "https://example.test/again.csv",
1697 + });
1698 + let again = yield* Queue.take(viewer.output);
1699 + while (again._tag !== "download") again = yield* Queue.take(viewer.output);
1700 + expect((yield* fs.stat(folder)).mode & 0o777).toBe(0o700);
1701 + }
1702 }),
1703 ).pipe(Effect.provide(layer)),
1704 );
apps/server/src/preview/ServerBrowser.ts
+16 -8
@@ -795,7 +795,11 @@ const make = Effect.gen(function* () {
795 navigationGenerations.set(request, ++tab.navigationGeneration);
796 clearAbortedNavigation(tab);
797 tab.loading = true;
798 - report(tab, { _tag: "Loading", url: request.url().slice(0, 2048), title: "" });
798 + report(tab, {
799 + _tag: "Loading",
800 + url: ServerBrowserPage.withoutDataPayload(request.url()).slice(0, 2048),
801 + title: "",
802 + });
803 });
804 page.on("load", () => {
805 tab.loading = false;
@@ -811,7 +815,7 @@ const make = Effect.gen(function* () {
815 page.on("requestfailed", (request) => {
816 const errorText = request.failure()?.errorText ?? "";
817 pushBounded(tab.networkEntries, {
814 - url: request.url(),
818 + url: ServerBrowserPage.withoutDataPayload(request.url()),
819 method: request.method(),
820 status: null,
821 failed: true,
@@ -844,7 +848,7 @@ const make = Effect.gen(function* () {
848 const { code, description } = ServerBrowserPage.parseNetError(errorText);
849 report(tab, {
850 _tag: "LoadFailed",
847 - url: request.url().slice(0, 2048),
851 + url: ServerBrowserPage.withoutDataPayload(request.url()).slice(0, 2048),
852 title: "",
853 code,
854 description,
@@ -852,7 +856,7 @@ const make = Effect.gen(function* () {
856 });
857 page.on("response", (response) => {
858 pushBounded(tab.networkEntries, {
855 - url: response.url(),
859 + url: ServerBrowserPage.withoutDataPayload(response.url()),
860 method: response.request().method(),
861 status: response.status(),
862 failed: false,
@@ -862,7 +866,7 @@ const make = Effect.gen(function* () {
866 page.on("console", (message) => {
867 pushBounded(tab.consoleEntries, {
868 level: message.type(),
865 - text: message.text().slice(0, 2_000),
869 + text: ServerBrowserPage.consoleText(message).slice(0, 2_000),
870 timestamp: new Date().toISOString(),
871 });
872 });
@@ -932,7 +936,7 @@ const make = Effect.gen(function* () {
936 }
937 report(tab, {
938 _tag: "LoadFailed",
935 - url: url.slice(0, 2048),
939 + url: ServerBrowserPage.withoutDataPayload(url).slice(0, 2048),
940 title: "",
941 code: -3,
942 description: "ERR_ABORTED",
@@ -949,8 +953,12 @@ const make = Effect.gen(function* () {
953 let offered: { readonly id: string; readonly fileName: string } | undefined;
954 try {
955 if (await download.failure()) return;
952 - await NodeFSP.mkdir(downloadDir(tab), { recursive: true });
956 + await NodeFSP.mkdir(downloadDir(tab), { recursive: true, mode: 0o700 });
957 + // mkdir leaves the mode of a folder that already exists, such as one an older build made.
958 + await NodeFSP.chmod(downloadDir(tab), 0o700);
959 await download.saveAs(path);
960 + // saveAs copies with the process umask; a download should stay readable by its owner only.
961 + await NodeFSP.chmod(path, 0o600);
962 const { size } = await NodeFSP.stat(path);
963 if (tab.closing || size > DOWNLOAD_MAX_BYTES) {
964 await NodeFSP.rm(path, { force: true });
@@ -961,7 +969,7 @@ const make = Effect.gen(function* () {
969 fileName: download.suggestedFilename().slice(0, 255) || "download",
970 path,
971 sizeBytes: size,
964 - url: download.url().slice(0, 2048),
972 + url: ServerBrowserPage.withoutDataPayload(download.url()).slice(0, 2048),
973 completedAt: new Date().toISOString(),
974 };
975 tab.downloads.push(saved);
apps/server/src/preview/ServerBrowserPage.test.ts
+58
@@ -178,6 +178,64 @@ describe("server browser element refs", () => {
178 expect(cut.slice(-8)).toBe("xxx•••••");
179 });
180
181 + it("shows a data: link or blocked data: navigation by media type only", async () => {
182 + // Chromium keeps raw spaces and quotes in both, so a payload can't be cut at either.
183 + await page.setContent(`
184 + <a download href="data:text/plain;charset=&quot;a,b&quot;,head PAYLOAD'tail">Key</a>
185 + <a href="https://example.com/data:report,2026">Report</a>
186 + <a href="#">data:text/plain,label</a>
187 + <a href='"data:text/plain,relative'>Relative</a>
188 + <a href=" data:text/plain,PAYLOAD">Spaced</a>
189 + <a href="da&#9;ta:text/plain,PAYLOAD">Tabbed</a>
190 + <a href="data:text/plain,head&#x2028;PAYLOAD">Separated</a>
191 + <a href="data:PAYLOAD,x">Typeless</a>
192 + <a href="&#11;data:text/plain,PAYLOAD">Vertical tab</a>
193 + <a href="&#8;data:text/plain,PAYLOAD">Backspace</a>
194 + <a id="controlled">Controlled</a>
195 + <a id="escaped">Escaped</a>
196 + <script>
197 + controlled.href = "\\x01data:text/plain,PAYLOAD";
198 + escaped.href = "\\x02da\\tta:text/plain,PAYLOAD";
199 + </script>`);
200 + const tree = String((await takeSnapshot()).accessibilityTree);
201 + expect(tree.match(/\/url: data:text\/plain,…$/gm)).toHaveLength(8);
202 + expect(tree).toContain("/url: data:,…");
203 + expect(tree).toContain("/url: https://example.com/data:report,2026");
204 + expect(tree).toMatch(/link "data:text\/plain,label" \[ref=t3-/);
205 + expect(tree).not.toMatch(/PAYLOAD|tail|charset|\u2028/);
206 + expect(tree).toContain('/url: "\\"data:text/plain,relative"');
207 + });
208 +
209 + it("redacts a data: link whose scheme a password value would mask", async () => {
210 + await page.setContent(`
211 + <input type="password" aria-label="Scheme" value="data">
212 + <a download href="data:text/plain,PAYLOAD">Key</a>`);
213 + const tree = String((await takeSnapshot()).accessibilityTree);
214 + expect(tree).toContain("/url: ••••••••:text/plain,…");
215 + expect(tree).not.toContain("PAYLOAD");
216 + });
217 +
218 + it("shows data: URLs in Chromium's console messages by media type, and page logs as written", async () => {
219 + const logged: Array<string> = [];
220 + page.on("console", (message) => logged.push(ServerBrowserPage.consoleText(message)));
221 + await page.setContent(`
222 + <meta http-equiv="Content-Security-Policy" content="img-src 'self'">
223 + <script>console.log("Downloaded data:text/plain,example")</script>
224 + <img src="data:image/png;base64,S0VZ">`);
225 + // Chromium opens a data: URL whose type is malformed too, and quotes it unchanged.
226 + for (const url of ["data:text/plain,head PAYLOAD'tail x", "data:text/plain charset,PAYLOAD"]) {
227 + const blocked = page.waitForEvent("console");
228 + await page.evaluate(`location.href = ${JSON.stringify(url)}`);
229 + await blocked;
230 + }
231 + expect(logged).toEqual([
232 + "Downloaded data:text/plain,example",
233 + "Loading the image 'data:image/png,…",
234 + "Not allowed to navigate top frame to data URL: data:text/plain,…",
235 + "Not allowed to navigate top frame to data URL: data:,…",
236 + ]);
237 + });
238 +
239 it("rejects refs from another tab", async () => {
240 await page.setContent("<button>same label</button>");
241 const locator = buttonLocator((await takeSnapshot()).accessibilityTree, "same label");
apps/server/src/preview/ServerBrowserPage.ts
+53 -2
@@ -18,7 +18,7 @@ import {
18 type PreviewAutomationWaitForInput,
19 } from "@t3tools/contracts";
20 import { constVoid } from "effect/Function";
21 -import type { CDPSession, Frame, Locator, Page } from "playwright-core";
21 +import type { CDPSession, ConsoleMessage, Frame, Locator, Page } from "playwright-core";
22 import * as NodeCrypto from "node:crypto";
23 import { BrowserControlInterrupted } from "./SessionControl.ts";
24
@@ -149,6 +149,54 @@ const SNAPSHOT_SCRIPT = `(() => {
149 })()`;
150
151 const MASKED_VALUE = "••••••••";
152 +
153 +/**
154 + * A `data:` URL carries its content, which can be a whole file, and URLs and diagnostics
155 + * reach tool results and provider logs. Keeps only the media type: the payload can hold any
156 + * character and parameters are page-chosen too.
157 + */
158 +export const withoutDataPayload = (url: string) => {
159 + // Read the scheme as a browser would: it ignores leading controls and spaces, and tabs and
160 + // newlines anywhere. A snapshot's YAML value may be quoted and escaped; an escape that isn't a
161 + // tab or newline still decodes to something that can't start a scheme.
162 + const value = url
163 + .replace(/^["']/, "")
164 + .replace(/\\(x[\da-f]{2}|u[\da-f]{4}|.)/gi, (_escape, code: string) =>
165 + code.length > 1
166 + ? String.fromCharCode(Number.parseInt(code.slice(1), 16))
167 + : ({ t: "\t", n: "\n", r: "\r", '"': '"', "\\": "\\" }[code] ?? "\u0001"),
168 + )
169 + .replace(/[\t\n\r]/g, "")
170 + // eslint-disable-next-line no-control-regex -- URL parsing ignores these controls.
171 + .replace(/^[\u0000-\u0020]+/, "");
172 + const parsed = /^data:([^,;]*)/i.exec(value);
173 + if (!parsed) return url;
174 + const type = parsed[1]!.trim();
175 + const mediaType = /^[\w.+-]{1,64}\/[\w.+-]{1,64}$/.test(type) ? type : "";
176 + return `data:${mediaType},…`;
177 +};
178 +
179 +/**
180 + * Chromium's own console messages, which carry no arguments, quote `data:` URLs whole, as in a
181 + * blocked navigation or a CSP violation. Everything after the first one goes, since Chromium
182 + * accepts any type and a payload can hold any character. What a page logs itself is its own
183 + * text, like anything else it shows, so it stays.
184 + */
185 +export const consoleText = (message: ConsoleMessage) =>
186 + message.args().length > 0
187 + ? message.text()
188 + : message.text().replace(/(?<![a-z\d])data:[^,;]*[,;][\s\S]*/i, withoutDataPayload);
189 +
190 +// Split on LF alone: a value keeps any U+2028 that a multiline regex would treat as a line end.
191 +const withoutTreeDataPayloads = (tree: string) =>
192 + tree
193 + .split("\n")
194 + .map((line) => {
195 + const url = /^(\s*- \/url: )(.*)$/s.exec(line);
196 + return url ? url[1] + withoutDataPayload(url[2]!) : line;
197 + })
198 + .join("\n");
199 +
200 // The broker waits 15s for the host; a snapshot, retry included, answers well before.
201 const SNAPSHOT_TIMEOUT_MS = 10_000;
202 // How long a frame in another process gets to answer; one that crashed never does.
@@ -534,7 +582,10 @@ const capture = async (
582 const [page, data] = await pageAndScreenshot;
583 if (state.generation !== generation) throw pageChanged();
584 const mask = (text: string) => (dotted ? text.replace(dotted, MASKED_VALUE) : text);
537 - const accessibilityTree = (dotted ? maskTree(tree, dotted) : tree)
585 + // Redacted before the cut, so a payload crossing it can't leave its start showing, and before
586 + // masking, which could hide the `data:` scheme behind a password value's dots.
587 + const redacted = withoutTreeDataPayloads(tree);
588 + const accessibilityTree = (dotted ? maskTree(redacted, dotted) : redacted)
589 .slice(0, MAX_VISIBLE_TEXT_LENGTH)
590 .replace(/\[ref=((?:f\d+)?e\d+)\]/g, (_match, nativeRef: string) => {
591 const ref = `t3-${generation}-${nativeRef}`;
packages/shared/src/preview.ts
+2
@@ -202,6 +202,8 @@ export function normalizePreviewUrl(rawUrl: string): string {
202 cause,
203 });
204 }
205 + // The server preview browser reports page URLs unredacted; a data: URL would put its
206 + // content in tool results and logs.
207 if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
208 throw new PreviewUrlNormalizationError({
209 inputLength: rawUrl.length,