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: constant-time comparison for the supervisor token (CWE-208) (#685)
Omkar Chebale46eb8e2dc48b
diff --git a/supervisor/src/authorisation.ts b/supervisor/src/authorisation.tsnew file mode 100644index 00000000..a11ccf44--- /dev/null+++ b/supervisor/src/authorisation.ts@@ -0,0 +1,19 @@+import { timingSafeEqual } from "node:crypto";++/**+ * Whether a request's `Authorization` header carries the supervisor's secret.+ *+ * Compared in constant time, as the server (`sameToken`) and the computer (`matchesToken`) already+ * compare theirs. A plain `!==` stops at the first differing character, so the time a refusal takes+ * says how much of a guess was right. This process holds the Docker socket, so it should leak no+ * less than the services it creates. Length still leaks; content does not.+ *+ * Kept out of `index.ts`, which exits at import without its environment, so a test can reach it.+ */+export function authorised(header: string | undefined, token: string): boolean {+ if (!token || !header) return false;+ const offered = Buffer.from(header);+ const expected = Buffer.from(`Bearer ${token}`);+ if (offered.length !== expected.length) return false;+ return timingSafeEqual(offered, expected);+}diff --git a/supervisor/src/index.ts b/supervisor/src/index.tsindex 3ed41df5..e995abc3 100644--- a/supervisor/src/index.ts+++ b/supervisor/src/index.ts@@ -1,5 +1,6 @@import { serve } from "bun";import { Hono } from "hono";+import { authorised } from "./authorisation";import { environmentFor } from "./environment";import {ComputerNotAnsweringError,@@ -77,7 +78,7 @@ const app = new Hono();app.use("*", async (context, next) => {// Health is open so an orchestrator can check it without holding the token.if (context.req.path === "/health") return next();- if (context.req.header("authorization") !== `Bearer ${token}`) {+ if (!authorised(context.req.header("authorization"), token)) {return context.json({ error: "Unauthorized." }, 401);}return next();diff --git a/supervisor/tests/authorisation.test.ts b/supervisor/tests/authorisation.test.tsnew file mode 100644index 00000000..369a8c45--- /dev/null+++ b/supervisor/tests/authorisation.test.ts@@ -0,0 +1,29 @@+import { describe, expect, test } from "bun:test";+import { authorised } from "../src/authorisation";++describe("who may drive the supervisor", () => {+ test("the exact bearer secret is admitted", () => {+ expect(authorised("Bearer s3cret-token", "s3cret-token")).toBe(true);+ });++ test.each([+ ["no header", undefined],+ ["an empty header", ""],+ ["the bare secret without Bearer", "s3cret-token"],+ ["a wrong secret of the same length", "Bearer s3cret-tokeX"],+ ["a prefix of the secret", "Bearer s3cret"],+ ["the secret with something appended", "Bearer s3cret-token2"],+ ["another scheme", "Basic s3cret-token"],+ ])("%s is refused", (_, header) => {+ expect(authorised(header, "s3cret-token")).toBe(false);+ });++ test("an empty configured secret admits nobody, not even an empty bearer", () => {+ expect(authorised("Bearer ", "")).toBe(false);+ });++ test("a multi-byte secret is compared by bytes and still matches only itself", () => {+ expect(authorised("Bearer clé-é", "clé-é")).toBe(true);+ expect(authorised("Bearer clé-e", "clé-é")).toBe(false);+ });+});