Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Harness: a phone tests only the key it pasted
aivsomkar46f669ad77b5
diff --git a/server/phone-standalone-keys.test.ts b/server/phone-standalone-keys.test.tsnew file mode 100644index 0000000000..8c199fc4c2--- /dev/null+++ b/server/phone-standalone-keys.test.ts@@ -0,0 +1,52 @@+// The standalone companion (`pnpm companion` beside a headless server) forwards a phone as plain loopback carrying only+// its mark: the harness still lets that phone change nothing but keys, and test nothing but the key it pasted.+import { readFileSync } from "node:fs";+import { createServer } from "node:http";+import type { AddressInfo } from "node:net";+import { join } from "node:path";+import { expect, it } from "vitest";++import { launchVerificationServer } from "../scripts/control-omb.ts";++const PHONE = { "x-openmausbot-companion": "1", "x-openmausbot-companion-device": "phone-1" };++async function provider() {+ const heard: string[] = [];+ const server = createServer((req, res) => {+ heard.push(String(req.headers.authorization ?? ""));+ res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ data: [{ id: "fixture-model" }] }));+ });+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));+ return { heard, server, url: `http://127.0.0.1:${(server.address() as AddressInfo).port}/v1` };+}++it("a phone through the standalone companion changes only keys, keeps them in config.json, and tests only the key it pasted", async () => {+ const saved = await provider();+ const elsewhere = await provider();+ const fixture = await launchVerificationServer();+ const send = async (method: string, path: string, body: unknown, headers: Record<string, string> = PHONE) =>+ (await fetch(`${fixture.info.url}${path}`, { method, headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body) })).status;+ const disk = () => JSON.parse(readFileSync(join(fixture.info.dataDir, "config.json"), "utf8"));+ try {+ expect(await send("PUT", "/api/config", { signIn: { admins: ["[email protected]"] } })).toBe(403);+ expect(disk().signIn?.admins ?? []).toEqual([]);+ // That query says the desktop already put the key in its encrypted store; a phone's key is in no such store.+ expect(await send("PUT", "/api/config?secretStorage=external", { mistral: { key: "phone-key" } })).toBe(200);+ expect(disk().mistral.key).toBe("phone-key");++ expect(await send("PUT", "/api/config", { openaiCompat: { url: saved.url, key: "saved-key" } }, {})).toBe(200);+ expect(await send("POST", "/api/keys/test", { provider: "openaiCompat", url: elsewhere.url })).toBe(400);+ expect(await send("POST", "/api/keys/test", { provider: "openaiCompat", key: "pasted-key", url: elsewhere.url })).toBe(200);+ expect(elsewhere.heard).toEqual([]);+ // The server's own model discovery calls the saved address too, so this hears more than the test.+ expect(saved.heard).toContain("Bearer pasted-key");++ // The computer itself still tests its saved key against an address it names.+ expect(await send("POST", "/api/keys/test", { provider: "openaiCompat", url: elsewhere.url }, {})).toBe(200);+ expect(elsewhere.heard).toEqual(["Bearer saved-key"]);+ } finally {+ await fixture.close();+ saved.server.close();+ elsewhere.server.close();+ }+}, 120_000);diff --git a/server/routes/status.ts b/server/routes/status.tsindex ce42b345b3..13bb4bbe63 100644--- a/server/routes/status.ts+++ b/server/routes/status.ts@@ -19,7 +19,7 @@ import { readThreadEvents } from "../thread-events.ts";import { parseUsageRange } from "../usage-ledger.ts";import type { ConfigStatus } from "./config.ts";import type { PrivateRecords } from "../host/private-records.ts";-import type { RequestAuth } from "../request-auth.ts";+import { fromPairedDevice, type RequestAuth } from "../request-auth.ts";import { PASS, type RouteHandler } from "./table.ts";export interface StatusRouteDeps {@@ -147,6 +147,9 @@ export function createStatusRoutes(deps: StatusRouteDeps): RouteHandler {if (method === "POST" && path === "/api/keys/test") {const body = await readBody(req, 8192);const provider = body?.provider;+ // A phone tests the key it pasted, at the address the computer already uses: never a saved key, never its own URL.+ const pairedDevice = fromPairedDevice(req, auth);+ if (pairedDevice && typeof body?.key !== "string") return json(res, 400, { error: "Paste a key to test." });if (provider === "treg") {if (body?.key !== undefined && typeof body.key !== "string") return json(res, 400, { error: "key must be a string" });const key = typeof body?.key === "string" ? body.key.trim() : cfg.treg?.key?.trim() || "";@@ -166,7 +169,7 @@ export function createStatusRoutes(deps: StatusRouteDeps): RouteHandler {const key = typeof body?.key === "string" ? body.key.trim() : saved?.key?.trim() || "";if (!key) return json(res, 400, { error: "No key to test. Paste one or save one first." });if (key.length > 512) return json(res, 400, { error: "That does not look like an API key." });- const url = typeof body?.url === "string" && body.url.trim() ? body.url.trim() : (saved && "url" in saved ? saved.url : undefined);+ const url = !pairedDevice && typeof body?.url === "string" && body.url.trim() ? body.url.trim() : (saved && "url" in saved ? saved.url : undefined);res.setHeader("cache-control", "no-store");const verdict = await checkProviderKey({ provider: kind, key, url });if (kind !== "anthropic") {