Skip to content

Commit e6467e3

Browse files
committed
fix(api): reject non-object pool-strategy bodies
Return 400 for null/array/scalar JSON instead of throwing 500, and use POOL_KEY_ANTHROPIC in the sticky-release test.
1 parent a96d705 commit e6467e3

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎src/codex/auth-api.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,12 @@ export async function handleCodexAuthAPI(
645645
url.pathname === "/api/codex-auth/pool-strategy"
646646
&& (req.method === "PUT" || req.method === "PATCH")
647647
) {
648-
let body: { strategy?: unknown; stickyLimit?: unknown };
649-
try { body = (await req.json()) as typeof body; } catch { return jsonResponse({ error: "Invalid JSON" }, 400); }
648+
let parsedBody: unknown;
649+
try { parsedBody = await req.json(); } catch { return jsonResponse({ error: "Invalid JSON" }, 400); }
650+
if (typeof parsedBody !== "object" || parsedBody === null || Array.isArray(parsedBody)) {
651+
return jsonResponse({ error: "body must be an object" }, 400);
652+
}
653+
const body = parsedBody as { strategy?: unknown; stickyLimit?: unknown };
650654
if (body.strategy === undefined && body.stickyLimit === undefined) {
651655
return jsonResponse({ error: "strategy or stickyLimit required" }, 400);
652656
}

‎src/server/management/oauth-account-routes.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
242242
});
243243
}
244244
if (url.pathname === "/api/oauth/accounts/pool" && (req.method === "PUT" || req.method === "PATCH")) {
245-
const body = await req.json().catch(() => ({})) as {
245+
const parsedBody = await req.json().catch(() => ({}));
246+
if (!isPlainRecord(parsedBody)) {
247+
return jsonResponse({ error: "body must be an object" }, 400);
248+
}
249+
const body = parsedBody as {
246250
provider?: unknown;
247251
enabled?: unknown;
248252
autoSwitchThreshold?: unknown;

‎tests/account-pool-management-api.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ describe("Codex account pool strategy management API", () => {
123123
expect(config.accountPoolStrategy).toBe("quota");
124124
expect(config.accountPoolStickyLimit).toBe(1);
125125
});
126+
127+
test("PUT /api/codex-auth/pool-strategy rejects non-object JSON bodies with 400", async () => {
128+
for (const raw of ["null", "[]", "\"round-robin\"", "1"]) {
129+
const req = new Request("http://localhost/api/codex-auth/pool-strategy", {
130+
method: "PUT",
131+
headers: { "Content-Type": "application/json" },
132+
body: raw,
133+
});
134+
const resp = await handleCodexAuthAPI(req, new URL(req.url), makeCodexConfig());
135+
expect(resp!.status).toBe(400);
136+
expect(await resp!.json()).toMatchObject({ error: "body must be an object" });
137+
}
138+
});
126139
});
127140
describe("Anthropic account pool strategy management API", () => {
128141
let testDir = "";
@@ -196,6 +209,23 @@ describe("Anthropic account pool strategy management API", () => {
196209
}
197210
});
198211

212+
test("PUT /api/oauth/accounts/pool rejects non-object JSON bodies with 400", async () => {
213+
const server = startServer(0);
214+
try {
215+
for (const raw of ["null", "[]", "\"round-robin\""]) {
216+
const res = await fetch(new URL("/api/oauth/accounts/pool", server.url), {
217+
method: "PUT",
218+
headers: { "Content-Type": "application/json" },
219+
body: raw,
220+
});
221+
expect(res.status).toBe(400);
222+
expect(await res.json()).toMatchObject({ error: "body must be an object" });
223+
}
224+
} finally {
225+
await server.stop(true);
226+
}
227+
});
228+
199229
test("PUT /api/oauth/accounts/pool rejects invalid stickyLimit", async () => {
200230
const server = startServer(0);
201231
try {

‎tests/anthropic-account-pool.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
22
import { mkdtempSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
5-
import { clearPoolRotationState, notePoolRotationFailure } from "../src/codex/pool-rotation";
5+
import { clearPoolRotationState, notePoolRotationFailure, POOL_KEY_ANTHROPIC } from "../src/codex/pool-rotation";
66
import {
77
anthropicSessionKeyFromParts,
88
bindAnthropicSessionAffinity,
@@ -303,7 +303,7 @@ describe("anthropic account pool", () => {
303303
const sticky = resolveAnthropicAccountForSession("sticky-1", config).accountId!;
304304
expect(resolveAnthropicAccountForSession("sticky-2", config).accountId).toBe(sticky);
305305

306-
notePoolRotationFailure("anthropic", sticky);
306+
notePoolRotationFailure(POOL_KEY_ANTHROPIC, sticky);
307307
const afterClear = resolveAnthropicAccountForSession("sticky-3", config).accountId;
308308
expect(afterClear).toBeTruthy();
309309
expect(afterClear).not.toBe(sticky);

0 commit comments

Comments
 (0)