Skip to content

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

20.6 KB
Raw Blame
import { spawn, type ChildProcess } from "node:child_process";
import { randomUUID } from "node:crypto";
import { existsSync, readFileSync, writeFileSync, mkdirSync, realpathSync, rmSync } from "node:fs";
import { join } from "node:path";
import { createInterface } from "node:readline";
import { afterAll, beforeAll, expect, it, vi } from "vitest";
import { launchVerificationServer, type VerificationServer } from "../scripts/control-omb.ts";
import { createComputerSharing, type SharedFolder } from "../electron/computer-sharing.mjs";
import { sessionCookieName } from "./request-auth.ts";

let fixture: VerificationServer;
let connector: ReturnType<typeof createComputerSharing>;
let proxy: ChildProcess;
let env: { id: string; origin: string; name: string };
let pairing: any;
let folder: SharedFolder;
let grantFile: string;
let bot: any;
let localSharingEnabled = true;
let sequence = 0;
const waiting = new Map<number, (value: any) => void>();
const evidence: string[] = [];
const api = async (method: string, path: string, body?: unknown, headers: Record<string, string> = {}) => {
  const response = await fetch(`${fixture.info.url}${path}`, {
    method, headers: { "content-type": "application/json", origin: fixture.info.url, ...headers },
    ...(body === undefined ? {} : { body: JSON.stringify(body) }),
  });
  return { status: response.status, body: await response.json() as any };
};
const rpc = (method: string, params?: unknown): Promise<any> => new Promise((resolve, reject) => {
  const id = ++sequence;
  // A shared terminal operation can legitimately run 30 seconds before its result crosses the connector and MCP boundaries.
  const timeout = setTimeout(() => { waiting.delete(id); reject(new Error(`MCP ${method} ${JSON.stringify(params)} timed out`)); }, 40_000);
  waiting.set(id, value => { clearTimeout(timeout); resolve(value); });
  proxy.stdin!.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
});
const tool = async (name: string, args: unknown = {}) => (await rpc("tools/call", { name, arguments: args })).result;
const computers = async () => JSON.parse((await tool("list_shared_computers")).content[0].text).computers;
const storedGrant = () => JSON.parse(readFileSync(grantFile, "utf8")).records[env.id];
const operation = async (action: string, rest = {}) => tool("shared_computer", { computer_id: storedGrant().id, folder_id: folder.id, action, ...rest });
const data = (result: any) => JSON.parse(result.content[0].text);
const markedWaitCommand = (marker: string) => process.platform === "win32"
  ? `[IO.File]::WriteAllText('${marker.replaceAll("'", "''")}', [string]$PID); Start-Sleep -Seconds 20`
  : `echo $$ > '${marker.replaceAll("'", "'\"'\"'")}'; sleep 20`;
const commandAlive = (marker: string) => {
  const pid = Number(readFileSync(marker, "utf8").trim());
  expect(Number.isInteger(pid) && pid > 0).toBe(true);
  try { process.kill(pid, 0); return true; }
  catch (error) { if ((error as NodeJS.ErrnoException).code === "ESRCH") return false; throw error; }
};

beforeAll(async () => {
  fixture = await launchVerificationServer({ FAKE_CLAUDE_MODE: "hang" });
  // Computer sharing ships off: turn it on before anything is paired, since routes, capability and agent tools read one gate.
  expect((await api("PATCH", "/api/config", { features: { sharedComputers: true } })).status).toBe(200);
  env = { id: "hosted-fixture", name: "Hosted fixture", origin: fixture.info.url };
  const local = join(fixture.info.dataDir, "shared-folder"); mkdirSync(local);
  folder = { id: randomUUID(), name: "Shared fixture", path: realpathSync(local), write: false };
  writeFileSync(join(local, "brief.txt"), "A real local file. 🌱");
  grantFile = join(fixture.info.dataDir, "desktop-profile", "computer-sharing.json");
  const opened = await api("POST", "/api/auth/pairing", { label: "Desktop fixture", scopes: ["client"] });
  expect(opened.status).toBe(200);
  pairing = (await api("POST", "/api/auth/pair", { code: opened.body.code })).body;
  expect(pairing.token).toMatch(/^omb_sess_/);
  connector = createComputerSharing({
    file: grantFile, environments: () => [env], cuaConnection: async () => null,
    enabled: async () => localSharingEnabled,
    fetch: (url: string, init: RequestInit) => fetch(url, { ...init, headers: { ...init.headers, authorization: `Bearer ${pairing.token}` } }),
  });
  bot = (await api("POST", "/api/bots", { name: "Shared desktop tester" })).body.bot;
  proxy = await agentProxyFor(bot.id, pairing.token, "Inspect the folder shared by my desktop; do not use files on the server.");
  createInterface({ input: proxy.stdout! }).on("line", line => { const msg = JSON.parse(line); waiting.get(msg.id)?.(msg); waiting.delete(msg.id); });
  await rpc("initialize", { protocolVersion: "2024-11-05" });
}, 30_000);

