Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Let a paired phone read a bot's skills and discard a file it uploaded
milind-soniaac32234e02d
diff --git a/companion/src/routes.ts b/companion/src/routes.tsindex 83e6a95067..198a42eeac 100644--- a/companion/src/routes.ts+++ b/companion/src/routes.ts@@ -132,6 +132,9 @@ 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$/ },+ // Read-only: a bot's skills by name, with what each is for and whether it is on. Adding, editing and switching them+ // stay on the host.+ { method: "GET", path: /^\/api\/bots\/[\w-]+\/skills$/ },// 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$/ },@@ -190,6 +193,9 @@ const ALLOWED: ReadonlyArray<{ method: string; path: RegExp }> = [{ method: "POST", path: /^\/api\/attachments$/ },// The harness honors Range on voice notes so a phone player can seek.{ method: "GET", path: /^\/api\/attachments\/[\w-]+\.(?:png|jpe?g|gif|webp|mp3|wav)$/i },+ // Discarding a file before it is sent. The harness records which device uploaded each file and deletes only that+ // device's own, and only while no message uses it.+ { method: "DELETE", path: /^\/api\/attachments\/[\w-]+\.[A-Za-z0-9]{1,10}$/ },// Raw upload, capped and stored under a generated filename by the harness.{ method: "POST", path: /^\/api\/files$/ },diff --git a/companion/test/routes.test.ts b/companion/test/routes.test.tsindex 1761d496b4..fc0ea07913 100644--- a/companion/test/routes.test.ts+++ b/companion/test/routes.test.ts@@ -99,10 +99,12 @@ describe("what the app may do", () => {["GET", "/api/threads/th_1/export"],["POST", "/api/threads/th_1/respond"],["GET", "/api/search"],+ ["GET", "/api/bots/bot_123/skills"],["POST", "/api/attachments"],["GET", "/api/attachments/avatar-123.webp"],["GET", "/api/attachments/voice-note-1.mp3"],["GET", "/api/attachments/voice-note-1.wav"],+ ["DELETE", "/api/attachments/3f2c9a1e-photo.jpg"],["POST", "/api/files"],["GET", "/api/tts/voices"],["POST", "/api/tts/prepare"],@@ -193,6 +195,38 @@ describe("what it may not", () => {}});+ it("reads a bot's skills and changes none of them", () => {+ expect(ask("GET", "/api/bots/bot_123/skills", false)?.status).toBe(401);+ for (const [method, path] of [+ ["POST", "/api/bots/bot_123/skills"],+ ["PUT", "/api/bots/bot_123/skills"],+ ["PATCH", "/api/bots/bot_123/skills"],+ ["DELETE", "/api/bots/bot_123/skills"],+ ["GET", "/api/bots/bot_123/skills/research"],+ ["PATCH", "/api/bots/bot_123/skills/research"],+ ["POST", "/api/bots/bot_123/skills/import"],+ ["GET", "/api/bots/bot_123/../skills"],+ ["GET", "/api/skills"],+ ] as Array<[string, string]>) {+ expect(allowed(method, path), `${method} ${path}`).toBe(false);+ }+ });++ it("discards one uploaded file by its generated name, never a path", () => {+ expect(ask("DELETE", "/api/attachments/3f2c9a1e-photo.jpg", false)?.status).toBe(401);+ for (const path of [+ "/api/attachments",+ "/api/attachments/../config.json",+ "/api/attachments/..%2Fconfig.json",+ "/api/attachments/sub/photo.jpg",+ "/api/attachments/photo",+ "/api/attachments/.hidden",+ "/api/attachments/photo.jpg.",+ ]) {+ expect(allowed("DELETE", path), path).toBe(false);+ }+ });+it("keeps endpoint refresh authenticated and exact-method only", () => {expect(ask("GET", "/api/companion/endpoints", false)?.status).toBe(401);expect(ask("GET", "/api/companion/endpoints")).toBeNull();diff --git a/server/host/member-uploads.test.ts b/server/host/member-uploads.test.tsnew file mode 100644index 0000000000..d313b383f4--- /dev/null+++ b/server/host/member-uploads.test.ts@@ -0,0 +1,53 @@+import { mkdtempSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, describe, expect, it } from "vitest";++import type { RequestAuth } from "../request-auth.ts";+import type { SessionRecord } from "../sessions.ts";+import { createUploadOwners, uploadsFor } from "./member-uploads.ts";++const made: string[] = [];+afterEach(() => {+ for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true });+});++function uploads() {+ const dataDir = mkdtempSync(join(tmpdir(), "omb-member-uploads-"));+ made.push(dataDir);+ return uploadsFor(createUploadOwners(dataDir), false);+}++const desktop: RequestAuth = { kind: "loopback", scopes: ["admin", "client"] };+const phone = (device: string): RequestAuth => ({ kind: "loopback", scopes: ["admin", "client"], companionDevice: device });+const session = (id: string): RequestAuth => ({+ kind: "session", via: "bearer", scopes: ["admin", "client"],+ session: { id, label: id, scopes: ["admin", "client"], createdAt: 0, lastSeenAt: 0, expiresAt: Number.MAX_SAFE_INTEGER } as unknown as SessionRecord,+});++describe("who may discard an uploaded file", () => {+ it("is the paired phone that uploaded it, and no other phone or the desktop", () => {+ const owners = uploads();+ owners.note(phone("phone-a"), "a.jpg");+ expect(owners.owns(phone("phone-a"), "a.jpg")).toBe(true);+ expect(owners.owns(phone("phone-b"), "a.jpg")).toBe(false);+ expect(owners.owns(desktop, "a.jpg")).toBe(false);+ expect(owners.owns(session("phone-a"), "a.jpg")).toBe(false);+ });++ it("keeps the desktop's own files the desktop's, out of every phone's reach", () => {+ const owners = uploads();+ owners.note(desktop, "d.png");+ expect(owners.owns(desktop, "d.png")).toBe(true);+ expect(owners.owns(phone("phone-a"), "d.png")).toBe(false);+ });++ it("keeps a signed-in session's files its own", () => {+ const owners = uploads();+ owners.note(session("s1"), "s.png");+ expect(owners.owns(session("s1"), "s.png")).toBe(true);+ expect(owners.owns(session("s2"), "s.png")).toBe(false);+ expect(owners.owns(desktop, "s.png")).toBe(false);+ expect(owners.owns(phone("s1"), "s.png")).toBe(false);+ });+});diff --git a/server/host/member-uploads.ts b/server/host/member-uploads.tsindex 35bafbfba5..fe7ae9e221 100644--- a/server/host/member-uploads.ts+++ b/server/host/member-uploads.ts@@ -41,10 +41,11 @@ export function createUploadOwners(dataDir: string) {};}-/** The thread routes' `uploads` (server/routes/threads.ts): a session's uploader is its person, anything else "local";- * on a Team Cloud (`team`) a member's are remembered past a restart. */+/** The thread routes' `uploads` (server/routes/threads.ts): a session's uploader is its person, a paired phone's is that+ * phone, anything else "local"; on a Team Cloud (`team`) a member's are remembered past a restart. */export function uploadsFor(owners: ReturnType<typeof createUploadOwners>, team: boolean) {- const uploaderOf = (auth: RequestAuth) => auth.kind === "session" ? personKey(auth.session) : "local";+ const uploaderOf = (auth: RequestAuth) => auth.kind === "session" ? personKey(auth.session)+ : auth.companionDevice ? `companion:${auth.companionDevice}` : "local";const teamMemberOf = (auth: RequestAuth) => team && auth.kind === "session" && auth.session.membershipAuthority === "team" ? personKey(auth.session) : undefined;return {note: (auth: RequestAuth, name: string) => owners.note(uploaderOf(auth), name, teamMemberOf(auth)),diff --git a/server/request-auth.test.ts b/server/request-auth.test.tsindex 429ef3dcd5..36a32169a0 100644--- a/server/request-auth.test.ts+++ b/server/request-auth.test.ts@@ -308,6 +308,10 @@ describe("resolveRequestAuth", () => {["GET", "/api/bots/b/browser/live"], ["POST", "/api/bots/b/browser/action"],]) expect(check(method, path).auth?.kind, path).toBe("loopback");expect(check("GET", "/api/phones/self/jobs").auth).toEqual({ kind: "loopback", scopes: ["admin", "client"], companionDevice: "phone-1" });+ expect(check("GET", "/api/bots/b/skills").auth?.kind).toBe("loopback");+ expect(check("POST", "/api/bots/b/skills").auth).toBeNull();+ expect(check("DELETE", "/api/attachments/abc-1.jpg").auth).toEqual({ kind: "loopback", scopes: ["admin", "client"], companionDevice: "phone-1" });+ expect(check("DELETE", "/api/attachments/..%2Fconfig.json").auth).toBeNull();const plain = resolveRequestAuth(request({ host: "127.0.0.1:8799", "x-openmausbot-companion": "1", "x-openmausbot-companion-device": "phone-1" }, "GET"), {sessions, cookieName, streamPath: "/api/events", url: new URL("/api/phones/self/jobs", "http://localhost"),loopbackMutationToken: "desktop-secret", companionMutationToken: "relay-secret",