Skip to content

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

fix(team): check current bot ownership after reading handoff bodies

Sebastian B3c04a89d9197
diff --git a/server/routes/team-owner-handoff.test.ts b/server/routes/team-owner-handoff.test.ts
new file mode 100644
index 0000000000..0308e561af
--- /dev/null
+++ b/server/routes/team-owner-handoff.test.ts
@@ -0,0 +1,47 @@
+import type { IncomingMessage, ServerResponse } from "node:http";
+import { describe, expect, it } from "vitest";
+
+import { DATA_DIR } from "../config.ts";
+import type { RequestAuth } from "../request-auth.ts";
+import { Store } from "../store.ts";
+import { createTeamPeople } from "../team-people.ts";
+import { createTeamOwnerRoutes } from "./team-owners.ts";
+
+const BOB = "[email protected]", ADA = "[email protected]";
+const auth: RequestAuth = { kind: "session", scopes: ["client"], via: "bearer", session: {
+ id: "bob-session", tokenHash: "0".repeat(64), label: "Bob's desktop", createdAt: 1, lastSeenAt: 1, expiresAt: 9_000,
+ email: BOB, teamRole: "member", membershipAuthority: "team", scopes: ["client"],
+} };
+
+describe("a handoff while the previous owner's request body is held", () => {
+ it.each([["owner", 403], ["visibility", 404], ["personal", 409]] as const)("rechecks the bot's current %s after reading the body", async (change, expectedStatus) => {
+ const store = new Store(() => ({ instanceId: "claude", model: "sonnet" }));
+ const bot = store.createBot({ name: "Scout", createdBy: BOB });
+ const people = createTeamPeople(DATA_DIR);
+ people.seen(BOB, "member");
+ people.seen(ADA, "admin");
+ let visible = true;
+ let release!: (body: unknown) => void;
+ let reading!: () => void;
+ const started = new Promise<void>((resolve) => { reading = resolve; });
+ const body = new Promise<unknown>((resolve) => { release = resolve; });
+ const route = createTeamOwnerRoutes({ team: { id: "acme", name: "Acme" } as never, people, store,
+ sees: () => visible, publicBot: (record) => record });
+ let status: number | undefined;
+ const path = `/api/bots/${bot.id}/reports-to`;
+ const pending = route({ req: {} as IncomingMessage, res: {} as ServerResponse,
+ path, url: new URL(path, "http://localhost"), method: "PUT", auth,
+ json: ((_res: ServerResponse, code: number) => { status = code; }) as never,
+ readBody: async () => { reading(); return body; },
+ });
+ await started;
+ if (change === "owner") store.patchBot(bot.id, { reportsTo: ADA });
+ if (change === "visibility") visible = false;
+ if (change === "personal") store.patchBot(bot.id, { defaultBot: { person: ADA } });
+ release({ email: BOB });
+ await pending;
+ expect(status).toBe(expectedStatus);
+ if (change === "owner") expect(store.bot(bot.id)?.reportsTo).toBe(ADA);
+ else expect(store.bot(bot.id)?.reportsTo).toBeUndefined();
+ });
+});
diff --git a/server/routes/team-owners.ts b/server/routes/team-owners.ts
index f5f5f2d86f..7c4f933786 100644
--- a/server/routes/team-owners.ts
+++ b/server/routes/team-owners.ts
@@ -117,14 +117,14 @@ export function createTeamOwnerRoutes(deps: TeamOwnerRouteDeps): RouteHandler {
return json(res, 200, { bots: changed.bots.map((bot) => deps.publicBot(bot)) });
}
+ const parsed = ownerBody.safeParse(await readBody(req));
+ if (!parsed.success) return json(res, 400, { error: "Send the email of the person it reports to." });
const bot = deps.store.bot(one![1]!);
if (!bot || !deps.sees(auth, bot.id)) return json(res, 404, { error: "no such bot" });
if (bot.defaultBot?.person) return json(res, 409, { error: `${bot.name} is ${nameOf(ownerEmail(bot.defaultBot.person))}'s own MausBot: it always reports to them.` });
if (!admin && (me === null || botOwner(bot) !== me)) {
return json(res, 403, { error: `Only ${team.name}'s admins, or the person ${bot.name} reports to, can change who it reports to.` });
}
- const parsed = ownerBody.safeParse(await readBody(req));
- if (!parsed.success) return json(res, 400, { error: "Send the email of the person it reports to." });
const { email } = parsed.data;
if (unknownPerson(email)) return json(res, 400, { error: `Choose someone in ${team.name}.` });
const changed = setReportsTo(deps.store, [bot.id], email);