/** The fake model hangs, so the turn and its agents capability stay live. */
async function agentProxyFor(botId: string, token: string, text: string): Promise<ChildProcess> {
  return proxyForTurn(async () => {
    expect((await api("POST", `/api/bots/${botId}/messages`, { text }, { authorization: `Bearer ${token}` })).status).toBe(202);
  });
}

function mcpClient(child: ChildProcess) {
  const replies = new Map<number, (value: any) => void>();
  createInterface({ input: child.stdout! }).on("line", line => { const msg = JSON.parse(line); replies.get(msg.id)?.(msg); replies.delete(msg.id); });
  let next = 1000;
  const request = (method: string, params: unknown): Promise<any> => new Promise(resolve => {
    const id = ++next; replies.set(id, msg => resolve(msg.result));
    child.stdin!.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
  });
  return { initialize: () => request("initialize", { protocolVersion: "2024-11-05" }), call: (name: string, args: unknown = {}) => request("tools/call", { name, arguments: args }) };
}

async function proxyForTurn(start: () => Promise<void>): Promise<ChildProcess> {
  const dump = join(fixture.info.dataDir, "fake-claude-dump.json");
  rmSync(dump, { force: true });
  await start();
  await vi.waitFor(() => expect(existsSync(dump)).toBe(true), { timeout: 15_000 });
  const agents = JSON.parse(readFileSync(dump, "utf8")).mcpConfig.mcpServers.agents;
  expect(agents.env.OMB_COMMS_TOKEN).toBeTruthy();
  return spawn(agents.command, agents.args, {
    env: { PATH: process.env.PATH, HOME: fixture.info.dataDir, ...agents.env }, stdio: ["pipe", "pipe", "pipe"],
  });
}

afterAll(async () => {
  connector?.close(); proxy?.kill();
  if (fixture) {
    const receipt = `${fixture.info.logPath}.computer-sharing.json`;
    writeFileSync(receipt, JSON.stringify({ fixture: fixture.info, checks: evidence, limitation: "Fake model; real MCP, HTTP pairing and filesystem. OS screen control is tested with a separate transport stand-in, never the user's screen." }, null, 2));
    console.info(`Computer-sharing evidence: ${receipt}`);
    await fixture.close();
  }
});

it("asks only after pairing; Not now remembers the choice and grants nothing", async () => {
  const info = await connector.observe(env);
  expect(info?.environmentId).toBeTruthy(); expect(info?.sessionId).toBe(pairing.session.id);
  connector.decline(env, info!);
  expect(await connector.observe(env)).toBeNull();
  expect(await computers()).toEqual([]);
  expect(connector.state(env.id)).toMatchObject({ enabled: false, folders: [], computer: false, terminal: false });
  evidence.push("post-pair identity and remembered Not now; zero default access");
});

it("executes real read-only folder requests through the real agent MCP and connector", async () => {
  await connector.save(env, { folders: [folder], terminal: false, computer: false }, await connector.identity(env));
  await expect.poll(() => connector.state(env.id).connected, { timeout: 8000 }).toBe(true);
  const available = await computers();
  expect(available).toHaveLength(1);
  expect(available[0].folders[0]).toMatchObject({ id: folder.id, write: false });
  expect(JSON.stringify(available)).not.toContain(folder.path);
  expect(data(await operation("list_files")).entries).toContainEqual({ name: "brief.txt", type: "file" });
  expect(data(await operation("read_file", { path: "brief.txt" })).content).toBe("A real local file. 🌱");
  const refused = await operation("write_file", { path: "brief.txt", content: "not allowed" });
  expect(refused.isError).toBe(true); expect(refused.content[0].text).toContain("read-only");
  expect((await operation("run_command", { command: "echo no" })).isError).toBe(true);
  expect((await operation("computer_tools")).isError).toBe(true);
  expect((await operation("read_file", { path: "../config.json" })).isError).toBe(true);
  evidence.push("real MCP → paired HTTP → local read, with write/terminal/screen/traversal denied");
});

