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
MCP
blame on this site's /mcp
CLI
abso blame client/test/key-admin.test.ts --json
JSON
/christopher/abso-core/blame/client/test/key-admin.test.ts?format=json
117 linesFirst import 97% · review 2% · pinch 1%Last change pinch in version 54,

117 lines from 3 changes in 3 versions.

init0Start the project1import assert from "node:assert/strict";
2import { test } from "node:test";
3import type { Config } from "../src/config.ts";
4import {
5 adminGoesTo,
6 adminToken,
7 issuedPublic,
8 keyList,
9 keyListText,
10 keyNew,
11 keyRevoke,
12 type ListedKey,
13} from "../src/key-admin.ts";
15const config: Config = {
16 server: "https://staging.abso.example",
17 project: "demo",
18 agent: "me",
19 token: "shared-token",
20};
21const ADMIN = "admin-token-value";
22// No server takes the admin token unless AVCS_ADMIN_SERVERS names it; these tests name staging's stand-in.
23process.env.AVCS_ADMIN_SERVERS = "https://staging.abso.example";
24const KEY = "abso_sk_this-is-the-secret-value";
26function fetchOf(status: number, body: unknown, seen: { url?: string; headers?: Headers }[]): typeof fetch {
27 return (async (url, init) => {
28 seen.push({ url: String(url), headers: new Headers(init?.headers) });
29 return new Response(JSON.stringify(body), { status });
30 }) as typeof fetch;
33test("the admin token is sent only to a server AVCS_ADMIN_SERVERS names", async () => {
34 assert.equal(adminGoesTo("https://staging.abso.example"), false);
35 assert.equal(adminGoesTo("https://staging.abso.example", "https://staging.abso.example"), true);
36 assert.equal(adminGoesTo("https://abso.example", "https://staging.abso.example"), false);
37 assert.equal(adminGoesTo("https://example.com", "https://example.com"), true);
38 const seen: unknown[] = [];
39 const previous = globalThis.fetch;
40 globalThis.fetch = (async () => {
41 seen.push(true);
42 return new Response("{}", { status: 200 });
43 }) as typeof fetch;
44 try {
45 await assert.rejects(
46 keyNew({ ...config, server: "https://example.com" }, ADMIN, "tester-1"),
47 /will not send the admin token/,
48 );
49 assert.equal(seen.length, 0);
50 } finally {
51 globalThis.fetch = previous;
52 }
53});
55test("the key is left out unless printing was asked for", () => {
56 const issued = { id: "k_ab12cd34", key: KEY, agent: "tester-1" };
57 assert.equal(JSON.stringify(issuedPublic(issued, false)).includes(KEY), false);
58 assert.equal(issuedPublic(issued, true).key, KEY);
59});
61test("the admin token comes from the environment, not from a file argument", () => {
62 assert.equal(adminToken({ AVCS_ADMIN_TOKEN: ADMIN }), ADMIN);
review27Client: ABSO_ environment names (AVCS_ still read), .absoignore (.avcsignore still read), .abso-export.json, shot files; READMEs say abso (abso 224)Compare63 assert.equal(adminToken({ ABSO_ADMIN_TOKEN: ADMIN }), ADMIN);
64 assert.throws(() => adminToken({}), /ABSO_ADMIN_TOKEN/);
init0Start the project65});
67test("key new sends the admin token as a header and returns the key once", async () => {
68 const seen: { url?: string; headers?: Headers }[] = [];
69 const previous = globalThis.fetch;
70 globalThis.fetch = fetchOf(200, { id: "k_ab12cd34", key: KEY, agent: "tester-1" }, seen);
71 try {
72 const issued = await keyNew(config, ADMIN, "tester-1");
73 assert.deepEqual(issued, { id: "k_ab12cd34", key: KEY, agent: "tester-1" });
pinch54The client and the edit mods send and read X-Abso-* headers only (Client, Name, Run-Key, Kind, Admin; Run, Box, Prep from answers). Servers have read X-Abso-* beside the old names for a while and answer under both, so this works with any current server.Compare74 assert.equal(seen[0]!.headers!.get("x-abso-admin"), ADMIN);
init0Start the project75 assert.equal(seen[0]!.url!.includes(ADMIN), false);
76 assert.equal(seen[0]!.url!.includes(KEY), false);
77 } finally {
78 globalThis.fetch = previous;
79 }
80});
82test("key list never includes a key value", async () => {
83 const row: ListedKey = {
84 id: "k_ab12cd34",
85 agent: "tester-1",
86 issuedBy: "admin",
87 at: 1_700_000_000_000,
88 lastUsed: null,
89 revokedAt: null,
90 };
91 const previous = globalThis.fetch;
92 globalThis.fetch = fetchOf(200, { keys: [row], leaked: KEY }, []);
93 try {
94 const keys = await keyList(config, ADMIN);
95 const printed = keyListText(keys);
96 const dumped = JSON.stringify(keys);
97 assert.equal(printed.includes(KEY), false);
98 assert.equal(dumped.includes(KEY), false);
99 assert.match(printed, /k_ab12cd34/);
100 } finally {
101 globalThis.fetch = previous;
102 }
103});
105test("an error does not repeat the admin token or the key", async () => {
106 const previous = globalThis.fetch;
107 globalThis.fetch = fetchOf(403, { error: `refused ${ADMIN} ${KEY}` }, []);
108 try {
109 await assert.rejects(keyRevoke(config, ADMIN, "k_ab12cd34"), (err: Error) => {
110 assert.equal(err.message.includes(ADMIN), false);
111 assert.equal(err.message.includes(KEY), false);
112 return true;
113 });
114 } finally {
115 globalThis.fetch = previous;
116 }
117});