Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(server): contain body checks and iterate request-sized thread references
Sebastian B1b1131d71377
diff --git a/server/body-thread-guards.e2e.test.ts b/server/body-thread-guards.e2e.test.tsnew file mode 100644index 0000000000..40fd52f6ce--- /dev/null+++ b/server/body-thread-guards.e2e.test.ts@@ -0,0 +1,23 @@+import { expect, it } from "vitest";++import { launchCloudSendFixture } from "../scripts/testing/cloud-send-fixture.ts";+import { hostTimeout } from "./testing/host-timeout.ts";++it("keeps a Team Cloud server alive when a member posts request-sized threadIds arrays", async () => {+ const fixture = await launchCloudSendFixture({ onePasswordToken: "synthetic-route-guard-token",+ team: { id: "team-guards", name: "Guards" } });+ try {+ const team = fixture.team!;+ const member = await team.pair({ email: "[email protected]", role: "member" });+ expect(member.status).toBe(200);+ for (const refs of [{ threadIds: Array<string>(200_000).fill("") }, { envelope: { threadIds: Array<string>(200_000).fill("") } }]) {+ const body = { memberIds: [], ...refs };+ expect(Buffer.byteLength(JSON.stringify(body))).toBeLessThan(1_000_000);+ const response = await team.as(member.token!, "POST", "/api/groups", body);+ expect(response.status, fixture.logs()).toBe(400);+ expect((await team.as(member.token!, "GET", "/api/bots")).status).toBe(200);+ }+ } finally {+ await fixture.close();+ }+}, hostTimeout(120_000));diff --git a/server/bot-visibility.test.ts b/server/bot-visibility.test.tsindex f0d4c6d694..3d7c83c520 100644--- a/server/bot-visibility.test.ts+++ b/server/bot-visibility.test.ts@@ -423,6 +423,13 @@ describe("a Team Cloud's audiences", () => {expect(bodyThreadIds(null)).toEqual([]);});+ it.each([false, true])("reads a request-sized threadIds array, nested: %s", (nested) => {+ const threadIds = Array<string>(200_000).fill("");+ threadIds[199_999] = "private-thread";+ const body = nested ? { envelope: { threadIds } } : { threadIds };+ expect(bodyThreadIds(body)).toEqual(threadIds);+ });+it("a bot sent to a member lists only the conversations that member may see", () => {const ben = new VisibleSet(open, openGroups, BOB, undefined, { of: audienceOf, person: BEN_KEY });const sent = memberBot({ id: "pub", tasks: [{ threadId: "t-pub" }, { threadId: "t-pub-2" }], vpsAlias: "x" }, ben);diff --git a/server/bot-visibility.ts b/server/bot-visibility.tsindex e466567c35..d888fd3d3d 100644--- a/server/bot-visibility.ts+++ b/server/bot-visibility.ts@@ -301,8 +301,11 @@ export function bodyThreadIds(body: unknown, depth = 1): string[] {const ids: string[] = [];for (const [key, value] of Object.entries(body)) {if (typeof value === "string" && THREAD_FIELD.test(key)) ids.push(value);- else if (key === "threadIds" && Array.isArray(value)) ids.push(...value.filter((id): id is string => typeof id === "string"));- else if (depth > 0 && value && typeof value === "object") ids.push(...bodyThreadIds(value, depth - 1));+ else if (key === "threadIds" && Array.isArray(value)) {+ for (const id of value) if (typeof id === "string") ids.push(id);+ } else if (depth > 0 && value && typeof value === "object") {+ for (const id of bodyThreadIds(value, depth - 1)) ids.push(id);+ }}return ids;}diff --git a/server/harness/http.test.ts b/server/harness/http.test.tsindex 9d07345236..e6c35db423 100644--- a/server/harness/http.test.ts+++ b/server/harness/http.test.ts@@ -1,8 +1,9 @@import type { IncomingMessage } from "node:http";+import { EventEmitter } from "node:events";import { Readable } from "node:stream";import { describe, expect, it } from "vitest";-import { readBody } from "./http.ts";+import { onParsedBody, parsedBodyOf, readBody } from "./http.ts";const request = (chunks: Buffer[]) => Readable.from(chunks) as unknown as IncomingMessage;@@ -13,6 +14,18 @@ function splitInsideCharacter(bytes: Buffer, from: number): [Buffer, Buffer] {}describe("readBody", () => {+ it("rejects a throwing body check without letting the exception escape the end callback", async () => {+ const req = new EventEmitter() as IncomingMessage;+ const error = new Error("body check failed");+ onParsedBody(req, () => { throw error; });+ const body = readBody(req);+ const rejected = expect(body).rejects.toBe(error);+ req.emit("data", Buffer.from('{"threadId":"private"}'));+ expect(() => req.emit("end")).not.toThrow();+ await rejected;+ expect(parsedBodyOf(req)).toBeUndefined();+ });+it("keeps a character whole when a chunk boundary splits it", async () => {const text = "مرحبا بالعالم 中文 😀 ".repeat(50);const bytes = Buffer.from(JSON.stringify({ text }));diff --git a/server/harness/http.ts b/server/harness/http.tsindex 98433ef7aa..0c2d58b022 100644--- a/server/harness/http.ts+++ b/server/harness/http.ts@@ -64,11 +64,16 @@ export function readBody(req: IncomingMessage, limit = 1_000_000): Promise<any>} catch {return fail(400, "invalid JSON body");}- const refused = bodyChecks.get(req)?.(body);- if (refused) return fail(refused.status, refused.error);- done = true;- parsedBodies.set(req, body);- resolve(body);+ try {+ const refused = bodyChecks.get(req)?.(body);+ if (refused) return fail(refused.status, refused.error);+ done = true;+ parsedBodies.set(req, body);+ resolve(body);+ } catch (error) {+ done = true;+ reject(error);+ }});req.on("error", (e) => fail(400, e instanceof Error ? e.message : String(e)));});