Skip to content

Commit 6467c19

Browse files
committed
fix(chutes): cancel userinfo error bodies
1 parent 0c565f3 commit 6467c19

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎src/agents/chutes-oauth.flow.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** Tests Chutes OAuth token exchange and refresh HTTP flows. */
2-
import { describe, expect, it } from "vitest";
2+
import { describe, expect, it, vi } from "vitest";
33
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
44
import {
55
CHUTES_TOKEN_ENDPOINT,
@@ -116,6 +116,45 @@ describe("chutes-oauth", () => {
116116
).rejects.toThrow("Chutes token exchange returned invalid expires_in");
117117
});
118118

119+
it("cancels failed userinfo response bodies during token exchange", async () => {
120+
const userInfoResponse = new Response("temporarily unavailable", { status: 503 });
121+
const cancel = vi.spyOn(userInfoResponse.body!, "cancel").mockResolvedValue(undefined);
122+
const fetchFn = withFetchPreconnect(async (input: RequestInfo | URL) => {
123+
const url = urlToString(input);
124+
if (url === CHUTES_TOKEN_ENDPOINT) {
125+
return new Response(
126+
JSON.stringify({
127+
access_token: "at_123",
128+
refresh_token: "rt_123",
129+
expires_in: 3600,
130+
}),
131+
{ status: 200, headers: { "Content-Type": "application/json" } },
132+
);
133+
}
134+
if (url === CHUTES_USERINFO_ENDPOINT) {
135+
return userInfoResponse;
136+
}
137+
return new Response("not found", { status: 404 });
138+
});
139+
140+
const creds = await exchangeChutesCodeForTokens({
141+
app: {
142+
clientId: "cid_test",
143+
redirectUri: "http://127.0.0.1:1456/oauth-callback",
144+
scopes: ["openid"],
145+
},
146+
code: "code_123",
147+
codeVerifier: "verifier_123",
148+
fetchFn,
149+
now: 1_000_000,
150+
});
151+
152+
expect(cancel).toHaveBeenCalledOnce();
153+
expect(creds.access).toBe("at_123");
154+
expect(creds.email).toBeUndefined();
155+
expect((creds as unknown as { accountId?: string }).accountId).toBeUndefined();
156+
});
157+
119158
it("refreshes tokens using stored client id and falls back to old refresh token", async () => {
120159
const fetchFn = withFetchPreconnect(async (input: RequestInfo | URL, init?: RequestInit) => {
121160
const url = urlToString(input);

‎src/agents/chutes-oauth.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ function resolveChutesExpiresAt(value: unknown, now: number): number | undefined
9797
});
9898
}
9999

100+
async function cancelUnreadResponseBody(response: Response): Promise<void> {
101+
if (!response.bodyUsed) {
102+
await response.body?.cancel().catch(() => undefined);
103+
}
104+
}
105+
100106
async function fetchChutesUserInfo(params: {
101107
accessToken: string;
102108
fetchFn?: typeof fetch;
@@ -106,6 +112,7 @@ async function fetchChutesUserInfo(params: {
106112
headers: { Authorization: `Bearer ${params.accessToken}` },
107113
});
108114
if (!response.ok) {
115+
await cancelUnreadResponseBody(response);
109116
return null;
110117
}
111118
const data = (await response.json()) as unknown;

0 commit comments

Comments
 (0)