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 85 to 86

Compare

+318 −0 in 1 place
new file, 318 lines
1+// A visitor folder: abso join <project> --reader with no sign-in, for a public project. It reads
2+// through the routes anyone can read and sends no credential. The rules, each a test:
3+// - no Authorization header and no cookie go out, with a token in the environment and a box sign-in
4+// present, and no redirect is followed;
5+// - join answers a private project and a missing one alike, by the status answer's code alone;
6+// - the commands it runs are an allow-list, and a new command fails this test until it's sorted;
7+// - its config comes from the command line only, never from an answer;
8+// - what comes back goes through the terminal filter, a 429's text too;
9+// - show prints a file and writes nothing, even for a path outside the folder.
11+import assert from "node:assert/strict";
12+import { execFile } from "node:child_process";
13+import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
14+import { createServer, type IncomingHttpHeaders, type Server } from "node:http";
15+import type { AddressInfo } from "node:net";
16+import { tmpdir } from "node:os";
17+import { join, resolve } from "node:path";
18+import { after, before, describe, it } from "node:test";
19+import { writeBox } from "../src/box.ts";
20+import { writeConfig } from "../src/config.ts";
21+import { isPublic } from "../src/join-commands.ts";
22+import { commandTable, main } from "../src/main.ts";
23+import { VISITOR_ELSEWHERE, VISITOR_READS } from "../src/visitor.ts";
25+const CLIENT = resolve(import.meta.dirname, "..", "abso.ts");
26+const ESC = "\x1b";
27+const TOKEN = "abso_test-token-never-sent";
28+const CREDENTIAL = "abso_bx_test-credential-never-sent";
30+/** Every command a visitor folder refuses. A new command fails the allow-list test until it's here or a read. */
31+const REFUSED = [
32+ "init",
33+ "key",
34+ "box",
35+ "whoami",
36+ "doctor",
37+ "orgs",
38+ "org",
39+ "skill",
40+ "join",
41+ "record",
42+ "publish",
43+ "bisect",
44+ "reader",
45+ "settle",
46+ "sync",
47+ "run",
48+ "serve",
49+ "mv",
50+ "review",
51+ "note",
52+ "notes",
53+ "ask",
54+ "waiting",
55+ "answer",
56+ "ack",
57+ "reply",
58+ "unqueue",
59+ "visibility",
60+ "ci",
61+ "shot",
62+ "prep",
63+ "fetch",
64+ "pause",
65+ "resume",
66+ "logs",
67+ "switch",
68+ "land",
69+ "draft-sync",
70+ "diff",
71+ "restore",
72+ "export",
73+ "announce",
74+ "start",
75+ "news",
76+ "fix-eol",
77+ "who",
78+ "drop",
79+ "pluck",
80+ "amend",
81+ "label",
82+ "labels",
83+ "follow",
84+ "check-ignore",
85+ "pipeline",
86+];
88+let site: Server;
89+let url: string;
90+// Where the site's redirect points: a visitor never gets here.
91+let elsewhere: Server;
92+let elsewhereUrl: string;
93+const heard: { url: string; headers: IncomingHttpHeaders }[] = [];
94+let elsewhereHeard = 0;
95+const dirs: string[] = [];
96+const temp = (name: string) => {
97+ const dir = mkdtempSync(join(tmpdir(), `abso-test-visitor-${name}-`));
98+ dirs.push(dir);
99+ return dir;
100+};
102+const json = (res: import("node:http").ServerResponse, status: number, body: unknown, headers = {}) =>
103+ res.writeHead(status, { "content-type": "application/json", ...headers }).end(JSON.stringify(body));
105+before(async () => {
106+ elsewhere = createServer((_req, res) => {
107+ elsewhereHeard++;
108+ json(res, 200, { version: 9, agents: [] });
109+ });
110+ await new Promise<void>((done) => elsewhere.listen(0, "127.0.0.1", done));
111+ elsewhereUrl = `http://127.0.0.1:${(elsewhere.address() as AddressInfo).port}`;
112+ site = createServer((req, res) => {
113+ heard.push({ url: req.url ?? "", headers: req.headers });
114+ const path = new URL(req.url ?? "/", "http://x").pathname;
115+ if (path === "/p/abso-core/status")
116+ // An answer naming another server: the folder's config never takes it.
117+ return json(res, 200, { version: 3, agents: [], server: "http://answer.example" });
118+ if (path === "/p/abso-core/file")
119+ return json(res, 200, { path: "a.txt", version: 1, text: `hi ${ESC}]52;c;aGk=\x07 there\n` });
120+ if (path === "/p/abso-core/log")
121+ return json(
122+ res,
123+ 429,
124+ { error: `too many signed-out reads from this address: 120 a minute; sign in, or try again ${ESC}[2Jlater` },
125+ { "Retry-After": "60" },
126+ );
127+ if (path === "/p/abso-core/where")
128+ return res.writeHead(302, { location: `${elsewhereUrl}/p/abso-core/where` }).end();
129+ // A private project answers 401 with a body that looks like a status: only the code counts.
130+ if (path === "/p/private/status") return json(res, 401, { version: 1, agents: [] });
131+ if (path === "/p/forbidden/status") return json(res, 403, { error: "members only" });
132+ if (path === "/p/missing/status") return json(res, 404, { error: "no such project" });
133+ json(res, 401, { error: "sign in" });
134+ });
135+ await new Promise<void>((done) => site.listen(0, "127.0.0.1", done));
136+ url = `http://127.0.0.1:${(site.address() as AddressInfo).port}`;
137+});
138+after(() => {
139+ site.close();
140+ elsewhere.close();
141+ for (const dir of dirs) rmSync(dir, { recursive: true, force: true });
142+});
144+/** Runs the client as its own process, in `cwd`, with `home` as its home and only the environment given. */
145+function client(
146+ cwd: string,
147+ home: string,
148+ args: string[],
149+ extra: Record<string, string> = {},
150+): Promise<{ code: number; stdout: string; stderr: string }> {
151+ const env: Record<string, string> = {
152+ PATH: process.env.PATH ?? "",
153+ HOME: home,
154+ USERPROFILE: home,
155+ AVCS_SERVERS_FILE: "no-servers.json",
156+ ABSO_ISSUES_SERVER: url,
157+ ...extra,
158+ };
159+ if (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot;
160+ return new Promise((done) => {
161+ execFile(process.execPath, [CLIENT, ...args], { cwd, env }, (error, stdout, stderr) =>
162+ done({ code: typeof error?.code === "number" ? error.code : 0, stdout, stderr }),
163+ );
164+ });
165+}
167+/** A visitor folder made by abso join, signed out, and the home it was made in. */
168+async function joined(): Promise<{ root: string; home: string; out: string }> {
169+ const home = temp("home");
170+ // In a parent of its own, so a test can tell nothing was written beside it.
171+ const root = join(temp("parent"), "folder");
172+ mkdirSync(root);
173+ const run = await client(root, home, ["join", "abso-core", "--reader", "--server", url]);
174+ assert.equal(run.code, 0, run.stderr);
175+ return { root, home, out: run.stdout };
176+}
178+describe("a visitor folder", () => {
179+ it("join --reader, signed out, to a public project writes only its config, from the command line", async () => {
180+ const { root, out } = await joined();
181+ assert.match(out, /Joined abso-core to read it signed out, at version 3/);
182+ assert.deepEqual(readdirSync(root), [".abso"]);
183+ assert.deepEqual(readdirSync(join(root, ".abso")), ["config.json"]);
184+ // The status answer named another server; the config keeps the one typed.
185+ assert.deepEqual(JSON.parse(readFileSync(join(root, ".abso", "config.json"), "utf8")), {
186+ server: url,
187+ project: "abso-core",
188+ visitor: true,
189+ });
190+ });
192+ it("sends no Authorization and no cookie, with a token set and a box sign-in present, and follows no redirect", async () => {
193+ const { root, home } = await joined();
194+ writeBox(
195+ {
196+ server: url,
197+ id: "b_0123abcd",
198+ credential: CREDENTIAL,
199+ grant: { projects: [{ project: "abso-core", role: "writer" }], max_agents: 3 },
200+ expiresAt: Date.now() + 86_400_000,
201+ },
202+ home,
203+ );
204+ const tokens = { ABSO_TOKEN: TOKEN, AVCS_TOKEN: TOKEN };
205+ heard.length = 0;
206+ elsewhereHeard = 0;
207+ assert.equal((await client(root, home, ["status"], tokens)).code, 0);
208+ assert.equal((await client(root, home, ["show", "1:a.txt"], tokens)).code, 0);
209+ const where = await client(root, home, ["where"], tokens);
210+ assert.notEqual(where.code, 0);
211+ assert.ok(heard.length >= 3);
212+ for (const { headers } of heard) {
213+ assert.equal(headers.authorization, undefined);
214+ assert.equal(headers.cookie, undefined);
215+ assert.doesNotMatch(JSON.stringify(headers), /never-sent/);
216+ }
217+ assert.equal(elsewhereHeard, 0);
218+ });
220+ it("asks fetch for no redirect and no credentials, join's public check too (a fetch spy)", async () => {
221+ const root = temp("spy");
222+ writeConfig(root, { server: url, project: "abso-core", agent: "", visitor: true });
223+ const real = globalThis.fetch;
224+ const calls: RequestInit[] = [];
225+ globalThis.fetch = (async (_input: unknown, init?: RequestInit) => {
226+ calls.push(init ?? {});
227+ return new Response(JSON.stringify({ version: 3, agents: [] }), { status: 200 });
228+ }) as typeof fetch;
229+ try {
230+ const out = await main(["status"], { cwd: root, env: { ABSO_TOKEN: TOKEN }, write: () => {} });
231+ assert.equal(out.exit, 0, out.stderr);
232+ assert.equal(await isPublic(url, "abso-core"), true);
233+ } finally {
234+ globalThis.fetch = real;
235+ }
236+ assert.equal(calls.length, 2);
237+ for (const init of calls) {
238+ assert.equal(init.redirect, "error");
239+ assert.equal(init.credentials, "omit");
240+ const headers = new Headers(init.headers);
241+ assert.equal(headers.get("authorization"), null);
242+ assert.equal(headers.get("cookie"), null);
243+ }
244+ });
246+ it("join answers a private project, a members-only one and a missing one alike, by the code alone", async () => {
247+ const runs = [];
248+ for (const project of ["private", "forbidden", "missing"]) {
249+ const root = temp("closed");
250+ runs.push({ root, ...(await client(root, temp("home"), ["join", project, "--reader", "--server", url])) });
251+ }
252+ for (const run of runs) {
253+ assert.equal(run.code, runs[0]!.code);
254+ assert.notEqual(run.code, 0);
255+ assert.equal(run.stderr, runs[0]!.stderr);
256+ assert.equal(existsSync(join(run.root, ".abso")), false);
257+ }
258+ assert.match(runs[0]!.stderr, /isn't signed in to .*abso login signs it in/);
259+ });
261+ it("runs only its reads: every other command is refused before any call, and a new command fails here", async () => {
262+ const sorted = new Set([...Object.keys(VISITOR_READS), ...VISITOR_ELSEWHERE, ...REFUSED]);
263+ for (const name of Object.keys(commandTable()))
264+ assert.ok(
265+ sorted.has(name),
266+ `abso ${name} is new: add it to VISITOR_READS if it only reads, else to REFUSED here`,
267+ );
268+ const root = temp("refuse");
269+ writeConfig(root, { server: url, project: "abso-core", agent: "", visitor: true });
270+ const real = globalThis.fetch;
271+ let calls = 0;
272+ globalThis.fetch = (async () => {
273+ calls++;
274+ return new Response("{}", { status: 200 });
275+ }) as typeof fetch;
276+ try {
277+ const tries = [
278+ ...REFUSED.map((c) => [c]),
279+ ["issue", "new", "a title"],
280+ ["issue", "3", "--comment", "hi"],
281+ ["issues", "--drafts"],
282+ ["blame", "a.txt", "--mine"],
283+ ];
284+ for (const argv of tries) {
285+ const out = await main(argv, { cwd: root, env: { ABSO_TOKEN: TOKEN }, write: () => {} });
286+ assert.notEqual(out.exit, 0, argv.join(" "));
287+ assert.match(out.stderr, /This folder reads abso-core signed out\. To work on it: abso login/, argv.join(" "));
288+ }
289+ } finally {
290+ globalThis.fetch = real;
291+ }
292+ assert.equal(calls, 0);
293+ });
295+ it("prints what comes back through the terminal filter, a 429's text too", async () => {
296+ const { root, home } = await joined();
297+ const show = await client(root, home, ["show", "1:a.txt"]);
298+ assert.equal(show.code, 0, show.stderr);
299+ assert.doesNotMatch(show.stdout, /\x1b/);
300+ assert.match(show.stdout, /hi \\x1b\]52;c;aGk=\\x07 there/);
301+ const log = await client(root, home, ["log"]);
302+ assert.notEqual(log.code, 0);
303+ assert.doesNotMatch(log.stderr + log.stdout, /\x1b/);
304+ assert.match(log.stderr, /too many signed-out reads from this address/);
305+ });
307+ it("show prints a file and writes nothing, even for a path outside the folder", async () => {
308+ const { root, home } = await joined();
309+ const parent = resolve(root, "..");
310+ const before = { here: readdirSync(root).sort(), above: readdirSync(parent).sort() };
311+ await client(root, home, ["show", "1:a.txt"]);
312+ const outside = await client(root, home, ["show", "1:../x"]);
313+ assert.notEqual(outside.code, 0);
314+ assert.deepEqual(readdirSync(root).sort(), before.here);
315+ assert.deepEqual(readdirSync(parent).sort(), before.above);
316+ assert.equal(existsSync(join(parent, "x")), false);
317+ });
318+});
MCP
compare on this site's /mcp
CLI
abso diff 85 86 client/test/visitor.test.ts --json
JSON
/christopher/abso-core/compare/85..86?file=client%2Ftest%2Fvisitor.test.ts&format=json