it("explicit edits and terminal work; old credentials and other sessions cannot impersonate the desktop", async () => {
  const old = storedGrant();
  await connector.save(env, { folders: [{ ...folder, write: true }], terminal: true, computer: false }, await connector.identity(env));
  await expect.poll(() => connector.state(env.id).connected, { timeout: 8000 }).toBe(true);
  const before = data(await operation("read_file", { path: "brief.txt" }));
  expect(data(await operation("write_file", { path: "brief.txt", content: "Reviewed", expected_sha256: before.sha256 })).written).toBe(true);
  expect(readFileSync(join(folder.path, "brief.txt"), "utf8")).toBe("Reviewed");
  expect(data(await operation("run_command", { command: "echo shared-desktop-ok" })).output).toContain("shared-desktop-ok");
  const grant = storedGrant();
  const auth = { authorization: `Bearer ${pairing.token}` };
  expect((await api("POST", `/api/shared-computers/${grant.id}/lease`, {}, auth)).status).toBe(403);
  expect((await api("POST", `/api/shared-computers/${old.id}/lease`, {}, { ...auth, "x-omb-computer-secret": old.secret })).status).toBe(403);
  const second = await api("POST", "/api/auth/pairing", { label: "Other desktop" });
  const other = (await api("POST", "/api/auth/pair", { code: second.body.code })).body;
  expect((await api("POST", `/api/shared-computers/${grant.id}/lease`, {}, { authorization: `Bearer ${other.token}`, "x-omb-computer-secret": grant.secret })).status).toBe(403);
  expect((await api("POST", "/api/desktop/shared-computer-control", { id: randomUUID(), action: "acquire" }, { authorization: `Bearer ${other.token}` })).status).toBe(403);
  const cookie = `${sessionCookieName(Number(new URL(env.origin).port), grant.environmentId)}=${pairing.token}`;
  expect((await api("POST", `/api/shared-computers/${grant.id}/lease`, {}, { cookie, origin: "https://evil.invalid", "x-omb-computer-secret": grant.secret })).status).toBe(403);
  evidence.push("explicit hash-guarded edit and real terminal; cross-session, stale-secret, CSRF, local-control gate refusals");
}, 60_000);

it("a big lent file changes by one passage, gains lines at its end, and is deleted only once read", async () => {
  await connector.save(env, { folders: [{ ...folder, write: true }], terminal: false, computer: false }, await connector.identity(env));
  await expect.poll(() => connector.state(env.id).connected, { timeout: 8000 }).toBe(true);
  const rules = "# Commands\n" + "- an unchanged rule that stays exactly as it is\n".repeat(720) + "- Where commands run: on the Mac\n";
  writeFileSync(join(folder.path, "commands.md"), rules);
  expect(Buffer.byteLength(rules)).toBeGreaterThan(34_000);

  const edit = { path: "commands.md", old_text: "- Where commands run: on the Mac", new_text: "- Where commands run: in the Cloud terminal" };
  const edited = await operation("edit_file", edit);
  expect(edited.isError, JSON.stringify(edited)).toBeFalsy();
  expect(readFileSync(join(folder.path, "commands.md"), "utf8")).toBe(rules.replace(edit.old_text, edit.new_text));
  expect(Buffer.byteLength(JSON.stringify(edit))).toBeLessThan(200);
  expect((await operation("edit_file", { path: "commands.md", old_text: "- an unchanged rule", new_text: "x" })).content[0].text).toContain("more than once");
  expect((await operation("edit_file", { path: "commands.md", old_text: "not there", new_text: "x" })).content[0].text).toContain("not found");

  const appended = await operation("append_file", { path: "data/metrics.jsonl", content: "{\"id\":\"x-0153\"}\n" });
  expect(appended.isError, JSON.stringify(appended)).toBeFalsy();
  await operation("append_file", { path: "data/metrics.jsonl", content: "{\"id\":\"x-0152\"}\n" });
  expect(readFileSync(join(folder.path, "data", "metrics.jsonl"), "utf8")).toBe("{\"id\":\"x-0153\"}\n{\"id\":\"x-0152\"}\n");

  writeFileSync(join(folder.path, "x-cookies.tmp"), "");
  expect((await operation("delete_file", { path: "x-cookies.tmp" })).content[0].text).toContain("expected_sha256");
  const seen = data(await operation("read_file", { path: "x-cookies.tmp" }));
  expect(data(await operation("delete_file", { path: "x-cookies.tmp", expected_sha256: seen.sha256 })).deleted).toBe(true);
  expect(existsSync(join(folder.path, "x-cookies.tmp"))).toBe(false);
  evidence.push("edit_file sends one passage of a 34 KB file; append_file adds lines; delete_file needs the file read first");
}, 60_000);

