Skip to content

Commit c63f70e

Browse files
fix(server): preview downloads no longer echo data: URL contents
A download from a data: URL was recorded with its full URL, so the file's contents came back in every preview status and in the provider event log. Browser-reported URLs and Chromium's own console messages now keep only a data: URL's media type, and saved downloads are private to the user. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 70a869d commit c63f70e

5 files changed

Lines changed: 137 additions & 12 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as NodeEvents from "node:events";
33
import * as NodeFSP from "node:fs/promises";
44

55
import * as NodeServices from "@effect/platform-node/NodeServices";
6+
import * as HostProcess from "@t3tools/shared/HostProcess";
67
import { it } from "@effect/vitest";
78
import {
89
EnvironmentId,
@@ -1644,7 +1645,7 @@ it.live.each([false, true])(
16441645
},
16451646
saveAs: (path: string) => NodeFSP.copyFile(original, path),
16461647
suggestedFilename: () => "report.csv",
1647-
url: () => "blob:http://localhost:5173/1",
1648+
url: () => 'data:text/csv;charset="a,b",a b\'c',
16481649
});
16491650
let offered = yield* Queue.take(viewer.output);
16501651
while (offered._tag !== "download") offered = yield* Queue.take(viewer.output);
@@ -1658,14 +1659,21 @@ it.live.each([false, true])(
16581659
});
16591660
expect(Option.isSome(file) && file.value.fileName).toBe("report.csv");
16601661
expect(yield* fs.readFileString(Option.getOrThrow(file).path)).toBe("a,b");
1662+
if ((yield* HostProcess.Platform) !== "win32") {
1663+
expect((yield* fs.stat(Option.getOrThrow(file).path)).mode & 0o777).toBe(0o600);
1664+
}
16611665
const status = yield* broker.invoke<PreviewAutomationStatus>({
16621666
scope,
16631667
tabId,
16641668
operation: "status",
16651669
input: {},
16661670
});
16671671
expect(status.downloads).toEqual([
1668-
expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }),
1672+
expect.objectContaining({
1673+
fileName: "report.csv",
1674+
sizeBytes: 3,
1675+
url: "data:text/csv,…",
1676+
}),
16691677
]);
16701678
expect(
16711679
Option.isNone(

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,11 @@ const make = Effect.gen(function* () {
795795
navigationGenerations.set(request, ++tab.navigationGeneration);
796796
clearAbortedNavigation(tab);
797797
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+
});
799803
});
800804
page.on("load", () => {
801805
tab.loading = false;
@@ -811,7 +815,7 @@ const make = Effect.gen(function* () {
811815
page.on("requestfailed", (request) => {
812816
const errorText = request.failure()?.errorText ?? "";
813817
pushBounded(tab.networkEntries, {
814-
url: request.url(),
818+
url: ServerBrowserPage.withoutDataPayload(request.url()),
815819
method: request.method(),
816820
status: null,
817821
failed: true,
@@ -844,15 +848,15 @@ const make = Effect.gen(function* () {
844848
const { code, description } = ServerBrowserPage.parseNetError(errorText);
845849
report(tab, {
846850
_tag: "LoadFailed",
847-
url: request.url().slice(0, 2048),
851+
url: ServerBrowserPage.withoutDataPayload(request.url()).slice(0, 2048),
848852
title: "",
849853
code,
850854
description,
851855
});
852856
});
853857
page.on("response", (response) => {
854858
pushBounded(tab.networkEntries, {
855-
url: response.url(),
859+
url: ServerBrowserPage.withoutDataPayload(response.url()),
856860
method: response.request().method(),
857861
status: response.status(),
858862
failed: false,
@@ -862,7 +866,7 @@ const make = Effect.gen(function* () {
862866
page.on("console", (message) => {
863867
pushBounded(tab.consoleEntries, {
864868
level: message.type(),
865-
text: message.text().slice(0, 2_000),
869+
text: ServerBrowserPage.consoleText(message).slice(0, 2_000),
866870
timestamp: new Date().toISOString(),
867871
});
868872
});
@@ -932,7 +936,7 @@ const make = Effect.gen(function* () {
932936
}
933937
report(tab, {
934938
_tag: "LoadFailed",
935-
url: url.slice(0, 2048),
939+
url: ServerBrowserPage.withoutDataPayload(url).slice(0, 2048),
936940
title: "",
937941
code: -3,
938942
description: "ERR_ABORTED",
@@ -949,8 +953,10 @@ const make = Effect.gen(function* () {
949953
let offered: { readonly id: string; readonly fileName: string } | undefined;
950954
try {
951955
if (await download.failure()) return;
952-
await NodeFSP.mkdir(downloadDir(tab), { recursive: true });
956+
await NodeFSP.mkdir(downloadDir(tab), { recursive: true, mode: 0o700 });
953957
await download.saveAs(path);
958+
// saveAs copies with the process umask; a download should stay readable by its owner only.
959+
await NodeFSP.chmod(path, 0o600);
954960
const { size } = await NodeFSP.stat(path);
955961
if (tab.closing || size > DOWNLOAD_MAX_BYTES) {
956962
await NodeFSP.rm(path, { force: true });
@@ -961,7 +967,7 @@ const make = Effect.gen(function* () {
961967
fileName: download.suggestedFilename().slice(0, 255) || "download",
962968
path,
963969
sizeBytes: size,
964-
url: download.url().slice(0, 2048),
970+
url: ServerBrowserPage.withoutDataPayload(download.url()).slice(0, 2048),
965971
completedAt: new Date().toISOString(),
966972
};
967973
tab.downloads.push(saved);

‎apps/server/src/preview/ServerBrowserPage.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,64 @@ describe("server browser element refs", () => {
178178
expect(cut.slice(-8)).toBe("xxx•••••");
179179
});
180180

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+
181239
it("rejects refs from another tab", async () => {
182240
await page.setContent("<button>same label</button>");
183241
const locator = buttonLocator((await takeSnapshot()).accessibilityTree, "same label");

‎apps/server/src/preview/ServerBrowserPage.ts‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
type PreviewAutomationWaitForInput,
1919
} from "@t3tools/contracts";
2020
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";
2222
import * as NodeCrypto from "node:crypto";
2323
import { BrowserControlInterrupted } from "./SessionControl.ts";
2424

@@ -149,6 +149,54 @@ const SNAPSHOT_SCRIPT = `(() => {
149149
})()`;
150150

151151
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+
152200
// The broker waits 15s for the host; a snapshot, retry included, answers well before.
153201
const SNAPSHOT_TIMEOUT_MS = 10_000;
154202
// How long a frame in another process gets to answer; one that crashed never does.
@@ -534,7 +582,10 @@ const capture = async (
534582
const [page, data] = await pageAndScreenshot;
535583
if (state.generation !== generation) throw pageChanged();
536584
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)
538589
.slice(0, MAX_VISIBLE_TEXT_LENGTH)
539590
.replace(/\[ref=((?:f\d+)?e\d+)\]/g, (_match, nativeRef: string) => {
540591
const ref = `t3-${generation}-${nativeRef}`;

‎packages/shared/src/preview.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ export function normalizePreviewUrl(rawUrl: string): string {
202202
cause,
203203
});
204204
}
205+
// The server preview browser reports page URLs unredacted; a data: URL would put its
206+
// content in tool results and logs.
205207
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
206208
throw new PreviewUrlNormalizationError({
207209
inputLength: rawUrl.length,

0 commit comments

Comments
 (0)