Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(openai-chat): mask secrets in rejected tool arguments before cutting the quote

AdityaUmalef19e689d34d6
diff --git a/server/drivers/openai-chat-protocol.test.ts b/server/drivers/openai-chat-protocol.test.ts
index 3b72b158cd..5cd6df33ea 100644
--- a/server/drivers/openai-chat-protocol.test.ts
+++ b/server/drivers/openai-chat-protocol.test.ts
@@ -239,4 +239,14 @@ describe("rejected tool arguments", () => {
expect(message.endsWith("xxEND")).toBe(true);
expect(message.length).toBeLessThan(1_200);
});
+
+ it("masks a secret before cutting, so one that crosses the cut never shows in part", () => {
+ const secret = "SYNTHETIC-SECRET-0123456789";
+ const text = `{"content":"${"x".repeat(480)}${secret}${"y".repeat(1_000)}`;
+ let message = "";
+ try { toolArguments(text, (raw) => raw.split(secret).join("[redacted]")); } catch (error) { message = (error as Error).message; }
+ expect(message.startsWith(`tool arguments are not complete JSON; you sent ${text.length} characters: `)).toBe(true);
+ expect(message).not.toContain("SYNTH");
+ expect(message).toContain("[red");
+ });
});
diff --git a/server/drivers/openai-chat-protocol.ts b/server/drivers/openai-chat-protocol.ts
index 568abbc790..d9808e2df3 100644
--- a/server/drivers/openai-chat-protocol.ts
+++ b/server/drivers/openai-chat-protocol.ts
@@ -24,13 +24,14 @@ export function object(value: unknown): Record<string, unknown> | undefined {
? value as Record<string, unknown> : undefined;
}
-/** A call's arguments as its tool gets them. An empty argument string is a tool with no arguments. */
-export function toolArguments(text: string): Record<string, unknown> {
+/** A call's arguments as its tool gets them. An empty argument string is a tool with no arguments. `redact` masks
+ * secrets in rejected arguments before they are cut down to quote, so a secret split by the cut can't slip past it. */
+export function toolArguments(text: string, redact: (text: string) => string = (same) => same): Record<string, unknown> {
let args: unknown;
try { args = JSON.parse(text.trim() || "{}"); }
- catch { throw new ChatProtocolError(`tool arguments are not complete JSON; you sent ${text.length} characters: ${shownArguments(text)}`); }
+ catch { throw new ChatProtocolError(`tool arguments are not complete JSON; you sent ${text.length} characters: ${shownArguments(redact(text))}`); }
const found = object(args);
- if (!found) throw new ChatProtocolError(`tool arguments must be a JSON object; you sent ${text.length} characters: ${shownArguments(text)}`);
+ if (!found) throw new ChatProtocolError(`tool arguments must be a JSON object; you sent ${text.length} characters: ${shownArguments(redact(text))}`);
return found;
}
diff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.ts
index 23a2f4ca4c..d5eff3a2e0 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -441,6 +441,9 @@ describe("createOpenAIChatRuntime tool approvals", () => {
["empty", "", "{}", undefined],
["blank", " ", "{}", undefined],
["led by a byte-order mark", '\uFEFF{"n":1}', '{"n":1}', undefined],
+ // The API key ("synthetic") crosses the 500-character cut: it is masked first, so no part of it is quoted back.
+ ["long and holding the API key across the cut", `{"content":"${"x".repeat(484)}synthetic${"y".repeat(1_000)}`, "{}",
+ `tool arguments are not complete JSON; you sent 1505 characters: {"content":"${"x".repeat(484)}[red … ${"y".repeat(500)}`],
])("replays a call whose arguments are %s as valid JSON, so a strict provider takes the next request", async (_kind, badArguments, replayedArguments, toolError) => {
const call = (id: string, argumentsText: string) =>
`data: ${JSON.stringify({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id, type: "function", function: { name: "fx_write", arguments: argumentsText } }] } }] })}\n\n`
diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.ts
index aaf86a7d0e..fd387fd068 100644
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@ -798,7 +798,7 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
let started = false;
let fatal: Error | undefined, title = call.function.name, skipRepeatCount = false;
try {
- const args = toolArguments(call.function.arguments);
+ const args = toolArguments(call.function.arguments, safeText);
const inputPreview = preview(args);
if (call.function.name === ASK_USER_TOOL) {
if (!questionAllowed || !allowsTool(scope, { kind: "native", name: ASK_USER_TOOL })) throw new Error("Tool selection excludes this tool");