fix(gamification): answer 400 for malformed JSON on the rotate, servers and federation score routes (#16730)

This PR: POST /api/gamification/rotate, /servers and /federation/score answer 400 "Invalid JSON body" when the body is not valid JSON, the same answer the module's other POST routes already return. Covered by tests/unit/gamification/malformed-json-400.test.ts.

⭐5. It opened after the pacocartones board, and the operator authorized it with the remaining profile PRs. No protected surface. The changelog fragment keeps the contributor's PENDING filename; the bullet is well-formed.

The focused test was not re-run in this squash. The diff is the same try/catch those sibling routes already use.

typecheck:core is red only on lines that already exist on origin/release/v3.8.52 (open-sse/translator/request/openai-to-claude.ts and open-sse/utils/dsmlToolCalls.ts are byte-identical; the never-typed pushes in response/openai-to-claude.ts sit outside this batch's hunks).
open-sse typecheck reports 3 errors in files byte-identical to origin/release/v3.8.52 (copilotAutoModel.ts TS2339 x2, request/openai-responses.ts TS2345 x1). The baseline was not widened.

⚠️ base-red inherited: #15306

Thanks @pacocartones.
This commit is contained in:
Paco Cartones
2026-10-16 04:13:23 +02:00
committed by GitHub
parent a0cdb10b04
commit ad37e8f8f5
5 changed files with 80 additions and 3 deletions

View File

@@ -0,0 +1 @@
- **fix(gamification):** `POST /api/gamification/rotate`, `POST /api/gamification/servers` and `POST /api/gamification/federation/score` called `request.json()` outside a `try`, so a body that is not valid JSON threw an unhandled `SyntaxError` instead of answering 400; they now answer 400 `Invalid JSON body`, as the other routes do.

View File

@@ -34,7 +34,15 @@ export async function POST(request: NextRequest) {
);
}
const body = await request.json();
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json(
{ error: "Invalid JSON body" },
{ status: 400, headers: CORS_HEADERS }
);
}
const schema = z.object({
apiKeyId: z.string(),
score: z.number(),

View File

@@ -15,7 +15,15 @@ export async function POST(request: NextRequest) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
const body = await request.json();
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json(
{ error: "Invalid JSON body" },
{ status: 400, headers: CORS_HEADERS }
);
}
const schema = z.object({
scope: z.enum(["weekly", "monthly"]),
});

View File

@@ -26,7 +26,15 @@ export async function POST(request: NextRequest) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
const body = await request.json();
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json(
{ error: "Invalid JSON body" },
{ status: 400, headers: CORS_HEADERS }
);
}
const schema = z.object({
name: z.string().min(1),
url: z.string().url(),

View File

@@ -0,0 +1,52 @@
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-gam-json-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET ?? "gam-test-secret";
delete process.env.INITIAL_PASSWORD;
const core = await import("../../../src/lib/db/core.ts");
const rotateRoute = await import("../../../src/app/api/gamification/rotate/route.ts");
const serversRoute = await import("../../../src/app/api/gamification/servers/route.ts");
const scoreRoute = await import("../../../src/app/api/gamification/federation/score/route.ts");
const servers = await import("../../../src/lib/gamification/servers.ts");
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
const malformed = (url: string, headers: Record<string, string> = {}) =>
new Request(url, {
method: "POST",
headers: { "content-type": "application/json", ...headers },
body: "not-json",
});
test("rotate: malformed JSON answers 400", async () => {
const res = await rotateRoute.POST(
malformed("http://localhost/api/gamification/rotate") as never
);
assert.equal(res.status, 400);
});
test("servers POST: malformed JSON answers 400", async () => {
const res = await serversRoute.POST(
malformed("http://localhost/api/gamification/servers") as never
);
assert.equal(res.status, 400);
});
test("federation score: malformed JSON answers 400", async () => {
await servers.connectServer("peer", "https://peer.example", "peer-secret");
const res = await scoreRoute.POST(
malformed("http://localhost/api/gamification/federation/score", {
authorization: "Bearer peer-secret",
}) as never
);
assert.equal(res.status, 400);
});