Skip to content

Commit b98b8e7

Browse files
fix(googlechat): cancel non-OK cert fetch response body before throwing (#109519)
* fix(googlechat): cancel non-OK cert fetch response body before throwing When fetchChatCerts receives a non-OK response from the Google Chat cert endpoint, it throws without cancelling the response body. Cancel the body before throwing, matching the pattern established in reef guard adapters (#109196) and fetchOpenRouterModels. * test(googlechat): cover cert response cancellation Co-authored-by: 毛宇豪0668001457 <[email protected]> * test(googlechat): satisfy strict call-order types Co-authored-by: 毛宇豪0668001457 <[email protected]> --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent e7c389b commit b98b8e7

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎extensions/googlechat/src/auth.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ async function fetchChatCerts(): Promise<Record<string, string>> {
120120
});
121121
try {
122122
if (!response.ok) {
123+
await response.body?.cancel().catch(() => undefined);
123124
throw new Error(`Failed to fetch Chat certs (${response.status})`);
124125
}
125126
const certs = await readGoogleChatCertsResponse(response);

‎extensions/googlechat/src/targets.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,40 @@ describe("verifyGoogleChatRequest", () => {
720720
expect(release).toHaveBeenCalledOnce();
721721
});
722722

723+
it("cancels a rejected Chat cert response before releasing the guard", async () => {
724+
expireGoogleChatCertCache();
725+
const cancel = vi.fn().mockResolvedValue(undefined);
726+
const release = vi.fn().mockResolvedValue(undefined);
727+
mocks.fetchWithSsrFGuard.mockResolvedValueOnce({
728+
response: {
729+
ok: false,
730+
status: 503,
731+
body: { cancel },
732+
} as unknown as Response,
733+
release,
734+
});
735+
736+
await expect(
737+
verifyGoogleChatRequest({
738+
bearer: "token",
739+
audienceType: "project-number",
740+
audience: "123456789",
741+
}),
742+
).resolves.toEqual({
743+
ok: false,
744+
reason: "Failed to fetch Chat certs (503)",
745+
});
746+
747+
expect(cancel).toHaveBeenCalledOnce();
748+
expect(release).toHaveBeenCalledOnce();
749+
const cancelOrder = cancel.mock.invocationCallOrder[0];
750+
const releaseOrder = release.mock.invocationCallOrder[0];
751+
if (cancelOrder === undefined || releaseOrder === undefined) {
752+
throw new Error("expected cancellation and guard release call-order records");
753+
}
754+
expect(cancelOrder).toBeLessThan(releaseOrder);
755+
});
756+
723757
it("reports malformed Chat cert JSON with a stable auth error", async () => {
724758
expireGoogleChatCertCache();
725759
const release = vi.fn(async () => {});

0 commit comments

Comments
 (0)