import assert from "node:assert/strict"; import { test } from "node:test"; import { LogMasker, MASK } from "../logs/log-masker.ts"; import { InvalidMasterKey, SecretBox, SecretUnreadable } from "../secrets/secret-box.ts"; import { namespaceSecretScope, openJobSecrets, repoSecretScope } from "../secrets/job-secrets.ts"; import { d1, migrated } from "../../backend/test/d1.ts"; const MASTER = btoa(String.fromCharCode(...new Uint8Array(32).map((_, i) => i * 7))); const scope = { owner: "repo:repo", environment: "production", name: "CLOUDFLARE_API_TOKEN" }; test("a sealed secret opens with the same scope", async () => { const box = await SecretBox.fromBase64(MASTER); const sealed = await box.seal("tok_123", scope); assert.equal(await box.open(sealed, scope), "tok_123"); assert.ok(!sealed.ciphertext.includes("tok_123")); }); test("sealing twice gives different ciphertexts", async () => { const box = await SecretBox.fromBase64(MASTER); const [a, b] = await Promise.all([box.seal("same", scope), box.seal("same", scope)]); assert.notEqual(a.ciphertext, b.ciphertext); assert.notEqual(a.wrappedKey, b.wrappedKey); }); test("a sealed secret copied to another repo, environment or name does not open", async () => { const box = await SecretBox.fromBase64(MASTER); const sealed = await box.seal("tok_123", scope); for (const moved of [{ ...scope, owner: "namespace:repo" }, { ...scope, environment: null }, { ...scope, name: "OTHER" }]) { await assert.rejects(box.open(sealed, moved), SecretUnreadable); } }); test("another master key cannot open it, and a bad master key is refused", async () => { const sealed = await (await SecretBox.fromBase64(MASTER)).seal("tok_123", scope); const other = await SecretBox.fromBase64(btoa("k".repeat(32))); await assert.rejects(other.open(sealed, scope), SecretUnreadable); await assert.rejects(SecretBox.fromBase64(btoa("short")), InvalidMasterKey); await assert.rejects(SecretBox.fromBase64("not base64!"), InvalidMasterKey); }); test("the masker hides a secret and its encoded forms", () => { const masker = new LogMasker(["s3cr3t/value+1"]); const lines = ["token=s3cr3t/value+1", `b64=${btoa("s3cr3t/value+1")}`, `url=${encodeURIComponent("s3cr3t/value+1")}`, `b64url=${btoa("s3cr3t/value+1").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "")}`, "plain"]; assert.equal(masker.push(lines.join("\n") + "\n"), ["token=***", "b64=***=", "url=***", "b64url=***", "plain"].join("\n") + "\n"); }); test("the masker finds a secret split across chunks", () => { const masker = new LogMasker(["hunter22"]); assert.equal(masker.push("password: hun"), ""); assert.equal(masker.push("ter22 ok\nnext"), `password: ${MASK} ok\n`); assert.equal(masker.flush(), "next"); }); test("the masker hides each line of a secret that spans lines, and ignores very short secrets", () => { const key = "-----BEGIN KEY-----\nMIIBVQIBADANBgkqhkiG9w0BAQEFAASC\n-----END KEY-----"; const masker = new LogMasker([key, "ab"]); assert.equal(masker.push("echo MIIBVQIBADANBgkqhkiG9w0BAQEFAASC and ab\n"), `echo ${MASK} and ab\n`); }); test("the masker sends a very long line in parts instead of holding it", () => { const masker = new LogMasker(["secret"]); const out = masker.push("x".repeat(70 * 1024) + "secret"); assert.equal(out.length, 70 * 1024 + MASK.length); }); test("the CI tables accept a run and refuse a secret name that is not upper snake case", async () => { const db = d1(migrated()); await db.prepare("INSERT INTO ci_runs (id, repo_id, sha, ref, trigger, actor_kind, actor_id, trust, secrets, trust_reason, state, config_hash, created_at) VALUES ('r', 'repo', 'abc', 'refs/heads/main', 'push', 'human', 'u1', 'trusted', 'all', 'x', 'queued', 'h', 0)").run(); await db.prepare("INSERT INTO ci_secrets (repo_id, environment, name, ciphertext, wrapped_key, updated_by, updated_at) VALUES ('repo', '', 'NPM_TOKEN_2', 'c', 'k', 'u1', 0)").run(); await assert.rejects(db.prepare("INSERT INTO ci_secrets (repo_id, environment, name, ciphertext, wrapped_key, updated_by, updated_at) VALUES ('repo', '', 'npm-token', 'c', 'k', 'u1', 0)").run()); await assert.rejects(db.prepare("INSERT INTO ci_secrets (repo_id, environment, name, ciphertext, wrapped_key, updated_by, updated_at) VALUES ('repo', '', '9LIVES', 'c', 'k', 'u1', 0)").run()); }); test("a job gets only the secrets it names: environment over repository over organization, and environment secrets only when allowed", async () => { const sqlite = migrated(); sqlite.exec("INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'acme', 'Acme', 'org', 0); INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@x', 'A', 'h', 0); INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'web', 'private', 'art', 'u1', 0);"); const db = d1(sqlite); const box = await SecretBox.fromBase64(MASTER); const put = async (table: "ci_secrets" | "ci_namespace_secrets", environment: string, name: string, value: string) => { const sealed = await box.seal(value, table === "ci_namespace_secrets" ? namespaceSecretScope("ns", name) : repoSecretScope("repo", environment || null, name)); if (table === "ci_namespace_secrets") await db.prepare("INSERT INTO ci_namespace_secrets (namespace_id, name, ciphertext, wrapped_key, updated_by, updated_at) VALUES ('ns', ?, ?, ?, 'u1', 0)").bind(name, sealed.ciphertext, sealed.wrappedKey).run(); else await db.prepare("INSERT INTO ci_secrets (repo_id, environment, name, ciphertext, wrapped_key, updated_by, updated_at) VALUES ('repo', ?, ?, ?, ?, 'u1', 0)").bind(environment, name, sealed.ciphertext, sealed.wrappedKey).run(); }; await put("ci_namespace_secrets", "", "TOKEN", "org"); await put("ci_namespace_secrets", "", "ORG_ONLY", "org-only"); await put("ci_secrets", "", "TOKEN", "repo"); await put("ci_secrets", "production", "TOKEN", "prod"); await put("ci_secrets", "", "UNNAMED", "x"); const request = { repoId: "repo", namespaceId: "ns", environment: "production", access: "repo" as const, approved: false, names: ["TOKEN", "ORG_ONLY", "MISSING"] }; const held = await openJobSecrets(db, MASTER, request); assert.deepEqual(held.values, { TOKEN: "repo", ORG_ONLY: "org-only" }); assert.equal(held.notes.length, 2); assert.deepEqual((await openJobSecrets(db, MASTER, { ...request, approved: true })).values, { TOKEN: "prod", ORG_ONLY: "org-only" }); assert.deepEqual((await openJobSecrets(db, MASTER, { ...request, access: "all" })).values, { TOKEN: "prod", ORG_ONLY: "org-only" }); assert.deepEqual((await openJobSecrets(db, MASTER, { ...request, access: "none", approved: true })).values, {}); await assert.rejects(openJobSecrets(db, undefined, request), /CI_SECRETS_KEY/); });