Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 2 to 318

Compare

+241 −0 in 1 place
new file, 241 lines
1+import assert from "node:assert/strict";
2+import { describe, it } from "node:test";
3+import type { Env } from "../src/env.ts";
4+import { authorize, handle, ISSUES_ROUTE, PROJECT_ROUTE, sameSecret } from "../src/router.ts";
5+
6+const TOKEN = "test-token-for-the-router";
7+
8+/** A project namespace whose objects record every call and answer what each test needs. */
9+function fakeEnv(extra: Partial<Env> = {}) {
10+ const calls: { project: string; method: string; args: unknown[] }[] = [];
11+ const object = (project: string) =>
12+ new Proxy(
13+ {},
14+ {
15+ get:
16+ (_t, method: string) =>
17+ async (...args: unknown[]) => {
18+ calls.push({ project, method, args });
19+ if (method === "call") return { status: 200, body: { op: args[1], agent: args[2], body: args[3] } };
20+ if (method === "putBlob") return { status: 200, body: { stored: true } };
21+ if (method === "head") return { version: 3, at: "now" };
22+ if (method === "checkAgentKey")
23+ return { ok: true, id: "k_1", as: "key k_1", role: "writer", agent: "carol", dirty: false };
24+ if (method === "issueAgentKey") return { id: "k_2", key: "abso_sk_new", agent: args[0] };
25+ return null;
26+ },
27+ },
28+ );
29+ const env = {
30+ PROJECT: { idFromName: (n: string) => n, get: (id: string) => object(id) },
31+ ISSUES_DO: { idFromName: (n: string) => n, get: () => ({}) },
32+ ABSO_TOKEN: TOKEN,
33+ ...extra,
34+ } as unknown as Env;
35+ return { env, calls };
36+}
38+const ctx = {} as ExecutionContext;
39+const noIssues = async () => new Response("issues", { status: 299 });
40+const request = (path: string, init: RequestInit & { token?: string | null } = {}) => {
41+ const { token = TOKEN, ...rest } = init;
42+ const headers = new Headers(rest.headers);
43+ if (token) headers.set("authorization", `Bearer ${token}`);
44+ return new Request(`http://abso.test${path}`, { ...rest, headers });
45+};
47+describe("routes", () => {
48+ it("forwards request identity separately from untrusted JSON body", async () => {
49+ const { env, calls } = fakeEnv();
50+ const id = "00000000-0000-4000-8000-000000000007";
51+ const response = await handle(
52+ request("/p/game/a/alice/record", {
53+ method: "POST",
54+ headers: { "content-type": "application/json", "Idempotency-Key": id },
55+ body: JSON.stringify({ files: {}, principal: "forged" }),
56+ }),
57+ env,
58+ ctx,
59+ noIssues as never,
60+ );
61+ assert.equal(response.status, 200);
62+ assert.equal(calls[0]!.args[7], id);
63+ assert.equal(calls[0]!.args[6], undefined);
64+ });
65+ it("matches the project's top routes and an agent's ops", () => {
66+ assert.deepEqual(PROJECT_ROUTE.exec("/p/game/status")?.slice(1, 3), ["game", "status"]);
67+ assert.deepEqual(PROJECT_ROUTE.exec("/p/game/a/alice/publish")?.slice(1), [
68+ "game",
69+ "a/alice/publish",
70+ "alice",
71+ "publish",
72+ ]);
73+ assert.equal(PROJECT_ROUTE.exec("/p/game/change/12")?.[2], "change/12");
74+ assert.equal(PROJECT_ROUTE.exec("/p/game/a/alice/run"), null, "boxes are not on the open server");
75+ assert.equal(PROJECT_ROUTE.exec("/p/game/ci/log"), null, "nor CI's output");
76+ assert.equal(PROJECT_ROUTE.exec("/p/game/members"), null);
77+ assert.deepEqual(ISSUES_ROUTE.exec("/issues/p/game/issues/4")?.slice(1), ["game", "/issues/4"]);
78+ assert.deepEqual(ISSUES_ROUTE.exec("/issues/p/game")?.slice(1), ["game", undefined]);
79+ });
81+ it("sends an agent's op to the project's object, with the JSON body", async () => {
82+ const { env, calls } = fakeEnv();
83+ const res = await handle(
84+ request("/p/game/a/alice/publish", { method: "POST", body: JSON.stringify({ message: "why" }) }),
85+ env,
86+ ctx,
87+ noIssues,
88+ );
89+ assert.equal(res.status, 200);
90+ assert.deepEqual(await res.json(), { op: "publish", agent: "alice", body: { message: "why" } });
91+ assert.equal(calls[0]?.project, "game");
92+ });
94+ it("reads take their query as the body, and a read refuses a POST-only op as a GET", async () => {
95+ const { env } = fakeEnv();
96+ const log = await handle(request("/p/game/log?limit=5"), env, ctx, noIssues);
97+ assert.deepEqual(await log.json(), { op: "log", body: { limit: "5" } });
98+ const publish = await handle(request("/p/game/a/alice/publish"), env, ctx, noIssues);
99+ assert.equal(publish.status, 405);
100+ });
102+ it("refuses a project name the engine wouldn't take", async () => {
103+ const { env, calls } = fakeEnv();
104+ const res = await handle(request("/p/..bad/status"), env, ctx, noIssues);
105+ assert.equal(res.status, 400);
106+ assert.equal(calls.length, 0);
107+ });
109+ it("answers head from the object, and ci and queue as empty", async () => {
110+ const { env } = fakeEnv();
111+ assert.deepEqual(await (await handle(request("/p/game/head"), env, ctx, noIssues)).json(), {
112+ version: 3,
113+ at: "now",
114+ });
115+ const ci = (await (await handle(request("/p/game/ci"), env, ctx, noIssues)).json()) as Record<string, unknown>;
116+ assert.equal(ci.red, null);
117+ assert.deepEqual(await (await handle(request("/p/game/queue"), env, ctx, noIssues)).json(), {
118+ items: [],
119+ paused: null,
120+ });
121+ });
123+ it("checks a blob's name before its bytes go anywhere", async () => {
124+ const { env, calls } = fakeEnv();
125+ const bad = await handle(request("/p/game/blobs/not-a-hash", { method: "PUT", body: "x" }), env, ctx, noIssues);
126+ assert.equal(bad.status, 400);
127+ const upper = await handle(
128+ request(`/p/game/blobs/${"A".repeat(64)}`, { method: "PUT", body: "x" }),
129+ env,
130+ ctx,
131+ noIssues,
132+ );
133+ assert.equal(upper.status, 400);
134+ assert.equal(calls.length, 0);
135+ const good = await handle(
136+ request(`/p/game/blobs/${"a".repeat(64)}`, { method: "PUT", body: "x" }),
137+ env,
138+ ctx,
139+ noIssues,
140+ );
141+ assert.equal(good.status, 200);
142+ assert.equal(calls[0]?.method, "putBlob");
143+ });
145+ it("hands /issues to the issues code with the rest of the path and who is calling", async () => {
146+ const { env } = fakeEnv();
147+ let seen: unknown;
148+ const res = await handle(request("/issues/p/game/issues?open=1"), env, ctx, async (_r, _e, _c, where) => {
149+ seen = where;
150+ return new Response("ok");
151+ });
152+ assert.equal(await res.text(), "ok");
153+ assert.deepEqual(seen, { project: "game", rest: "/issues", auth: { kind: "token" } });
154+ });
156+ it("gives the issues code a key's agent and role", async () => {
157+ const { env } = fakeEnv({ AGENT_KEYS: "on", KEY_HMAC_SECRET: "s" });
158+ let seen: unknown;
159+ await handle(request("/issues/p/game/issues", { token: "abso_sk_abc" }), env, ctx, async (_r, _e, _c, where) => {
160+ seen = where;
161+ return new Response("ok");
162+ });
163+ assert.deepEqual(seen, { project: "game", rest: "/issues", auth: { kind: "key", agent: "carol", role: "writer" } });
164+ });
166+ it("answers /version without a token", async () => {
167+ const { env } = fakeEnv({ ABSO_TOKEN: undefined });
168+ const res = await handle(request("/version", { token: null }), env, ctx, noIssues);
169+ assert.equal(res.status, 200);
170+ });
171+});
173+describe("auth", () => {
174+ it("refuses everything when no shared token is set", async () => {
175+ const { env, calls } = fakeEnv({ ABSO_TOKEN: undefined });
176+ const res = await handle(request("/p/game/status", { token: "anything" }), env, ctx, noIssues);
177+ assert.equal(res.status, 503);
178+ assert.match(((await res.json()) as { error: string }).error, /no ABSO_TOKEN/);
179+ const empty = await authorize(request("/p/game/status", { token: null }), { ...env, ABSO_TOKEN: "" });
180+ assert.ok(empty instanceof Response && empty.status === 503);
181+ assert.equal(calls.length, 0);
182+ });
184+ it("refuses a missing or wrong token, and takes the right one", async () => {
185+ const { env, calls } = fakeEnv();
186+ assert.equal((await handle(request("/p/game/status", { token: null }), env, ctx, noIssues)).status, 401);
187+ assert.equal((await handle(request("/p/game/status", { token: "wrong" }), env, ctx, noIssues)).status, 401);
188+ assert.equal((await handle(request("/p/game/status", { token: `${TOKEN}x` }), env, ctx, noIssues)).status, 401);
189+ assert.equal((await handle(request("/", { token: "wrong" }), env, ctx, noIssues)).status, 401);
190+ assert.equal(calls.length, 0);
191+ assert.equal((await handle(request("/p/game/status"), env, ctx, noIssues)).status, 200);
192+ });
194+ it("takes the token under its old name, AVCS_TOKEN", async () => {
195+ const { env } = fakeEnv({ ABSO_TOKEN: undefined, AVCS_TOKEN: TOKEN });
196+ assert.deepEqual(await authorize(request("/p/game/status"), env), { kind: "token" });
197+ });
199+ it("treats an agent key as a key only when keys are on", async () => {
200+ const off = fakeEnv();
201+ const refused = await authorize(request("/p/game/status", { token: "abso_sk_abc" }), off.env);
202+ assert.ok(refused instanceof Response && refused.status === 401);
203+ const on = fakeEnv({ AGENT_KEYS: "on", KEY_HMAC_SECRET: "s" });
204+ assert.deepEqual(await authorize(request("/p/game/status", { token: "abso_sk_abc" }), on.env), {
205+ kind: "key",
206+ presented: "abso_sk_abc",
207+ });
208+ });
210+ it("compares secrets whatever their lengths", async () => {
211+ assert.equal(await sameSecret("abc", "abc"), true);
212+ assert.equal(await sameSecret("abc", "abcd"), false);
213+ assert.equal(await sameSecret("", "abc"), false);
214+ });
216+ it("issues agent keys only with the admin token beside the shared one", async () => {
217+ const { env, calls } = fakeEnv({ AGENT_KEYS: "on", KEY_HMAC_SECRET: "s", ABSO_ADMIN_TOKEN: "admin-secret" });
218+ const path = "/admin/agent-keys?project=game&agent=carol";
219+ assert.equal((await handle(request(path, { method: "POST" }), env, ctx, noIssues)).status, 403);
220+ const wrong = request(path, { method: "POST", headers: { "x-abso-admin": "nope" } });
221+ assert.equal((await handle(wrong, env, ctx, noIssues)).status, 403);
222+ const key = request(path, { method: "POST", token: "abso_sk_abc", headers: { "x-abso-admin": "admin-secret" } });
223+ assert.equal((await handle(key, env, ctx, noIssues)).status, 401);
224+ assert.equal(calls.length, 0);
225+ const ok = request(path, { method: "POST", headers: { "x-abso-admin": "admin-secret" } });
226+ const res = await handle(ok, env, ctx, noIssues);
227+ assert.equal(res.status, 200);
228+ assert.deepEqual(await res.json(), { id: "k_2", key: "abso_sk_new", agent: "carol" });
229+ });
231+ it("has no agent keys routes while keys are off", async () => {
232+ const { env } = fakeEnv({ ABSO_ADMIN_TOKEN: "admin-secret" });
233+ const res = await handle(
234+ request("/admin/agent-keys?project=game", { headers: { "x-abso-admin": "admin-secret" } }),
235+ env,
236+ ctx,
237+ noIssues,
238+ );
239+ assert.equal(res.status, 404);
240+ });
241+});
MCP
compare on this site's /mcp
CLI
abso diff 2 318 server/test/router.test.ts --json
JSON
/christopher/abso-core/compare/2..318?file=server%2Ftest%2Frouter.test.ts&format=json