{"draft":null,"from":91,"to":318,"files":[{"path":"client/test/run-key.test.ts","added":96,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/run-key.test.ts\n@@ -0,0 +1,96 @@\n+// A pipeline run's key (ABSO_KEY_FILE): read from the file the runner wrote, sent only where a token may go,\n+// held in memory only, and a run key's refusals reach the job as the server wrote them.\n+\n+import assert from \"node:assert/strict\";\n+import { mkdtempSync, writeFileSync } from \"node:fs\";\n+import { createServer } from \"node:http\";\n+import type { AddressInfo } from \"node:net\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\n+import { test } from \"node:test\";\n+import { Api, ApiError } from \"../src/api.ts\";\n+import { credentialLabel, installKey, readConfig, runKey, tokenFor, writeConfig } from \"../src/config.ts\";\n+\n+const KEY = \"abso_sk_runkeyabcdefghijklmnopqrstuvwx\";\n+const LOCAL = \"http://127.0.0.1:8787\";\n+\n+function keyFile(text: string): string {\n+\tconst path = join(mkdtempSync(join(tmpdir(), \"abso-run-key-\")), \"key\");\n+\twriteFileSync(path, text);\n+\treturn path;\n+}\n+\n+function folder(server = LOCAL) {\n+\tconst root = mkdtempSync(join(tmpdir(), \"abso-run-key-\"));\n+\twriteConfig(root, { server, project: \"p\", agent: \"run-7-check-test\" });\n+\treturn root;\n+}\n+\n+const NO_SERVERS = { ABSO_SERVERS_FILE: \"no-servers.json\" };\n+\n+test(\"ABSO_KEY_FILE's key is the credential when no token or folder key is set\", () => {\n+\tconst env = { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY) };\n+\tassert.equal(runKey(env), KEY);\n+\tassert.equal(tokenFor(LOCAL, env, folder()), KEY);\n+\tassert.deepEqual(credentialLabel(folder(), LOCAL, env), { kind: \"pipeline run key\", id: null });\n+\tassert.equal(readConfig(folder(), env).token, KEY);\n+});\n+\n+test(\"a trailing newline in the key file is not part of the key\", () => {\n+\tassert.equal(runKey({ ABSO_KEY_FILE: keyFile(`${KEY}\\n`) }), KEY);\n+});\n+\n+test(\"ABSO_TOKEN and a folder's own key both come before the run key\", () => {\n+\tconst env = { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY), ABSO_TOKEN: \"shared-token-value\" };\n+\tassert.equal(tokenFor(LOCAL, env, folder()), \"shared-token-value\");\n+\tassert.deepEqual(credentialLabel(folder(), LOCAL, env), { kind: \"shared token\", id: null });\n+\tconst root = folder();\n+\tinstallKey(root, JSON.stringify({ key: \"abso_sk_folderkeyabcdefghijklmnopqrstu\", id: \"k_ab12cd34\" }));\n+\tassert.equal(\n+\t\ttokenFor(LOCAL, { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY) }, root),\n+\t\t\"abso_sk_folderkeyabcdefghijklmnopqrstu\",\n+\t);\n+});\n+\n+test(\"the run key goes only where a token may, and the refusal doesn't hold it\", () => {\n+\tconst env = { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY) };\n+\tassert.throws(\n+\t\t() => tokenFor(\"https://someone-else.example\", env, folder(\"https://someone-else.example\")),\n+\t\t(err: Error) => /sends your token only to/.test(err.message) && !err.message.includes(KEY),\n+\t);\n+});\n+\n+test(\"a key file abso can't read, or that holds no key, is named without its contents\", () => {\n+\tconst missing = join(tmpdir(), \"abso-no-such-key-file\");\n+\tassert.throws(() => runKey({ ABSO_KEY_FILE: missing }), /can't read/);\n+\tconst wrong = keyFile(\"not-a-key secret-looking-text\");\n+\tassert.throws(\n+\t\t() => runKey({ ABSO_KEY_FILE: wrong }),\n+\t\t(err: Error) => /doesn't hold an abso key/.test(err.message) && !err.message.includes(\"secret-looking-text\"),\n+\t);\n+});\n+\n+test(\"a run key's refusal reaches the job as the server wrote it, and without the key\", async () => {\n+\tlet sent = \"\";\n+\tconst server = createServer((req, res) => {\n+\t\tsent = req.headers.authorization ?? \"\";\n+\t\tres.writeHead(403, { \"content-type\": \"application/json\" });\n+\t\tres.end(JSON.stringify({ error: \"a pipeline run's key can't land\" }));\n+\t});\n+\tawait new Promise<void>((ok) => server.listen(0, \"127.0.0.1\", ok));\n+\ttry {\n+\t\tconst url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;\n+\t\tconst config = readConfig(folder(url), { ...NO_SERVERS, ABSO_KEY_FILE: keyFile(KEY) });\n+\t\tawait assert.rejects(\n+\t\t\tnew Api(config).land(\"try\"),\n+\t\t\t(err: Error) =>\n+\t\t\t\terr instanceof ApiError &&\n+\t\t\t\terr.status === 403 &&\n+\t\t\t\t/a pipeline run's key can't land/.test(err.message) &&\n+\t\t\t\t!err.message.includes(KEY),\n+\t\t);\n+\t\tassert.equal(sent, `Bearer ${KEY}`);\n+\t} finally {\n+\t\tserver.close();\n+\t}\n+});"}],"unmatched":[]}