fix(browser): bound source-map fetches and retain predecessor requests

This commit is contained in:
morluto
2026-10-09 00:49:30 +08:00
parent 135d102f9d
commit eaf5734083
7 changed files with 406 additions and 21 deletions
@@ -22,7 +22,7 @@ const inspectNetwork = async (options: FakeOptions) => {
return { browser, inspection: result.value };
};
describe("CdpBrowserProvider: redirect errors", () => {
describe("CdpBrowserProvider: redirect errors: redirect response validation", () => {
it.each([
{ label: "missing", url: undefined, reason: "invalid_protocol_value" },
{ label: "malformed", url: "http://%", reason: "invalid_protocol_value" },
@@ -113,6 +113,41 @@ describe("CdpBrowserProvider: redirect errors", () => {
);
},
);
});
describe("CdpBrowserProvider: redirect errors: preserving authorized predecessor evidence", () => {
it("preserves predecessor evidence when a continuation omits redirectResponse", async () => {
const { browser, inspection } = await inspectNetwork({
malformedRedirectResponse: true,
omitRedirectResponse: true,
});
expect(inspection.network.requests).toHaveLength(1);
expect(inspection.network.requests[0]).toMatchObject({
request_id: "request-1",
url: `${browser.allowedOrigin}/malformed-redirect-prior`,
status: 201,
redirects: [
{
url: `${browser.allowedOrigin}/api?token=network-secret`,
response_url: `${browser.allowedOrigin}/api?token=network-secret`,
},
],
body_shapes: { status: "partial", response: null },
});
expect(inspection.metadata.responses).toHaveLength(1);
expect(inspection.completeness.unavailable_sections).toContain(
"network_requests",
);
expect(inspection.completeness.excluded).toContainEqual({
section: "network_requests",
reason: "invalid_protocol_value",
count: expect.any(Number),
});
expect(JSON.stringify(inspection)).not.toContain(
"malformed-redirect-final",
);
});
it("keeps authorized prior evidence when the later response URL is disallowed", async () => {
const { browser, inspection } = await inspectNetwork({
@@ -100,7 +100,7 @@ describe("source-map redirect URL resolution", () => {
);
});
describe("web source-map fetching and validation", () => {
describe("web source-map fetching and validation: fetching maps and following redirects", () => {
it("fetches without credentials and derives mappings and original modules", async () => {
const calls: Array<{ url: string; init: RequestInit | undefined }> = [];
const map = JSON.stringify({
@@ -216,6 +216,155 @@ describe("web source-map fetching and validation", () => {
expect(calls).toHaveLength(8);
expect(result.items[0]?.status).toBe("included");
});
});
describe("web source-map fetching and validation: enforcing fetch limits and validating responses", () => {
it("settles an endless unique-URL redirect chain at the operation deadline", async () => {
let calls = 0;
const server = createServer((incoming, response) => {
calls += 1;
const next =
Number(
new URL(incoming.url ?? "/", "http://local").searchParams.get("n") ??
"0",
) + 1;
response.writeHead(302, { location: `/map?n=${String(next)}` }).end();
});
try {
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
const address = server.address();
if (address === null || typeof address === "string")
throw new TypeError("Expected a TCP listener address");
const localOrigin = `http://127.0.0.1:${String(address.port)}`;
const url = `${localOrigin}/map?n=0`;
const result = await fetchWebSourceMaps(
[{ ...request, fetchUrl: url, declaredUrl: url }],
input({ allowed_origins: [localOrigin] }),
undefined,
{ fetch, timeoutMs: 100 },
);
expect(calls).toBeGreaterThan(1);
expect(result).toMatchObject({
status: "unavailable",
processed: 1,
items: [
{
status: "fetch_failed",
artifact: null,
limitation: "Source-map fetching exceeded its operation deadline.",
},
],
});
} finally {
server.closeAllConnections();
if (server.listening)
await new Promise<void>((resolve, reject) => {
server.close((error) =>
error === undefined ? resolve() : reject(error),
);
});
}
});
it("cancels a streamed response when its retained-byte budget is exceeded", async () => {
let responseClosed = false;
let markResponseClosed: (() => void) | undefined;
const responseClosedPromise = new Promise<void>((resolve) => {
markResponseClosed = resolve;
});
const server = createServer((_incoming, response) => {
response.on("close", () => {
responseClosed = true;
markResponseClosed?.();
});
response.writeHead(200, { "content-type": "application/json" });
response.write("x".repeat(1_024));
const interval = setInterval(() => response.write("x".repeat(1_024)), 2);
response.on("close", () => clearInterval(interval));
});
try {
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
const address = server.address();
if (address === null || typeof address === "string")
throw new TypeError("Expected a TCP listener address");
const localOrigin = `http://127.0.0.1:${String(address.port)}`;
const url = `${localOrigin}/map`;
const result = await fetchWebSourceMaps(
[{ ...request, fetchUrl: url, declaredUrl: url }],
input({ allowed_origins: [localOrigin] }),
undefined,
{ fetch, maxResponseBytes: 512 },
);
let timeout: ReturnType<typeof setTimeout> | undefined;
const closeConfirmed = await Promise.race([
responseClosedPromise.then(() => true),
new Promise<boolean>((resolve) => {
timeout = setTimeout(() => resolve(false), 500);
}),
]);
if (timeout !== undefined) clearTimeout(timeout);
expect(closeConfirmed).toBe(true);
expect(responseClosed).toBe(true);
expect(result).toMatchObject({
status: "unavailable",
items: [
{
status: "fetch_failed",
artifact: null,
limitation:
"Source-map response exceeded the retained-byte budget.",
},
],
});
} finally {
server.closeAllConnections();
if (server.listening)
await new Promise<void>((resolve, reject) => {
server.close((error) =>
error === undefined ? resolve() : reject(error),
);
});
}
});
it("cancels a pending response reader on caller abort", async () => {
let markPullStarted: (() => void) | undefined;
const pullStarted = new Promise<void>((resolve) => {
markPullStarted = resolve;
});
let markCancelled: (() => void) | undefined;
const cancelled = new Promise<void>((resolve) => {
markCancelled = resolve;
});
const body = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new TextEncoder().encode("{"));
},
pull() {
markPullStarted?.();
},
cancel() {
markCancelled?.();
},
});
const controller = new AbortController();
const abortReason = new Error("caller stopped source-map fetch");
const pending = fetchWebSourceMaps([request], input(), controller.signal, {
fetch: async () => new Response(body),
});
await pullStarted;
controller.abort(abortReason);
await expect(pending).rejects.toBe(abortReason);
await cancelled;
});
it("reports malformed source-map JSON as invalid", async () => {
const result = await fetchWebSourceMaps([request], input(), undefined, {
+1 -1
View File
@@ -403,7 +403,7 @@ const emitMalformedRedirect = (
requestId: "request-1",
type: "Fetch",
request: { url: finalUrl, method: "GET" },
redirectResponse,
...(options.omitRedirectResponse === true ? {} : { redirectResponse }),
timestamp: 9,
});
return finalUrl;
+1
View File
@@ -78,6 +78,7 @@ export interface FakeOptions {
readonly redirectFromDisallowedOrigin?: boolean;
readonly redirectWithinOrigin?: boolean;
readonly malformedRedirectResponse?: boolean;
readonly omitRedirectResponse?: boolean;
readonly redirectResponseUrl?: string;
readonly redirectResponseEnvelope?: unknown;
readonly responseAfterMalformedUrl?: string;