Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { mkdtempSync, rmSync } from "node:fs";
import { createServer, type Server } from "node:http";
import type { AddressInfo } from "node:net";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { BotLogins } from "../bot-logins.ts";
import { json, readBody } from "../harness/http.ts";
import { VaultStore } from "../vault-store.ts";
import { ONE_PASSWORD_REFUSED, OnePasswordLogins, sdkOnePasswordClient } from "../onepassword.ts";
import { fakeOnePasswordSdk } from "../testing/fake-onepassword.ts";
import { requiredScope, type RequestAuth } from "../request-auth.ts";
import { createLoginRoutes, LOGIN_ERRORS } from "./logins.ts";
import { dispatchRoutes } from "./table.ts";
const PASSWORD = "fake-Settings-pass-1";
const NEW_PASSWORD = "fake-Settings-pass-2";
const KEY = "JBSWY3DPEHPK3PXP";
const TOKEN = "ops_fake-route-token";
const VAULT_PASSWORD = "fake-vault-pass";
const servers: Server[] = [];
const dirs: string[] = [];
afterEach(async () => {
await Promise.all(servers.splice(0).map((server) => new Promise((done) => server.close(done))));
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
async function serve() {
const dir = mkdtempSync(join(tmpdir(), "omb-login-routes-"));
dirs.push(dir);
const store = new VaultStore({ dataDir: dir });
const vaults = { v1: { title: "Shared with OMB", items: [
{ id: "i1", title: "Example", urls: ["https://vault.example.com"], username: "vault-user", password: VAULT_PASSWORD },
] } };
let down: Error | null = null;
const { sdk } = fakeOnePasswordSdk({ token: TOKEN, vaults, fail: () => down });
const onePassword = new OnePasswordLogins({ token: () => store.onePasswordToken(), grants: () => store.onePasswordGrants(), itemGrants: () => store.onePasswordItemGrants(),
connect: (token) => sdkOnePasswordClient(sdk, token, "0.0.0") });
const logins = new BotLogins(store, onePassword);
const routes = [createLoginRoutes(logins.routes((id) => ["pepper", "quill"].includes(id)))];
const answers: string[] = [];
const server = createServer(async (req, res) => {
const url = new URL(req.url ?? "/", "http://localhost");
const auth = { kind: "loopback", scopes: ["admin"] } as unknown as RequestAuth;
if (!await dispatchRoutes(routes, { req, res, url, path: url.pathname, method: req.method ?? "GET", auth, json, readBody })) json(res, 404, { from: "inline routes" });
});
servers.push(server);
await new Promise<void>((ready) => server.listen(0, "127.0.0.1", ready));
const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
const call = async (method: string, path: string, body?: unknown, type = "application/json") => {
const response = await fetch(base + path, { method, headers: { "content-type": type }, ...(body === undefined ? {} : { body: JSON.stringify(body) }) });
const text = await response.text();
answers.push(text);
return { status: response.status, body: JSON.parse(text), cache: response.headers.get("cache-control") };
};
return { store, logins, call, answers, vaults, setDown: (error: Error | null) => { down = error; } };
}
describe("Settings → Logins routes", () => {
it("are for admins only", () => {
for (const method of ["GET", "POST"]) expect(requiredScope(method, "/api/logins")).toBe("admin");
for (const method of ["PATCH", "DELETE"]) expect(requiredScope(method, "/api/logins/abc")).toBe("admin");
expect(requiredScope("PATCH", "/api/logins/1password/vaults/v1")).toBe("admin");
expect(requiredScope("PATCH", "/api/logins/1password/items/v1/i1")).toBe("admin");
expect(requiredScope("PUT", "/api/logins/1password/items")).toBe("admin");
});
it("add, list, change and delete a login, and never return its password or 2FA key", async () => {
const { store, call, answers } = await serve();
const added = await call("POST", "/api/logins", { site: "https://accounts.example.com/login", username: " [email protected] ", password: PASSWORD, totp: KEY, bots: ["pepper"] });
expect(added.status).toBe(201);
expect(added.body.login).toMatchObject({ site: "example.com", hosts: ["accounts.example.com"], exactHost: false, username: "[email protected]", hasTotp: true, source: "settings", bots: ["pepper"] });
const id = added.body.login.id;
const listed = await call("GET", "/api/logins");
expect(listed).toMatchObject({ status: 200, cache: "no-store" });
expect(listed.body).toEqual({ logins: [added.body.login], onePassword: { connected: false } });
expect((await call("PATCH", `/api/logins/${id}`, { password: NEW_PASSWORD, bots: "all" })).body.login).toMatchObject({ bots: "all", hasTotp: true });
expect(store.get(id)?.password).toBe(NEW_PASSWORD);
expect((await call("PATCH", `/api/logins/${id}`, { totp: null })).body.login.hasTotp).toBe(false);
expect((await call("PATCH", `/api/logins/${id}`, {})).status).toBe(200);
expect(store.get(id)?.password).toBe(NEW_PASSWORD);
for (const answer of answers) for (const secret of [PASSWORD, NEW_PASSWORD, KEY]) expect(answer).not.toContain(secret);
expect((await call("PATCH", `/api/logins/${id}`, { exactHost: true })).body.login).toMatchObject({ exactHost: true, hosts: ["accounts.example.com"] });
expect((await call("PATCH", `/api/logins/${id}`, { exactHost: "yes" })).body).toEqual({ error: LOGIN_ERRORS.exactHost });
expect(store.get(id)?.exactHost).toBe(true);
expect((await call("PATCH", `/api/logins/${id}`, { site: "https://d1abc.cloudfront.net/sign-in" })).body.login)
.toMatchObject({ site: "d1abc.cloudfront.net", hosts: ["d1abc.cloudfront.net"], exactHost: true, username: "[email protected]" });
expect(store.get(id)?.password).toBe(NEW_PASSWORD);
expect(store.usableFor("d1abc.cloudfront.net", "pepper").map((login) => login.id)).toEqual([id]);
expect(store.usableFor("accounts.example.com", "pepper")).toEqual([]);
expect((await call("PATCH", `/api/logins/${id}`, { site: "javascript:alert(1)" })).body).toEqual({ error: LOGIN_ERRORS.site });
expect(store.get(id)?.site).toBe("d1abc.cloudfront.net");
expect((await call("PATCH", `/api/logins/${id}`, { site: " " })).body).toEqual({ error: LOGIN_ERRORS.site });
const other = (await call("POST", "/api/logins", { site: "portal.example.edu", username: "[email protected]", password: PASSWORD })).body.login.id;
expect(await call("PATCH", `/api/logins/${id}`, { site: "https://portal.example.edu/login" })).toMatchObject({ status: 409, body: { error: LOGIN_ERRORS.taken } });
expect(store.get(id)?.site).toBe("d1abc.cloudfront.net");
expect(await call("DELETE", `/api/logins/${other}`)).toMatchObject({ status: 200 });
expect(await call("DELETE", `/api/logins/${id}`)).toMatchObject({ status: 200, body: { ok: true } });
expect((await call("GET", "/api/logins")).body.logins).toEqual([]);
expect((await call("DELETE", `/api/logins/${id}`)).status).toBe(404);
});
it.each([
[{ site: "not a site", username: "a", password: PASSWORD }, LOGIN_ERRORS.site],
[{ site: "javascript:alert(1)", username: "a", password: PASSWORD }, LOGIN_ERRORS.site],
[{ site: "example.com", username: "a" }, LOGIN_ERRORS.password],
[{ site: "example.com", username: "a", password: "" }, LOGIN_ERRORS.password],
[{ site: "example.com", username: "a", password: "x".repeat(513) }, LOGIN_ERRORS.long],
[{ site: "example.com", username: "a", password: PASSWORD, totp: "not a key" }, LOGIN_ERRORS.totp],
[{ site: "example.com", username: "a", password: PASSWORD, bots: ["nobody"] }, LOGIN_ERRORS.bots],
[{ site: "example.com", username: "a", password: PASSWORD, exactHost: 1 }, LOGIN_ERRORS.exactHost],
])("refuses a login it cannot save: %j", async (body, error) => {
const { call } = await serve();
expect(await call("POST", "/api/logins", body)).toMatchObject({ status: 400, body: { error } });
expect((await call("GET", "/api/logins")).body.logins).toEqual([]);
});
it("connects 1Password with a token it checks first, lists its logins read-only, and never returns the token", async () => {
const { store, call, answers } = await serve();
expect((await call("GET", "/api/logins")).body).toEqual({ logins: [], onePassword: { connected: false } });
for (const token of ["", "ops_ has spaces", "ops_wrong-fake-token", "eyJhbGciOiJFUzI1NiJ9.fake-connect-token"]) {
expect((await call("POST", "/api/logins/1password", { token })).status, token).toBe(400);
}
expect((await call("POST", "/api/logins/1password", { token: "ops_wrong-fake-token" })).body.error).toBe(ONE_PASSWORD_REFUSED);
// A Connect token or a password is not a service account token: said so before 1Password is asked.
expect((await call("POST", "/api/logins/1password", { token: "eyJhbGciOiJFUzI1NiJ9.fake-connect-token" })).body.error).toBe(LOGIN_ERRORS.notServiceAccount);
expect(store.onePasswordToken()).toBeNull();
expect(await call("POST", "/api/logins/1password", { token: TOKEN })).toMatchObject({ status: 200, body: { onePassword: { connected: true, vaults: [{ id: "v1", title: "Shared with OMB", items: 1, bots: "all" }], items: 1 } } });
expect(store.onePasswordToken()).toBe(TOKEN);
const listed = await call("GET", "/api/logins");
expect(listed.body.logins).toEqual([expect.objectContaining({ site: "example.com", hosts: ["vault.example.com"], username: "vault-user", source: "1password", bots: "all" })]);
const id = listed.body.logins[0].id as string;
for (const method of ["PATCH", "DELETE"]) {
expect(await call(method, `/api/logins/${encodeURIComponent(id)}`, method === "PATCH" ? { password: "fake-x-pass" } : undefined))
.toMatchObject({ status: 409, body: { error: LOGIN_ERRORS.readOnly } });
}
expect((await call("POST", "/api/logins/1password/refresh", {})).body.onePassword).toMatchObject({ connected: true, items: 1 });
expect(await call("DELETE", "/api/logins/1password")).toMatchObject({ status: 200, body: { onePassword: { connected: false } } });
expect(store.onePasswordToken()).toBeNull();
expect((await call("GET", "/api/logins")).body.logins).toEqual([]);
for (const answer of answers) for (const secret of [TOKEN, VAULT_PASSWORD]) expect(answer).not.toContain(secret);
});
it("keeps a vault's logins to the bots chosen for it, and only for a vault the token reads", async () => {
const { store, logins, call } = await serve();
await call("POST", "/api/logins/1password", { token: TOKEN });
expect(await call("PATCH", "/api/logins/1password/vaults/v1", { bots: ["quill"] }))
.toMatchObject({ status: 200, body: { onePassword: { vaults: [{ id: "v1", bots: ["quill"] }] } } });
expect(store.onePasswordGrants()).toEqual({ v1: ["quill"] });
expect(await logins.candidates("pepper", "vault.example.com")).toEqual([]);
expect(logins.sitesFor("pepper")).toEqual([]);
expect((await logins.candidates("quill", "vault.example.com")).map((login) => login.username)).toEqual(["vault-user"]);
expect((await call("GET", "/api/logins")).body.logins[0].bots).toEqual(["quill"]);
expect((await call("PATCH", "/api/logins/1password/vaults/v1", { bots: ["nobody"] })).body).toEqual({ error: LOGIN_ERRORS.bots });
expect((await call("PATCH", "/api/logins/1password/vaults/v1", {})).status).toBe(400);
expect((await call("PATCH", "/api/logins/1password/vaults/v9", { bots: "all" })).status).toBe(404);
expect((await call("PATCH", "/api/logins/1password/vaults/v1", { bots: "all" })).body.onePassword.vaults[0].bots).toBe("all");
expect(store.onePasswordGrants()).toEqual({});
expect((await logins.candidates("pepper", "vault.example.com")).map((login) => login.username)).toEqual(["vault-user"]);
});
it("looks in 1Password again before calling an item gone, and never calls it gone while 1Password cannot be read", async () => {
const { store, call, vaults, setDown } = await serve();
await call("POST", "/api/logins/1password", { token: TOKEN });
// Added in 1Password after the last load: found on a second look, not refused.
vaults.v1.items.push({ id: "i2", title: "New", urls: ["https://new.example.com"], username: "new-user", password: VAULT_PASSWORD });
expect((await call("PATCH", "/api/logins/1password/items/v1/i2", { bots: ["pepper"] })).status).toBe(200);
expect(store.onePasswordItemGrants()).toEqual({ "v1:i2": ["pepper"] });
// 1Password unreadable: an item not in the last load is "try again", not "no such item".
setDown(new Error("fetch failed"));
const unread = await call("PATCH", "/api/logins/1password/items/v1/i3", { bots: [] });
expect(unread.status).toBe(503);
// Handing an item back to its vault's choice never waits on 1Password.
expect((await call("PATCH", "/api/logins/1password/items/v1/i2", { bots: null })).status).toBe(200);
expect(store.onePasswordItemGrants()).toEqual({});
});
it("takes every item's own bots at once from another computer, dropping the ones it no longer has", async () => {
const { store, call } = await serve();
await call("POST", "/api/logins/1password", { token: TOKEN });
await call("PATCH", "/api/logins/1password/items/v1/i1", { bots: "all" });
// An item not loaded here yet is kept too, so it opens no wider than where it was chosen.
const sent = await call("PUT", "/api/logins/1password/items", { items: { "v1:i9": [], "v2:x1": ["quill"] } });
expect(sent).toMatchObject({ status: 200, body: { items: 2 } });
expect(store.onePasswordItemGrants()).toEqual({ "v1:i9": [], "v2:x1": ["quill"] });
expect((await call("PUT", "/api/logins/1password/items", { items: { "v1:i1": ["nobody"] } })).status).toBe(400);
expect((await call("PUT", "/api/logins/1password/items", { items: { "v1/i1": [] } })).status).toBe(400);
expect((await call("PUT", "/api/logins/1password/items", { items: [] })).status).toBe(400);
expect(store.onePasswordItemGrants()).toEqual({ "v1:i9": [], "v2:x1": ["quill"] });
// keep: only the items named change; every other one stays as it was.
expect((await call("PUT", "/api/logins/1password/items", { items: { "v1:i9": ["quill"] }, keep: true })).status).toBe(200);
expect(store.onePasswordItemGrants()).toEqual({ "v1:i9": ["quill"], "v2:x1": ["quill"] });
expect((await call("PUT", "/api/logins/1password/items", { items: {} })).status).toBe(200);
expect(store.onePasswordItemGrants()).toEqual({});
});
it("keeps one 1Password item to its own bots ahead of its vault's, and hands it back on null", async () => {
const { store, logins, call, answers } = await serve();
await call("POST", "/api/logins/1password", { token: TOKEN });
await call("PATCH", "/api/logins/1password/vaults/v1", { bots: [] });
expect(await logins.candidates("pepper", "vault.example.com")).toEqual([]);
const opened = await call("PATCH", "/api/logins/1password/items/v1/i1", { bots: ["pepper"] });
expect(opened.status).toBe(200);
expect(opened.body.logins[0]).toMatchObject({ bots: ["pepper"], onePassword: { item: "v1:i1", ownBots: true } });
expect(store.onePasswordItemGrants()).toEqual({ "v1:i1": ["pepper"] });
expect((await logins.candidates("pepper", "vault.example.com")).map((login) => login.username)).toEqual(["vault-user"]);
expect(await logins.candidates("quill", "vault.example.com")).toEqual([]);
// The vault's own choice does not reopen or close an item with bots of its own.
await call("PATCH", "/api/logins/1password/vaults/v1", { bots: "all" });
expect(await logins.candidates("quill", "vault.example.com")).toEqual([]);
expect((await call("PATCH", "/api/logins/1password/items/v1/i1", { bots: ["nobody"] })).body).toEqual({ error: LOGIN_ERRORS.bots });
expect((await call("PATCH", "/api/logins/1password/items/v1/i1", {})).status).toBe(400);
expect((await call("PATCH", "/api/logins/1password/items/v1/i9", { bots: "all" })).status).toBe(404);
expect((await call("PATCH", "/api/logins/1password/items/v9/i1", { bots: "all" })).status).toBe(404);
expect(store.onePasswordItemGrants()).toEqual({ "v1:i1": ["pepper"] });
const back = await call("PATCH", "/api/logins/1password/items/v1/i1", { bots: null });
expect(back.body.logins[0]).toMatchObject({ bots: "all", onePassword: { item: "v1:i1", ownBots: false } });
expect(store.onePasswordItemGrants()).toEqual({});
expect((await logins.candidates("quill", "vault.example.com")).map((login) => login.username)).toEqual(["vault-user"]);
for (const answer of answers) { expect(answer).not.toContain(VAULT_PASSWORD); expect(answer).not.toContain(TOKEN); }
});
it("takes changes as JSON only, so another page's form cannot post one", async () => {
const { call } = await serve();
expect((await call("POST", "/api/logins", { site: "example.com", password: PASSWORD }, "text/plain")).status).toBe(415);
expect((await call("POST", "/api/logins/1password", { token: TOKEN }, "text/plain")).status).toBe(415);
});
});