Multiplayer agent harness for work.
Use one canonical request path for routing and authorization (#2579)
Regan Bella8487c1ef395
diff --git a/src/api/routes/index.ts b/src/api/routes/index.tsindex 33162b951..928028b5d 100644--- a/src/api/routes/index.ts+++ b/src/api/routes/index.ts@@ -104,6 +104,7 @@ export const apiRoutes: ReadonlyArray<Route<ApiCtx>> = [...projectRoutes,...contextPolicyRoutes,...cronRoutes,+ ...loopItemRoutes,...loopRoutes,...reachRoutes,...webhookRoutes,@@ -116,7 +117,6 @@ export const apiRoutes: ReadonlyArray<Route<ApiCtx>> = [...environmentRoutes,...emojiRoutes,...inboxRoutes,- ...loopItemRoutes,...deploymentRoutes,...egressAuditRoutes,...authBrokerRoutes,diff --git a/src/api/server.ts b/src/api/server.tsindex 58edce589..862ce2a0c 100644--- a/src/api/server.ts+++ b/src/api/server.ts@@ -62,6 +62,15 @@ async function capabilityAdminDenied(const WRITE_METHODS = new Set(["POST", "PUT", "PATCH", "DELETE"]);+function needlessEscape(escaped: string): boolean {+ const char = String.fromCharCode(Number.parseInt(escaped.slice(0, 2), 16));+ return encodeURIComponent(char) === char;+}++function canonicalPath(req: IncomingMessage, pathname: string): boolean {+ return (req.url ?? "/").split("?", 1)[0] === pathname && !pathname.split("%").slice(1).some(needlessEscape);+}+function capabilityFromHeaders(req: IncomingMessage): string | null {const h = req.headers[CAPABILITY_HEADER];return typeof h === "string" && h ? h : null;@@ -493,6 +502,10 @@ function buildServer(app: App, deps: ServerOptions, allowUnsignedSourceAuth: boonameRequest(base.method, "/deployment-proxy/*");return;}+ if (!canonicalPath(req, base.pathname)) {+ sendJson(res, 404, { error: "not_found", message: `${base.method} ${base.pathname}` });+ return void req.resume();+ }const raw = findRoute(rawRoutes, base.method, base.pathname);if (raw) {if ("path" in raw.route) nameRequest(base.method, raw.route.path);diff --git a/test/admin-agent-capability.test.ts b/test/admin-agent-capability.test.tsindex f45c860ee..363dd0aa3 100644--- a/test/admin-agent-capability.test.ts+++ b/test/admin-agent-capability.test.ts@@ -398,6 +398,51 @@ test("grant management refuses agent tokens outright — promote/revoke stays in}});+test("admin walls hold for every spelling of a path, and encoded params still route", async () => {+ const s = start();+ try {+ const live = await capFor("admin-alice");+ const unattended = await capFor("admin-alice", { live: false });+ for (const [path, status] of [+ ["/v1/admin/grants", 403],+ ["/v1/%61dmin/grants", 404],+ ["/v1/admin/gr%61nts", 404],+ ] as const) {+ const promote = await fetch(`${s.base}${path}`, {+ method: "POST",+ headers: { "x-agent-capability": live, "content-type": "application/json" },+ body: JSON.stringify({ principalId: "U9", role: "org_admin", scopeId: ORG }),+ });+ assert.equal(promote.status, status, `promote via ${path}`);+ }+ for (const [path, status] of [+ ["/v1/admin/grants/admin-bob", 403],+ ["/v1/%61dmin/grants/admin-bob", 404],+ ] as const) {+ const revoke = await fetch(`${s.base}${path}?scope=${encodeURIComponent(ORG)}&role=org_admin`, {+ method: "DELETE",+ headers: { "x-agent-capability": unattended },+ });+ assert.equal(revoke.status, status, `unattended revoke via ${path}`);+ }+ const whoami = async (who: string) =>+ (+ (await (+ await fetch(`${s.base}/v1/admin/whoami`, { headers: { "x-agent-capability": await capFor(who) } })+ ).json()) as any+ ).isAdmin;+ assert.equal(await whoami("U9"), false);+ assert.equal(await whoami("admin-bob"), true);++ const scope = await fetch(`${s.base}/v1/admin/scopes/${encodeURIComponent(ORG)}`, {+ headers: { "x-agent-capability": live },+ });+ assert.equal(scope.status, 200);+ } finally {+ await s.close();+ }+});+test("MCP server registration refuses agent tokens — the agent cannot change which calls need approval", async () => {const s = start();try {diff --git a/test/portal-identity-gate.test.ts b/test/portal-identity-gate.test.tsindex b2d6b57ca..fb02df171 100644--- a/test/portal-identity-gate.test.ts+++ b/test/portal-identity-gate.test.ts@@ -363,6 +363,19 @@ describe("user-scoped routes require a portal-verified actor when enforcement isassert.equal(r.status, 403);});+ it("a web turn is gated whichever way its path is spelled", async () => {+ const turn = {+ surface: "web",+ text: "hi",+ actor: { externalId: "U2" },+ conversation: { kind: "dm", threadRef: "t" },+ };+ const identity = { "x-portal-identity": await token("U1") };+ assert.equal((await post("/v1/turns", turn, identity)).status, 403);+ assert.equal((await post("/v1/%74urns", turn, identity)).status, 404);+ assert.equal((await post("/v1/%74urns", turn)).status, 404);+ });+it("a slack turn carries no portal identity and is not gated (different trust authority)", async () => {const r = await post("/v1/turns", {surface: "slack",diff --git a/test/route-table.test.ts b/test/route-table.test.tsindex 6718e19ff..5c9b2ecbb 100644--- a/test/route-table.test.ts+++ b/test/route-table.test.ts@@ -1,6 +1,7 @@import { test } from "node:test";import assert from "node:assert/strict";import { compilePath, findRoute, type Route, type BaseCtx } from "../src/api/routes/route.ts";+import { apiRoutes } from "../src/api/routes/index.ts";function matched(path: string, pathname: string): Record<string, string> | null {const out: Record<string, string> = {};@@ -54,3 +55,14 @@ test("findRoute: a custom match route still works and yields empty params", () =assert.equal(found?.route, route);assert.deepEqual(found?.params, {});});++test("each API route's literals survive URL decoding and its own path resolves to it", () => {+ for (const route of apiRoutes) {+ if (!("path" in route)) continue;+ const segments = route.path.split("/");+ const literals = segments.filter((segment) => !segment.startsWith(":"));+ assert.deepEqual(literals.map(encodeURIComponent), literals, route.path);+ const sample = segments.map((segment) => (segment.startsWith(":") ? "x" : segment)).join("/");+ assert.equal(findRoute(apiRoutes, route.method, sample)?.route, route, `${route.method} ${route.path}`);+ }+});