Skip to content

Commit ecb8699

Browse files
MarcoDotIOclaude
andcommitted
fix(server): preview_network skips a body whose size it cannot learn
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f2a4e33 commit ecb8699

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ it.live("agents list a human's requests but read one's headers and bodies only o
15731573
frame: () => page,
15741574
headers: () => ({ "content-type": "application/json", authorization: "Bearer t" }),
15751575
postDataBuffer: () => Buffer.from('{"password":"hunter2"}'),
1576+
sizes: async () => ({ responseBodySize: 24 }),
15761577
};
15771578
page.emit("request", login);
15781579
page.emit("response", {

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const makeRequest = (
2020
readonly headers?: Record<string, string>;
2121
readonly postData?: string;
2222
readonly errorText?: string;
23-
readonly responseBodySize?: number;
23+
readonly responseBodySize?: number | null;
2424
} = {},
2525
) => ({
2626
url: () => url,
@@ -29,9 +29,10 @@ const makeRequest = (
2929
headers: () => options.headers ?? {},
3030
postDataBuffer: () => (options.postData === undefined ? null : Buffer.from(options.postData)),
3131
failure: () => (options.errorText === undefined ? null : { errorText: options.errorText }),
32+
// Playwright gives a finished request its sizes; null stands for the call failing.
3233
sizes: async () => {
33-
if (options.responseBodySize === undefined) throw new Error("Unable to fetch sizes");
34-
return { responseBodySize: options.responseBodySize };
34+
if (options.responseBodySize === null) throw new Error("Unable to fetch sizes");
35+
return { responseBodySize: options.responseBodySize ?? 64 };
3536
},
3637
});
3738

@@ -269,7 +270,24 @@ describe("NetworkLog", () => {
269270
network.finish(request);
270271

271272
const { requests } = await network.log.query("tab-1", { requestId: "r1" });
272-
expect(requests[0]?.responseBody).toEqual({ truncated: false, omitted: "unavailable" });
273+
expect(requests[0]?.responseBody).toEqual({
274+
sizeBytes: 64,
275+
truncated: false,
276+
omitted: "unavailable",
277+
});
278+
});
279+
280+
it("never reads a body whose size neither the headers nor Chromium report", async () => {
281+
const network = setup();
282+
const request = makeRequest("http://localhost:5173/api/unknown", { responseBodySize: null });
283+
const response = makeResponse(request, 200, { "content-type": "application/json" }, "{}");
284+
network.start(request);
285+
network.respond(response);
286+
network.finish(request);
287+
288+
const { requests } = await network.log.query("tab-1", { requestId: "r1" });
289+
expect(requests[0]?.responseBody).toEqual({ truncated: true, omitted: "unavailable" });
290+
expect(response.body).not.toHaveBeenCalled();
273291
});
274292

275293
it("keeps the newest 200 requests and says when a listed id has aged out", async () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const responseBodyOf = async (response: Response): Promise<PreviewAutomationNetw
9191
// Both sizes count encoded bytes, and body() decompresses, so a compressed body has no bound.
9292
const compressed =
9393
(headers["content-encoding"] ?? "identity").trim().toLowerCase() !== "identity";
94-
if (compressed || (size !== undefined && size > BODY_FETCH_BYTES)) {
94+
if (compressed || size === undefined || size > BODY_FETCH_BYTES) {
9595
return { ...known, truncated: true, omitted: "unavailable" };
9696
}
9797
try {

0 commit comments

Comments
 (0)