Skip to content

Commit 63f8b84

Browse files
authored
fix(network): guarded redirects survive cleanup rejection (#111156)
* fix(network): preserve redirects when cleanup fails * test(network): bound redirect response assertion * test(network): honor stream cancellation contract
1 parent 84ad63e commit 63f8b84

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎src/agents/tools/web-fetch.ssrf.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function redirectResponse(location: string): Response {
1616
ok: false,
1717
status: 302,
1818
headers: makeFetchHeaders({ location }),
19-
body: { cancel: vi.fn() },
19+
body: { cancel: vi.fn(async () => undefined) },
2020
} as unknown as Response;
2121
}
2222

‎src/infra/net/fetch-guard.ssrf.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,57 @@ describe("fetchWithSsrFGuard hardening", () => {
949949
await result.release();
950950
});
951951

952+
it("preserves redirects when response body cancellation rejects", async () => {
953+
const unhandledRejections: unknown[] = [];
954+
const onUnhandledRejection = (reason: unknown) => {
955+
unhandledRejections.push(reason);
956+
};
957+
const cancel = vi.fn(() => {
958+
throw new Error("redirect cancellation failed");
959+
});
960+
const fetchImpl = vi
961+
.fn()
962+
.mockResolvedValueOnce(
963+
new Response(new ReadableStream<Uint8Array>({ cancel }), {
964+
status: 302,
965+
headers: { location: "https://cdn.example.com/asset" },
966+
}),
967+
)
968+
.mockResolvedValueOnce(okResponse("redirected"));
969+
process.on("unhandledRejection", onUnhandledRejection);
970+
let result: Awaited<ReturnType<typeof fetchWithSsrFGuard>> | undefined;
971+
972+
try {
973+
result = await fetchWithSsrFGuard({
974+
url: "https://api.example.com/start",
975+
fetchImpl,
976+
lookupFn: createPublicLookup(),
977+
});
978+
979+
const reader = result.response.body?.getReader();
980+
if (!reader) {
981+
throw new Error("expected redirected response body");
982+
}
983+
try {
984+
const firstChunk = await reader.read();
985+
expect(firstChunk.done).toBe(false);
986+
expect(new TextDecoder().decode(firstChunk.value)).toBe("redirected");
987+
await expect(reader.read()).resolves.toEqual({ done: true, value: undefined });
988+
} finally {
989+
reader.releaseLock();
990+
}
991+
expect(cancel).toHaveBeenCalledOnce();
992+
await new Promise<void>((resolve) => {
993+
setImmediate(resolve);
994+
});
995+
expect(unhandledRejections).toStrictEqual([]);
996+
} finally {
997+
await result?.release();
998+
process.off("unhandledRejection", onUnhandledRejection);
999+
expect(process.listeners("unhandledRejection")).not.toContain(onUnhandledRejection);
1000+
}
1001+
});
1002+
9521003
it("strips sensitive headers when redirect crosses origins", async () => {
9531004
const lookupFn = createPublicLookup();
9541005
const fetchImpl = vi

‎src/infra/net/fetch-guard.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ async function fetchWithSsrFGuardInternal(
698698
throw new Error("Redirect loop detected");
699699
}
700700
visited.add(nextVisitKey);
701-
void response.body?.cancel();
701+
void response.body?.cancel().catch(() => undefined);
702702
await closeDispatcher(dispatcher);
703703
currentUrl = nextUrl;
704704
continue;

0 commit comments

Comments
 (0)