Skip to content

Commit 30f4f7f

Browse files
cxbAsDevclaude
andcommitted
fix(openrouter): bound OAuth API key response read to prevent OOM
Co-Authored-By: Claude <[email protected]>
1 parent 0ce10d7 commit 30f4f7f

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎extensions/openrouter/oauth.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,42 @@ describe("OpenRouter OAuth", () => {
312312
it("exposes stable auth choice metadata", () => {
313313
expect(OPENROUTER_OAUTH_CHOICE_ID).toBe("openrouter-oauth");
314314
});
315+
316+
it("rejects oversized OpenRouter OAuth API key responses", async () => {
317+
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValueOnce(makeOversizedOAuthJsonResponse());
318+
319+
await expect(
320+
exchangeOpenRouterOAuthCode({
321+
code: "AUTHCODE",
322+
codeVerifier: "verifier-1",
323+
fetchImpl,
324+
}),
325+
).rejects.toThrow("OpenRouter OAuth key response exceeds");
326+
});
315327
});
328+
329+
/**
330+
* Builds a JSON response body larger than the 16 MiB OAuth cap so the bounded
331+
* reader cancels the stream mid-flight; proves oversized API key responses are
332+
* rejected before full buffering.
333+
*/
334+
function makeOversizedOAuthJsonResponse(): Response {
335+
const ONE_MIB = 1024 * 1024;
336+
const TOTAL_CHUNKS = 18;
337+
const chunk = new Uint8Array(ONE_MIB);
338+
let pulled = 0;
339+
const body = new ReadableStream<Uint8Array>({
340+
pull(controller) {
341+
if (pulled >= TOTAL_CHUNKS) {
342+
controller.close();
343+
return;
344+
}
345+
pulled += 1;
346+
controller.enqueue(chunk);
347+
},
348+
});
349+
return new Response(body, {
350+
status: 200,
351+
headers: { "Content-Type": "application/json" },
352+
});
353+
}

‎extensions/openrouter/oauth.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "openclaw/plugin-sdk/provider-auth";
1010
import { generateOAuthState } from "openclaw/plugin-sdk/provider-auth-runtime";
1111
import { readResponseTextLimited } from "openclaw/plugin-sdk/provider-http";
12+
import { readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime";
1213
import { applyOpenrouterConfig, OPENROUTER_DEFAULT_MODEL_REF } from "./onboard.js";
1314

1415
const PROVIDER_ID = "openrouter";
@@ -25,6 +26,7 @@ export const OPENROUTER_OAUTH_CODE_CHALLENGE_METHOD = "S256";
2526
const OPENROUTER_OAUTH_TIMEOUT_MS = 5 * 60 * 1000;
2627
const OPENROUTER_OAUTH_FETCH_TIMEOUT_MS = 30 * 1000;
2728
const OPENROUTER_OAUTH_ERROR_BODY_LIMIT_BYTES = 8 * 1024;
29+
const OPENROUTER_OAUTH_RESPONSE_MAX_BYTES = 16 * 1024 * 1024;
2830
const OPENROUTER_OAUTH_PROFILE_ID = "openrouter:default";
2931

3032
type OpenRouterOAuthCallbackResult = {
@@ -75,7 +77,12 @@ function extractOpenRouterError(value: unknown): string | undefined {
7577

7678
async function readResponseBody(response: Response): Promise<unknown> {
7779
const text = response.ok
78-
? await response.text()
80+
? new TextDecoder().decode(
81+
await readResponseWithLimit(response, OPENROUTER_OAUTH_RESPONSE_MAX_BYTES, {
82+
onOverflow: ({ maxBytes }) =>
83+
new Error(`OpenRouter OAuth key response exceeds ${maxBytes} bytes`),
84+
}),
85+
)
7986
: await readResponseTextLimited(response, OPENROUTER_OAUTH_ERROR_BODY_LIMIT_BYTES).catch(
8087
() => "",
8188
);

0 commit comments

Comments
 (0)