Skip to content

Commit 68d9d9b

Browse files
fix(chutes): cancel userinfo error response body before returning null
1 parent 19b5e56 commit 68d9d9b

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎extensions/chutes/oauth.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,47 @@ describe("chutes plugin OAuth", () => {
270270
expect(timeoutSpy).toHaveBeenCalledTimes(2);
271271
});
272272

273+
it("cancels the userinfo error response body when profile lookup fails", async () => {
274+
let canceled = false;
275+
let bytesPulled = 0;
276+
const userInfoResponse = new Response(
277+
new ReadableStream<Uint8Array>({
278+
pull(controller) {
279+
if (bytesPulled > 0) {
280+
controller.close();
281+
return;
282+
}
283+
bytesPulled += 1;
284+
controller.enqueue(new TextEncoder().encode("temporarily unavailable"));
285+
},
286+
cancel() {
287+
canceled = true;
288+
},
289+
}),
290+
{ status: 503 },
291+
);
292+
const fetchFn = vi.fn(async (input: RequestInfo | URL) => {
293+
const url = fetchInputUrl(input);
294+
if (url === CHUTES_TOKEN_ENDPOINT) {
295+
return new Response(
296+
'{"access_token":"at_123","refresh_token":"rt_123","expires_in":3600}',
297+
{ status: 200, headers: { "Content-Type": "application/json" } },
298+
);
299+
}
300+
if (url === CHUTES_USERINFO_ENDPOINT) {
301+
return userInfoResponse;
302+
}
303+
return new Response("not found", { status: 404 });
304+
});
305+
306+
const credentials = await loginWithFetch(fetchFn);
307+
308+
expect(canceled).toBe(true);
309+
expect(credentials.access).toBe("at_123");
310+
expect(credentials.email).toBeUndefined();
311+
expect(credentials.accountId).toBeUndefined();
312+
});
313+
273314
it("cancels authentication when the caller aborts during userinfo", async () => {
274315
const controller = new AbortController();
275316
const reason = new Error("cancelled by caller");

‎extensions/chutes/oauth.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ async function fetchChutesUserInfo(params: {
129129
}),
130130
});
131131
if (!response.ok) {
132+
// Release the connection instead of leaving the error body to idle timeout.
133+
await response.body?.cancel().catch(() => undefined);
132134
return null;
133135
}
134136
const data = await readProviderJsonResponse<unknown>(response, "Chutes userinfo");

0 commit comments

Comments
 (0)