it("another person's conversation can neither see nor use the lent desktop", async () => {
  const opened = await api("POST", "/api/auth/pairing", { label: "Another person", scopes: ["client"] });
  const intruder = (await api("POST", "/api/auth/pair", { code: opened.body.code })).body;
  const theirs = (await api("POST", "/api/bots", { name: "Their bot" })).body.bot;
  const other = await agentProxyFor(theirs.id, intruder.token, "List the shared computers and read brief.txt from the first one.");
  const { initialize, call } = mcpClient(other);
  try {
    await initialize();
    expect(await computers()).toHaveLength(1);
    expect(JSON.parse((await call("list_shared_computers")).content[0].text).computers).toEqual([]);
    const refused = await call("shared_computer", { computer_id: storedGrant().id, folder_id: folder.id, action: "read_file", path: "brief.txt" });
    expect(refused.isError).toBe(true);
    expect(JSON.stringify(refused)).not.toContain("Reviewed");
    const noted = await call("memory_update", { action: "append", text: "A note from another person's chat." });
    expect(noted.isError, JSON.stringify(noted)).toBeFalsy();
  } finally { other.kill(); }
  evidence.push("owner partition: another paired person's bot sees no computers and cannot read the owner's file by id");
}, 45_000);

it("a routine acts for nobody, even one the lender created: it sees and uses no computer", async () => {
  const routineBot = (await api("POST", "/api/bots", { name: "Routine bot" })).body.bot;
  const owner = { authorization: `Bearer ${pairing.token}` };
  const routine = await proxyForTurn(async () => {
    const created = await api("POST", "/api/routines", {
      name: "Nightly desktop read", prompt: "Read brief.txt from my shared computer.", botId: routineBot.id, enabled: false,
      schedule: { type: "interval", everyMinutes: 60, anchorAt: Date.now() + 3_600_000 },
    }, owner);
    expect(created.status, JSON.stringify(created.body)).toBe(201);
    expect((await api("POST", `/api/routines/${created.body.routine.id}/run`, undefined, owner)).status).toBe(201);
  });
  const { initialize, call } = mcpClient(routine);
  try {
    await initialize();
    expect(await computers()).toHaveLength(1);
    expect(JSON.parse((await call("list_shared_computers")).content[0].text).computers).toEqual([]);
    const refused = await call("shared_computer", { computer_id: storedGrant().id, folder_id: folder.id, action: "read_file", path: "brief.txt" });
    expect(refused.isError).toBe(true);
    expect(refused.content[0].text).toContain("only a conversation started by the person who shared it");
  } finally { routine.kill(); }
  evidence.push("fail-closed principal: a routine turn (created by the lender) sees no computers");
}, 45_000);

it("a bot whose computer is off cannot drive the lent screen, even when screen control is lent", async () => {
  await connector.save(env, { folders: [{ ...folder, write: true }], terminal: true, computer: true }, await connector.identity(env));
  await expect.poll(() => connector.state(env.id).connected, { timeout: 8000 }).toBe(true);
  expect((await api("PATCH", `/api/bots/${bot.id}`, { computer: "off" })).status).toBe(200);
  try {
    const refused = await operation("computer_tools");
    expect(refused.isError).toBe(true);
    expect(refused.content[0].text).toContain("This bot has no computer");
    expect(data(await operation("list_files")).entries.length).toBeGreaterThan(0);
  } finally {
    expect((await api("PATCH", `/api/bots/${bot.id}`, { computer: null })).status).toBe(200);
  }
  evidence.push("a bot set to no computer is refused the lent screen before anything reaches the desktop");
}, 30_000);

