Skip to content

Commit 6fc0303

Browse files
committed
fix(chutes): bound oauth token error bodies
1 parent 6ef4684 commit 6fc0303

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

‎extensions/chutes/oauth.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22
import { describe, expect, it, vi } from "vitest";
33
import { loginChutes } from "./oauth.js";
44

5+
function boundedErrorResponse(body: string, status = 500): {
6+
response: Response;
7+
cancel: ReturnType<typeof vi.fn>;
8+
releaseLock: ReturnType<typeof vi.fn>;
9+
text: ReturnType<typeof vi.fn>;
10+
} {
11+
const encoded = new TextEncoder().encode(body);
12+
let read = false;
13+
const cancel = vi.fn(async () => undefined);
14+
const releaseLock = vi.fn();
15+
const text = vi.fn(async () => {
16+
throw new Error("response.text() should not be called");
17+
});
18+
const response = {
19+
ok: false,
20+
status,
21+
headers: new Headers(),
22+
body: {
23+
getReader: () => ({
24+
read: async () => {
25+
if (read) {
26+
return { done: true, value: undefined };
27+
}
28+
read = true;
29+
return { done: false, value: encoded };
30+
},
31+
cancel,
32+
releaseLock,
33+
}),
34+
},
35+
text,
36+
} as unknown as Response;
37+
38+
return { response, cancel, releaseLock, text };
39+
}
40+
541
describe("chutes plugin OAuth", () => {
642
it("rejects unsafe token lifetimes before storing credentials", async () => {
743
const fetchFn = vi.fn(async (input: RequestInfo | URL) => {
@@ -33,4 +69,47 @@ describe("chutes plugin OAuth", () => {
3369
}),
3470
).rejects.toThrow("Chutes token exchange returned invalid expires_in");
3571
});
72+
73+
it("bounds token exchange error bodies without requiring response.text()", async () => {
74+
const errorResponse = boundedErrorResponse(
75+
`${"chutes token unavailable ".repeat(1024)}tail-marker`,
76+
502,
77+
);
78+
const fetchFn = vi.fn(async (input: RequestInfo | URL) => {
79+
const url =
80+
typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
81+
if (url === "https://api.chutes.ai/idp/token") {
82+
return errorResponse.response;
83+
}
84+
return new Response("not found", { status: 404 });
85+
});
86+
87+
let error: unknown;
88+
try {
89+
await loginChutes({
90+
app: {
91+
clientId: "cid_test",
92+
redirectUri: "http://127.0.0.1:1456/oauth-callback",
93+
scopes: ["openid"],
94+
},
95+
manual: true,
96+
createState: () => "state_test",
97+
onAuth: vi.fn(async () => {}),
98+
onPrompt: vi.fn(
99+
async () => "http://127.0.0.1:1456/oauth-callback?code=code_test&state=state_test",
100+
),
101+
fetchFn,
102+
});
103+
} catch (caught) {
104+
error = caught;
105+
}
106+
107+
expect(error).toBeInstanceOf(Error);
108+
const message = (error as Error).message;
109+
expect(message).toContain("Chutes token exchange failed: chutes token unavailable");
110+
expect(message).not.toContain("tail-marker");
111+
expect(errorResponse.text).not.toHaveBeenCalled();
112+
expect(errorResponse.cancel).toHaveBeenCalledTimes(1);
113+
expect(errorResponse.releaseLock).toHaveBeenCalledTimes(1);
114+
});
36115
});

‎extensions/chutes/oauth.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import {
88
parseOAuthCallbackInput,
99
waitForLocalOAuthCallback,
1010
} from "openclaw/plugin-sdk/provider-auth-runtime";
11+
import { readResponseTextLimited } from "openclaw/plugin-sdk/provider-http";
1112
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
1213

1314
const CHUTES_AUTHORIZE_ENDPOINT = "https://api.chutes.ai/idp/authorize";
1415
const CHUTES_TOKEN_ENDPOINT = "https://api.chutes.ai/idp/token";
1516
const CHUTES_USERINFO_ENDPOINT = "https://api.chutes.ai/idp/userinfo";
17+
const CHUTES_TOKEN_ERROR_BODY_LIMIT_BYTES = 8 * 1024;
1618

1719
type OAuthPrompt = {
1820
message: string;
@@ -152,7 +154,11 @@ async function exchangeChutesCodeForTokens(params: {
152154
body,
153155
});
154156
if (!response.ok) {
155-
throw new Error(`Chutes token exchange failed: ${await response.text()}`);
157+
const detail = await readResponseTextLimited(
158+
response,
159+
CHUTES_TOKEN_ERROR_BODY_LIMIT_BYTES,
160+
).catch(() => "");
161+
throw new Error(`Chutes token exchange failed: ${detail}`);
156162
}
157163

158164
const data = (await response.json()) as {

0 commit comments

Comments
 (0)