{"draft":null,"from":45,"to":318,"files":[{"path":"server/test/router.test.ts","added":241,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/router.test.ts\n@@ -0,0 +1,241 @@\n+import assert from \"node:assert/strict\";\n+import { describe, it } from \"node:test\";\n+import type { Env } from \"../src/env.ts\";\n+import { authorize, handle, ISSUES_ROUTE, PROJECT_ROUTE, sameSecret } from \"../src/router.ts\";\n+\n+const TOKEN = \"test-token-for-the-router\";\n+\n+/** A project namespace whose objects record every call and answer what each test needs. */\n+function fakeEnv(extra: Partial<Env> = {}) {\n+\tconst calls: { project: string; method: string; args: unknown[] }[] = [];\n+\tconst object = (project: string) =>\n+\t\tnew Proxy(\n+\t\t\t{},\n+\t\t\t{\n+\t\t\t\tget:\n+\t\t\t\t\t(_t, method: string) =>\n+\t\t\t\t\tasync (...args: unknown[]) => {\n+\t\t\t\t\t\tcalls.push({ project, method, args });\n+\t\t\t\t\t\tif (method === \"call\") return { status: 200, body: { op: args[1], agent: args[2], body: args[3] } };\n+\t\t\t\t\t\tif (method === \"putBlob\") return { status: 200, body: { stored: true } };\n+\t\t\t\t\t\tif (method === \"head\") return { version: 3, at: \"now\" };\n+\t\t\t\t\t\tif (method === \"checkAgentKey\")\n+\t\t\t\t\t\t\treturn { ok: true, id: \"k_1\", as: \"key k_1\", role: \"writer\", agent: \"carol\", dirty: false };\n+\t\t\t\t\t\tif (method === \"issueAgentKey\") return { id: \"k_2\", key: \"abso_sk_new\", agent: args[0] };\n+\t\t\t\t\t\treturn null;\n+\t\t\t\t\t},\n+\t\t\t},\n+\t\t);\n+\tconst env = {\n+\t\tPROJECT: { idFromName: (n: string) => n, get: (id: string) => object(id) },\n+\t\tISSUES_DO: { idFromName: (n: string) => n, get: () => ({}) },\n+\t\tABSO_TOKEN: TOKEN,\n+\t\t...extra,\n+\t} as unknown as Env;\n+\treturn { env, calls };\n+}\n+\n+const ctx = {} as ExecutionContext;\n+const noIssues = async () => new Response(\"issues\", { status: 299 });\n+const request = (path: string, init: RequestInit & { token?: string | null } = {}) => {\n+\tconst { token = TOKEN, ...rest } = init;\n+\tconst headers = new Headers(rest.headers);\n+\tif (token) headers.set(\"authorization\", `Bearer ${token}`);\n+\treturn new Request(`http://abso.test${path}`, { ...rest, headers });\n+};\n+\n+describe(\"routes\", () => {\n+\tit(\"forwards request identity separately from untrusted JSON body\", async () => {\n+\t\tconst { env, calls } = fakeEnv();\n+\t\tconst id = \"00000000-0000-4000-8000-000000000007\";\n+\t\tconst response = await handle(\n+\t\t\trequest(\"/p/game/a/alice/record\", {\n+\t\t\t\tmethod: \"POST\",\n+\t\t\t\theaders: { \"content-type\": \"application/json\", \"Idempotency-Key\": id },\n+\t\t\t\tbody: JSON.stringify({ files: {}, principal: \"forged\" }),\n+\t\t\t}),\n+\t\t\tenv,\n+\t\t\tctx,\n+\t\t\tnoIssues as never,\n+\t\t);\n+\t\tassert.equal(response.status, 200);\n+\t\tassert.equal(calls[0]!.args[7], id);\n+\t\tassert.equal(calls[0]!.args[6], undefined);\n+\t});\n+\tit(\"matches the project's top routes and an agent's ops\", () => {\n+\t\tassert.deepEqual(PROJECT_ROUTE.exec(\"/p/game/status\")?.slice(1, 3), [\"game\", \"status\"]);\n+\t\tassert.deepEqual(PROJECT_ROUTE.exec(\"/p/game/a/alice/publish\")?.slice(1), [\n+\t\t\t\"game\",\n+\t\t\t\"a/alice/publish\",\n+\t\t\t\"alice\",\n+\t\t\t\"publish\",\n+\t\t]);\n+\t\tassert.equal(PROJECT_ROUTE.exec(\"/p/game/change/12\")?.[2], \"change/12\");\n+\t\tassert.equal(PROJECT_ROUTE.exec(\"/p/game/a/alice/run\"), null, \"boxes are not on the open server\");\n+\t\tassert.equal(PROJECT_ROUTE.exec(\"/p/game/ci/log\"), null, \"nor CI's output\");\n+\t\tassert.equal(PROJECT_ROUTE.exec(\"/p/game/members\"), null);\n+\t\tassert.deepEqual(ISSUES_ROUTE.exec(\"/issues/p/game/issues/4\")?.slice(1), [\"game\", \"/issues/4\"]);\n+\t\tassert.deepEqual(ISSUES_ROUTE.exec(\"/issues/p/game\")?.slice(1), [\"game\", undefined]);\n+\t});\n+\n+\tit(\"sends an agent's op to the project's object, with the JSON body\", async () => {\n+\t\tconst { env, calls } = fakeEnv();\n+\t\tconst res = await handle(\n+\t\t\trequest(\"/p/game/a/alice/publish\", { method: \"POST\", body: JSON.stringify({ message: \"why\" }) }),\n+\t\t\tenv,\n+\t\t\tctx,\n+\t\t\tnoIssues,\n+\t\t);\n+\t\tassert.equal(res.status, 200);\n+\t\tassert.deepEqual(await res.json(), { op: \"publish\", agent: \"alice\", body: { message: \"why\" } });\n+\t\tassert.equal(calls[0]?.project, \"game\");\n+\t});\n+\n+\tit(\"reads take their query as the body, and a read refuses a POST-only op as a GET\", async () => {\n+\t\tconst { env } = fakeEnv();\n+\t\tconst log = await handle(request(\"/p/game/log?limit=5\"), env, ctx, noIssues);\n+\t\tassert.deepEqual(await log.json(), { op: \"log\", body: { limit: \"5\" } });\n+\t\tconst publish = await handle(request(\"/p/game/a/alice/publish\"), env, ctx, noIssues);\n+\t\tassert.equal(publish.status, 405);\n+\t});\n+\n+\tit(\"refuses a project name the engine wouldn't take\", async () => {\n+\t\tconst { env, calls } = fakeEnv();\n+\t\tconst res = await handle(request(\"/p/..bad/status\"), env, ctx, noIssues);\n+\t\tassert.equal(res.status, 400);\n+\t\tassert.equal(calls.length, 0);\n+\t});\n+\n+\tit(\"answers head from the object, and ci and queue as empty\", async () => {\n+\t\tconst { env } = fakeEnv();\n+\t\tassert.deepEqual(await (await handle(request(\"/p/game/head\"), env, ctx, noIssues)).json(), {\n+\t\t\tversion: 3,\n+\t\t\tat: \"now\",\n+\t\t});\n+\t\tconst ci = (await (await handle(request(\"/p/game/ci\"), env, ctx, noIssues)).json()) as Record<string, unknown>;\n+\t\tassert.equal(ci.red, null);\n+\t\tassert.deepEqual(await (await handle(request(\"/p/game/queue\"), env, ctx, noIssues)).json(), {\n+\t\t\titems: [],\n+\t\t\tpaused: null,\n+\t\t});\n+\t});\n+\n+\tit(\"checks a blob's name before its bytes go anywhere\", async () => {\n+\t\tconst { env, calls } = fakeEnv();\n+\t\tconst bad = await handle(request(\"/p/game/blobs/not-a-hash\", { method: \"PUT\", body: \"x\" }), env, ctx, noIssues);\n+\t\tassert.equal(bad.status, 400);\n+\t\tconst upper = await handle(\n+\t\t\trequest(`/p/game/blobs/${\"A\".repeat(64)}`, { method: \"PUT\", body: \"x\" }),\n+\t\t\tenv,\n+\t\t\tctx,\n+\t\t\tnoIssues,\n+\t\t);\n+\t\tassert.equal(upper.status, 400);\n+\t\tassert.equal(calls.length, 0);\n+\t\tconst good = await handle(\n+\t\t\trequest(`/p/game/blobs/${\"a\".repeat(64)}`, { method: \"PUT\", body: \"x\" }),\n+\t\t\tenv,\n+\t\t\tctx,\n+\t\t\tnoIssues,\n+\t\t);\n+\t\tassert.equal(good.status, 200);\n+\t\tassert.equal(calls[0]?.method, \"putBlob\");\n+\t});\n+\n+\tit(\"hands /issues to the issues code with the rest of the path and who is calling\", async () => {\n+\t\tconst { env } = fakeEnv();\n+\t\tlet seen: unknown;\n+\t\tconst res = await handle(request(\"/issues/p/game/issues?open=1\"), env, ctx, async (_r, _e, _c, where) => {\n+\t\t\tseen = where;\n+\t\t\treturn new Response(\"ok\");\n+\t\t});\n+\t\tassert.equal(await res.text(), \"ok\");\n+\t\tassert.deepEqual(seen, { project: \"game\", rest: \"/issues\", auth: { kind: \"token\" } });\n+\t});\n+\n+\tit(\"gives the issues code a key's agent and role\", async () => {\n+\t\tconst { env } = fakeEnv({ AGENT_KEYS: \"on\", KEY_HMAC_SECRET: \"s\" });\n+\t\tlet seen: unknown;\n+\t\tawait handle(request(\"/issues/p/game/issues\", { token: \"abso_sk_abc\" }), env, ctx, async (_r, _e, _c, where) => {\n+\t\t\tseen = where;\n+\t\t\treturn new Response(\"ok\");\n+\t\t});\n+\t\tassert.deepEqual(seen, { project: \"game\", rest: \"/issues\", auth: { kind: \"key\", agent: \"carol\", role: \"writer\" } });\n+\t});\n+\n+\tit(\"answers /version without a token\", async () => {\n+\t\tconst { env } = fakeEnv({ ABSO_TOKEN: undefined });\n+\t\tconst res = await handle(request(\"/version\", { token: null }), env, ctx, noIssues);\n+\t\tassert.equal(res.status, 200);\n+\t});\n+});\n+\n+describe(\"auth\", () => {\n+\tit(\"refuses everything when no shared token is set\", async () => {\n+\t\tconst { env, calls } = fakeEnv({ ABSO_TOKEN: undefined });\n+\t\tconst res = await handle(request(\"/p/game/status\", { token: \"anything\" }), env, ctx, noIssues);\n+\t\tassert.equal(res.status, 503);\n+\t\tassert.match(((await res.json()) as { error: string }).error, /no ABSO_TOKEN/);\n+\t\tconst empty = await authorize(request(\"/p/game/status\", { token: null }), { ...env, ABSO_TOKEN: \"\" });\n+\t\tassert.ok(empty instanceof Response && empty.status === 503);\n+\t\tassert.equal(calls.length, 0);\n+\t});\n+\n+\tit(\"refuses a missing or wrong token, and takes the right one\", async () => {\n+\t\tconst { env, calls } = fakeEnv();\n+\t\tassert.equal((await handle(request(\"/p/game/status\", { token: null }), env, ctx, noIssues)).status, 401);\n+\t\tassert.equal((await handle(request(\"/p/game/status\", { token: \"wrong\" }), env, ctx, noIssues)).status, 401);\n+\t\tassert.equal((await handle(request(\"/p/game/status\", { token: `${TOKEN}x` }), env, ctx, noIssues)).status, 401);\n+\t\tassert.equal((await handle(request(\"/\", { token: \"wrong\" }), env, ctx, noIssues)).status, 401);\n+\t\tassert.equal(calls.length, 0);\n+\t\tassert.equal((await handle(request(\"/p/game/status\"), env, ctx, noIssues)).status, 200);\n+\t});\n+\n+\tit(\"takes the token under its old name, AVCS_TOKEN\", async () => {\n+\t\tconst { env } = fakeEnv({ ABSO_TOKEN: undefined, AVCS_TOKEN: TOKEN });\n+\t\tassert.deepEqual(await authorize(request(\"/p/game/status\"), env), { kind: \"token\" });\n+\t});\n+\n+\tit(\"treats an agent key as a key only when keys are on\", async () => {\n+\t\tconst off = fakeEnv();\n+\t\tconst refused = await authorize(request(\"/p/game/status\", { token: \"abso_sk_abc\" }), off.env);\n+\t\tassert.ok(refused instanceof Response && refused.status === 401);\n+\t\tconst on = fakeEnv({ AGENT_KEYS: \"on\", KEY_HMAC_SECRET: \"s\" });\n+\t\tassert.deepEqual(await authorize(request(\"/p/game/status\", { token: \"abso_sk_abc\" }), on.env), {\n+\t\t\tkind: \"key\",\n+\t\t\tpresented: \"abso_sk_abc\",\n+\t\t});\n+\t});\n+\n+\tit(\"compares secrets whatever their lengths\", async () => {\n+\t\tassert.equal(await sameSecret(\"abc\", \"abc\"), true);\n+\t\tassert.equal(await sameSecret(\"abc\", \"abcd\"), false);\n+\t\tassert.equal(await sameSecret(\"\", \"abc\"), false);\n+\t});\n+\n+\tit(\"issues agent keys only with the admin token beside the shared one\", async () => {\n+\t\tconst { env, calls } = fakeEnv({ AGENT_KEYS: \"on\", KEY_HMAC_SECRET: \"s\", ABSO_ADMIN_TOKEN: \"admin-secret\" });\n+\t\tconst path = \"/admin/agent-keys?project=game&agent=carol\";\n+\t\tassert.equal((await handle(request(path, { method: \"POST\" }), env, ctx, noIssues)).status, 403);\n+\t\tconst wrong = request(path, { method: \"POST\", headers: { \"x-abso-admin\": \"nope\" } });\n+\t\tassert.equal((await handle(wrong, env, ctx, noIssues)).status, 403);\n+\t\tconst key = request(path, { method: \"POST\", token: \"abso_sk_abc\", headers: { \"x-abso-admin\": \"admin-secret\" } });\n+\t\tassert.equal((await handle(key, env, ctx, noIssues)).status, 401);\n+\t\tassert.equal(calls.length, 0);\n+\t\tconst ok = request(path, { method: \"POST\", headers: { \"x-abso-admin\": \"admin-secret\" } });\n+\t\tconst res = await handle(ok, env, ctx, noIssues);\n+\t\tassert.equal(res.status, 200);\n+\t\tassert.deepEqual(await res.json(), { id: \"k_2\", key: \"abso_sk_new\", agent: \"carol\" });\n+\t});\n+\n+\tit(\"has no agent keys routes while keys are off\", async () => {\n+\t\tconst { env } = fakeEnv({ ABSO_ADMIN_TOKEN: \"admin-secret\" });\n+\t\tconst res = await handle(\n+\t\t\trequest(\"/admin/agent-keys?project=game\", { headers: { \"x-abso-admin\": \"admin-secret\" } }),\n+\t\t\tenv,\n+\t\t\tctx,\n+\t\t\tnoIssues,\n+\t\t);\n+\t\tassert.equal(res.status, 404);\n+\t});\n+});"}],"unmatched":[]}