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
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/test/run-key.test.ts?format=json
96 linespinch 100%Last change pinch in version 95, 1 version changed it
1// A pipeline run's key (ABSO_KEY_FILE): read from the file the runner wrote, sent only where a token may go,
2// held in memory only, and a run key's refusals reach the job as the server wrote them.
3
4import assert from "node:assert/strict";
5import { mkdtempSync, writeFileSync } from "node:fs";
6import { createServer } from "node:http";
7import type { AddressInfo } from "node:net";
8import { tmpdir } from "node:os";
9import { join } from "node:path";
10import { test } from "node:test";
11import { Api, ApiError } from "../src/api.ts";
12import { credentialLabel, installKey, readConfig, runKey, tokenFor, writeConfig } from "../src/config.ts";
14const KEY = "abso_sk_runkeyabcdefghijklmnopqrstuvwx";
15const LOCAL = "http://127.0.0.1:8787";
17function keyFile(text: string): string {
18 const path = join(mkdtempSync(join(tmpdir(), "abso-run-key-")), "key");
19 writeFileSync(path, text);
20 return path;
23function folder(server = LOCAL) {
24 const root = mkdtempSync(join(tmpdir(), "abso-run-key-"));
25 writeConfig(root, { server, project: "p", agent: "run-7-check-test" });
26 return root;
29const NO_SERVERS = { ABSO_SERVERS_FILE: "no-servers.json" };
31test("ABSO_KEY_FILE's key is the credential when no token or folder key is set", () => {
32 const env = { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY) };
33 assert.equal(runKey(env), KEY);
34 assert.equal(tokenFor(LOCAL, env, folder()), KEY);
35 assert.deepEqual(credentialLabel(folder(), LOCAL, env), { kind: "pipeline run key", id: null });
36 assert.equal(readConfig(folder(), env).token, KEY);
37});
39test("a trailing newline in the key file is not part of the key", () => {
40 assert.equal(runKey({ ABSO_KEY_FILE: keyFile(`${KEY}\n`) }), KEY);
41});
43test("ABSO_TOKEN and a folder's own key both come before the run key", () => {
44 const env = { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY), ABSO_TOKEN: "shared-token-value" };
45 assert.equal(tokenFor(LOCAL, env, folder()), "shared-token-value");
46 assert.deepEqual(credentialLabel(folder(), LOCAL, env), { kind: "shared token", id: null });
47 const root = folder();
48 installKey(root, JSON.stringify({ key: "abso_sk_folderkeyabcdefghijklmnopqrstu", id: "k_ab12cd34" }));
49 assert.equal(
50 tokenFor(LOCAL, { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY) }, root),
51 "abso_sk_folderkeyabcdefghijklmnopqrstu",
52 );
53});
55test("the run key goes only where a token may, and the refusal doesn't hold it", () => {
56 const env = { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY) };
57 assert.throws(
58 () => tokenFor("https://someone-else.example", env, folder("https://someone-else.example")),
59 (err: Error) => /sends your token only to/.test(err.message) && !err.message.includes(KEY),
60 );
61});
63test("a key file abso can't read, or that holds no key, is named without its contents", () => {
64 const missing = join(tmpdir(), "abso-no-such-key-file");
65 assert.throws(() => runKey({ ABSO_KEY_FILE: missing }), /can't read/);
66 const wrong = keyFile("not-a-key secret-looking-text");
67 assert.throws(
68 () => runKey({ ABSO_KEY_FILE: wrong }),
69 (err: Error) => /doesn't hold an abso key/.test(err.message) && !err.message.includes("secret-looking-text"),
70 );
71});
73test("a run key's refusal reaches the job as the server wrote it, and without the key", async () => {
74 let sent = "";
75 const server = createServer((req, res) => {
76 sent = req.headers.authorization ?? "";
77 res.writeHead(403, { "content-type": "application/json" });
78 res.end(JSON.stringify({ error: "a pipeline run's key can't land" }));
79 });
80 await new Promise<void>((ok) => server.listen(0, "127.0.0.1", ok));
81 try {
82 const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
83 const config = readConfig(folder(url), { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY) });
84 await assert.rejects(
85 new Api(config).land("try"),
86 (err: Error) =>
87 err instanceof ApiError &&
88 err.status === 403 &&
89 /a pipeline run's key can't land/.test(err.message) &&
90 !err.message.includes(KEY),
91 );
92 assert.equal(sent, `Bearer ${KEY}`);
93 } finally {
94 server.close();
95 }
96});