Skip to content

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

Let phones without admin rights read a bot's Data

milind-sonia6efa565b580
diff --git a/companion/src/routes.ts b/companion/src/routes.ts
index 1a9903e986..83e6a95067 100644
--- a/companion/src/routes.ts
+++ b/companion/src/routes.ts
@@ -132,6 +132,11 @@ const ALLOWED: ReadonlyArray<{ method: string; path: RegExp }> = [
// Names and on/off only. The MCP settings list, which carries commands and
// addresses, stays closed.
{ method: "GET", path: /^\/api\/bots\/[\w-]+\/mcp-servers$/ },
+ // The bot's Data, read-only: the sheet and tables, a page of rows (a POST
+ // only for its body), a chart card's picture. Running SQL stays on the host.
+ { method: "GET", path: /^\/api\/bots\/[\w-]+\/data$/ },
+ { method: "POST", path: /^\/api\/bots\/[\w-]+\/data\/page$/ },
+ { method: "GET", path: /^\/api\/bots\/[\w-]+\/data\/cards\/c_\d+\/image$/ },
// Team memory edits are content, not execution policy. The section rides as
// a query string.
{ method: "GET", path: /^\/api\/team-memory$/ },
diff --git a/companion/test/routes.test.ts b/companion/test/routes.test.ts
index e40e5ff24d..1761d496b4 100644
--- a/companion/test/routes.test.ts
+++ b/companion/test/routes.test.ts
@@ -357,6 +357,25 @@ describe("what it may not", () => {
] as const) expect(allowed(method, path), `${method} ${path}`).toBe(false);
});
+ it("reads a bot's Data and nothing that runs, changes or exports it", () => {
+ expect(allowed("GET", "/api/bots/bot_123/data")).toBe(true);
+ expect(allowed("POST", "/api/bots/bot_123/data/page")).toBe(true);
+ expect(allowed("GET", "/api/bots/bot_123/data/cards/c_2/image")).toBe(true);
+ for (const [method, path] of [
+ ["POST", "/api/bots/bot_123/data/run"],
+ ["POST", "/api/bots/bot_123/data/cancel"],
+ ["POST", "/api/bots/bot_123/data/export"],
+ ["PATCH", "/api/bots/bot_123/data/cards/c_2"],
+ ["DELETE", "/api/bots/bot_123/data/cards/c_2"],
+ ["GET", "/api/bots/bot_123/data/tables/orders/stats"],
+ ["POST", "/api/bots/bot_123/data"],
+ ["GET", "/api/bots/bot_123/data/page"],
+ ["GET", "/api/bots/bot_123/data/cards/../../config"],
+ ["GET", "/api/bots/bot_123/data/cards/c_2/image/extra"],
+ ] as const) expect(ask(method, path)?.status, `${method} ${path}`).toBe(404);
+ expect(ask("GET", "/api/bots/bot_123/data", false)?.status).toBe(401);
+ });
+
it("lets a paired phone delete one bot, and nothing beside it", () => {
expect(allowed("DELETE", "/api/bots/bot_123")).toBe(true);
expect(allowed("DELETE", "/api/bots")).toBe(false);
diff --git a/server/bot-access.test.ts b/server/bot-access.test.ts
index aff091b69f..f36e3d1f51 100644
--- a/server/bot-access.test.ts
+++ b/server/bot-access.test.ts
@@ -79,6 +79,8 @@ describe("what a request needs", () => {
expect(levelNeeded("POST", "/api/bots/b1/bot-api/new-key")).toBe("edit");
expect(levelNeeded("GET", "/api/bots/b1/bot-api")).toBe("view");
expect(levelNeeded("POST", "/api/groups/g1/read")).toBe("view");
+ expect(levelNeeded("POST", "/api/bots/b1/data/page")).toBe("view");
+ expect(levelNeeded("POST", "/api/bots/b1/data/run")).toBe("chat");
expect(levelNeeded("POST", "/api/bots/b1/tasks/t1")).toBe("view");
expect(levelNeeded("POST", "/api/bots/b1/tasks/delete")).toBe("chat");
expect(levelNeeded("POST", "/api/bots/b1/tasks")).toBe("chat");
diff --git a/server/host/bot-access-gate.ts b/server/host/bot-access-gate.ts
index cb10830c62..d9f3f48090 100644
--- a/server/host/bot-access-gate.ts
+++ b/server/host/bot-access-gate.ts
@@ -35,7 +35,7 @@ export function levelNeeded(method: string, path: string): Exclude<BotAccessLeve
if (/^\/api\/bots\/[\w-]+$/.test(path)) return null;
if (/^\/api\/bots\/[\w-]+\/(?:access|bot-api(?:\/new-key)?)$/.test(path)) return "edit";
if (/^\/api\/routines\/[\w-]+$/.test(path)) return null;
- if (/^\/api\/(?:bots|groups)\/[\w-]+\/read$/.test(path) || /^\/api\/bots\/[\w-]+\/access-request$/.test(path)) return "view";
+ if (/^\/api\/(?:bots|groups)\/[\w-]+\/read$/.test(path) || /^\/api\/bots\/[\w-]+\/(?:access-request|data\/page)$/.test(path)) return "view";
if (verb === "POST" && OPEN_CONVERSATION.test(path)) return "view";
if (/^\/api\/routine-runs\/[\w-]+\/seen$/.test(path)) return "view";
return "chat";
diff --git a/server/request-auth.test.ts b/server/request-auth.test.ts
index fd96910081..e3405e338c 100644
--- a/server/request-auth.test.ts
+++ b/server/request-auth.test.ts
@@ -106,6 +106,28 @@ describe("scopes", () => {
] as const) expect(requiredScope(method, path), `${method} ${path}`).toBe("admin");
});
+ it("lets a client read a bot's Data and nothing that runs, changes or exports it", () => {
+ for (const [method, path] of [
+ ["GET", "/api/bots/bot-1/data"],
+ ["POST", "/api/bots/bot-1/data/page"],
+ ["GET", "/api/bots/bot-1/data/cards/c_12/image"],
+ ] as const) expect(requiredScope(method, path), `${method} ${path}`).toBe("client");
+ for (const [method, path] of [
+ ["POST", "/api/bots/bot-1/data/run"],
+ ["POST", "/api/bots/bot-1/data/cancel"],
+ ["POST", "/api/bots/bot-1/data/export"],
+ ["PATCH", "/api/bots/bot-1/data/cards/c_12"],
+ ["DELETE", "/api/bots/bot-1/data/cards/c_12"],
+ ["GET", "/api/bots/bot-1/data/tables/orders/stats"],
+ ["GET", "/api/bots/bot-1/data/tables/orders/histogram"],
+ ["POST", "/api/bots/bot-1/data"],
+ ["GET", "/api/bots/bot-1/data/page"],
+ ["POST", "/api/bots/bot-1/data/cards/c_12/image"],
+ ["GET", "/api/bots/bot-1/data/cards/x/image"],
+ ["GET", "/api/bots/bot-1/data/cards/c_12/image/extra"],
+ ] as const) expect(requiredScope(method, path), `${method} ${path}`).toBe("admin");
+ });
+
it("keeps a card's \"Use a saved one\" an admin's, since picking one lets a bot use a Vault item", () => {
for (const [method, path] of [
["GET", "/api/bots/bot-1/secret-cards/card-1/saved"],
diff --git a/server/request-auth.ts b/server/request-auth.ts
index 0dd4263f4f..c11da28c0c 100644
--- a/server/request-auth.ts
+++ b/server/request-auth.ts
@@ -386,6 +386,11 @@ export const CLIENT_ALLOW: ReadonlyArray<{ methods: readonly string[]; path: Reg
{ methods: ["POST"], path: /^\/api\/bots\/[\w-]+\/browser\/action$/ },
{ methods: ["GET"], path: /^\/api\/bots\/[\w-]+\/browser\/(?:stream|download\/[\w-]+)$/ },
{ methods: ["POST"], path: /^\/api\/bots\/[\w-]+\/browser\/upload$/ },
+ // A bot's Data, read-only as a phone shows it: the sheet and tables, a page of a table or card (a catalog lookup, never
+ // SQL), a chart card's picture. Running, changing and exporting stay an admin's (data/routes.ts).
+ { methods: ["GET"], path: /^\/api\/bots\/[\w-]+\/data$/ },
+ { methods: ["POST"], path: /^\/api\/bots\/[\w-]+\/data\/page$/ },
+ { methods: ["GET"], path: /^\/api\/bots\/[\w-]+\/data\/cards\/c_\d+\/image$/ },
{ methods: ["GET"], path: /^\/api\/bots\/[\w-]+\/connector-cards\/[\w-]+\/status$/ },
{ methods: ["POST"], path: /^\/api\/bots\/[\w-]+\/connector-cards\/[\w-]+\/(?:resume|dismiss)$/ },
// A member signs in to their own MCP server from its card; the handler keeps an app's card and a shared server's an admin's.
diff --git a/server/team-phone-data.e2e.test.ts b/server/team-phone-data.e2e.test.ts
new file mode 100644
index 0000000000..c2f0e83bd3
--- /dev/null
+++ b/server/team-phone-data.e2e.test.ts
@@ -0,0 +1,86 @@
+// A phone without admin rights opens a bot's Data read-only (the mobile app's Data screen): the sheet, a page of rows and
+// a chart card's picture. Tried as a Team Cloud member, whose session is the same client scope a `pair --phone` phone gets:
+// a bot they can't see answers exactly as a missing one, and nothing that runs, changes or exports data opens to them.
+import { randomBytes } from "node:crypto";
+import { symlinkSync } from "node:fs";
+import { join } from "node:path";
+import { fileURLToPath } from "node:url";
+import { expect, it } from "vitest";
+
+import { launchCloudSendFixture } from "../scripts/testing/cloud-send-fixture.ts";
+import { DATA_ROUTES } from "../shared/data-surface.ts";
+import { hostTimeout } from "./testing/host-timeout.ts";
+
+const NO_SUCH_BOT = { status: 404, body: { error: "no such bot" } };
+
+it("lets a member's phone read a visible bot's Data, and nothing of a bot they can't see", async () => {
+ const fx = await launchCloudSendFixture({ onePasswordToken: `ops_fake-${randomBytes(8).toString("hex")}`, team: { id: "team-phone-data", name: "Acme" } });
+ try {
+ const team = fx.team!;
+ // The fixture's native tree holds a fake 1Password SDK only; DuckDB loads on first use, so it can join it now.
+ for (const scope of ["@duckdb", "@resvg"]) symlinkSync(fileURLToPath(new URL(`../node_modules/${scope}`, import.meta.url)), join(team.home, "native", "node_modules", scope), "junction");
+ const owner = (await team.pair({ email: "[email protected]", role: "owner" })).token!;
+ const member = (await team.pair({ email: "[email protected]", role: "member" })).token!;
+ expect((await team.as(member, "GET", "/api/auth/session")).body.scopes).toEqual(["client"]);
+ const made = async (name: string, visibility: string) => {
+ const created = await team.as(owner, "POST", "/api/bots", { name, visibility });
+ expect(created.status, JSON.stringify(created.body)).toBe(201);
+ const bot = created.body.bot as { id: string; threadId: string };
+ // What the bot's own data tools do in a turn: a table, and a chart card of it on the sheet.
+ const token = await team.capability(bot.id, bot.threadId, "data");
+ const call = async (name: string, args: Record<string, unknown>) => {
+ const answer = await fetch(`${team.base}/api/internal/data/mcp`, {
+ method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
+ body: JSON.stringify({ method: "tools/call", params: { name, arguments: args } }),
+ });
+ const text = await answer.text();
+ expect(answer.status, text).toBe(200);
+ const { result } = JSON.parse(text) as { result: { isError?: boolean; structuredContent: Record<string, any>; content?: Array<{ text: string }> } };
+ expect(result.isError, result.content?.[0]?.text).not.toBe(true);
+ return result.structuredContent;
+ };
+ await call("data_sql", { sql: "CREATE TABLE orders AS SELECT * FROM (VALUES ('north', 3), ('south', 5), ('west', 8)) t(region, amount)" });
+ const chart = await call("data_show", { kind: "chart", title: "By region", table: "orders", chart: { type: "bar", x: "region", y: "amount" } });
+ return { id: bot.id, card: chart.id as string };
+ };
+ const shared = await made("Ledger", "everyone");
+ const hidden = await made("Vault", "admins");
+
+ const sheet = await team.as(member, "GET", DATA_ROUTES.sheet(shared.id));
+ expect(sheet.status, JSON.stringify(sheet.body)).toBe(200);
+ expect(sheet.body.tables.map((table: { name: string }) => table.name)).toEqual(["orders"]);
+ expect(sheet.body.sheet.cards.map((card: { id: string; kind: string }) => [card.id, card.kind])).toEqual([[shared.card, "chart"]]);
+ // The page body the phone sends (BotData.kt), for a table and for a card's result.
+ const page = await team.as(member, "POST", DATA_ROUTES.page(shared.id), { table: "orders", offset: 0, limit: 100 });
+ expect(page.status, JSON.stringify(page.body)).toBe(200);
+ expect(page.body).toMatchObject({ rowCount: 3, offset: 0 });
+ expect(page.body.columns.map((column: { name: string }) => column.name)).toEqual(["region", "amount"]);
+ expect((await team.as(member, "POST", DATA_ROUTES.page(shared.id), { cardId: shared.card, offset: 0, limit: 100 })).body.rowCount).toBe(3);
+ const image = await fetch(`${team.base}${DATA_ROUTES.image(shared.id, shared.card)}?format=png&theme=dark&width=360&scale=2.0`, { headers: { ...fx.edge, authorization: `Bearer ${member}` } });
+ expect(image.status).toBe(200);
+ expect(image.headers.get("content-type")).toBe("image/png");
+ expect(Buffer.from(await image.arrayBuffer()).subarray(1, 4).toString()).toBe("PNG");
+
+ // A bot they can't see is a bot that isn't there, on every Data route.
+ for (const id of [hidden.id, "missing-bot"]) {
+ expect(await team.as(member, "GET", DATA_ROUTES.sheet(id)), id).toEqual(NO_SUCH_BOT);
+ expect(await team.as(member, "POST", DATA_ROUTES.page(id), { table: "orders", offset: 0, limit: 100 }), id).toEqual(NO_SUCH_BOT);
+ expect(await team.as(member, "GET", DATA_ROUTES.image(id, hidden.card)), id).toEqual(NO_SUCH_BOT);
+ }
+ expect((await team.as(owner, "GET", DATA_ROUTES.sheet(hidden.id))).status).toBe(200);
+
+ // Running SQL, cancelling, changing a card and exporting stay an admin's.
+ for (const [method, path, body] of [
+ ["POST", DATA_ROUTES.run(shared.id), { sql: "SELECT 1" }],
+ ["POST", DATA_ROUTES.cancel(shared.id), { cardId: shared.card }],
+ ["PATCH", DATA_ROUTES.card(shared.id, shared.card), { pinned: true }],
+ ["DELETE", DATA_ROUTES.card(shared.id, shared.card), undefined],
+ ["POST", DATA_ROUTES.export(shared.id), { cardId: shared.card, format: "csv" }],
+ ["GET", DATA_ROUTES.stats(shared.id, "orders"), undefined],
+ ] as const) expect((await team.as(member, method, path, body)).status, `${method} ${path}`).toBe(403);
+ const after = await team.as(owner, "GET", DATA_ROUTES.sheet(shared.id));
+ expect(after.body.sheet.cards.map((card: { id: string; pinned?: boolean }) => [card.id, card.pinned ?? false])).toEqual([[shared.card, false]]);
+ } finally {
+ await fx.close();
+ }
+}, hostTimeout(180_000));