|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | + |
| 4 | +// env-allowlist imports @earendil-works/pi-ai (for findEnvKeys). That needs node >=22.19.0 and |
| 5 | +// installed deps, so it skips on a below-floor dev box and runs in CI, where |
| 6 | +// PI_DISPATCH_REQUIRE_WORKER_TESTS=1 turns a skip into a hard failure. A skipped security test is |
| 7 | +// an unverified one -- the same discipline as the runner's loader tests. |
| 8 | +let mod; |
| 9 | +let importError; |
| 10 | +try { |
| 11 | + mod = await import("../src/env-allowlist.mjs"); |
| 12 | +} catch (error) { |
| 13 | + importError = error; |
| 14 | +} |
| 15 | +if (!mod && process.env.PI_DISPATCH_REQUIRE_WORKER_TESTS === "1") { |
| 16 | + throw new Error(`env-allowlist tests are REQUIRED here but pi-ai could not import.\n${importError}`); |
| 17 | +} |
| 18 | +const skip = mod ? false : `pi-ai not installed (node ${process.version} < 22.19.0); CI runs these`; |
| 19 | +const { buildContainerEnv, providerKeyVars } = mod ?? {}; |
| 20 | + |
| 21 | +const HOST = { |
| 22 | + ANTHROPIC_API_KEY: "sk-ant-real", |
| 23 | + OPENAI_API_KEY: "sk-openai-real", |
| 24 | + // The stray host variable no-broad-env-into-container exists to defend against: |
| 25 | + AWS_SECRET_ACCESS_KEY: "must-not-leak", |
| 26 | + HOME: "/root", |
| 27 | + PATH: "/usr/bin", |
| 28 | +}; |
| 29 | + |
| 30 | +test("derives the provider key var from the host env, in precedence order", { skip }, () => { |
| 31 | + assert.deepEqual(providerKeyVars("anthropic", HOST), ["ANTHROPIC_API_KEY"]); |
| 32 | + assert.deepEqual(providerKeyVars("openai", HOST), ["OPENAI_API_KEY"]); |
| 33 | + // OAuth outranks API key -- the array order is the precedence. |
| 34 | + assert.deepEqual( |
| 35 | + providerKeyVars("anthropic", { ...HOST, ANTHROPIC_OAUTH_TOKEN: "oauth" }), |
| 36 | + ["ANTHROPIC_OAUTH_TOKEN", "ANTHROPIC_API_KEY"], |
| 37 | + ); |
| 38 | +}); |
| 39 | + |
| 40 | +test("an unconfigured provider yields undefined (=> refuse before spend)", { skip }, () => { |
| 41 | + assert.equal(providerKeyVars("google", HOST), undefined); |
| 42 | +}); |
| 43 | + |
| 44 | +test("the container env is a CLOSED set: only the provider key, never the whole host", { skip }, () => { |
| 45 | + const env = buildContainerEnv({ |
| 46 | + provider: "anthropic", |
| 47 | + model: "claude-x", |
| 48 | + maxTurns: 20, |
| 49 | + jobId: "abc", |
| 50 | + githubToken: "ghs_scoped", |
| 51 | + hostEnv: HOST, |
| 52 | + }); |
| 53 | + assert.equal(env.ANTHROPIC_API_KEY, "sk-ant-real"); |
| 54 | + assert.equal(env.GITHUB_TOKEN, "ghs_scoped"); |
| 55 | + assert.equal(env.PI_PROVIDER, "anthropic"); |
| 56 | + // The stray host secrets are NOT forwarded. |
| 57 | + assert.equal(env.AWS_SECRET_ACCESS_KEY, undefined); |
| 58 | + assert.equal(env.HOME, undefined); |
| 59 | + assert.equal(env.OPENAI_API_KEY, undefined); // wrong provider's key not forwarded either |
| 60 | +}); |
| 61 | + |
| 62 | +test("a local-folder job (no token) gets NO GITHUB_TOKEN var at all -- not an empty one", { skip }, () => { |
| 63 | + const env = buildContainerEnv({ |
| 64 | + provider: "anthropic", |
| 65 | + model: "m", |
| 66 | + maxTurns: 5, |
| 67 | + jobId: "j", |
| 68 | + githubToken: undefined, |
| 69 | + hostEnv: HOST, |
| 70 | + }); |
| 71 | + assert.ok(!("GITHUB_TOKEN" in env), "absent token must mean absent variable"); |
| 72 | +}); |
| 73 | + |
| 74 | +test("an unconfigured provider throws a config-tagged error (=> pre-spend refusal)", { skip }, () => { |
| 75 | + assert.throws( |
| 76 | + () => buildContainerEnv({ provider: "google", model: "m", maxTurns: 5, jobId: "j", hostEnv: HOST }), |
| 77 | + (e) => e.piDispatchConfig === true, |
| 78 | + ); |
| 79 | +}); |
0 commit comments