it("revocation stops a running command, prevents further access and stays off after restart", async () => {
  const pending = operation("run_command", { command: process.platform === "win32" ? "Start-Sleep -Seconds 20" : "sleep 20" });
  await new Promise(resolve => setTimeout(resolve, 150));
  connector.revoke(env);
  expect((await pending).isError).toBe(true);
  await expect.poll(computers).toEqual([]);
  expect((await operation("read_file", { path: "brief.txt" })).isError).toBe(true);
  connector.close();
  connector = createComputerSharing({ file: grantFile, environments: () => [env], fetch, cuaConnection: async () => null, enabled: async () => true });
  connector.start(); expect(connector.state(env.id).enabled).toBe(false);
  expect(await computers()).toEqual([]);
  evidence.push("in-flight cancellation, no further access, durable revocation across connector restart");
});

it("disabling the local gate cancels a live job even while the remote workspace still permits sharing", async () => {
  connector.close();
  connector = createComputerSharing({
    file: grantFile, environments: () => [env], cuaConnection: async () => null,
    enabled: async () => localSharingEnabled,
    fetch: (url: string, init: RequestInit) => fetch(url, { ...init, headers: { ...init.headers, authorization: `Bearer ${pairing.token}` } }),
  });
  await connector.save(env, { folders: [folder], terminal: true, computer: false }, await connector.identity(env));
  await expect.poll(() => connector.state(env.id).connected, { timeout: 8000 }).toBe(true);
  const marker = join(folder.path, "local-gate-command-started");
  const pending = operation("run_command", { command: markedWaitCommand(marker) });
  await expect.poll(() => existsSync(marker), { timeout: 15_000 }).toBe(true);
  expect(commandAlive(marker)).toBe(true);
  localSharingEnabled = false;
  await expect.poll(() => connector.state(env.id).connected, { timeout: 5000 }).toBe(false);
  expect((await pending).isError).toBe(true);
  await expect.poll(() => commandAlive(marker), { timeout: 5000 }).toBe(false);
  expect((await api("GET", "/.well-known/openmausbot/environment")).body.capabilities.sharedComputers).toBe(true);
  await expect(connector.identity(env)).rejects.toThrow("turned off on this computer");
  expect((await operation("read_file", { path: "brief.txt" })).isError).toBe(true);
  evidence.push("local flag withdrawal cancels a live remote command and prevents further access despite remote opt-in");
}, 45_000);

it("withdrawing the workspace flag closes pending jobs and refuses a previously advertised tool", async () => {
  connector.close(); localSharingEnabled = true;
  connector = createComputerSharing({
    file: grantFile, environments: () => [env], cuaConnection: async () => null,
    enabled: async () => localSharingEnabled,
    fetch: (url: string, init: RequestInit) => fetch(url, { ...init, headers: { ...init.headers, authorization: `Bearer ${pairing.token}` } }),
  });
  await connector.save(env, { folders: [folder], terminal: true, computer: false }, await connector.identity(env));
  await expect.poll(() => connector.state(env.id).connected, { timeout: 8000 }).toBe(true);
  const marker = join(folder.path, "workspace-gate-command-started");
  const pending = operation("run_command", { command: markedWaitCommand(marker) });
  await expect.poll(() => existsSync(marker), { timeout: 15_000 }).toBe(true);
  expect(commandAlive(marker)).toBe(true);
  expect((await api("PATCH", "/api/config", { features: { sharedComputers: false } })).status).toBe(200);
  expect((await pending).isError).toBe(true);
  await expect.poll(() => commandAlive(marker), { timeout: 5000 }).toBe(false);
  const staleTool = await tool("list_shared_computers");
  expect(staleTool.isError).toBe(true);
  expect(staleTool.content[0].text).toContain("unknown internal endpoint");
  expect((await api("GET", "/.well-known/openmausbot/environment")).body.capabilities).not.toHaveProperty("sharedComputers");
  evidence.push("workspace flag withdrawal closes in-flight requests and refuses tools advertised to an earlier provider turn");
}, 45_000);