Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Sign-in lists: an empty OMB_SIGNIN_* leaves Settings in charge, and a change the environment overrules is refused
milind-soni24591c93e2b8
diff --git a/deploy/.env.example b/deploy/.env.exampleindex 4ead3a93aa..9121dc32dc 100644--- a/deploy/.env.example+++ b/deploy/.env.example@@ -8,7 +8,8 @@ ENGINES=@anthropic-ai/claude-code# Optional: let people sign in with an emailed code instead of a pairing# code. Addresses or @domain entries, comma-separated. Admins get full-# access; members get chat and approvals only.+# access; members get chat and approvals only. Left empty, Settings → Remote+# access manages the lists; one set here decides its list instead.OMB_SIGNIN_EMAILS=OMB_SIGNIN_MEMBER_EMAILS=diff --git a/deploy/docker-compose.yml b/deploy/docker-compose.ymlindex 8876a0dd94..af2bc5bd2c 100644--- a/deploy/docker-compose.yml+++ b/deploy/docker-compose.yml@@ -35,12 +35,14 @@ services:OMB_PUBLIC_URL: https://${DOMAIN}OMB_SERVER_NAME: ${OMB_SERVER_NAME:-${DOMAIN}}# Who may sign in with an emailed code at /pair (docs/self-hosting.md,- # "Sign in with your email"). Empty means pairing codes only.- OMB_SIGNIN_EMAILS: ${OMB_SIGNIN_EMAILS:-}- OMB_SIGNIN_MEMBER_EMAILS: ${OMB_SIGNIN_MEMBER_EMAILS:-}- # Send the codes through your own mail server (same section of the docs).- OMB_SIGNIN_SMTP_URL: ${OMB_SIGNIN_SMTP_URL:-}- OMB_SIGNIN_SMTP_FROM: ${OMB_SIGNIN_SMTP_FROM:-}+ # "Sign in with your email"), and your own mail server for the codes.+ # With no value here, each is passed only when .env sets it. A list+ # .env leaves empty is Settings → Remote access's to manage; one that+ # names someone decides that list, and Settings refuses to change it.+ OMB_SIGNIN_EMAILS:+ OMB_SIGNIN_MEMBER_EMAILS:+ OMB_SIGNIN_SMTP_URL:+ OMB_SIGNIN_SMTP_FROM:healthcheck:test: ["CMD-SHELL", "curl -sf http://127.0.0.1:8799/api/health | grep -q openmausbot"]interval: 30sdiff --git a/docs/self-hosting.md b/docs/self-hosting.mdindex 4d795a1a89..6ec81ed8d2 100644--- a/docs/self-hosting.md+++ b/docs/self-hosting.md@@ -795,8 +795,11 @@ An entry is an address or `@domain` (everyone at that domain). Admins getthe same access as a pairing code from `openmausbot serve`; users get thechat-only scope, the same as `openmausbot pair --phone`. The same lists livein `config.json` under `signIn.admins` and `signIn.members` and can be changed-through the settings API without a restart; the environment variables win-when set, which is how a container or a service unit is bootstrapped.+through the settings API without a restart. An environment variable that+names someone wins over its list, which is how a container or a service unit+is bootstrapped; while it does, Settings refuses to change that list and says+which variable to change instead. One that is empty, as `.env.example` and the+Compose files leave them, leaves the list to Settings.The code itself comes from `accounts.openmausbot.com`, the OpenMausBotaccount service, so your server needs no email credentials. Your server asksdiff --git a/server/cli.ts b/server/cli.tsindex 2682c18613..ac4a505ceb 100644--- a/server/cli.ts+++ b/server/cli.ts@@ -44,7 +44,7 @@ import { checkReach, envPublicUrl, lanProblem, localLinkBase, noRouteLines, publimport { lanUrl } from "./lan-address.ts";import { isLoopbackHost } from "../electron/server-address.mjs";import { parseDuration } from "./headless/args.ts";-import type { AppConfig } from "./config.ts";+import { signInFromEnv, type AppConfig } from "./config.ts";import {cleanupTunnelOrigin,createTunnelAccount,@@ -444,7 +444,7 @@ export async function runStatus(options: CliOptions, io: CliIo = defaultIo()): Preturn body ? 0 : 1;}-/** Edits the sign-in allow-list in config.json, server live or stopped (read per request); OMB_SIGNIN_EMAILS wins when set. */+/** Edits the sign-in allow-list in config.json, server live or stopped (read per request); OMB_SIGNIN_EMAILS wins when it names someone. */export async function runAccess(options: CliOptions, io: CliIo = defaultIo()): Promise<number> {const file = join(options.dataDir, "config.json");let raw: Record<string, unknown> = {};@@ -462,7 +462,7 @@ export async function runAccess(options: CliOptions, io: CliIo = defaultIo()): Pconst list = (value: unknown) => parseAllowList(Array.isArray(value) ? value.map(String).join(",") : "");const admins = list(Reflect.get(current, "admins"));const members = list(Reflect.get(current, "members"));- const overridden = process.env.OMB_SIGNIN_EMAILS !== undefined || process.env.OMB_SIGNIN_MEMBER_EMAILS !== undefined;+ const overridden = Object.keys(signInFromEnv()).length > 0; // a variable that names nobody leaves the list to this fileconst write = async (next: { admins: string[]; members: string[] }) => {mkdirSync(options.dataDir, { recursive: true, mode: 0o700 });writeFileAtomic(file, `${JSON.stringify({ ...raw, signIn: next }, null, 2)}\n`, { mode: 0o600 });diff --git a/server/config.ts b/server/config.tsindex 18feef8615..d1d1edee0b 100644--- a/server/config.ts+++ b/server/config.ts@@ -1,6 +1,8 @@// ~/.openmausbot/config.json, with env vars overriding its credentials.import { readFileSync, mkdirSync, existsSync, renameSync } from "node:fs";import { dataHome } from "./data-dir.ts";+import { signInFromEnv } from "./sign-in-env.ts";+export { signInFromEnv };import { join } from "node:path";import { isDeepStrictEqual } from "node:util";import { z } from "zod";@@ -783,12 +785,8 @@ export function loadConfig(): AppConfig {if (process.env.OMB_OPENAI_IMAGE_KEY !== undefined) cfg.imageGen.key = process.env.OMB_OPENAI_IMAGE_KEY;if (process.env.OMB_CUSTOM_IMAGE_KEY !== undefined) cfg.imageGen.customApiKey = process.env.OMB_CUSTOM_IMAGE_KEY;if (process.env.OMB_GEMINI_IMAGE_KEY !== undefined) cfg.imageGen.geminiKey = process.env.OMB_GEMINI_IMAGE_KEY;- const splitEmails = (value: string) => value.split(/[,\s]+/).map((entry) => entry.trim().toLowerCase()).filter(Boolean);- if (process.env.OMB_SIGNIN_EMAILS !== undefined || process.env.OMB_SIGNIN_MEMBER_EMAILS !== undefined) {- cfg.signIn = { ...cfg.signIn };- if (process.env.OMB_SIGNIN_EMAILS !== undefined) cfg.signIn.admins = splitEmails(process.env.OMB_SIGNIN_EMAILS);- if (process.env.OMB_SIGNIN_MEMBER_EMAILS !== undefined) cfg.signIn.members = splitEmails(process.env.OMB_SIGNIN_MEMBER_EMAILS);- }+ const signInEnv = signInFromEnv();+ if (signInEnv.admins || signInEnv.members) cfg.signIn = { ...cfg.signIn, ...signInEnv };return cfg;}diff --git a/server/routes/config.ts b/server/routes/config.tsindex ddf69df1d0..f17f1ef5dd 100644--- a/server/routes/config.ts+++ b/server/routes/config.ts@@ -1,4 +1,5 @@import { liveSavePatch } from "../live-config.ts";+import { signInEnvRefusal } from "../sign-in-env.ts";import * as boat from "../boat.ts";import { boatCreateRecoverySnapshot, retireDeletedBoatCreate } from "../boat-create-idempotency.ts";import { boatDeletionSnapshot } from "../boat-delete-journal.ts";@@ -149,6 +150,9 @@ export function createConfigRoutes(deps: ConfigRouteDeps): RouteHandler {if (CLOUD_HOME && (patch.signIn?.admins?.length || patch.signIn?.members?.length)) {return json(res, 403, { ...userError("serverError.cloud.personal"), code: "cloud_personal" });}+ // Saved, it would be overruled by the environment at once: say so instead of answering 200 with nothing changed.+ const signInPinned = signInEnvRefusal(patch.signIn);+ if (signInPinned) return json(res, 409, { error: signInPinned, code: "signin_env" });if (patch.newBotDefaults) {if (patch.newBotDefaults.profile.modelSelection) {const checked = checkedModelSelection(patch.newBotDefaults.profile.modelSelection);diff --git a/server/sign-in-env.test.ts b/server/sign-in-env.test.tsnew file mode 100644index 0000000000..8716c1274b--- /dev/null+++ b/server/sign-in-env.test.ts@@ -0,0 +1,36 @@+import { describe, expect, it } from "vitest";++import { signInEnvRefusal, signInFromEnv } from "./sign-in-env.ts";++describe("sign-in lists from the environment", () => {+ it("leaves Settings in charge of a list whose variable is absent, empty or blank, as a compose file's ${X:-} passes it", () => {+ for (const value of [undefined, "", " ", " , "]) {+ expect(signInFromEnv({ OMB_SIGNIN_EMAILS: value, OMB_SIGNIN_MEMBER_EMAILS: value }), JSON.stringify(value)).toEqual({});+ }+ });++ it("takes a list the environment names, lower-cased, each variable on its own", () => {+ expect(signInFromEnv({ OMB_SIGNIN_EMAILS: "[email protected], @team.example.test", OMB_SIGNIN_MEMBER_EMAILS: "" }))+ .toEqual({ admins: ["[email protected]", "@team.example.test"] });+ expect(signInFromEnv({ OMB_SIGNIN_MEMBER_EMAILS: "[email protected] [email protected]" }))+ .toEqual({ members: ["[email protected]", "[email protected]"] });+ });+});++describe("a Settings change the environment would overrule", () => {+ const env = { OMB_SIGNIN_EMAILS: "", OMB_SIGNIN_MEMBER_EMAILS: "[email protected]" };++ it("is refused for the list the environment decides, naming the variable and where to change it", () => {+ const refusal = signInEnvRefusal({ admins: [], members: ["[email protected]", "[email protected]"] }, env);+ expect(refusal).toContain("OMB_SIGNIN_MEMBER_EMAILS is set where this server starts (its .env)");+ expect(refusal).toContain("as a member");+ expect(signInEnvRefusal({ admins: ["[email protected]"] }, { OMB_SIGNIN_EMAILS: "[email protected]" })).toContain("OMB_SIGNIN_EMAILS");+ });++ it("goes ahead for a list the environment leaves to Settings, and for the decided list sent back unchanged", () => {+ // Settings sends both lists whenever it changes one: the decided one comes back as the server showed it.+ expect(signInEnvRefusal({ admins: ["[email protected]"], members: ["[email protected]"] }, env)).toBeNull();+ expect(signInEnvRefusal({ members: ["[email protected]"] }, { OMB_SIGNIN_EMAILS: "", OMB_SIGNIN_MEMBER_EMAILS: "" })).toBeNull();+ expect(signInEnvRefusal(undefined, env)).toBeNull();+ });+});diff --git a/server/sign-in-env.ts b/server/sign-in-env.tsnew file mode 100644index 0000000000..c5d47b1b68--- /dev/null+++ b/server/sign-in-env.ts@@ -0,0 +1,38 @@+// Who may sign in with an emailed code can come from the environment (OMB_SIGNIN_EMAILS for admins,+// OMB_SIGNIN_MEMBER_EMAILS for members) instead of Settings. A list the environment names wins while the server runs.+// A variable that is set but names nobody leaves Settings in charge: Compose files pass `${OMB_SIGNIN_EMAILS:-}`, which+// is an empty string, not an absent variable, whenever .env does not set it.++export const SIGN_IN_ENV = { admins: "OMB_SIGNIN_EMAILS", members: "OMB_SIGNIN_MEMBER_EMAILS" } as const;++export type SignInList = keyof typeof SIGN_IN_ENV;++export function splitEmails(value: string): string[] {+ return value.split(/[,\s]+/).map((entry) => entry.trim().toLowerCase()).filter(Boolean);+}++/** The lists the environment decides: only those whose variable names at least one address. */+export function signInFromEnv(env: NodeJS.ProcessEnv = process.env): Partial<Record<SignInList, string[]>> {+ const lists: Partial<Record<SignInList, string[]>> = {};+ for (const list of ["admins", "members"] as const) {+ const named = splitEmails(env[SIGN_IN_ENV[list]] ?? "");+ if (named.length) lists[list] = named;+ }+ return lists;+}++/** Why a Settings change to the sign-in lists cannot take effect, or null. A list sent as the environment has it is+ * no change (Settings sends both lists whenever it changes one). */+export function signInEnvRefusal(asked: Partial<Record<SignInList, string[]>> | undefined, env: NodeJS.ProcessEnv = process.env): string | null {+ if (!asked) return null;+ const pinned = signInFromEnv(env);+ const same = (a: string[], b: string[]) => [...new Set(a)].sort().join(",") === [...new Set(b)].sort().join(",");+ for (const list of ["admins", "members"] as const) {+ const wanted = asked[list];+ const fixed = pinned[list];+ if (wanted === undefined || fixed === undefined || same(splitEmails(wanted.join(",")), fixed)) continue;+ const name = SIGN_IN_ENV[list];+ return `${name} is set where this server starts (its .env), and it decides who may sign in ${list === "admins" ? "as an admin" : "as a member"}, so this change would not take effect. Change ${name} there, or remove it to manage this list here.`;+ }+ return null;+}