fix(browser): retain effective source-map response urls

Preserve source-map response identity and legacy omission, admit identities before body reading, and retain completed results and cleanup causes within the existing output budget. Price fixed envelopes once.

Closes #1729.
Closes #1738.

Co-authored-by: allisaid <curruptdwastaken@gmail.com>
This commit is contained in:
clyde
2026-10-11 15:35:43 +08:00
committed by GitHub
parent 81dc64e3cd
commit 062723e44e
9 changed files with 636 additions and 100 deletions
@@ -119,26 +119,27 @@ it("parses ECMA-426 HTTP source-map prefixes while retaining exact response evid
});
describe("source-map redirect URL resolution", () => {
it.each(["/maps/current", "/assets/v2/app.js.map"])(
const deliveredPath =
"/assets/v2/app.js.map?revision=2&token=public-selection";
it.each(["/maps/current", deliveredPath])(
"resolves relative sources against the delivered map at %s",
async (initialPath) => {
const calls: string[] = [];
const text = JSON.stringify({
version: 3,
names: [],
sources: ["../src/main.ts"],
sourcesContent: ["import './dependency.ts';"],
mappings: "AAAA",
});
const server = createServer((incoming, response) => {
calls.push(incoming.url ?? "");
if (incoming.url === "/maps/current") {
response.writeHead(302, { location: "/assets/v2/app.js.map" }).end();
response.writeHead(302, { location: deliveredPath }).end();
return;
}
response.setHeader("content-type", "application/json");
response.end(
JSON.stringify({
version: 3,
names: [],
sources: ["../src/main.ts"],
sourcesContent: ["import './dependency.ts';"],
mappings: "AAAA",
}),
);
response.end(text);
});
try {
await new Promise<void>((resolve, reject) => {
@@ -156,7 +157,7 @@ describe("source-map redirect URL resolution", () => {
);
expect(calls).toEqual(
initialPath === "/maps/current"
? [initialPath, "/assets/v2/app.js.map"]
? [initialPath, deliveredPath]
: [initialPath],
);
expect(result).toMatchObject({
@@ -164,6 +165,12 @@ describe("source-map redirect URL resolution", () => {
items: [
{
declared_url: declaredUrl,
effective_url: `${localOrigin}${deliveredPath}`,
artifact: {
text,
bytes: Buffer.byteLength(text),
sha256: createHash("sha256").update(text).digest("hex"),
},
original_sources: [
{ source: `${localOrigin}/assets/src/main.ts` },
],
@@ -188,6 +195,74 @@ describe("source-map redirect URL resolution", () => {
}
},
);
it.each([
[404, "unavailable", "fetch_failed"],
[200, "not-json", "invalid"],
])(
"retains the delivered URL when its HTTP %s response cannot supply a map",
async (responseStatus, body, itemStatus) => {
const effectiveUrl = `${origin}${deliveredPath}`;
const calls: string[] = [];
const result = await fetchWebSourceMaps([request], input(), undefined, {
fetch: (url) => {
calls.push(String(url));
return Promise.resolve(
calls.length === 1
? new Response(null, {
status: 302,
headers: { location: effectiveUrl },
})
: new Response(body, { status: responseStatus }),
);
},
});
expect(calls).toEqual([request.fetchUrl, effectiveUrl]);
expect(result.items[0]).toMatchObject({
status: itemStatus,
declared_url: request.declaredUrl,
effective_url: effectiveUrl,
artifact: null,
});
},
);
it("retains the last observed response URL when the next redirect fetch rejects", async () => {
const nextUrl = `${origin}${deliveredPath}`;
const calls: string[] = [];
const result = await fetchWebSourceMaps([request], input(), undefined, {
fetch: (url) => {
calls.push(String(url));
return calls.length === 1
? Promise.resolve(
new Response(null, {
status: 302,
headers: { location: nextUrl },
}),
)
: Promise.reject(new TypeError("fixture connection failed"));
},
});
expect(calls).toEqual([request.fetchUrl, nextUrl]);
expect(result.items[0]).toMatchObject({
status: "fetch_failed",
declared_url: request.declaredUrl,
effective_url: request.fetchUrl,
artifact: null,
});
});
it("reports an absent effective URL when the first fetch rejects", async () => {
const result = await fetchWebSourceMaps([request], input(), undefined, {
fetch: () => Promise.reject(new TypeError("fixture connection failed")),
});
expect(result.items[0]).toMatchObject({
status: "fetch_failed",
declared_url: request.declaredUrl,
effective_url: null,
artifact: null,
});
});
});
describe("web source-map fetching and validation: fetching maps and following redirects", () => {
@@ -275,7 +350,7 @@ describe("web source-map fetching and validation: fetching maps and following re
expect(calls).toEqual([request.fetchUrl]);
expect(result).toMatchObject({
status: "unavailable",
items: [{ status: "policy_filtered" }],
items: [{ status: "policy_filtered", effective_url: request.fetchUrl }],
});
expectInvalidSourceMaps({
...result,
@@ -309,7 +384,7 @@ describe("web source-map fetching and validation: fetching maps and following re
expect(calls).toEqual([]);
expect(result).toMatchObject({
status: "unavailable",
items: [{ status: "policy_filtered" }],
items: [{ status: "policy_filtered", effective_url: null }],
});
},
);
@@ -982,38 +1057,111 @@ describe("web source-map collection", () => {
});
describe("source-map output admission", () => {
it("admits a complete maximum-size map set when its real output fits", async () => {
const localOrigin = "http://127.0.0.1:9222";
const mapUrl = `${localOrigin}/m`;
const requests = Array.from({ length: 50_000 }, (_, index) => ({
...request,
scriptKey: `scr_${index.toString(16).padStart(64, "0")}`,
declaredUrl: mapUrl,
fetchUrl: mapUrl,
}));
const map = JSON.stringify({
version: 3,
names: [],
sources: ["x"],
mappings: "AAAA",
});
const result = await fetchWebSourceMaps(
requests,
input({ allowed_origins: [localOrigin] }),
undefined,
{
timeoutMs: 120_000,
fetch: () => Promise.resolve(new Response(map, { status: 200 })),
},
);
it.each([
{ selectionMiB: 8, responseStatus: "invalid" },
{ selectionMiB: 16, responseStatus: "invalid" },
{ selectionMiB: 16, responseStatus: "included" },
])(
"accounts for JSON escaping in a $selectionMiB MiB effective URL for $responseStatus responses",
async ({ selectionMiB, responseStatus }) => {
const effectiveUrl = `${origin}/delivered.map?selection=${"\\".repeat(selectionMiB * 1_024 * 1_024)}`;
const secondRequest = { ...request, scriptKey: `scr_${"2".repeat(64)}` };
let calls = 0;
const result = await fetchWebSourceMaps(
[request, secondRequest],
input(),
undefined,
{
fetch: () => {
calls += 1;
return Promise.resolve(
calls === 1
? validMapResponse()
: calls === 2
? new Response(null, {
status: 302,
headers: { location: effectiveUrl },
})
: responseStatus === "included"
? validMapResponse()
: new Response("not-json", { status: 200 }),
);
},
},
);
expect(calls).toBe(3);
expect(result).toMatchObject({
status: "partial",
requested: 2,
processed: selectionMiB === 8 ? 2 : 1,
});
if (selectionMiB === 8) {
expect(result.items[1]).toMatchObject({
status: "invalid",
declared_url: secondRequest.declaredUrl,
});
expect(result.items[1]?.effective_url === effectiveUrl).toBe(true);
} else {
expect(result.items).toHaveLength(1);
expect(result.limitation).toContain("1 requested map");
}
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(
32 * 1_024 * 1_024,
);
},
);
expect(result.status).toBe("included");
expect(result.limitation).toBeUndefined();
expect(result.items).toHaveLength(requests.length);
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(
32 * 1_024 * 1_024,
);
}, 120_000);
it.each([
{ localOrigin: "http://[::1]:1", expectedStatus: "included" },
{ localOrigin: "http://127.0.0.1:9222", expectedStatus: "partial" },
])(
"admits a maximum-size map set according to its actual output at $localOrigin",
async ({ localOrigin, expectedStatus }) => {
const mapUrl = `${localOrigin}/m`;
const requests = Array.from({ length: 50_000 }, (_, index) => ({
...request,
scriptKey: `scr_${index.toString(16).padStart(64, "0")}`,
declaredUrl: mapUrl,
fetchUrl: mapUrl,
}));
const map = JSON.stringify({
version: 3,
names: [],
sources: ["x"],
mappings: "AAAA",
});
const result = await fetchWebSourceMaps(
requests,
input({ allowed_origins: [localOrigin] }),
undefined,
{
timeoutMs: 120_000,
fetch: () => Promise.resolve(new Response(map, { status: 200 })),
},
);
expect(result.status).toBe(expectedStatus);
if (expectedStatus === "included") {
expect(result.limitation).toBeUndefined();
expect(result.items).toHaveLength(requests.length);
} else {
expect(result.items.length).toBeLessThan(requests.length);
expect(result.limitation).toContain(
`${String(requests.length - result.items.length)} requested maps`,
);
expect(
(Buffer.byteLength(JSON.stringify(result.items[0])) + 1) *
requests.length,
).toBeGreaterThan(32 * 1_024 * 1_024);
}
expect(result.processed).toBe(result.items.length);
expect(result.requested).toBe(requests.length);
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(
32 * 1_024 * 1_024,
);
},
120_000,
);
it("reserves large failure contexts and reports unprocessed requests as unknown", async () => {
const largeDeclaredUrl = `${origin}/${"u".repeat(8 * 1_024 * 1_024)}`;
@@ -4,7 +4,11 @@ import { createServer } from "node:http";
import { expect, it } from "vitest";
import { fetchWebSourceMaps } from "../../../src/browser/WebSourceMapFetcher.js";
import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js";
import {
analyzeWebBundleInputSchema,
webSourceMapsSchema,
} from "../../../src/domain/webBundleAnalysis.js";
import { WEB_SOURCE_MAP_LIMITS } from "../../../src/domain/webSourceLocation.js";
it.each([
{ status: 300, followsRedirect: false },
@@ -70,6 +74,10 @@ it.each([
expect(result.items[0]?.status).toBe(
followsRedirect ? "included" : "fetch_failed",
);
expect(result.items[0]).toMatchObject({
declared_url: url,
effective_url: followsRedirect ? `${origin}/delivered.map` : url,
});
if (!followsRedirect)
expect(result.items[0]?.limitation).toBe(
`Source-map server returned HTTP ${String(status)}.`,
@@ -118,8 +126,184 @@ it("keeps the declared URL and reports malformed redirect Location evidence", as
expect(result.items[0]).toMatchObject({
status: "fetch_failed",
declared_url: declaredUrl,
effective_url: declaredUrl,
limitation: expect.stringContaining("invalid Location URL"),
});
expect(result.items[0]?.limitation).toContain(declaredUrl);
expect(result.items[0]?.limitation).toContain("http://[invalid");
});
it.each(["invalid JSON", "invalid UTF-8", "HTTP failure", "response limit"])(
"retains a redirected response URL for %s",
async (failure) => {
const origin = "https://app.example.test";
const declaredUrl = `${origin}/declared.map`;
const fetchedUrl = `${origin}/maps/current.map?token=source-value#fragment`;
const result = await fetchWebSourceMaps(
[{ scriptKey: "script", declaredUrl, fetchUrl: declaredUrl }],
analyzeWebBundleInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page",
allowed_origins: [origin],
fetch_source_maps: true,
}),
undefined,
{
...(failure === "response limit" ? { maxResponseBytes: 1 } : {}),
fetch: (url) =>
Promise.resolve(
String(url) === declaredUrl
? new Response(null, {
status: 302,
headers: { location: fetchedUrl },
})
: new Response(
failure === "invalid UTF-8"
? Uint8Array.from([0xff])
: "not-json",
{ status: failure === "HTTP failure" ? 503 : 200 },
),
),
},
);
expect(result.items[0]).toMatchObject({
declared_url: declaredUrl,
effective_url: fetchedUrl,
status:
failure === "invalid JSON" || failure === "invalid UTF-8"
? "invalid"
: "fetch_failed",
artifact: null,
});
const item = result.items[0];
if (item === undefined) throw new Error("Expected a fetched map result");
const legacyItem = { ...item };
delete legacyItem.effective_url;
const legacy = webSourceMapsSchema.parse({
...result,
items: [legacyItem],
});
expect(legacy.items[0]).not.toHaveProperty("effective_url");
},
);
it.each([
{ cancellationFails: false, reason: null },
{ cancellationFails: true, reason: new Error("fixture cancel failure") },
{ cancellationFails: true, reason: undefined },
])(
"reserves a fetched URL before reading an over-cap response; cancellation failure=$cancellationFails, reason=$reason",
async ({ cancellationFails, reason }) => {
const origin = "https://app.example.test";
const fetchedUrl = `${origin}/${"u".repeat(WEB_SOURCE_MAP_LIMITS.outputBytes)}`;
let cancelled = false;
let read = false;
const body = new ReadableStream<Uint8Array>(
{
pull(controller) {
read = true;
controller.enqueue(
new TextEncoder().encode(
'{"version":3,"sources":[],"names":[],"mappings":""}',
),
);
controller.close();
},
cancel() {
cancelled = true;
if (cancellationFails) throw reason;
},
},
{ highWaterMark: 0 },
);
const pending = fetchWebSourceMaps(
[
{
scriptKey: "script",
declaredUrl: `${origin}/declared.map`,
fetchUrl: fetchedUrl,
},
],
analyzeWebBundleInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page",
allowed_origins: [origin],
fetch_source_maps: true,
}),
undefined,
{ fetch: () => Promise.resolve(new Response(body)) },
);
if (cancellationFails) {
await expect(pending).rejects.toMatchObject({
_tag: "AnalysisResourceConstraintError",
message: expect.stringContaining("Expanded source-map evidence"),
cleanupIncomplete: true,
cleanupResources: ["source-map-response"],
cause: expect.objectContaining({
errors: expect.arrayContaining([reason]),
}),
});
} else {
await expect(pending).resolves.toMatchObject({
status: "unavailable",
requested: 1,
processed: 0,
items: [],
limitation: expect.stringContaining("1 requested map"),
});
}
expect(cancelled).toBe(true);
expect(read).toBe(false);
},
);
it("charges complete escaped redirect errors and cancels every discarded response", async () => {
const origin = "https://app.example.test";
const location = `http://[${'\\"'.repeat(3_000)}`;
const request = {
scriptKey: "script",
declaredUrl: `${origin}/map`,
fetchUrl: `${origin}/map`,
};
const input = analyzeWebBundleInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page",
allowed_origins: [origin],
fetch_source_maps: true,
});
let calls = 0;
let cancelled = 0;
const host = {
fetch: () => {
calls += 1;
return Promise.resolve(
new Response(
new ReadableStream<Uint8Array>({
cancel() {
cancelled += 1;
},
}),
{ status: 302, headers: { location } },
),
);
},
};
const control = await fetchWebSourceMaps([request], input, undefined, host);
expect(control.items[0]?.limitation).toContain(JSON.stringify(location));
expect(control.items[0]?.limitation?.length).toBeGreaterThan(512);
const result = await fetchWebSourceMaps(
Array.from({ length: 1_500 }, () => request),
input,
undefined,
host,
);
expect(result).toMatchObject({ status: "unavailable", requested: 1_500 });
expect(result.processed).toBe(result.items.length);
expect(result.processed).toBeLessThan(result.requested);
expect(result.limitation).toContain("unknown");
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(
WEB_SOURCE_MAP_LIMITS.outputBytes,
);
expect(calls).toBeLessThan(1_501);
expect(cancelled).toBe(calls);
});
+82
View File
@@ -13,6 +13,7 @@ import { JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE } from "../../../src/contract
import { TOOL_CONTRACTS } from "../../../src/contracts/toolContracts.js";
import { createServer } from "../../../src/server/createServer.js";
import { observed } from "../../fixtures/analysisExecution.js";
import { runCliJson } from "../../fixtures/cliJsonProcess.js";
import {
startFakeCdpBrowser,
type FakeCdpBrowser,
@@ -336,6 +337,87 @@ const assertScreenshotPngFailures = async (client: Client): Promise<void> => {
});
};
it("delivers the source-map response URL through authenticated MCP Evidence", async () => {
const terminalPath = "/maps/v1/app.js.map?revision=2&token=source-value";
const browser = await startFakeCdpBrowser({
sourceMapRedirect: terminalPath,
sourceMapBody: JSON.stringify({
version: 3,
names: [],
sources: ["../src/original.js"],
sourcesContent: ["export const value = 1;"],
mappings: "AAAA",
}),
});
browsers.push(browser);
const connected = await connectBrowser();
const result = await connected.client.callTool({
name: "analyze_web_bundle",
arguments: {
cdp_endpoint: browser.endpoint,
target_id: "allowed-page",
allowed_origins: [browser.allowedOrigin],
observation_ms: 0,
fetch_source_maps: true,
},
});
expect(result.isError).not.toBe(true);
const evidence = parseEvidence(result.structuredContent);
const expectedResult = {
observations: {
source_maps: {
status: "included",
items: [
{
declared_url: `${browser.allowedOrigin}/app.js.map?token=map-secret`,
effective_url: `${browser.allowedOrigin}${terminalPath}`,
original_sources: [
{ source: `${browser.allowedOrigin}/maps/src/original.js` },
],
},
],
},
},
};
expect(evidence.normalized_result).toMatchObject(expectedResult);
expect(result.content).toHaveLength(1);
expect(result.content[0]).toMatchObject({
type: "text",
text: JSON.stringify(evidence),
});
const cliEvidence = parseEvidence(
await runCliJson([
"analyze-web-bundle",
browser.endpoint,
"allowed-page",
"--allowed-origins",
browser.allowedOrigin,
"--observation-ms",
"0",
"--fetch-source-maps",
"--json",
]),
);
expect(cliEvidence.normalized_result).toMatchObject(expectedResult);
const mapRequests = browser.httpRequests.filter(({ url }) =>
url.includes(".map"),
);
expect(mapRequests.map(({ url }) => url)).toEqual([
"/app.js.map?token=map-secret",
terminalPath,
"/app.js.map?token=map-secret",
terminalPath,
]);
expect(
mapRequests.every(
({ authorization, cookie, referer }) =>
authorization === undefined &&
cookie === undefined &&
referer === undefined,
),
).toBe(true);
});
it("does not attach to a target outside the request's allowed origin scope", async () => {
const browser = await startFakeCdpBrowser();
browsers.push(browser);
+25 -8
View File
@@ -1,4 +1,8 @@
import { createServer, type IncomingMessage } from "node:http";
import {
createServer,
type IncomingMessage,
type ServerResponse,
} from "node:http";
import { WebSocket, WebSocketServer } from "ws";
@@ -34,13 +38,7 @@ export const startFakeCdpBrowser = async (
referer: request.headers.referer,
});
response.setHeader("content-type", "application/json");
if (
request.url?.startsWith("/app.js.map") === true &&
options.sourceMapBody !== undefined
) {
response.end(options.sourceMapBody);
return;
}
if (serveSourceMap(request, response, options)) return;
if (request.url === "/json/version") {
response.end(
options.oversizedDiscovery === true
@@ -222,3 +220,22 @@ const replyToCommand = (
if (options.closeAfterMethod === command.method)
setImmediate(() => socket.close());
};
const serveSourceMap = (
request: IncomingMessage,
response: ServerResponse,
options: FakeOptions,
): boolean => {
if (options.sourceMapBody === undefined) return false;
if (request.url?.startsWith("/app.js.map") === true) {
if (options.sourceMapRedirect !== undefined)
response.writeHead(302, { location: options.sourceMapRedirect }).end();
else response.end(options.sourceMapBody);
return true;
}
if (request.url === options.sourceMapRedirect) {
response.end(options.sourceMapBody);
return true;
}
return false;
};
+1
View File
@@ -93,6 +93,7 @@ export interface FakeOptions {
readonly webSocketWithoutFrames?: boolean;
readonly invalidBinaryWebSocketEvent?: boolean;
readonly sourceMapBody?: string;
readonly sourceMapRedirect?: string;
readonly sessionTimeline?:
| "same_origin"
| "outside_policy"