Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
Harden sign-in, and show what a Bot is doing (#87)
David McKay018d4932faa6
diff --git a/.env.example b/.env.exampleindex 63fa2d52..ce629bfa 100644--- a/.env.example+++ b/.env.example@@ -11,10 +11,11 @@ TENANT_PACKAGE_DIR=../examples/fintech# deployment mints, so its own conversations stay identifiable. Unset, the tenant package's id is# used, which tells two packages apart but not two copies of one.# DEPLOYMENT_ID=-# Sign-in. All of this is commented out, and a clone with none of it set is one administrator with-# no sign-in at all, which is how you reach the product without registering an OAuth client first.-# Somewhere other people can get to, an unconfigured deployment refuses to start rather than serving-# an open one. OPENBOT_SINGLE_USER=true says you meant it.+# Sign-in. The line below runs the deployment as one administrator with no sign-in at all, which is+# how you reach the product without registering an OAuth client first. Delete it and configure a+# provider before anybody else can reach this: while it is set, every visitor is an administrator.+# With no provider and this line gone, the deployment refuses to start rather than guessing.+OPENBOT_SINGLE_USER=true## Configure ANY ONE of the three providers to turn sign-in on. Configure several and the sign-in# screen offers several, which is the normal shape for a company mid-migration.diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 945ff421..e279fc5f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,7 +13,15 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.Two configurations now refuse to start:- A provider configured with no `INITIAL_ADMIN_EMAILS`. Set it to at least one address.-- No provider at all with `NODE_ENV=production`. Configure one, or set `OPENBOT_SINGLE_USER=true`.+- No provider at all and no `OPENBOT_SINGLE_USER=true`. Configure a provider, or set that to say you+ meant a deployment where every visitor is one administrator. This no longer depends on `NODE_ENV`,+ which is unset by default and so let exactly the dangerous case through. A deployment already+ running open needs the line added before it will start again.++Registering an OpenID Connect provider needs every host in its discovery document in+`TRUSTED_ORIGINS`, not only the issuer. Better Auth 1.7 checks each endpoint it finds, so a Google+issuer also needs `oauth2.googleapis.com` and `openidconnect.googleapis.com`. Registration is+refused with the untrusted host named.Sessions survive and nobody signs in again.@@ -63,8 +71,37 @@ Sessions survive and nobody signs in again.path, rather than reporting an element it was never about.- **`COMPUTER_SANDBOX=on`** turns on Chromium's own sandbox where the host permits user namespaces.Which way it went is printed at start-up either way.+- **You can watch what a Bot is doing, not only what it is looking at.** The screen answered half the+ question: a Bot spending two minutes in a terminal showed a blank browser and one grey line per+ command, with the output nowhere. A command line in the transcript now opens to show what it+ printed, its exit code, and whether it was cut short or stopped. Beside the screen there is an+ Activity tab carrying every command, file read, file write and listing as they happen, newest+ first, with a count on the tab so a Bot working away from the browser is visible without switching+ to it. A saved file shows its path and size, never its contents. This is a live view of the open+ conversation; the record is still the audit trail.+- **Sign-in is on the audit trail.** Rows for signing in, for being refused, and for the configured+ administrator list granting somebody the role. Two questions had no answer before: who granted+ themselves administrator by editing `INITIAL_ADMIN_EMAILS`, and whether somebody just removed had+ ever been here, since removing them deletes the sessions that were the only evidence. A trail that+ is unavailable never blocks a sign-in.### Fixed+- **A deployment with no identity provider came up open by default.** Covered under Changed above,+ and listed here too because it is the one on this list that was reachable from the internet.+- **Registering a company's identity provider was owned by whoever registered it.** Better Auth+ answers its own listing route with only the providers the person asking registered, and refuses a+ removal from anybody else, so a second administrator opened the Identity providers screen, found+ it empty, and registered one that already existed. Worse, the row cascaded from that person's user+ row: deleting the administrator who set sign-in up deleted the company's sign-in with them. What is+ registered is a fact about the deployment, so reads and removals go through OpenBot's own+ administrator-only routes against the whole table, and a provider outlives the person who added it.+- **A customer's client secret was in the clear.** The SSO plugin writes `oidc_config` and+ `saml_config` as plaintext JSON, with the OAuth client secret for that company's directory inside+ them: the one secret here not going through `KEY_ENCRYPTION_KEY`. Both are now encrypted at rest.+ Rows written before this still read, and are re-encrypted the next time they are written. OAuth+ access and refresh tokens use Better Auth's own encryption, keyed on `BETTER_AUTH_SECRET`.+- **A failed provider registration looked like a button that did not work.** The error was rendered+ on the page behind the dialog, which was covering it.- **A Bot could become root inside its container.** `sudo` was granted as `NOPASSWD: ALL`, and thecomment above it named the two conditions that made that acceptable: the container being one Bot'salone, and not holding a database. The image meets neither, because the supervisor is deliberately@@ -119,14 +156,20 @@ Sessions survive and nobody signs in again.### Changed-- **A deployment with no identity provider is one administrator, without a flag.** That is how a- fresh clone reaches the product. Where `NODE_ENV=production`, an unconfigured deployment now- refuses to start instead, because a public URL where every visitor is an administrator is silent- and looks like it works. `OPENBOT_SINGLE_USER=true` replaces `OPENBOT_DEV_NO_AUTH`, which is still- honoured, and is how somebody says they meant an open deployment.-- **Requires Better Auth 1.7**, which adds an `issuer` to every account. Migrations `0002` to `0004`- add the column, backfill existing rows with their provider's real issuer, and then make it- required, so nobody is asked to sign in again.+- **Running with no sign-in takes a flag and nothing else.** It used to be locked with+ `NODE_ENV=production`, which is exactly backwards: `NODE_ENV` is unset unless somebody sets it, so+ a container on a VM with a hand-written env file and no identity provider served every visitor on+ the internet as an administrator, silently, because nothing looked wrong from the outside. A+ deployment with no provider now refuses to start unless `OPENBOT_SINGLE_USER=true` says it was+ meant. `.env.example` ships that line switched on, so a clone still runs with no configuration at+ all, and the line is greppable in a way a default never was. `OPENBOT_DEV_NO_AUTH` is still+ honoured.+- **Requires Better Auth 1.7**, which adds an `issuer` to every account. Migrations `0002` and `0003`+ add the column and backfill existing rows with their provider's real issuer, so nobody is asked to+ sign in again. The column stays nullable on purpose: a rolling deploy runs migrations and then+ serves from old and new replicas at once, and an old replica writes an account without it, so+ making it required in the same release would break the first sign-in of everybody who landed on a+ replica that had not been replaced yet. The constraint belongs to a later release.- **Where a Bot's computer runs is now a plug.** One `ComputerProvider` interface sits under thegateway, with the Docker supervisor as one implementation and a shared computer as another. Acomputer somewhere else is an adapter rather than a change to the governed path. Thanks todiff --git a/README.md b/README.mdindex 12e58624..855a9ad1 100644--- a/README.md+++ b/README.md@@ -27,7 +27,7 @@ your own machine.> **Alpha, and under active development.** OpenBot is early. Expect rough edges and bugs, and expect things to move. Issues and pull requests are welcome.-> **Runs on your machine.** Everything below is written for a laptop. With no identity provider configured OpenBot admits every request as one administrator, so a fresh clone reaches the product without registering an OAuth client. [Sign-in](#sign-in) turns that off.+> **Runs on your machine.** Everything below is written for a laptop. `.env.example` carries `OPENBOT_SINGLE_USER=true`, which admits every request as one administrator, so a fresh clone reaches the product without registering an OAuth client first. [Sign-in](#sign-in) turns that off, and is required before anybody else can reach the deployment.## What it is@@ -124,7 +124,7 @@ as one replica for now.| -------------------- | ------------------------------------------------------------------ || `/` | Start and browse channels. || `/agents` | Create, edit, duplicate, hide, delete, and launch coworkers. |-| `/channel/:id` | Converse with one coworker and view its live screen/profile panel. |+| `/channel/:id` | Converse with one coworker, watch its screen, and see what it ran. || `/bot` | Direct chat with a Bot; `?agent=<id>` selects one. || `/skills` | Create and enable personal skills. || `/settings` | User preferences. |@@ -143,6 +143,7 @@ as one replica for now.- **A shell, not just a browser**: a Bot can run a command in its workspace, install what it needs, and process a file it saved. Through the same gate as everything else, so a rule can refuse a shell outright or refuse particular commands, and the command is on the record either way. The command inherits PATH, locale, terminal and proxy variables, not the rest of the deployment's environment.- **The gateway is the only way in**: it resolves the target from a server-held snapshot, evaluates the policy, writes the audit row, and only then calls the computer. There is no path that acts without the record existing first.- **CEL policy, fail closed**: rules can inspect `tool.name`, `intent`, `bot.id`, `actor.id`, `page.url`, `page.host`, `element.*`, `key`, `file.*` and `mcp.*`. Deny is evaluated before allow, a missing policy permits nothing, and a broken rule refuses rather than opens.+- **Watch what it is doing**: the screen shows what a Bot is looking at, and the Activity tab beside it shows what it ran, read and saved, with the output. A command line in the transcript opens to the same thing. A saved file shows its path and size, never its contents.- **Take the wheel**: a Bot that hits a login wall or a 2FA prompt asks for help. Control is handed over in the same panel and recorded as `computer.help_requested`, `computer.control_taken` and `computer.control_released`. While a person is driving, Bot actions are refused rather than queued.- **Secrets never enter the transcript**: the trail records that a secret was requested and how long it was, not what it said.- **Bring your own agent**: any AG-UI endpoint is a Bot, on a framework or hand written. Endpoints are validated with the same target checks used for browser navigation, and an auth header is stored write-only.@@ -193,7 +194,7 @@ Settings worth knowing:| Variable | Use || ------------------------------------ | ------------------------------------------------------------------------- |-| `OPENBOT_SINGLE_USER` | Admits every request as one administrator where an unconfigured deployment would otherwise refuse to start. |+| `OPENBOT_SINGLE_USER` | Admits every request as one administrator. Required when no identity provider is configured; `.env.example` ships it on. || `OPENAI_BASE_URL` | Answers the OpenAI-shaped calls from somewhere else: a gateway, a proxy. || `ANTHROPIC_BASE_URL`, `GOOGLE_GENERATIVE_AI_BASE_URL` | The same, for those two APIs. || `COMPUTER_TOKEN` | Secret every Bot computer request must present. `start.sh` sets one. |@@ -231,9 +232,11 @@ More detail: [docs/architecture.md](docs/architecture.md).## Sign in-Nothing configured means one administrator and no sign-in, which is how a fresh clone reaches the-product. Configure **any one** of Google, Microsoft or Okta to turn sign-in on. Configure more than-one and the sign-in screen offers each of them.+`.env.example` ships `OPENBOT_SINGLE_USER=true`, which is one administrator and no sign-in: how a+fresh clone reaches the product without registering an OAuth client first. Delete that line and+configure **any one** of Google, Microsoft or Okta before anybody else can reach the deployment.+With neither, it refuses to start rather than admitting everybody as an administrator. Configure+more than one provider and the sign-in screen offers each of them.These four are needed whichever you pick:@@ -264,6 +267,10 @@ OKTA_OAUTH_ISSUER=https://example.okta.com/oauth2/defaultRestart. Accounts, sessions and roles are stored in the same PostgreSQL database as everything else.+A company's own SAML or OpenID Connect provider is registered while the deployment runs, under+Admin → Identity providers, and routed by email domain. An OIDC registration needs every host in the+provider's discovery document listed in `TRUSTED_ORIGINS`, not only the issuer.+- `INITIAL_ADMIN_EMAILS` is required, because nothing else grants the administrator role and noscreen can promote somebody afterwards. It is re-read on every sign-in, so editing it takes effectthe next time that person signs in.diff --git a/app/src/components/computer/activity-log.tsx b/app/src/components/computer/activity-log.tsxnew file mode 100644index 00000000..33353c29--- /dev/null+++ b/app/src/components/computer/activity-log.tsx@@ -0,0 +1,122 @@+/**+ * What the Bot has been doing on its computer, beside the screen that shows what it is looking at.+ *+ * The screen answered half the question. A Bot that spends two minutes in a terminal installing a+ * package showed a blank browser and one grey line per command in the transcript, so the honest+ * answer to "what is it doing" was "something, on a machine holding your logins". This is the other+ * half: every command, what it printed, and what it exited with, as it happens.+ *+ * This session only. The record is the audit trail, which is on the server and survives a reload;+ * this is a window, and it is allowed to be empty when a tab is opened on a conversation that+ * already happened.+ */+import { IconFile, IconFolder, IconTerminal2 } from "@tabler/icons-react";+import { useSyncExternalStore } from "react";+import {+ activityFor,+ type ComputerActivity,+ subscribeToActivity,+} from "@/lib/computers/activity";+import { CommandOutput } from "./command-output";++const ICONS = {+ command: IconTerminal2,+ read_file: IconFile,+ write_file: IconFile,+ list_files: IconFolder,+} as const;++/** What the line says was done. The subject beside it says what it was done to. */+const LABELS = {+ command: "Ran",+ read_file: "Read",+ write_file: "Saved",+ list_files: "Listed",+} as const;++function timeOf(at: number): string {+ return new Date(at).toLocaleTimeString(undefined, {+ hour: "2-digit",+ minute: "2-digit",+ second: "2-digit",+ });+}++function Entry({ entry }: { entry: ComputerActivity }) {+ const Icon = ICONS[entry.kind];+ const failed =+ entry.refused === true ||+ (typeof entry.exitCode === "number" && entry.exitCode !== 0);++ return (+ <li className="border-border/60 border-b py-2 last:border-b-0">+ <div className="flex items-baseline gap-2">+ <Icon+ aria-hidden+ className={`size-3.5 shrink-0 translate-y-0.5 ${+ failed ? "text-destructive" : "text-muted-foreground"+ }`}+ />+ <span className="shrink-0 text-muted-foreground text-xs">+ {LABELS[entry.kind]}+ </span>+ {/*+ The command wraps rather than truncating. In the transcript one line per call is what keeps+ it readable; here the whole point is to see exactly what ran, and a long command with its+ middle cut out is the one thing this pane must not do.+ */}+ <code className="min-w-0 break-all font-mono text-xs">+ {entry.subject}+ </code>+ <span className="ml-auto shrink-0 text-[10px] text-muted-foreground tabular-nums">+ {timeOf(entry.at)}+ </span>+ </div>++ {entry.refused === true ? (+ <p className="mt-1 pl-5 text-destructive text-xs">+ {entry.output || "A boundary refused it."}+ </p>+ ) : (+ <div className="mt-1 max-h-64 overflow-auto pl-5">+ <CommandOutput+ {...(entry.exitCode !== undefined+ ? { exitCode: entry.exitCode }+ : {})}+ output={entry.output}+ timedOut={entry.timedOut === true}+ truncated={entry.truncated === true}+ />+ </div>+ )}+ </li>+ );+}++export function ActivityLog({ computerId }: { computerId: string }) {+ const entries = useSyncExternalStore(+ subscribeToActivity,+ () => activityFor(computerId),+ () => activityFor(computerId),+ );++ if (entries.length === 0) {+ return (+ <p className="py-6 text-center text-muted-foreground text-sm">+ Nothing yet. Commands the Bot runs, and files it reads, appear here as+ they happen.+ </p>+ );+ }++ // Newest first: what somebody watching wants is what just happened, without scrolling for it.+ const newestFirst = [...entries].reverse();++ return (+ <ul className="min-w-0">+ {newestFirst.map((entry) => (+ <Entry entry={entry} key={entry.id} />+ ))}+ </ul>+ );+}diff --git a/app/src/components/computer/command-output.tsx b/app/src/components/computer/command-output.tsxnew file mode 100644index 00000000..502c61a5--- /dev/null+++ b/app/src/components/computer/command-output.tsx@@ -0,0 +1,55 @@+/**+ * What a command printed.+ *+ * Shown behind the chevron on the transcript line and in the pane beside the screen, so the two say+ * the same thing in the same shape. Monospace and pre-wrapped, because shell output is laid out in+ * columns and reflowing it makes `ls -l` unreadable.+ *+ * A command that printed nothing says so. Blank space under an expanded line reads as a component+ * that failed to render, and "it printed nothing" is a real and common answer.+ */+export function CommandOutput({+ output,+ exitCode,+ truncated,+ timedOut,+}: {+ output: string;+ /** Absent for a file read or a listing, which have no exit status. */+ exitCode?: number;+ /** The far side cut the output short. Said out loud rather than left to be inferred. */+ truncated?: boolean;+ /** The command ran too long and was stopped, so what is here is not the whole story. */+ timedOut?: boolean;+}) {+ const failed = typeof exitCode === "number" && exitCode !== 0;++ return (+ <div className="space-y-1.5">+ {truncated ? (+ <p className="text-muted-foreground text-xs">+ Output was cut short at the start. What follows is the end of it.+ </p>+ ) : null}+ {output ? (+ <pre className="whitespace-pre-wrap break-words font-mono text-xs leading-relaxed">+ {output}+ </pre>+ ) : (+ <p className="text-muted-foreground text-xs italic">+ It printed nothing.+ </p>+ )}+ {timedOut ? (+ <p className="text-destructive text-xs">+ It ran too long and was stopped.+ </p>+ ) : null}+ {failed ? (+ <p className="text-amber-600 text-xs dark:text-amber-500">+ Exit code {exitCode}.+ </p>+ ) : null}+ </div>+ );+}diff --git a/app/src/lib/computers/activity.ts b/app/src/lib/computers/activity.tsnew file mode 100644index 00000000..fb75f2f2--- /dev/null+++ b/app/src/lib/computers/activity.ts@@ -0,0 +1,88 @@+/**+ * What a Bot has been doing on its computer, other than browsing.+ *+ * The screen answers "what is it looking at" and nothing answered "what is it doing". A shell+ * command rendered in the transcript as one grey line, `Ran a command rg --version`, with the output+ * nowhere: the model saw it, decided which part mattered, and the person watching had to take its+ * word. The same was true of reading a file and listing a folder. That is a poor deal on a machine+ * holding somebody's logins, and it is the thing an operator asks about first.+ *+ * A live view of this session rather than a record. The record is the audit trail, which is on the+ * server, survives a reload and is what an investigation reads; this is the pane beside the screen,+ * and it is allowed to be gone when the tab is closed. Keeping it in the browser means no new+ * endpoint, no polling, and no second copy of command output in the database.+ *+ * Written from the tool handlers, which run exactly once per call. A tool's `render` runs on every+ * re-render, so recording from there would append the same command repeatedly.+ */++/** One thing a Bot did on its computer. */+export type ComputerActivity = {+ id: string;+ /** When it happened, for the timestamp beside it. */+ at: number;+ kind: "command" | "read_file" | "write_file" | "list_files";+ /** The command, or the path. What the line says it acted on. */+ subject: string;+ /** What came back. Empty when a command printed nothing, which is worth showing as itself. */+ output: string;+ /** Present for a command. Non-zero is drawn as a failure. */+ exitCode?: number;+ /** A boundary refused it. Drawn differently from a command that ran and failed. */+ refused?: boolean;+ /** The far side cut the output short, or stopped the command. Said out loud rather than implied. */+ truncated?: boolean;+ timedOut?: boolean;+};++/**+ * The most recent entries per computer.+ *+ * Bounded, because a Bot working through a large job runs a lot of commands and this is a pane, not+ * an archive. The oldest go first: what somebody watching wants is what just happened.+ */+const LIMIT = 200;++const byComputer = new Map<string, ComputerActivity[]>();+const listeners = new Set<() => void>();++/** A stable empty array, so `useSyncExternalStore` does not see a new value on every render. */+const NONE: ComputerActivity[] = [];++let counter = 0;++export function recordActivity(+ computerId: string,+ entry: Omit<ComputerActivity, "id" | "at">,+): void {+ counter += 1;+ const existing = byComputer.get(computerId) ?? [];+ const next = [+ ...existing,+ { ...entry, id: `activity-${counter}`, at: Date.now() },+ ];+ byComputer.set(computerId, next.slice(-LIMIT));+ for (const listener of listeners) listener();+}++export function activityFor(computerId: string): ComputerActivity[] {+ return byComputer.get(computerId) ?? NONE;+}++export function subscribeToActivity(listener: () => void): () => void {+ listeners.add(listener);+ return () => {+ listeners.delete(listener);+ };+}++/**+ * Forget one computer's activity.+ *+ * Wiping a computer deletes the machine those commands ran on, so leaving them on screen would+ * describe something that no longer exists.+ */+export function clearActivity(computerId: string): void {+ byComputer.delete(computerId);+ for (const listener of listeners) listener();+}diff --git a/app/src/lib/copilot/computer-tools.tsx b/app/src/lib/copilot/computer-tools.tsxindex d72f35f9..ff12a34b 100644--- a/app/src/lib/copilot/computer-tools.tsx+++ b/app/src/lib/copilot/computer-tools.tsx@@ -1,9 +1,11 @@import { useFrontendTool } from "@copilotkit/react-core/v2";import { z } from "zod";-import { tryClient } from "@/lib/client";import { ToolLine } from "@/components/channels/tool-line";+import { CommandOutput } from "@/components/computer/command-output";import { ComputerView } from "@/components/computer/computer-view";-import { readControl, type ControlState } from "@/lib/computers/control";+import { tryClient } from "@/lib/client";+import { recordActivity } from "@/lib/computers/activity";+import { type ControlState, readControl } from "@/lib/computers/control";import { useActiveBotHolder } from "./active-bot";import { reportComputerActivity } from "./computer-activity";@@ -12,7 +14,7 @@ import { reportComputerActivity } from "./computer-activity";*//** What every computer call returns to the model: either the result, or a reason it did not happen. */-type ToolOutcome = Record<string, unknown> & { ok: boolean };+export type ToolOutcome = Record<string, unknown> & { ok: boolean };/*** Human-assistance wait window. Long enough for a user to return, finite so the run can unblock.@@ -106,8 +108,59 @@ type ComputerOutcome = {staleRefs?: boolean;elements?: unknown[];element?: { role?: string; name?: string };+ /** What a shell call reports back, so the line can show the output rather than only the command. */+ exitCode?: number;+ stdout?: string;+ stderr?: string;+ /** The far side cut the output short, or stopped the command. */+ truncated?: boolean;+ timedOut?: boolean;+ /** A file write. The size, never what was written. */+ bytes?: number;+ /** A file read. Named `text` on the way back and `contents` on the way in. */+ text?: string;};+/**+ * What a call printed, as text a person can read.+ *+ * One helper because the three surfaces this feeds all want the same thing and shape it differently:+ * a command has `stdout` and `stderr`, a file read has `contents`, a listing has `entries`. A refusal+ * carries only its reason, and that is the most useful thing on the line.+ *+ * Never guesses. Something with none of those fields gives an empty string, and the pane says the+ * call printed nothing rather than inventing a summary.+ */+export function outputOf(result: ToolOutcome): string {+ if (result.refused === true || result.ok === false) {+ return typeof result.reason === "string" ? result.reason : "";+ }++ // `text`, which is what the read route answers with. Not `contents`: that is the name on the way+ // in, and reading it back gave an empty pane for a file the Bot had just read out loud.+ if (typeof result.text === "string") return result.text;++ if (Array.isArray(result.entries)) {+ return result.entries+ .map((entry) => {+ if (!entry || typeof entry !== "object") return String(entry);+ const { path, kind, bytes } = entry as Record<string, unknown>;+ const label = String(path ?? "");+ // A trailing slash for a folder, the way a terminal marks one, so a listing of a workspace+ // full of folders does not read as a list of extensionless files.+ if (kind === "folder") return `${label}/`;+ return typeof bytes === "number" ? `${label} ${bytes} bytes` : label;+ })+ .join("\n");+ }++ const stdout = typeof result.stdout === "string" ? result.stdout : "";+ const stderr = typeof result.stderr === "string" ? result.stderr : "";+ // Both, in the order a terminal shows them, and labelled only when there is something on stderr:+ // most commands write nothing there and a permanent empty heading is noise.+ return stderr ? `${stdout}${stdout ? "\n" : ""}${stderr}` : stdout;+}+/*** Parse the SDK-render result string so the transcript can distinguish success, refusal, and failure.*/@@ -550,11 +603,20 @@ export function ComputerTools() {.optional().describe("Optional folder to list. Omit for the whole workspace."),}),- handler: async (input: { path?: string }) =>- callComputer(bot.current, "/files/list", {+ handler: async (input: { path?: string }) => {+ const computerId = bot.current;+ const result = await callComputer(computerId, "/files/list", {method: "POST",body: input ?? {},- }),+ });+ recordActivity(computerId, {+ kind: "list_files",+ subject: input?.path ?? "the workspace",+ output: outputOf(result),+ ...(result.refused === true ? { refused: true } : {}),+ });+ return result;+ },render: ({ result, status }) => {const outcome = outcomeOf(result);const entries = Array.isArray(outcome.entries) ? outcome.entries : [];@@ -587,11 +649,20 @@ export function ComputerTools() {.string().describe("Path relative to your workspace, such as notes.md"),}),- handler: async (input: { path: string }) =>- callComputer(bot.current, "/files/read", {+ handler: async (input: { path: string }) => {+ const computerId = bot.current;+ const result = await callComputer(computerId, "/files/read", {method: "POST",body: input,- }),+ });+ recordActivity(computerId, {+ kind: "read_file",+ subject: input.path,+ output: outputOf(result),+ ...(result.refused === true ? { refused: true } : {}),+ });+ return result;+ },render: ({ args, result, status }) => {const outcome = outcomeOf(result);return (@@ -632,22 +703,46 @@ export function ComputerTools() {handler: async (input: { command: string },{ signal }: { signal?: AbortSignal } = {},- ) =>- callComputer(- bot.current,+ ) => {+ const computerId = bot.current;+ const result = await callComputer(+ computerId,"/exec",{ method: "POST", body: input },signal,- ),+ );+ /*+ * Recorded here rather than in `render`, which runs again on every re-render and would append+ * the same command each time. This is the only place that runs once per call and has both the+ * command and what it printed.+ */+ recordActivity(computerId, {+ kind: "command",+ subject: input.command,+ output: outputOf(result),+ ...(typeof result.exitCode === "number"+ ? { exitCode: result.exitCode }+ : {}),+ ...(result.refused === true ? { refused: true } : {}),+ ...(result.truncated === true ? { truncated: true } : {}),+ ...(result.timedOut === true ? { timedOut: true } : {}),+ });+ return result;+ },render: ({ args, result, status }) => {const outcome = outcomeOf(result);/*- * The command, not its output. A person watching wants to know what their Bot just ran on a- * machine holding their logins, and that is the command; the output belongs in the answer the- * Bot gives, where the model has already decided which part of it mattered.+ * The command on the line, its output behind the chevron.+ *+ * The line stays one line, because a transcript of a Bot working through twenty commands is+ * unreadable if each one dumps a screenful. But the output has to be reachable: this ran on a+ * machine holding somebody's logins, and "take the model's word for what it printed" is not an+ * answer. The pane beside the screen shows the same thing without expanding anything.*/+ const printed = outputOf(outcome as ToolOutcome);+ const exit = typeof outcome.exitCode === "number" ? outcome.exitCode : 0;return (- <ActionLine+ <ToolLinerunning={status !== "complete"}label="Ran a command"detail={@@ -658,8 +753,17 @@ export function ComputerTools() {: undefined}refused={outcome.refused === true}- failed={didNotWork(outcome)}- />+ failed={didNotWork(outcome) || exit !== 0}+ >+ {status === "complete" ? (+ <CommandOutput+ output={printed}+ exitCode={exit}+ truncated={outcome.truncated === true}+ timedOut={outcome.timedOut === true}+ />+ ) : null}+ </ToolLine>);},});@@ -686,11 +790,30 @@ export function ComputerTools() {path: string;contents: string;append?: boolean;- }) =>- callComputer(bot.current, "/files/write", {+ }) => {+ const computerId = bot.current;+ const result = await callComputer(computerId, "/files/write", {method: "POST",body: input,- }),+ });+ /*+ * The path and the size, never the contents. A Bot may well be saving something it was told in+ * confidence, and the write route declines to echo it back for exactly that reason; putting it+ * in a pane would undo that.+ */+ recordActivity(computerId, {+ kind: "write_file",+ subject: input.path,+ output:+ result.refused === true+ ? outputOf(result)+ : typeof result.bytes === "number"+ ? `${result.bytes} bytes${input.append === true ? ", appended" : ""}`+ : "",+ ...(result.refused === true ? { refused: true } : {}),+ });+ return result;+ },render: ({ args, result, status }) => {const outcome = outcomeOf(result);return (diff --git a/app/src/lib/identity-providers/mutations.ts b/app/src/lib/identity-providers/mutations.tsindex fec6cf36..d59a94a4 100644--- a/app/src/lib/identity-providers/mutations.ts+++ b/app/src/lib/identity-providers/mutations.ts@@ -78,16 +78,22 @@ export function registerIdentityProviderMutationOptions(});}+/**+ * Remove one.+ *+ * Our own route, not Better Auth's `delete-provider`, which refuses unless the person asking is the+ * one who registered it. That left a provider nobody could remove the moment the administrator who+ * set it up had left, which is exactly when somebody needs to.+ */export function deleteIdentityProviderMutationOptions(queryClient: QueryClient,) {return mutationOptions({mutationFn: async (providerId: string): Promise<void> => {- await client("/api/auth/sso/delete-provider", {- method: "POST",- body: { providerId },- fallback: FALLBACK,- });+ await client(+ `/api/admin/identity-providers/${encodeURIComponent(providerId)}`,+ { method: "DELETE", fallback: FALLBACK },+ );},onSuccess: () => invalidateProviders(queryClient),});diff --git a/app/src/lib/identity-providers/queries.ts b/app/src/lib/identity-providers/queries.tsindex 1c9e3b39..8104bcbf 100644--- a/app/src/lib/identity-providers/queries.ts+++ b/app/src/lib/identity-providers/queries.ts@@ -15,6 +15,13 @@ export type IdentityProvider = {domain: string;/** Which protocol it speaks. SAML is what most enterprise identity teams hand over. */protocol: "saml" | "oidc";+ /**+ * Whether the person who registered it still has an account here. Null once they are gone.+ *+ * Shown so somebody auditing a deployment can see that a provider outlived whoever set it up,+ * which is the normal case a year in and used to be the case where the provider vanished instead.+ */+ registeredBy: string | null;};export const identityProviderKeys = {@@ -25,34 +32,20 @@ export const identityProviderKeys = {/*** The registered providers.*- * Read from Better Auth's own route rather than one of ours, because the plugin owns the table and a- * second reader would be a second answer. The payload carries no client secret or signing key: the- * fields below are all this asks for.+ * Read from our own admin route, not Better Auth's `GET /sso/providers`. That one answers with the+ * providers the person asking registered themselves, so two administrators saw two different+ * deployments and the second one to open this screen found it empty and registered a provider that+ * already existed. What is registered is a fact about the deployment.+ *+ * The payload carries no client secret or signing certificate; the server's projection cannot express+ * them.*/export function identityProviderListQueryOptions() {return queryOptions({queryKey: identityProviderKeys.list(),- queryFn: async (): Promise<IdentityProvider[]> => {- // `{ providers: [...] }`, not a bare array. Better Auth's own routes carry their own- // envelope, which is why this reads the body rather than passing a key to `client`.- const response = await client("/api/auth/sso/providers", {+ queryFn: (): Promise<IdentityProvider[]> =>+ client("/api/admin/identity-providers", "providers", {fallback: "Could not load identity providers",- });- const { providers = [] } = (await response.json()) as {- providers?: {- providerId: string;- issuer: string;- domain: string;- samlConfig?: unknown;- }[];- };-- return providers.map((provider) => ({- providerId: provider.providerId,- issuer: provider.issuer,- domain: provider.domain,- protocol: provider.samlConfig ? "saml" : "oidc",- }));- },+ }),});}diff --git a/app/src/routes/_authed/_app/channel/$channelId.tsx b/app/src/routes/_authed/_app/channel/$channelId.tsxindex 06541d0f..b2e00869 100644--- a/app/src/routes/_authed/_app/channel/$channelId.tsx+++ b/app/src/routes/_authed/_app/channel/$channelId.tsx@@ -2,16 +2,18 @@ import { IconDeviceDesktop, IconSettings } from "@tabler/icons-react";import { useQuery } from "@tanstack/react-query";import { createFileRoute } from "@tanstack/react-router";import { motion, useReducedMotion } from "motion/react";-import { useEffect, useRef } from "react";+import { useEffect, useRef, useState, useSyncExternalStore } from "react";import { z } from "zod";import { AgentProfile } from "@/components/agents/agent-profile";import { ChannelAvatar } from "@/components/channels/avatar";import { ChannelChat } from "@/components/channels/channel-chat";+import { ActivityLog } from "@/components/computer/activity-log";import { ComputerView } from "@/components/computer/computer-view";import { useNeedsYou } from "@/components/computer/needs-you";import { DetailPanel } from "@/components/layout/detail-panel";import { Button } from "@/components/ui/button";import { type AgentChannel, channelQueryOptions } from "@/lib/channels/queries";+import { activityFor, subscribeToActivity } from "@/lib/computers/activity";import { onComputerActivity } from "@/lib/copilot/computer-activity";const chatSearchSchema = z.object({@@ -33,6 +35,17 @@ export const Route = createFileRoute("/_authed/_app/channel/$channelId")({component: RouteComponent,});+/**+ * What the Bot is looking at, and what it is doing.+ *+ * Two surfaces rather than one. The screen was the only window into a Bot's computer, so a Bot that+ * spent two minutes in a terminal showed a blank browser and nothing else: the honest answer to+ * "what is it doing" was "something, on a machine holding your logins". The second tab is the shell+ * and the workspace, and it fills up while the screen sits still.+ *+ * The screen stays the default, because most work is browsing and it is the surface somebody has to+ * take the wheel on. The count on the other tab is what says the Bot is busy somewhere else.+ */function ComputerViewPanel({agentId,name,@@ -40,13 +53,53 @@ function ComputerViewPanel({agentId: string;name?: string;}) {+ const [showing, setShowing] = useState<"screen" | "activity">("screen");+ const activity = useSyncExternalStore(+ subscribeToActivity,+ () => activityFor(agentId),+ () => activityFor(agentId),+ );+return (- <div className="px-4 mt-4">+ <div className="mt-4 px-4"><div className="p-4">- <ComputerView active computerId={agentId} />- <span className="flex items-center justify-center w-full text-center text-muted-foreground mt-4 text-sm">- {name || "Agent"}'s screen- </span>+ <div className="mb-3 flex gap-2">+ <Button+ onClick={() => setShowing("screen")}+ size="sm"+ variant={showing === "screen" ? "default" : "outline"}+ >+ Screen+ </Button>+ <Button+ onClick={() => setShowing("activity")}+ size="sm"+ variant={showing === "activity" ? "default" : "outline"}+ >+ Activity+ {activity.length > 0 ? (+ <span className="ml-1.5 tabular-nums opacity-70">+ {activity.length}+ </span>+ ) : null}+ </Button>+ </div>++ {/*+ Both mounted, one hidden. Unmounting the screen would drop its socket and its polling, so+ looking at the terminal for a moment would cost the live view and the take-the-wheel prompt+ that rides on it.+ */}+ <div className={showing === "screen" ? undefined : "hidden"}>+ <ComputerView active computerId={agentId} />+ <span className="mt-4 flex w-full items-center justify-center text-center text-muted-foreground text-sm">+ {name || "Agent"}'s screen+ </span>+ </div>++ <div className={showing === "activity" ? undefined : "hidden"}>+ <ActivityLog computerId={agentId} />+ </div></div></div>);diff --git a/app/src/routes/_authed/admin/identity-providers.tsx b/app/src/routes/_authed/admin/identity-providers.tsxindex 64f21231..124ff2f9 100644--- a/app/src/routes/_authed/admin/identity-providers.tsx+++ b/app/src/routes/_authed/admin/identity-providers.tsx@@ -64,7 +64,9 @@ function IdentityProvidersPage() {const [open, setOpen] = useState(false);const [draft, setDraft] = useState(EMPTY);- const failure = register.error ?? remove.error;+ // A removal failure belongs on the page: there is no dialog to put it in. A registration failure+ // is shown inside the dialog instead, where the person who caused it is looking.+ const failure = remove.error;function submit(submission: React.FormEvent) {submission.preventDefault();@@ -286,6 +288,17 @@ function IdentityProvidersPage() {</div></>)}++ {/*+ Here as well as on the page behind, because while this is open the page behind it is+ not visible. A registration refused by the identity provider or by Better Auth left+ the dialog sitting there unchanged, which reads as the button not working.+ */}+ {register.error ? (+ <p className="text-destructive text-sm" role="alert">+ {register.error.message}+ </p>+ ) : null}</DialogBody><DialogFooter className="mt-4"><Buttondiff --git a/app/tests/computer-activity.test.ts b/app/tests/computer-activity.test.tsnew file mode 100644index 00000000..4164f64d--- /dev/null+++ b/app/tests/computer-activity.test.ts@@ -0,0 +1,140 @@+import { beforeEach, describe, expect, test } from "bun:test";+import {+ activityFor,+ clearActivity,+ recordActivity,+} from "../src/lib/computers/activity";+import { outputOf } from "../src/lib/copilot/computer-tools";++/**+ * What a Bot did on its computer, other than browse.+ *+ * The screen said what it was looking at and nothing said what it was doing: a shell command was one+ * grey line with the output nowhere, so a person watching a Bot work on a machine holding their+ * logins had to take the model's word for what it printed.+ *+ * `outputOf` is the part that reads a result, and it is the part that gets it wrong: it looked for+ * `contents` on a file read, which is the name on the way in rather than the way back, and the pane+ * said "It printed nothing" about a file the Bot had just read out loud. These pin the field names.+ */++describe("reading what a call printed", () => {+ test("a command is its stdout", () => {+ expect(outputOf({ ok: true, stdout: "ripgrep 14.1.0\n", stderr: "" })).toBe(+ "ripgrep 14.1.0\n",+ );+ });++ test("a failing command keeps its stderr, which is the whole message", () => {+ expect(+ outputOf({+ ok: true,+ stdout: "",+ stderr: "ls: cannot access '/nope'",+ exitCode: 2,+ }),+ ).toBe("ls: cannot access '/nope'");+ });++ test("both streams, in the order a terminal shows them", () => {+ expect(outputOf({ ok: true, stdout: "one", stderr: "and a warning" })).toBe(+ "one\nand a warning",+ );+ });++ test("a file read is its text", () => {+ // `text`, not `contents`. `contents` is the field on the way in, and reading it back gave an+ // empty pane for a file that had just been read.+ expect(outputOf({ ok: true, path: "notes.md", text: "hello" })).toBe(+ "hello",+ );+ });++ test("a listing marks folders the way a terminal does", () => {+ expect(+ outputOf({+ ok: true,+ entries: [+ { path: "notes", kind: "folder" },+ { path: "notes.md", kind: "file", bytes: 5 },+ ],+ }),+ ).toBe("notes/\nnotes.md 5 bytes");+ });++ test("a refusal is its reason, which is the useful part", () => {+ expect(+ outputOf({ ok: false, refused: true, reason: "A boundary said no." }),+ ).toBe("A boundary said no.");+ });++ test("something with none of those fields gives nothing rather than a guess", () => {+ // The pane then says the call printed nothing, which is honest. Inventing a summary from an+ // unrecognised shape is how a view starts lying about what happened.+ expect(outputOf({ ok: true })).toBe("");+ });+});++describe("the activity a pane shows", () => {+ beforeEach(() => {+ clearActivity("bot-1");+ clearActivity("bot-2");+ });++ test("keeps what a Bot ran, in the order it ran it", () => {+ recordActivity("bot-1", { kind: "command", subject: "ls", output: "a\nb" });+ recordActivity("bot-1", {+ kind: "command",+ subject: "pwd",+ output: "/workspace",+ });++ expect(activityFor("bot-1").map((entry) => entry.subject)).toEqual([+ "ls",+ "pwd",+ ]);+ });++ test("keeps one Bot's work out of another's", () => {+ recordActivity("bot-1", { kind: "command", subject: "ls", output: "" });++ expect(activityFor("bot-2")).toEqual([]);+ });++ test("a computer nothing has happened on is empty rather than undefined", () => {+ expect(activityFor("never-used")).toEqual([]);+ });++ test("wiping a computer forgets what ran on it", () => {+ // Reset deletes the machine those commands ran on, so leaving them on screen would describe+ // something that no longer exists.+ recordActivity("bot-1", { kind: "command", subject: "ls", output: "" });+ clearActivity("bot-1");++ expect(activityFor("bot-1")).toEqual([]);+ });++ test("stops growing, because this is a pane and not an archive", () => {+ for (let index = 0; index < 250; index += 1) {+ recordActivity("bot-1", {+ kind: "command",+ subject: `command-${index}`,+ output: "",+ });+ }++ const kept = activityFor("bot-1");+ expect(kept).toHaveLength(200);+ // The oldest go first: what somebody watching wants is what just happened.+ expect(kept[0]?.subject).toBe("command-50");+ expect(kept.at(-1)?.subject).toBe("command-249");+ });++ test("every entry is distinguishable, so two identical commands both show", () => {+ recordActivity("bot-1", { kind: "command", subject: "ls", output: "" });+ recordActivity("bot-1", { kind: "command", subject: "ls", output: "" });++ const [first, second] = activityFor("bot-1");+ expect(first?.id).not.toBe(second?.id);+ });+});diff --git a/docs/architecture.md b/docs/architecture.mdindex 025527c9..9c108b30 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -87,6 +87,12 @@ While a person controls the browser, Bot actions are refused rather than queued.Secret entry is separate from chat content. The audit trail records that a secret was requested or supplied and the character count, not the secret value.+## Watching a Bot work++Two surfaces beside the conversation. The screen is the live browser, proxied over a websocket and gated on the same question as every other route about that Bot. The Activity tab is what the Bot did away from the browser: every command with its output and exit code, every file read, write and listing, newest first.++Activity is held in the browser for the open conversation and is gone on reload. It is a window rather than a record; the record is the audit trail, which is server-side, survives restarts, and is what an investigation reads. A saved file contributes its path and size and never its contents, matching the write route, which declines to echo them because a Bot may be saving something it was told in confidence.+## Coworkers and channelsA coworker is a durable Bot profile:@@ -150,8 +156,11 @@ Connector credentials are stored through the credential vault and referenced by- Sign-in is Google, Microsoft or Okta from the environment, plus SAML and OpenID Connect providers registered at runtime and routed by email domain. One resolver answers both questions a run asks about a person, whose threads these are and which Bots they may run, so the two can never disagree.- `INITIAL_ADMIN_EMAILS` is a floor: an address it names is made an administrator at every sign-in and cannot be demoted from the People screen. Everybody else's role is decided there, and every change writes an audit row.- Registering, changing or removing an identity provider is administrator-only. Better Auth's SSO plugin guards those routes with a session alone, which would let any signed-in person register a provider for a domain.+- A registered identity provider belongs to the deployment, not to whoever registered it. Better Auth scopes its own listing and removal to the registering user and cascades the row from that user, so two administrators saw two different deployments and deleting the one who set sign-in up would have deleted the company's sign-in. Reads and removals go through OpenBot's own administrator-only routes against the whole table.+- A provider's client secret and SAML signing material are encrypted at rest with `KEY_ENCRYPTION_KEY`, through a wrapper on the Better Auth storage adapter, since the plugin stores them as plaintext JSON. OAuth access and refresh tokens use Better Auth's own encryption, keyed on `BETTER_AUTH_SECRET`.+- Signing in, being refused, and being granted the administrator role by configuration each write an audit row. Without them nothing recorded that somebody who could edit `INITIAL_ADMIN_EMAILS` had promoted themselves, and revoking a person deleted the sessions that were the only evidence they had been here.- Removing somebody deletes their sessions and denies their address, because deleting the user row alone is not removal: the next sign-in through the provider recreates it.-- With no identity provider configured, every request is one fixed administrator. That is refused with `NODE_ENV=production` unless `OPENBOT_SINGLE_USER=true` says it was meant.+- With no identity provider configured, the deployment refuses to start unless `OPENBOT_SINGLE_USER=true` says every request may be one fixed administrator. That flag is the only thing that permits it; `NODE_ENV` does not.- `KEY_ENCRYPTION_KEY` must be a base64-encoded 32-byte value. The example key is refused with `NODE_ENV=production`.- Credential plaintext is encrypted at rest, never returned by APIs, and redacted from audit events.- Browser navigation allows `http` and `https`; cloud metadata addresses are refused under every configuration.diff --git a/docs/configuration.md b/docs/configuration.mdindex 83b5cfb5..4836f0d4 100644--- a/docs/configuration.md+++ b/docs/configuration.md@@ -34,7 +34,7 @@ All four Intelligence values are required together. Missing any of them stops se| Variable | Default | Meaning || -------------------- | ---------------------------------- | ------------------------------------------------------------------- || `PORT` | `3001` | API server port. |-| `NODE_ENV` | unset | `production` enables startup refusals for local-only settings. |+| `NODE_ENV` | unset | `production` refuses the example `KEY_ENCRYPTION_KEY`. It does not decide whether sign-in is required; see `OPENBOT_SINGLE_USER`. || `TENANT_PACKAGE_DIR` | `../examples/fintech` | Tenant package directory, resolved from `server/`. || `DEPLOYMENT_ID` | the tenant package's id | Names this deployment inside a shared Intelligence project. || `OPENAI_API_KEY` | unset | Default model key for built-in agents and both shipped Bots. |@@ -96,7 +96,7 @@ Two things are worth knowing before pointing a deployment at any gateway. Not ev| Variable | Meaning || ---------------------------- | -------------------------------------------------------------------------------------- |-| `OPENBOT_SINGLE_USER` | One fixed administrator and no sign-in. Only read when no identity provider is configured, and only needed where `NODE_ENV=production` would otherwise refuse to start. |+| `OPENBOT_SINGLE_USER` | One fixed administrator and no sign-in. **Required** when no identity provider is configured, or the deployment refuses to start. Ignored when one is. || `GOOGLE_OAUTH_CLIENT_ID` | Google OAuth client id. || `GOOGLE_OAUTH_CLIENT_SECRET` | Google OAuth client secret. || `MICROSOFT_OAUTH_CLIENT_ID` | Microsoft Entra ID application id. |@@ -107,9 +107,15 @@ Two things are worth knowing before pointing a deployment at any gateway. Not ev| `OKTA_OAUTH_ISSUER` | Which Okta, for example `https://example.okta.com/oauth2/default`. || `BETTER_AUTH_SECRET` | At least 32 characters. Required with any provider. || `BETTER_AUTH_URL` | Public API server base URL, where OAuth callbacks return. Required with any provider. |-| `TRUSTED_ORIGINS` | Comma-separated app origins accepted by the API. |+| `TRUSTED_ORIGINS` | Comma-separated app origins accepted by the API, plus every host in a registered OIDC provider's discovery document. || `INITIAL_ADMIN_EMAILS` | Comma-separated administrators. **Required** with any provider. |+**With no provider at all, `OPENBOT_SINGLE_USER=true` is required.** A deployment that configures+nothing to sign anybody in and does not say that was deliberate refuses to start, naming what to+configure, because a public URL where every visitor is an administrator fails silently. `NODE_ENV`+does not enter into it. `.env.example` ships the line switched on, so a clone runs with no+configuration at all.+**Any one provider turns sign-in on**, and several may be configured at once. Each provider's id andsecret must be set together, Okta additionally needs its issuer, and any of them requires`BETTER_AUTH_SECRET`, `BETTER_AUTH_URL` and `INITIAL_ADMIN_EMAILS`. Every incomplete combination is@@ -122,6 +128,18 @@ screen, which is what guarantees a way back in. Everybody else's role is decidedSAML and OpenID Connect providers are not configured here. They are registered while the deploymentruns, under Admin → Identity providers, and routed by email domain.+**Registering an OpenID Connect provider needs its endpoints in `TRUSTED_ORIGINS`.** Better Auth+fetches the discovery document and refuses any endpoint inside it that is not a trusted origin, which+is what stops a registration pointing the deployment at an address of somebody else's choosing. It is+every host in the document and not only the issuer, so a Google issuer also needs+`oauth2.googleapis.com` and `openidconnect.googleapis.com`; a typical Okta tenant serves all of them+from one host and needs only that. A registration refused this way names the host it objected to.++What is registered belongs to the deployment rather than to whoever registered it. Every+administrator sees the same list and can remove any of it, and a provider outlives the person who+added it. The client secret and any SAML signing material are encrypted at rest with+`KEY_ENCRYPTION_KEY`.+The redirect URI to register with each provider is `<BETTER_AUTH_URL>/api/auth/callback/<provider>`,where `<provider>` is `google`, `microsoft` or `okta`.diff --git a/docs/deployment.md b/docs/deployment.mdindex d08c4808..6dfc167f 100644--- a/docs/deployment.md+++ b/docs/deployment.md@@ -74,10 +74,10 @@ and the fix would not be available.`COMPUTER_TOKEN` is generated at start if you do not set one. Both processes that need it are insidethe container, so there is nothing to share it with.-**Authentication is required.** With no identity provider configured the image refuses to start,-because `NODE_ENV=production` is set and a public URL where every visitor is an administrator fails-silently. Configure Google, Microsoft or Okta, or set `OPENBOT_SINGLE_USER=true` to say you meant an-open deployment.+**Authentication is required.** With no identity provider configured the deployment refuses to start,+because a public URL where every visitor is an administrator fails silently: it looks like it works.+Configure Google, Microsoft or Okta, or set `OPENBOT_SINGLE_USER=true` to say you meant an open+deployment. `NODE_ENV` does not affect this.**Put TLS in front of it.** Not only for the cookies. A page served from `http://<address>` is not asecure context, which removes a set of browser APIs that are present on `http://localhost` and sodiff --git a/docs/development.md b/docs/development.mdindex 66d8b9d9..3c84c575 100644--- a/docs/development.md+++ b/docs/development.md@@ -61,6 +61,13 @@ generator produced. If the generated SQL will not work — `ADD COLUMN ... NOT Nthat already has rows — split it instead: generate the column nullable, add the data step, thengenerate the constraint.+**A constraint that tightens an existing column belongs to a later release**, not to the release that+adds the column. A rolling deploy runs the migrations and then serves from old and new replicas at+once, and an old replica writes rows without the new column: under `NOT NULL` its writes start+failing, so the release that added the column breaks for everybody who lands on a replica that has+not been replaced yet. Ship the column nullable, let the fleet turn over, then tighten it. `issuer`+on `accounts` is the worked example: the column is nullable and no migration tightens it.+**A data step is its own migration**, created with the flag that exists for it:```shdiff --git a/server/drizzle/0004_account_issuer_required.sql b/server/drizzle/0004_account_issuer_required.sqldeleted file mode 100644index c4503b53..00000000--- a/server/drizzle/0004_account_issuer_required.sql+++ /dev/null@@ -1 +0,0 @@-ALTER TABLE "accounts" ALTER COLUMN "issuer" SET NOT NULL;\ No newline at end of filediff --git a/server/drizzle/0004_identity_provider_outlives_its_registrar.sql b/server/drizzle/0004_identity_provider_outlives_its_registrar.sqlnew file mode 100644index 00000000..0d0f7fd6--- /dev/null+++ b/server/drizzle/0004_identity_provider_outlives_its_registrar.sql@@ -0,0 +1,3 @@+ALTER TABLE "sso_providers" DROP CONSTRAINT "sso_providers_user_id_users_id_fk";+--> statement-breakpoint+ALTER TABLE "sso_providers" ADD CONSTRAINT "sso_providers_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;\ No newline at end of filediff --git a/server/drizzle/meta/0004_snapshot.json b/server/drizzle/meta/0004_snapshot.jsonindex 6c7a300f..40d05989 100644--- a/server/drizzle/meta/0004_snapshot.json+++ b/server/drizzle/meta/0004_snapshot.json@@ -1,5 +1,5 @@{- "id": "ab829598-5c7b-4ed7-9641-79d8dbe2bd19",+ "id": "29b80b3f-45c0-4fa9-9f10-ef27b693786f","prevId": "09dc7c9c-6132-4413-a291-72aee0a30b2c","version": "7","dialect": "postgresql",@@ -30,7 +30,7 @@"name": "issuer","type": "text","primaryKey": false,- "notNull": true+ "notNull": false},"user_id": {"name": "user_id",@@ -1336,7 +1336,7 @@"tableTo": "users","columnsFrom": ["user_id"],"columnsTo": ["id"],- "onDelete": "cascade",+ "onDelete": "set null","onUpdate": "no action"}},diff --git a/server/drizzle/meta/_journal.json b/server/drizzle/meta/_journal.jsonindex 3f06f8bf..c033483a 100644--- a/server/drizzle/meta/_journal.json+++ b/server/drizzle/meta/_journal.json@@ -33,8 +33,8 @@{"idx": 4,"version": "7",- "when": 1787275307878,- "tag": "0004_account_issuer_required",+ "when": 1787324562029,+ "tag": "0004_identity_provider_outlives_its_registrar","breakpoints": true},{diff --git a/server/src/app.ts b/server/src/app.tsindex 6234ce6f..d8b8eb55 100644--- a/server/src/app.ts+++ b/server/src/app.ts@@ -19,6 +19,7 @@ import {type RoleRepository,requireAdmin,} from "./auth/guards";+import type { IdentityProviderStore } from "./auth/identity-provider-store";import type { ChannelEventHub } from "./channels/events";import { type ChannelStore, createChannelRoutes } from "./channels/routes";import type { ThreadIdentity } from "./channels/thread-identity";@@ -137,12 +138,13 @@ export function createApp(*/peopleStore?: PeopleStore,/**- * How many enterprise identity providers are registered.+ * The enterprise identity providers this deployment has registered.*- * A count rather than the store, because the only question anybody outside Better Auth asks is- * whether the sign-in screen should offer the email box at all.+ * Read here rather than through Better Auth's own listing route, which scopes to the person asking:+ * a company's Okta tenant belongs to the deployment, not to whichever administrator pasted the+ * metadata in. See identity-provider-store.ts.*/- ssoProviderCount?: () => Promise<number>,+ identityProviders?: IdentityProviderStore,) {const app = new Hono<{ Variables: AppVariables }>();@@ -172,7 +174,7 @@ export function createApp(* that routes by domain; naming the providers would tell anybody who loads the page which* companies use this deployment, which is not theirs to have before they sign in.*/- ssoConfigured: (await ssoProviderCount?.()) ? true : false,+ ssoConfigured: ((await identityProviders?.list()) ?? []).length > 0,}),);/*@@ -397,6 +399,73 @@ export function createApp(return context.json({ person: await peopleStore.find(userId) });});+ /*+ * The identity providers this deployment has registered.+ *+ * Not Better Auth's own `GET /sso/providers`, which answers with the ones the person asking+ * registered themselves. Two administrators therefore saw two different deployments: the second+ * one to open this screen found it empty and registered a provider that was already there. What is+ * registered is a fact about the deployment, so every administrator sees the same list.+ */+ app.get("/api/admin/identity-providers", requireUser, async (context) => {+ const denied = requireAdmin(context);+ if (denied) {+ return denied;+ }+ if (!identityProviders) {+ return context.json(+ { error: "Identity providers are not available." },+ 503,+ );+ }++ return context.json({ providers: await identityProviders.list() });+ });++ /*+ * Remove one.+ *+ * Ours rather than Better Auth's `delete-provider`, which refuses unless the person asking is the+ * one who registered it. That leaves a provider nobody can remove as soon as the administrator who+ * set it up has left, which is the same moment somebody most needs to.+ */+ app.delete(+ "/api/admin/identity-providers/:providerId",+ requireUser,+ async (context) => {+ const denied = requireAdmin(context);+ if (denied) {+ return denied;+ }+ if (!identityProviders) {+ return context.json(+ { error: "Identity providers are not available." },+ 503,+ );+ }++ const providerId = context.req.param("providerId");+ const removed = await identityProviders.remove(providerId);+ if (!removed) {+ // A screen somebody left open, or two administrators removing the same one. Saying so beats+ // reporting success for something that was not there.+ return context.json({ error: "There is no such provider." }, 404);+ }++ if (auditStore) {+ await recordAuditEvent(auditStore, {+ eventType: "identity_provider.removed",+ targetType: "identity_provider",+ targetId: providerId,+ actorUserId: context.var.actor.id,+ payload: { removedBy: context.var.actor.email },+ });+ }++ return context.json({ removed: true });+ },+ );+app.get("/api/admin/credentials", requireUser, async (context) => {const denied = requireAdmin(context);if (denied) {diff --git a/server/src/audit.ts b/server/src/audit.tsindex 7bf748a4..f288d3c0 100644--- a/server/src/audit.ts+++ b/server/src/audit.ts@@ -165,6 +165,31 @@ export const auditEventTypes = ["person.role_changed","person.access_revoked","person.access_restored",+ /*+ * Getting in, and being turned away.+ *+ * The trail had nothing about sign-in at all, which left two questions unanswerable. Anybody who+ * could edit `INITIAL_ADMIN_EMAILS` granted themselves the administrator role on their next+ * sign-in and no row anywhere said it had happened, because the floor is re-applied silently by+ * design. And revoking somebody deletes their sessions, which were the only record that they had+ * ever been here: after a revocation the deployment could not show that the person had signed in,+ * let alone when or how often.+ *+ * `session.refused` is the one somebody investigating actually reaches for. A revoked person still+ * holding a bookmark, or an address outside the deployment trying the front door, produces nothing+ * else anywhere.+ */+ "session.signed_in",+ "session.refused",+ "person.admin_by_configuration",+ /*+ * A company's own identity provider, added or taken away.+ *+ * Whoever holds this decides who can sign in at all, so the two ends of its life belong on the+ * trail next to the roles it hands out.+ */+ "identity_provider.registered",+ "identity_provider.removed",] as const;export type AuditEventType = (typeof auditEventTypes)[number];diff --git a/server/src/auth/dev-actor.ts b/server/src/auth/dev-actor.tsindex c06e0bc1..6e6ea6af 100644--- a/server/src/auth/dev-actor.ts+++ b/server/src/auth/dev-actor.ts@@ -6,14 +6,16 @@ import type { AppVariables, AuthenticatedActor } from "./guards";/*** A signed-in person, without signing in.*- * A clone with no identity provider configured is one administrator, so `bun run dev` reaches the- * product without registering an OAuth client first. That is the whole point of it: nobody should- * have to set up Entra to look at a Bot.+ * A deployment with no identity provider is one administrator and no sign-in, so `bun run dev`+ * reaches the product without registering an OAuth client first. Nobody should have to set up Entra+ * to look at a Bot. `.env.example` ships that line switched on, so a clone still runs with no+ * configuration at all.*- * The lock is `NODE_ENV`, not a flag. Somewhere reachable by other people, an unconfigured- * deployment refuses to start and names what to configure, because a public URL where every visitor- * is an administrator is the failure this exists to prevent, and it is silent: it looks like it- * works. `OPENBOT_SINGLE_USER=true` is how somebody says they meant it anyway.+ * The lock is the flag and nothing else. It used to be `NODE_ENV`, which is exactly backwards:+ * `NODE_ENV` is unset by default, so the one case it had to catch was the one it let through. A+ * container run on a VM with a hand-written env file and no provider served every visitor on the+ * internet as an administrator, and said nothing, because it looked like it was working. Now the+ * deployment refuses to start unless somebody wrote down that they meant it.** The actor is an administrator so admin surfaces can be reached too, and its id is fixed so* Intelligence threads and memory stay attached to the same person across restarts.@@ -68,20 +70,18 @@ export function singleUserEnabled(): boolean {if (hasProvider) return false;- // Said explicitly, which is the only way to run open where other people can reach it.+ // Said explicitly, which is the only way to run with no sign-in at all. Not a default, and not+ // inferred from anything: every signal that could stand in for "this is only my laptop" is absent+ // by default on a server too, which is what made the previous NODE_ENV check useless.const asked =environment.OPENBOT_SINGLE_USER?.trim() === "true" ||// The name this had before. Still honoured so an existing .env keeps working.environment.OPENBOT_DEV_NO_AUTH?.trim() === "true";if (asked) return true;- if (environment.NODE_ENV === "production") {- throw new Error(- "No identity provider is configured. Set GOOGLE_OAUTH_*, MICROSOFT_OAUTH_* or OKTA_OAUTH_* with BETTER_AUTH_SECRET and BETTER_AUTH_URL, or set OPENBOT_SINGLE_USER=true to run with one administrator and no sign-in. Refusing to start rather than serving an open deployment.",- );- }-- return true;+ throw new Error(+ "No identity provider is configured. Set GOOGLE_OAUTH_*, MICROSOFT_OAUTH_* or OKTA_OAUTH_* with BETTER_AUTH_SECRET and BETTER_AUTH_URL, or set OPENBOT_SINGLE_USER=true to run with one administrator and no sign-in. Refusing to start rather than serving a deployment where every visitor is an administrator.",+ );}/** A guard that admits everybody as {@link DEV_ACTOR}. Only ever mounted when singleUserEnabled(). */diff --git a/server/src/auth/encrypt-sso-config.ts b/server/src/auth/encrypt-sso-config.tsnew file mode 100644index 00000000..e4acbe8e--- /dev/null+++ b/server/src/auth/encrypt-sso-config.ts@@ -0,0 +1,185 @@+/**+ * The client secret a company's identity provider gave us, encrypted at rest.+ *+ * `sso_providers.oidc_config` is a JSON blob Better Auth's SSO plugin writes, and inside it is the+ * OAuth client secret for that company's directory. `saml_config` is the same shape and holds the+ * signing material. The plugin stores both as plaintext text columns, which is out of step with the+ * rest of this deployment: every other secret it keeps goes through `KEY_ENCRYPTION_KEY`, and a+ * database backup, a read replica, or anybody with SELECT on one table should not be one query away+ * from a customer's directory.+ *+ * The plugin gives no hook for this, so the seam is the adapter. Better Auth reads and writes every+ * row through one, so wrapping it is the only place that catches all of them: the register route, the+ * sign-in path that reads the config back, and any listing.+ *+ * Only two fields on one model. Everything else passes through untouched, because a wrapper that+ * tried to be general would be a second, worse copy of the adapter.+ *+ * Reading tolerates plaintext. A deployment that registered a provider before this existed has+ * plaintext in the column, and refusing to read it would lock that company out of their own sign-in+ * at the moment they upgraded. The value is re-encrypted the next time it is written.+ */+import { decryptSecret, encryptSecret } from "../credentials";++/** The model and fields this touches. Named once so nothing drifts. */+const SSO_MODEL = "ssoProvider";+const ENCRYPTED_FIELDS = ["oidcConfig", "samlConfig"] as const;++/**+ * The narrow slice of the adapter this wraps.+ *+ * Typed structurally rather than imported: Better Auth's `DBAdapter` is generic over the whole+ * options object, and naming it here would tie this file to a type that changes shape between minor+ * versions for reasons that have nothing to do with two string fields.+ */+type Rowish = Record<string, unknown>;++type AdapterMethod = (data: never) => Promise<unknown>;++type AdapterLike = {+ create: AdapterMethod;+ update: AdapterMethod;+ updateMany?: AdapterMethod;+ findOne: AdapterMethod;+ findMany: AdapterMethod;+};++/** The four methods, seen as this file needs to see them rather than as Better Auth declares them. */+type RowishAdapter = Record<+ "create" | "update" | "updateMany" | "findOne" | "findMany",+ ((data: Rowish) => Promise<unknown>) | undefined+>;++function isEnvelope(value: string): boolean {+ // What `encryptSecret` produces. Anything else is a value written before this wrapper existed.+ return value.startsWith('{"version":1');+}++async function encryptFields(+ key: string,+ row: Rowish | undefined,+): Promise<Rowish | undefined> {+ if (!row) return row;+ const next = { ...row };+ for (const field of ENCRYPTED_FIELDS) {+ const value = next[field];+ if (typeof value === "string" && value && !isEnvelope(value)) {+ next[field] = await encryptSecret(key, value);+ }+ }+ return next;+}++async function decryptFields<T>(key: string, row: T): Promise<T> {+ if (!row || typeof row !== "object") return row;+ const next = { ...(row as Rowish) };+ for (const field of ENCRYPTED_FIELDS) {+ const value = next[field];+ if (typeof value === "string" && value && isEnvelope(value)) {+ try {+ next[field] = await decryptSecret(key, value);+ } catch (error) {+ /*+ * A row this deployment cannot read.+ *+ * The usual cause is a changed `KEY_ENCRYPTION_KEY`, and the honest answer is to leave the+ * field out rather than hand the plugin ciphertext it will try to parse as JSON. Sign-in+ * through that provider then fails with the plugin's own "not configured" error, which is+ * true, and this line says why.+ */+ console.error(+ JSON.stringify({+ type: "sso-config-decrypt-failed",+ field,+ note: "The stored identity provider config cannot be read with this KEY_ENCRYPTION_KEY. Sign-in through that provider will fail until it is re-registered.",+ error: String(error),+ }),+ );+ next[field] = null;+ }+ }+ }+ return next as T;+}++/** Whether this call is about the SSO provider table at all. */+function isSsoModel(data: Rowish): boolean {+ return data.model === SSO_MODEL;+}++/**+ * Wrap an adapter factory so the identity provider config is ciphertext in the database.+ *+ * A factory rather than an adapter, because that is what Better Auth is handed and what it calls+ * once with its own resolved options. Wrapping the factory means the wrapper is in place for every+ * adapter Better Auth ever builds, including the one a plugin asks for.+ *+ * The returned adapter is the same adapter with four methods replaced. Anything Better Auth or a+ * plugin reaches for that is not one of those four is passed straight through, including+ * `transaction`, so this cannot quietly disable a capability by not knowing about it.+ */+export function encryptSsoConfig<Options, Adapter extends AdapterLike>(+ createAdapter: (options: Options) => Adapter,+ encryptionKey: string,+): (options: Options) => Adapter {+ return (options) => wrapAdapter(createAdapter(options), encryptionKey);+}++function wrapAdapter<T extends AdapterLike>(+ adapter: T,+ encryptionKey: string,+): T {+ // One cast, here, rather than at every call below. Better Auth's adapter type is generic over its+ // whole options object and describes each method with a model-specific payload; this file only+ // ever reads `model` and one nested object off it.+ const inner = adapter as unknown as RowishAdapter;+ const wrapped = {+ ...adapter,++ create: async (data: Rowish) => {+ if (!isSsoModel(data)) return inner.create?.(data);+ const result = await inner.create?.({+ ...data,+ data: await encryptFields(encryptionKey, data.data as Rowish),+ });+ // Returned to the caller in the clear: the register route answers with what it stored, and the+ // encryption is about the column rather than about the request.+ return decryptFields(encryptionKey, result);+ },++ update: async (data: Rowish) => {+ if (!isSsoModel(data)) return inner.update?.(data);+ const result = await inner.update?.({+ ...data,+ update: await encryptFields(encryptionKey, data.update as Rowish),+ });+ return decryptFields(encryptionKey, result);+ },++ findOne: async (data: Rowish) => {+ const result = await inner.findOne?.(data);+ if (!isSsoModel(data)) return result;+ return decryptFields(encryptionKey, result);+ },++ findMany: async (data: Rowish) => {+ const result = await inner.findMany?.(data);+ if (!isSsoModel(data) || !Array.isArray(result)) return result;+ return Promise.all(+ result.map((row) => decryptFields(encryptionKey, row)),+ );+ },+ };++ if (inner.updateMany) {+ (wrapped as unknown as RowishAdapter).updateMany = async (data: Rowish) => {+ if (!isSsoModel(data)) return inner.updateMany?.(data);+ return inner.updateMany?.({+ ...data,+ update: await encryptFields(encryptionKey, data.update as Rowish),+ });+ };+ }++ return wrapped as unknown as T;+}diff --git a/server/src/auth/identity-provider-store.ts b/server/src/auth/identity-provider-store.tsnew file mode 100644index 00000000..1a37cd0d--- /dev/null+++ b/server/src/auth/identity-provider-store.ts@@ -0,0 +1,87 @@+/**+ * The identity providers this deployment has registered, as facts about the deployment.+ *+ * Better Auth's SSO plugin owns the table and the registration route, and both are right: parsing a+ * company's OIDC discovery document or SAML metadata is work worth not repeating. What it also does+ * is stamp the registering person's id on the row and then answer `GET /sso/providers` with+ * `providers.filter(p => p.userId === session.user.id)`, and refuse a delete unless the same person+ * asks. That is the correct model for a personal integration and the wrong one for this: a company's+ * Okta tenant is not the property of whichever administrator happened to paste the metadata in.+ *+ * Two things went wrong from it. A second administrator opening the Identity providers screen saw an+ * empty list and re-registered a provider that was already there, and the person who set it up+ * leaving the company took the company's sign-in with them, because the row cascaded from their user+ * row. So reads and removals come through here, against the whole table, and the column that used to+ * decide existence is now only a record of who registered it.+ *+ * Registration still goes to Better Auth. This never writes a provider, so there is one parser for+ * metadata and one place a client secret is stored.+ */+import { eq } from "drizzle-orm";+import type { Database } from "../db/client";+import { ssoProviders } from "../db/schema";++/**+ * A registered provider, as an administrator screen may see it.+ *+ * Deliberately not the row. `oidcConfig` and `samlConfig` carry a client secret and a signing+ * certificate, and no projection that includes them can be safe to send to a browser, so the shape+ * that leaves this module cannot express them.+ */+export type RegisteredIdentityProvider = {+ providerId: string;+ issuer: string;+ /** The email domain that routes somebody here. */+ domain: string;+ /** Which protocol it speaks. SAML is what most enterprise identity teams hand over. */+ protocol: "saml" | "oidc";+ /** Whether the person who registered it still has an account here. Null once they are gone. */+ registeredBy: string | null;+};++export type IdentityProviderStore = {+ /** Every provider this deployment holds, whoever registered it. */+ list: () => Promise<RegisteredIdentityProvider[]>;+ /**+ * Forget one. Answers whether there was one to forget, so a caller can tell a stale screen from a+ * failure rather than reporting both as success.+ */+ remove: (providerId: string) => Promise<boolean>;+};++export function createIdentityProviderStore(+ database: Database,+): IdentityProviderStore {+ return {+ list: async () => {+ const rows = await database+ .select({+ providerId: ssoProviders.providerId,+ issuer: ssoProviders.issuer,+ domain: ssoProviders.domain,+ samlConfig: ssoProviders.samlConfig,+ userId: ssoProviders.userId,+ })+ .from(ssoProviders)+ .orderBy(ssoProviders.domain);++ return rows.map((row) => ({+ providerId: row.providerId,+ issuer: row.issuer,+ domain: row.domain,+ // A row carries one config or the other, and which one is the protocol.+ protocol: row.samlConfig ? "saml" : "oidc",+ registeredBy: row.userId,+ }));+ },++ remove: async (providerId) => {+ const removed = await database+ .delete(ssoProviders)+ .where(eq(ssoProviders.providerId, providerId))+ .returning({ providerId: ssoProviders.providerId });++ return removed.length > 0;+ },+ };+}diff --git a/server/src/auth/index.ts b/server/src/auth/index.tsindex 5afc14d4..cb79b612 100644--- a/server/src/auth/index.ts+++ b/server/src/auth/index.ts@@ -4,6 +4,8 @@ import { betterAuth } from "better-auth";import { APIError } from "better-auth/api";import { genericOAuth, okta } from "better-auth/plugins";import { eq } from "drizzle-orm";+import type { AuditEventInput, AuditStore } from "../audit";+import { recordAuditEvent } from "../audit";import type { DeploymentConfig } from "../config";import type { Database } from "../db/client";import {@@ -13,8 +15,34 @@ import {users,verifications,} from "../db/schema";+import { encryptSsoConfig } from "./encrypt-sso-config";import { applyConfiguredAdmin, seedRole } from "./roles";+/**+ * Write a row about a sign-in, and never let the writing of it stop one.+ *+ * These run inside Better Auth's own hooks, where a thrown error becomes a refused sign-in. A trail+ * that is briefly unavailable must not lock everybody out of the deployment, so the failure is+ * logged where an operator will see it and the sign-in continues.+ */+async function record(+ auditStore: AuditStore | undefined,+ event: AuditEventInput,+): Promise<void> {+ if (!auditStore) return;+ try {+ await recordAuditEvent(auditStore, event);+ } catch (error) {+ console.error(+ JSON.stringify({+ type: "sign-in-audit-write-failed",+ eventType: event.eventType,+ error: String(error),+ }),+ );+ }+}+/*** An address for somebody arriving from Entra, whatever claim it turned up in.*@@ -68,6 +96,18 @@ export function createAuth(* having worked while quietly not having.*/isRevoked?: (email: string) => Promise<boolean>,+ /**+ * Where getting in, and being turned away, are written down.+ *+ * Sign-in was the one thing this deployment did that left no trace. Two questions could not be+ * answered at all: who granted themselves the administrator role by editing the configuration, and+ * whether a person somebody has just removed had ever been here, because removing them deletes the+ * sessions that were the only evidence.+ *+ * Optional and never fatal. A trail that is unavailable must not stop somebody signing in, so every+ * write below is guarded and its failure is logged rather than raised.+ */+ auditStore?: AuditStore,) {const authConfig = config.auth;if (!authConfig) {@@ -127,11 +167,34 @@ export function createAuth(baseURL: authConfig.baseUrl,secret: authConfig.secret,trustedOrigins: authConfig.trustedOrigins,- database: drizzleAdapter(database, {- provider: "pg",- usePlural: true,- schema: { users, sessions, accounts, verifications, ssoProviders },- }),+ /*+ * Wrapped, so a company's client secret is ciphertext in the column.+ *+ * The SSO plugin writes `oidc_config` and `saml_config` as plaintext JSON, and the client secret+ * for a customer's directory is inside them. Every other secret this deployment keeps goes+ * through `KEY_ENCRYPTION_KEY`; these two were the exception. See encrypt-sso-config.ts.+ */+ database: encryptSsoConfig(+ drizzleAdapter(database, {+ provider: "pg",+ usePlural: true,+ schema: { users, sessions, accounts, verifications, ssoProviders },+ }),+ config.keyEncryptionKey,+ ),+ account: {+ /*+ * The provider's access and refresh tokens, encrypted at rest.+ *+ * Better Auth's own mechanism, which uses `BETTER_AUTH_SECRET` rather than+ * `KEY_ENCRYPTION_KEY`. Deliberately theirs: it encrypts on the way into storage and decrypts+ * on the way out, in the one place that knows every path a token takes, and hand-rolling that+ * inside somebody else's storage layer is how rows become permanently unreadable. It also+ * tolerates the plaintext already in the column, so switching it on does not invalidate the+ * accounts of everybody who has already signed in.+ */+ encryptOAuthTokens: true,+ },plugins,socialProviders: {...(authConfig.google ? { google: authConfig.google } : {}),@@ -158,6 +221,16 @@ export function createAuth(*/before: async (user) => {if (await isRevoked?.(user.email)) {+ // The row a removed person coming back produces. Nothing else records the attempt:+ // no user row is written and no session exists to look at afterwards.+ await record(auditStore, {+ eventType: "session.refused",+ targetType: "person",+ payload: {+ email: user.email,+ reason: "access removed by an administrator",+ },+ });throw new APIError("FORBIDDEN", {message: "Your access to this deployment has been removed.",});@@ -192,6 +265,16 @@ export function createAuth(.where(eq(users.id, session.userId)).limit(1);if (user && (await isRevoked?.(user.email))) {+ await record(auditStore, {+ eventType: "session.refused",+ targetType: "person",+ targetId: session.userId,+ actorUserId: session.userId,+ payload: {+ email: user.email,+ reason: "access removed by an administrator",+ },+ });throw new APIError("FORBIDDEN", {message: "Your access to this deployment has been removed.",});@@ -205,11 +288,47 @@ export function createAuth(* in silently does nothing. Only promotes, and only addresses the list names: everybody* else's role belongs to the admin screen.*/- await applyConfiguredAdmin(+ const promoted = await applyConfiguredAdmin(database,session.userId,authConfig.initialAdminEmails,);++ const [user] = await database+ .select({ email: users.email })+ .from(users)+ .where(eq(users.id, session.userId))+ .limit(1);++ /*+ * The promotion, on the trail.+ *+ * The floor is applied silently by design, which meant anybody who could edit+ * `INITIAL_ADMIN_EMAILS` made themselves an administrator and nothing anywhere said so.+ * Written only when the role actually changed, so a returning administrator does not+ * produce one of these on every sign-in.+ */+ if (promoted) {+ await record(auditStore, {+ eventType: "person.admin_by_configuration",+ targetType: "person",+ targetId: session.userId,+ actorUserId: session.userId,+ payload: {+ email: user?.email,+ reason:+ "this address is named in INITIAL_ADMIN_EMAILS, so the configuration granted it",+ },+ });+ }++ await record(auditStore, {+ eventType: "session.signed_in",+ targetType: "person",+ targetId: session.userId,+ actorUserId: session.userId,+ payload: { email: user?.email },+ });},},},diff --git a/server/src/auth/roles.ts b/server/src/auth/roles.tsindex 59201dc5..5f99c919 100644--- a/server/src/auth/roles.ts+++ b/server/src/auth/roles.ts@@ -77,14 +77,18 @@ export async function seedRole(** Everybody else is left exactly as they are, because their role is the admin screen's to decide and* a sign-in that overwrote it would make that screen lie the moment they came back.+ *+ * Answers whether this call is what granted the role, so the caller can put that on the audit trail.+ * Anybody who can edit the configuration can make themselves an administrator, and until this+ * returned something there was no row anywhere saying it had happened.*/export async function applyConfiguredAdmin(database: Database,userId: string,initialAdminEmails: readonly string[],-): Promise<void> {+): Promise<boolean> {// Nothing configured cannot promote anybody, so there is no reason to read the user back.- if (initialAdminEmails.length === 0) return;+ if (initialAdminEmails.length === 0) return false;const [user] = await database.select({ email: users.email })@@ -94,9 +98,19 @@ export async function applyConfiguredAdmin(// No user means a session is being made for somebody who is not there, which is not this module's// to report: Better Auth is about to fail on its own and would only be given a worse message here.- if (!user) return;+ if (!user) return false;++ if (!isConfiguredAdmin(user.email, initialAdminEmails)) return false;- if (!isConfiguredAdmin(user.email, initialAdminEmails)) return;+ const [already] = await database+ .select({ role: userRoles.role })+ .from(userRoles)+ .where(and(eq(userRoles.userId, userId), eq(userRoles.role, "admin")))+ .limit(1);await setRole(database, userId, "admin");++ // Whether this sign-in is what granted it. The caller writes an audit row when it did, and a+ // returning administrator must not produce one on every sign-in.+ return !already;}diff --git a/server/src/db/schema/core.ts b/server/src/db/schema/core.tsindex abf26409..7547d34e 100644--- a/server/src/db/schema/core.ts+++ b/server/src/db/schema/core.ts@@ -79,11 +79,17 @@ export const accounts = pgTable(** Required by Better Auth from 1.7. A real OIDC provider supplies its own* (`https://accounts.google.com`), and one without gets a synthetic- * `local:oauth:<providerId>`, so the column is never empty. It exists because `providerId`- * alone stopped being enough once a deployment can register more than one OIDC provider: two- * companies' Okta tenants are both "okta" and are not the same directory.+ * `local:oauth:<providerId>`, so nothing this deployment writes leaves it empty. It exists+ * because `providerId` alone stopped being enough once a deployment can register more than one+ * OIDC provider: two companies' Okta tenants are both "okta" and are not the same directory.+ *+ * Nullable in the database, deliberately, even though every write fills it. A rolling deploy runs+ * the migrations and then serves from old and new replicas at once, and an old replica inserts an+ * account without this column. Under `NOT NULL` that insert fails, so the release that adds the+ * column would break the first sign-in of everybody who landed on a replica that had not been+ * replaced yet. The constraint belongs to a later release, once no replica predates the column.*/- issuer: text("issuer").notNull(),+ issuer: text("issuer"),userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),@@ -149,7 +155,18 @@ export const ssoProviders = pgTable("sso_providers", {issuer: text("issuer").notNull(),oidcConfig: text("oidc_config"),samlConfig: text("saml_config"),- userId: text("user_id").references(() => users.id, { onDelete: "cascade" }),+ /**+ * Who registered it, as a note rather than as an owner.+ *+ * Better Auth writes this and then scopes its own listing and delete routes to it, which is the+ * right model for a personal integration and the wrong one here: a company's Okta tenant does not+ * belong to whichever administrator pasted the metadata in. Reads and removals go through+ * identity-provider-store.ts instead, against the whole table.+ *+ * `set null` and not `cascade`. Under cascade, removing the person who set sign-in up deleted the+ * deployment's sign-in with them, which is a bad afternoon for a company whose IT lead has left.+ */+ userId: text("user_id").references(() => users.id, { onDelete: "set null" }),providerId: text("provider_id").notNull().unique(),organizationId: text("organization_id"),domain: text("domain").notNull(),diff --git a/server/src/index.ts b/server/src/index.tsindex 352a600e..4ff13dce 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -8,6 +8,7 @@ import { createAuditReader, createAuditStore, recordAuditEvent } from "./audit";import { createAuth } from "./auth";import { DEV_ACTOR, initializeDevActorUser } from "./auth/dev-actor";import { createRoleRepository } from "./auth/guards";+import { createIdentityProviderStore } from "./auth/identity-provider-store";import type { OpenBotRole } from "./auth/roles";import {createChannelEventHub,@@ -42,7 +43,6 @@ import {resolveModelApiKey,} from "./credentials";import { createDatabase } from "./db/client";-import { ssoProviders } from "./db/schema";import { createPeopleStore } from "./people/store";import { createPluginStore } from "./plugins/store";import { grantedTools } from "./plugins/tools";@@ -169,8 +169,19 @@ const peopleStore = createPeopleStore(database,config.auth?.initialAdminEmails ?? [],);+const identityProviderStore = createIdentityProviderStore(database);+/*+ * Built before `auth` for the same reason the people store is: sign-in writes to the trail, and the+ * store that receives those rows has to exist before anything can sign in.+ */+const signInAuditStore = createAuditStore(database);const auth = config.auth- ? createAuth(config, database, (email) => peopleStore.isRevoked(email))+ ? createAuth(+ config,+ database,+ (email) => peopleStore.isRevoked(email),+ signInAuditStore,+ ): undefined;const computerProvider = config.computer? createComputerProvider(config.computer)@@ -388,13 +399,9 @@ const app = createApp(threadIdentity,// Who has signed in, and what an administrator may do about them.peopleStore,- // Whether the sign-in screen should offer the email box that routes by domain.- async () => {- const [row] = await database- .select({ count: sql<number>`count(*)::int` })- .from(ssoProviders);- return row?.count ?? 0;- },+ // The enterprise identity providers registered here. Read as facts about the deployment rather+ // than through Better Auth's own listing, which answers per person. See identity-provider-store.ts.+ identityProviderStore,);/**diff --git a/server/tests/config.test.ts b/server/tests/config.test.tsindex 60e580b0..5e470996 100644--- a/server/tests/config.test.ts+++ b/server/tests/config.test.ts@@ -56,7 +56,7 @@ describe("deployment configuration", () => {expect(config.tenantPackageDirectory).toBe("../examples/fintech");});- test("allows deployment without an authentication provider", () => {+ test("allows deployment without an authentication provider, when asked to", () => {const config = loadConfig({DATABASE_URL: baseEnvironment.DATABASE_URL,KEY_ENCRYPTION_KEY: baseEnvironment.KEY_ENCRYPTION_KEY,@@ -66,6 +66,9 @@ describe("deployment configuration", () => {COPILOTKIT_LICENSE_TOKEN: baseEnvironment.COPILOTKIT_LICENSE_TOKEN,MANAGED_AGENT_AG_UI_URL: baseEnvironment.MANAGED_AGENT_AG_UI_URL,MANAGED_AGENT_TOKEN: baseEnvironment.MANAGED_AGENT_TOKEN,+ // Explicit, because no provider means every visitor is the administrator and a deployment has+ // to say it meant that. See single-user.test.ts.+ OPENBOT_SINGLE_USER: "true",});expect(config.auth).toBeUndefined();@@ -185,6 +188,9 @@ describe("deployment configuration", () => {INITIAL_ADMIN_EMAILS: "[email protected]",};+ /** What a deployment with no provider has to say before it is allowed to come up. */+ const OPEN = { OPENBOT_SINGLE_USER: "true" };+test("enables Microsoft, and admits any account until told a directory", () => {const config = loadConfig({...withoutSignIn,@@ -289,13 +295,22 @@ describe("deployment configuration", () => {});test("asks for no administrator when nothing signs anybody in", () => {- // One administrator either way, and no list to write. Requiring one here would mean a fresh- // clone could not start.- expect(() => loadConfig(withoutSignIn)).not.toThrow();+ // One administrator either way, and no list to write. Requiring one here as well would mean a+ // deployment had to name an administrator for a mode that has exactly one.+ expect(() => loadConfig({ ...withoutSignIn, ...OPEN })).not.toThrow();+ });++ test("refuses to start with no provider and nothing saying that was meant", () => {+ // The whole of the sign-in story in one line. This used to come up open, and `NODE_ENV` was the+ // only thing standing between a bare-VM deployment and serving every visitor as an+ // administrator, which is unset by default on exactly that deployment.+ expect(() => loadConfig(withoutSignIn)).toThrow(+ "No identity provider is configured",+ );});test("is off, and lists nothing, when no provider is configured", () => {- const config = loadConfig(withoutSignIn);+ const config = loadConfig({ ...withoutSignIn, ...OPEN });expect(config.auth).toBeUndefined();expect(configuredAuthProviders(config.auth)).toEqual([]);diff --git a/server/tests/encrypt-sso-config.test.ts b/server/tests/encrypt-sso-config.test.tsnew file mode 100644index 00000000..ced5e9d3--- /dev/null+++ b/server/tests/encrypt-sso-config.test.ts@@ -0,0 +1,147 @@+import { describe, expect, test } from "bun:test";+import { encryptSsoConfig } from "../src/auth/encrypt-sso-config";+import { decryptSecret, encryptSecret } from "../src/credentials";++/**+ * A customer's identity provider client secret, in the column.+ *+ * Better Auth's SSO plugin writes `oidc_config` as plaintext JSON with the OAuth client secret for+ * that company's directory inside it, which is the one secret in this deployment that was not going+ * through `KEY_ENCRYPTION_KEY`. The wrapper is the seam; these are what it has to be true of.+ */++const KEY = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";++const OIDC_CONFIG = JSON.stringify({+ clientId: "openbot",+ clientSecret: "the-customers-directory-secret",+ discoveryEndpoint: "https://acme.okta.com/.well-known/openid-configuration",+});++/** An adapter that records what it was asked to store, standing in for the database. */+function fakeAdapter() {+ const stored: Record<string, unknown>[] = [];+ let nextRead: Record<string, unknown> | null = null;++ const adapter = {+ create: async (data: Record<string, unknown>) => {+ stored.push(data.data as Record<string, unknown>);+ return data.data;+ },+ update: async (data: Record<string, unknown>) => {+ stored.push(data.update as Record<string, unknown>);+ return data.update;+ },+ findOne: async () => nextRead,+ findMany: async () => (nextRead ? [nextRead] : []),+ };++ return {+ adapter,+ stored,+ holds: (row: Record<string, unknown>) => {+ nextRead = row;+ },+ };+}++function wrap(adapter: ReturnType<typeof fakeAdapter>["adapter"]) {+ // The wrapper takes a factory, because that is what Better Auth is handed.+ return encryptSsoConfig(() => adapter, KEY)({});+}++describe("the identity provider config in the database", () => {+ test("is ciphertext, and the client secret is not in it", async () => {+ const fake = fakeAdapter();+ await wrap(fake.adapter).create({+ model: "ssoProvider",+ data: { providerId: "acme", oidcConfig: OIDC_CONFIG },+ });++ const written = fake.stored[0]?.oidcConfig as string;+ expect(written).not.toContain("the-customers-directory-secret");+ // And it is our envelope, so the same key that reads every other secret here reads this one.+ expect(await decryptSecret(KEY, written)).toBe(OIDC_CONFIG);+ });++ test("comes back in the clear, so the plugin sees what it wrote", async () => {+ const fake = fakeAdapter();+ fake.holds({+ providerId: "acme",+ oidcConfig: await encryptSecret(KEY, OIDC_CONFIG),+ });++ const row = (await wrap(fake.adapter).findOne({+ model: "ssoProvider",+ })) as { oidcConfig: string };++ expect(row.oidcConfig).toBe(OIDC_CONFIG);+ });++ test("a row written before this existed still reads, so an upgrade does not lock anybody out", async () => {+ const fake = fakeAdapter();+ fake.holds({ providerId: "acme", oidcConfig: OIDC_CONFIG });++ const row = (await wrap(fake.adapter).findOne({+ model: "ssoProvider",+ })) as { oidcConfig: string };++ // Plaintext, left alone. Refusing it would take a company's sign-in away at the moment they+ // upgraded, which is a worse outcome than a column that is encrypted from its next write on.+ expect(row.oidcConfig).toBe(OIDC_CONFIG);+ });++ test("a SAML provider's signing config is covered too", async () => {+ const fake = fakeAdapter();+ const samlConfig = JSON.stringify({ privateKey: "-----BEGIN PRIVATE KEY" });+ await wrap(fake.adapter).create({+ model: "ssoProvider",+ data: { providerId: "acme", samlConfig },+ });++ expect(fake.stored[0]?.samlConfig).not.toContain("BEGIN PRIVATE KEY");+ });++ test("every other model passes straight through", async () => {+ // A wrapper that touched user or session rows would be a second, worse copy of the adapter.+ const fake = fakeAdapter();+ await wrap(fake.adapter).create({+ model: "user",+ data: { email: "[email protected]", oidcConfig: "not-a-secret-here" },+ });++ expect(fake.stored[0]?.oidcConfig).toBe("not-a-secret-here");+ });++ test("an already-encrypted value is not encrypted twice", async () => {+ const fake = fakeAdapter();+ const envelope = await encryptSecret(KEY, OIDC_CONFIG);+ await wrap(fake.adapter).create({+ model: "ssoProvider",+ data: { providerId: "acme", oidcConfig: envelope },+ });++ // Double-wrapping would decrypt to an envelope rather than to the config, and the plugin would+ // fail to parse it as JSON on the next sign-in.+ expect(await decryptSecret(KEY, fake.stored[0]?.oidcConfig as string)).toBe(+ OIDC_CONFIG,+ );+ });++ test("a value this key cannot read is dropped rather than handed over as ciphertext", async () => {+ const fake = fakeAdapter();+ const otherKey = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=";+ fake.holds({+ providerId: "acme",+ oidcConfig: await encryptSecret(otherKey, OIDC_CONFIG),+ });++ const row = (await wrap(fake.adapter).findOne({+ model: "ssoProvider",+ })) as { oidcConfig: string | null };++ // KEY_ENCRYPTION_KEY changed. Handing the plugin ciphertext would have it try to parse an+ // envelope as an OIDC config; null makes it say the provider is not configured, which is true.+ expect(row.oidcConfig).toBeNull();+ });+});diff --git a/server/tests/roles.test.ts b/server/tests/roles.test.tsindex 8d9a2308..a3405ba5 100644--- a/server/tests/roles.test.ts+++ b/server/tests/roles.test.ts@@ -125,16 +125,33 @@ describe("setRole", () => {* to whoever administers the deployment, and a sign-in that rewrote it would make that screen lie* the moment they came back.*/-function databaseWithUser(email: string | null): {+function databaseWithUser(+ email: string | null,+ /**+ * Whether this person is already an administrator.+ *+ * `applyConfiguredAdmin` reads it so it can answer whether this sign-in is what granted the role.+ * The caller writes an audit row when it did, and a returning administrator must not produce one+ * on every sign-in.+ */+ alreadyAdmin = false,+): {database: Database;written: string[];} {const written: string[] = [];const database = {- select: () => ({+ // Two different reads go through here: the user's address, and whether they already hold the+ // role. Told apart by the projection, because that is the only thing the double can see.+ select: (projection?: Record<string, unknown>) => ({from: () => ({where: () => ({- limit: async () => (email === null ? [] : [{ email }]),+ limit: async () => {+ if (projection && "role" in projection) {+ return alreadyAdmin ? [{ role: "admin" }] : [];+ }+ return email === null ? [] : [{ email }];+ },}),}),}),@@ -165,6 +182,44 @@ describe("applyConfiguredAdmin", () => {expect(written).toEqual(["admin"]);});+ /*+ * Whether this sign-in is what granted the role.+ *+ * The floor is re-applied silently on every sign-in, which meant anybody who could edit+ * `INITIAL_ADMIN_EMAILS` made themselves an administrator with nothing anywhere recording it. The+ * caller writes the audit row, and it needs to know which sign-in did it, or every sign-in by+ * every administrator would produce one and the row would say nothing.+ */+ test("says so when this sign-in is what granted the role", async () => {+ const { database } = databaseWithUser("[email protected]");++ expect(+ await applyConfiguredAdmin(database, "u1", ["[email protected]"]),+ ).toBe(true);+ });++ test("says nothing when they were already an administrator", async () => {+ const { database } = databaseWithUser("[email protected]", true);++ expect(+ await applyConfiguredAdmin(database, "u1", ["[email protected]"]),+ ).toBe(false);+ });++ test("says nothing for somebody the configuration does not name", async () => {+ const { database } = databaseWithUser("[email protected]");++ expect(+ await applyConfiguredAdmin(database, "u1", ["[email protected]"]),+ ).toBe(false);+ });++ test("says nothing when nothing is configured", async () => {+ const { database } = databaseWithUser("[email protected]");++ expect(await applyConfiguredAdmin(database, "u1", [])).toBe(false);+ });+/*** The reason this is a floor and not the whole answer.*diff --git a/server/tests/single-user.test.ts b/server/tests/single-user.test.tsnew file mode 100644index 00000000..16eaebac--- /dev/null+++ b/server/tests/single-user.test.ts@@ -0,0 +1,70 @@+import { describe, expect, test } from "bun:test";+import { singleUserEnabled } from "../src/auth/dev-actor";++/**+ * Whether this deployment lets every visitor in as one administrator.+ *+ * The most dangerous boolean in the codebase and it had no test. It used to be locked with+ * `NODE_ENV === "production"`, which is exactly the wrong way round: `NODE_ENV` is unset unless+ * somebody sets it, so a container on a VM with a hand-written env file and no identity provider+ * served the whole internet as an administrator, silently, because nothing was wrong from the+ * outside. It now takes an explicit flag and refuses to start without one.+ *+ * The cases below are the four a deployment can actually be in.+ */+describe("running with no sign-in", () => {+ test("a configured provider always wins, whatever else is set", () => {+ // A provider means sign-in, so the flag cannot half-disable it. Asserted with the flag on,+ // because the dangerous reading is "the flag opens a deployment that has a provider".+ expect(singleUserEnabled({ OPENBOT_SINGLE_USER: "true" }, true)).toBe(+ false,+ );+ expect(singleUserEnabled({}, true)).toBe(false);+ });++ test("no provider and the flag set runs open, because somebody said so", () => {+ expect(singleUserEnabled({ OPENBOT_SINGLE_USER: "true" }, false)).toBe(+ true,+ );+ });++ test("the older name for the flag still works, so an existing .env keeps running", () => {+ expect(singleUserEnabled({ OPENBOT_DEV_NO_AUTH: "true" }, false)).toBe(+ true,+ );+ });++ test("no provider and no flag refuses to start", () => {+ // The whole point. Not a warning, not a default: a deployment that would admit everybody as an+ // administrator does not come up, and says what to configure.+ expect(() => singleUserEnabled({}, false)).toThrow(+ /No identity provider is configured/,+ );+ });++ test("NODE_ENV decides nothing, in either direction", () => {+ // The previous lock. A server with NODE_ENV unset was the case it had to catch and the case it+ // let through, so neither value may change the answer now.+ expect(() =>+ singleUserEnabled({ NODE_ENV: "development" }, false),+ ).toThrow();+ expect(() =>+ singleUserEnabled({ NODE_ENV: "production" }, false),+ ).toThrow();+ expect(+ singleUserEnabled(+ { NODE_ENV: "production", OPENBOT_SINGLE_USER: "true" },+ false,+ ),+ ).toBe(true);+ });++ test("a flag set to anything but true is not a yes", () => {+ // Read exactly, so `OPENBOT_SINGLE_USER=false` and a stray `1` both refuse rather than opening.+ for (const value of ["false", "1", "yes", "TRUE", ""]) {+ expect(() =>+ singleUserEnabled({ OPENBOT_SINGLE_USER: value }, false),+ ).toThrow();+ }+ });+});