Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: reject unsafe numeric literals at connector input
Afloat68f36e9e3948
diff --git a/server/connector-arguments.ts b/server/connector-arguments.tsindex 7c06bbe647..6857e40d01 100644--- a/server/connector-arguments.ts+++ b/server/connector-arguments.ts@@ -49,7 +49,7 @@ function decimalValue(text: string): string | null {return `${match[1]}${digits}e${exponent}`;}-function preservesNumericText(text: string, value: number): boolean {+export function preservesNumericText(text: string, value: number): boolean {if (!Number.isFinite(value) || (Number.isInteger(value) && !Number.isSafeInteger(value))) return false;const original = decimalValue(text);return original !== null && original === decimalValue(String(value));diff --git a/server/connector-proxy.test.ts b/server/connector-proxy.test.tsindex 1ad6f8a622..730c986cb2 100644--- a/server/connector-proxy.test.ts+++ b/server/connector-proxy.test.ts@@ -47,6 +47,47 @@ afterEach(async () => {});describe("connector MCP bridge", () => {+ it("rejects unsafe numeric literals before forwarding calls and accepts the next safe request", async () => {+ const calls: unknown[] = [];+ const upstream = await listen((request, response) => {+ let body = "";+ request.on("data", (chunk) => { body += chunk; });+ request.on("end", () => {+ const rpc = JSON.parse(body);+ calls.push(rpc.params.arguments);+ response.writeHead(200, { "content-type": "application/json" });+ response.end(JSON.stringify({ jsonrpc: "2.0", id: rpc.id, result: { content: [{ type: "text", text: "accepted" }] } }));+ });+ });+ const lines = start({ OMB_CONNECTOR_UPSTREAM_URL: upstream });+ const unsafe = [+ '{"id":9007199254740993}',+ '{"id":-9007199254740993}',+ '{"tools":[{"arguments":{"amount":1e400}}]}',+ '{"tools":[{"arguments":{"amount":1e-400}}]}',+ '{"amount":1.0000000000000001}',+ '9007199254740993',+ ];+ for (const [index, args] of unsafe.entries()) {+ const reply = nextJson(lines);+ child!.stdin.write(`{"jsonrpc":"2.0","id":${index},"method":"tools/call","params":{"name":"FIXTURE_NUMBER","arguments":${args}}}\n`);+ const refused = await reply;+ expect(refused.id).toBe(index);+ expect(refused.result.isError).toBe(true);+ expect(refused.result.content[0].text).toMatch(/not sent.*numeric literal/);+ expect(calls).toEqual([]);+ }+ const safe = { id: "9007199254740993", values: [Number.MAX_SAFE_INTEGER, -0.125, 1e-7, 42], code: "1e400" };+ const reply = nextJson(lines);+ child!.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: 10, method: "tools/call", params: { name: "FIXTURE_NUMBER", arguments: safe } })}\n`);+ expect((await reply).result.isError).toBeUndefined();+ expect(calls).toEqual([safe]);+ const metadataReply = nextJson(lines);+ child!.stdin.write('{"jsonrpc":"2.0","id":11,"method":"tools/call","params":{"name":"FIXTURE_NUMBER","arguments":{"value":42},"_meta":{"arguments":{"limit":1e400}}}}\n');+ expect((await metadataReply).result.isError).toBeUndefined();+ expect(calls).toEqual([safe, { value: 42 }]);+ });+it("turns agent connection requests into authenticated chat-card requests", async () => {let received: any = null;const harness = await listen((request, response) => {diff --git a/server/connector-proxy.ts b/server/connector-proxy.tsindex 103d23cbb0..3f81f2504c 100644--- a/server/connector-proxy.ts+++ b/server/connector-proxy.ts@@ -12,7 +12,7 @@ import {withAuthConfigParameter,} from "./connector-advertisement.ts";import { APP_TOOL_NAMES_ENV, APP_TOOLS_ENV, appToolAnswer, appToolCall, appToolEntries, parseAppToolsEnv } from "./connector-app-tools.ts";-import { ARGUMENT_REFUSAL, argumentRefusalNote, fitArguments, fittedArgumentsNote } from "./connector-arguments.ts";+import { ARGUMENT_REFUSAL, argumentRefusalNote, fitArguments, fittedArgumentsNote, preservesNumericText } from "./connector-arguments.ts";import { annotateFailedCall } from "./connector-scope-hint.ts";import { withSignedOutHint } from "./connector-signed-out-hint.ts";import { AUTH_CONFIG_ID } from "../shared/connector-availability.ts";@@ -337,11 +337,30 @@ input.on("line", (line) => {const trimmed = line.trim();if (!trimmed) return;let message: Json;+ const unsafeContainers = new WeakSet<object>();+ const unsafeArgumentOwners = new WeakSet<object>();try {- message = JSON.parse(trimmed) as Json;+ message = JSON.parse(trimmed, function (key: string, value: unknown, context?: { source?: string }) {+ const unsafe = typeof value === "number"+ ? context?.source === undefined || !preservesNumericText(context.source, value)+ : value !== null && typeof value === "object" && unsafeContainers.has(value);+ if (unsafe) {+ unsafeContainers.add(this);+ if (key === "arguments") unsafeArgumentOwners.add(this);+ }+ return value;+ }) as Json;} catch {return;}+ if (message.method === "tools/call" && message.params !== null && typeof message.params === "object"+ && unsafeArgumentOwners.has(message.params)) {+ if (message.id !== undefined) send(textResult(message.id,+ 'This connected-app call was not sent: an argument contains a numeric literal that cannot be preserved. '+ + 'Send precision-sensitive IDs as JSON strings when the tool schema accepts strings, for example '+ + '{"record_id":"9007199254740993"}; otherwise check the original value before retrying.', true));+ return;+ }void handle(message).catch((error) => {if (message.id === undefined) return;const method = String(message.method ?? "");