- MCP
- blame on this site's /mcp
- CLI
- abso blame client/test/token-file.test.ts --json
- JSON
- /christopher/abso-core/blame/client/test/token-file.test.ts?format=json
157 linesreadspeed 100%Last change readspeed in version 271,
157 lines from 2 changes in 1 version.
readspeed271No command picks up a credential from a file nobody asked for
Follows "abso init signs in with the box sign-in" (issue 25), and applies it to every command.
- A folder with a key signs in with that key and nothing else: not ABSO_TOKEN, not ~/.abso/token, even when the server refuses the key. A key file that can't be read is now an error ("abso key install puts a key back") instead of a quiet fall back to another credential.
- With no key, a command signs in with ABSO_TOKEN (or a pipeline run's ABSO_KEY_FILE). This machine's ~/.abso/token is used only by a command given --token-file, which every command that talks to a server now takes. Without it, a refused call says "You're not signed in. Run abso login, then abso join <project>. ~/.abso/token is used only with --token-file." abso doctor says the same about a token file it finds unused. The edit hook and MCP tools never read it.
- The admin token (ABSO_ADMIN_TOKEN, or ~/.abso/admin-token) is read only by abso key and abso flag. Each says "Using the admin token, from ..." every time it uses it. A test holds that no other module reads the file.
What changes for you: a folder with no key that relied on ~/.abso/token needs --token-file on each command, or ABSO_TOKEN, or a key (abso login, then abso join).
Tests: client/test/token-file.test.ts (3, new); client/test/client.test.ts's MCP check covers --token-file. All client tests pass (443).Compare1// A credential is never picked up from a file nobody asked for. A folder with a key signs in with that key only, and a
2// key file that can't be read is an error, not a reason to try another credential. With no key, this machine's
3// ~/.abso/token signs in only for a command given --token-file. The admin token is read by admin commands alone, and
4// each says when it uses it.
6import assert from "node:assert/strict";
7import { execFile } from "node:child_process";
8import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
9import { tmpdir } from "node:os";
10import { join, resolve } from "node:path";
11import { after, before, describe, it } from "node:test";
12import { tokenFor } from "../src/config.ts";
13import { adminTokenWords } from "../src/key-admin.ts";
14import { type Fake, startFake } from "./fake-server.ts";
16const CLIENT = resolve(import.meta.dirname, "..", "abso.ts");
17const TOKEN = "file-token-not-secret";
18const KEY = "abso_sk_folder-key-not-secret";
19let fake: Fake;
20const dirs: string[] = [];
21const temp = () => {
22 const dir = mkdtempSync(join(tmpdir(), "abso-test-token-file-"));
23 dirs.push(dir);
24 return dir;
25};
27before(async () => {
28 fake = await startFake(0, TOKEN);
29});
30after(async () => {
31 await fake.close();
32 for (const dir of dirs) rmSync(dir, { recursive: true, force: true });
33});
35function homeWithToken(): string {
36 const dir = temp();
37 mkdirSync(join(dir, ".abso"), { recursive: true });
38 writeFileSync(join(dir, ".abso", "token"), `${TOKEN}\n`);
39 return dir;
40}
42/** A joined folder on `server`, with the given key file text, or none. */
43function folder(server: string, key?: string): string {
44 const dir = temp();
45 mkdirSync(join(dir, ".abso"));
46 writeFileSync(join(dir, ".abso", "config.json"), JSON.stringify({ server, project: "p", agent: "me" }));
47 if (key !== undefined) writeFileSync(join(dir, ".abso", "key"), key);
48 return dir;
49}
51/** Runs `fn` with this home as the user's, so the token file is the test's own. */
52function asHome<T>(home: string, fn: () => T): T {
53 const saved = { HOME: process.env.HOME, USERPROFILE: process.env.USERPROFILE };
54 process.env.HOME = home;
55 process.env.USERPROFILE = home;
56 try {
57 return fn();
58 } finally {
59 for (const [k, v] of Object.entries(saved)) {
60 if (v === undefined) delete process.env[k];
61 else process.env[k] = v;
62 }
63 }
64}
66function client(
67 home: string,
68 cwd: string,
69 ...args: string[]
70): Promise<{ code: number; stdout: string; stderr: string }> {
71 const env: Record<string, string> = {
72 PATH: process.env.PATH ?? "",
73 HOME: home,
74 USERPROFILE: home,
75 AVCS_SERVERS_FILE: "no-servers.json",
76 ABSO_NO_BROWSER: "1",
77 };
78 if (process.env.CLAUDE_CONFIG_DIR) env.CLAUDE_CONFIG_DIR = process.env.CLAUDE_CONFIG_DIR;
79 if (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot;
80 return new Promise((done) => {
81 execFile(process.execPath, [CLIENT, ...args], { cwd, env }, (error, stdout, stderr) =>
82 done({ code: typeof error?.code === "number" ? error.code : 0, stdout, stderr }),
83 );
84 });
85}
87describe("credentials only where asked", () => {
88 it("a folder's key is the only credential it sends; an unreadable key file is an error", () => {
89 const server = "http://127.0.0.1:1";
90 asHome(homeWithToken(), () => {
91 const keyed = folder(server, JSON.stringify({ server, key: KEY, id: "k_0123abcd" }));
92 assert.equal(tokenFor(server, {}, keyed), KEY);
93 assert.equal(tokenFor(server, {}, keyed, true), KEY, "even with --token-file");
94 assert.equal(tokenFor(server, { ABSO_TOKEN: "env-token" }, keyed), KEY, "even with ABSO_TOKEN");
95 assert.throws(() => tokenFor(server, {}, folder(server, "not a key")), /key file .* can't be read/);
96 const keyless = folder(server);
97 assert.equal(tokenFor(server, {}, keyless), undefined, "the token file is never read by itself");
98 assert.equal(tokenFor(server, {}, keyless, true), TOKEN, "with --token-file it is");
99 assert.equal(tokenFor(server, { ABSO_TOKEN: "env-token" }, keyless), "env-token");
100 });
101 });
103 it("a keyless folder: refused as signed out, naming --token-file; with --token-file it signs in", async () => {
104 const home = homeWithToken();
105 const made = await client(
106 home,
107 (() => {
108 const d = temp();
109 writeFileSync(join(d, "a.txt"), "hi\n");
110 return d;
111 })(),
112 "init",
113 "--project",
114 "p",
115 "--agent",
116 "first",
117 "--token-file",
118 "--server",
119 fake.url,
120 );
121 assert.equal(made.code, 0, made.stderr);
122 const keyless = folder(fake.url);
123 const unasked = await client(home, keyless, "status");
124 assert.notEqual(unasked.code, 0);
125 assert.match(
126 unasked.stderr,
127 /You're not signed in\. Run abso login, then abso join <project>\. ~\/\.abso\/token is used only with --token-file\./,
128 );
129 const asked = await client(home, keyless, "status", "--token-file");
130 assert.equal(asked.code, 0, asked.stderr);
131 assert.match(asked.stdout, /Project p/);
132 });
134 it("the admin token is read by admin commands alone, and they say when they use it", () => {
135 assert.equal(adminTokenWords({ ABSO_ADMIN_TOKEN: "x" }), "Using the admin token, from ABSO_ADMIN_TOKEN.");
136 assert.equal(adminTokenWords({}), "Using the admin token, from ~/.abso/admin-token.");
137 const src = resolve(import.meta.dirname, "..", "src");
138 const readers: string[] = [];
139 const users: string[] = [];
140 for (const name of readdirSync(src).filter((n) => n.endsWith(".ts"))) {
141 const text = readFileSync(join(src, name), "utf8");
142 if (/"admin-token"/.test(text)) readers.push(name);
143 if (/\badminToken\(/.test(text.replace(/export function adminToken\(/, ""))) users.push(name);
144 }
145 assert.deepEqual(readers, ["key-admin.ts"], "only key-admin.ts reads ~/.abso/admin-token");
146 assert.deepEqual(users.sort(), ["flag-commands.ts", "main.ts"], "abso flag and abso key, and nothing else");
147 const main = readFileSync(join(src, "main.ts"), "utf8");
148 assert.match(
149 main,
150 /const admin = adminToken\(env\.env\);\s+\(env\.writeErr \?\? env\.write\)\(`\$\{adminTokenWords/,
151 );
152 assert.match(
153 readFileSync(join(src, "flag-commands.ts"), "utf8"),
154 /if \(admin\)\s+\(env\.writeErr \?\? env\.write\)\(`\$\{adminTokenWords/,
155 );
156 });
157});