fix(chat): answer 400 for null or non-object entries in tools[] and input[] (#16615)

This PR: a null or non-object entry in tools[], and a null entry in input[], is answered with 400 ("tools: Expected array of objects" / "input: Expected array of objects or strings") instead of a 500. Plain string items in input stay valid. Covered by tests/unit/chat-tools-input-entry-objects.test.ts.

The checks live in admissionRejectionLog.rejectPayload, which chat.ts already calls. chat.ts stays byte-identical to the release tip, at the 2697-line ceiling. The non-object body guard from #16668 is unchanged.

Validated on the contributor branch after that move. tests/unit/chat-tools-input-entry-objects.test.ts (3) and tests/unit/chat-non-object-body.test.ts (12) passed, 15/15. The combined pacocartones board 5a429eab had already passed 82 focused tests; complexity 3038 (baseline 3218) and cognitive 1401 (baseline 1437).

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-15 21:12:21 +02:00
committed by GitHub
parent c4d1f3edab
commit a598d20e78
3 changed files with 94 additions and 2 deletions

View File

@@ -0,0 +1 @@
- **fix(chat):** a `null` (or non-object) entry in `tools[]`, or a `null` entry in `input[]`, made `generateSessionId` and the OpenAI→Claude tool conversion throw a `TypeError` (`Cannot read properties of null (reading 'name')`) and the request ended as a 500. They are now answered with a 400 and a clear message (`tools: Expected array of objects`, `input: Expected array of objects or strings`), in the same place and style as the `messages` entry guard (#12643, #13755). Plain string items in `input` stay valid ([#16615](https://github.com/diegosouzapw/OmniRoute/pull/16615)) — thanks @pacocartones.

View File

@@ -95,13 +95,27 @@ function rejectWith(
}
/**
* 400 before handleChat reads messages off a payload that would throw into a 500.
* Returns the same body when the payload may proceed.
* 400 before handleChat reads messages, tools, or input off a payload that would
* throw into a 500. Returns the same body when the payload may proceed.
*/
export function rejectPayload(body: unknown, request: ChatPayloadRequest, reqId: string): unknown {
if (body === null || typeof body !== "object") {
log.warn("CHAT", "Rejecting request whose body is not a JSON object");
return rejectWith(request, reqId, null, "Request body must be a JSON object");
}
const record = body as { tools?: unknown; input?: unknown };
if (
Array.isArray(record.tools) &&
record.tools.some(
(entry) => entry === null || typeof entry !== "object" || Array.isArray(entry)
)
) {
log.warn("CHAT", "Rejecting request with non-object tools entries");
return rejectWith(request, reqId, body, "tools: Expected array of objects");
}
if (Array.isArray(record.input) && record.input.some((entry) => entry === null)) {
log.warn("CHAT", "Rejecting request with null input entries");
return rejectWith(request, reqId, body, "input: Expected array of objects or strings");
}
return body;
}

View File

@@ -0,0 +1,77 @@
import test from "node:test";
import assert from "node:assert/strict";
import { createChatPipelineHarness } from "../integration/_chatPipelineHarness.ts";
// `tools` and `input` are `z.array(z.unknown())` in the route schema, like `messages`. #12643 made
// a `messages` entry that is not an object answer 400; a `null` entry in `tools` or `input` still
// reached `generateSessionId` and the translators and surfaced as a 500 (a TypeError on `null.name`
// or `null.role`).
const harness = await createChatPipelineHarness("chat-tools-input-entry-objects");
const { handleChat, buildRequest, resetStorage, seedConnection } = harness;
test.beforeEach(async () => {
await resetStorage();
});
test.after(async () => {
await harness.cleanup();
});
async function post(extra: Record<string, unknown>) {
await seedConnection("anthropic", { apiKey: "sk-ant" });
let upstreamCalled = false;
globalThis.fetch = async () => {
upstreamCalled = true;
return new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
};
let status = 0;
let message = "";
try {
const response = await handleChat(
buildRequest({
body: {
model: "anthropic/claude-haiku-4-5",
messages: [{ role: "user", content: "hi" }],
...extra,
},
})
);
status = response.status;
message = ((await response.json()) as { error?: { message?: string } }).error?.message ?? "";
} catch (error) {
// A throw out of handleChat is what the route turns into a 500.
status = 500;
message = error instanceof Error ? error.message : String(error);
}
return { status, message, upstreamCalled };
}
test("tools: [null] is rejected with a clear 400", async () => {
const { status, message, upstreamCalled } = await post({ tools: [null] });
assert.equal(status, 400, `expected 400, got ${status}: ${message}`);
assert.match(message, /tools: Expected array of objects/i);
assert.equal(upstreamCalled, false, "must not forward upstream");
});
test("tools with a string or number entry are rejected with a clear 400", async () => {
for (const tools of [[{ type: "function", function: { name: "f" } }, "oops"], [42]]) {
const { status, message, upstreamCalled } = await post({ tools });
assert.equal(status, 400, `expected 400 for ${JSON.stringify(tools)}, got ${status}: ${message}`);
assert.match(message, /tools: Expected array of objects/i);
assert.equal(upstreamCalled, false);
}
});
test("input: [null] is rejected with a clear 400, plain string items stay valid", async () => {
const bad = await post({ input: [null, { role: "user", content: "hi" }] });
assert.equal(bad.status, 400, `expected 400, got ${bad.status}: ${bad.message}`);
assert.match(bad.message, /input: Expected array of objects or strings/i);
assert.equal(bad.upstreamCalled, false);
const ok = await post({ input: ["plain turn", { role: "user", content: "hi" }] });
assert.notEqual(ok.status, 400, `string items must not be rejected: ${ok.message}`);
});