Skip to content
refs
History
98 lines · 6.7 KBRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { LogMasker, MASK } from "../logs/log-masker.ts";
4import { InvalidMasterKey, SecretBox, SecretUnreadable } from "../secrets/secret-box.ts";
5import { namespaceSecretScope, openJobSecrets, repoSecretScope } from "../secrets/job-secrets.ts";
6import { d1, migrated } from "../../backend/test/d1.ts";
7
8const MASTER = btoa(String.fromCharCode(...new Uint8Array(32).map((_, i) => i * 7)));
9const scope = { owner: "repo:repo", environment: "production", name: "CLOUDFLARE_API_TOKEN" };
10
11test("a sealed secret opens with the same scope", async () => {
12 const box = await SecretBox.fromBase64(MASTER);
13 const sealed = await box.seal("tok_123", scope);
14 assert.equal(await box.open(sealed, scope), "tok_123");
15 assert.ok(!sealed.ciphertext.includes("tok_123"));
16});
17
18test("sealing twice gives different ciphertexts", async () => {
19 const box = await SecretBox.fromBase64(MASTER);
20 const [a, b] = await Promise.all([box.seal("same", scope), box.seal("same", scope)]);
21 assert.notEqual(a.ciphertext, b.ciphertext);
22 assert.notEqual(a.wrappedKey, b.wrappedKey);
23});
24
25test("a sealed secret copied to another repo, environment or name does not open", async () => {
26 const box = await SecretBox.fromBase64(MASTER);
27 const sealed = await box.seal("tok_123", scope);
28 for (const moved of [{ ...scope, owner: "namespace:repo" }, { ...scope, environment: null }, { ...scope, name: "OTHER" }]) {
29 await assert.rejects(box.open(sealed, moved), SecretUnreadable);
30 }
31});
32
33test("another master key cannot open it, and a bad master key is refused", async () => {
34 const sealed = await (await SecretBox.fromBase64(MASTER)).seal("tok_123", scope);
35 const other = await SecretBox.fromBase64(btoa("k".repeat(32)));
36 await assert.rejects(other.open(sealed, scope), SecretUnreadable);
37 await assert.rejects(SecretBox.fromBase64(btoa("short")), InvalidMasterKey);
38 await assert.rejects(SecretBox.fromBase64("not base64!"), InvalidMasterKey);
39});
40
41test("the masker hides a secret and its encoded forms", () => {
42 const masker = new LogMasker(["s3cr3t/value+1"]);
43 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"];
44 assert.equal(masker.push(lines.join("\n") + "\n"), ["token=***", "b64=***=", "url=***", "b64url=***", "plain"].join("\n") + "\n");
45});
46
47test("the masker finds a secret split across chunks", () => {
48 const masker = new LogMasker(["hunter22"]);
49 assert.equal(masker.push("password: hun"), "");
50 assert.equal(masker.push("ter22 ok\nnext"), `password: ${MASK} ok\n`);
51 assert.equal(masker.flush(), "next");
52});
53
54test("the masker hides each line of a secret that spans lines, and ignores very short secrets", () => {
55 const key = "-----BEGIN KEY-----\nMIIBVQIBADANBgkqhkiG9w0BAQEFAASC\n-----END KEY-----";
56 const masker = new LogMasker([key, "ab"]);
57 assert.equal(masker.push("echo MIIBVQIBADANBgkqhkiG9w0BAQEFAASC and ab\n"), `echo ${MASK} and ab\n`);
58});
59
60test("the masker sends a very long line in parts instead of holding it", () => {
61 const masker = new LogMasker(["secret"]);
62 const out = masker.push("x".repeat(70 * 1024) + "secret");
63 assert.equal(out.length, 70 * 1024 + MASK.length);
64});
65
66test("the CI tables accept a run and refuse a secret name that is not upper snake case", async () => {
67 const db = d1(migrated());
68 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();
69 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();
70 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());
71 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());
72});
73
74test("a job gets only the secrets it names: environment over repository over organization, and environment secrets only when allowed", async () => {
75 const sqlite = migrated();
76 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);");
77 const db = d1(sqlite);
78 const box = await SecretBox.fromBase64(MASTER);
79 const put = async (table: "ci_secrets" | "ci_namespace_secrets", environment: string, name: string, value: string) => {
80 const sealed = await box.seal(value, table === "ci_namespace_secrets" ? namespaceSecretScope("ns", name) : repoSecretScope("repo", environment || null, name));
81 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();
82 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();
83 };
84 await put("ci_namespace_secrets", "", "TOKEN", "org");
85 await put("ci_namespace_secrets", "", "ORG_ONLY", "org-only");
86 await put("ci_secrets", "", "TOKEN", "repo");
87 await put("ci_secrets", "production", "TOKEN", "prod");
88 await put("ci_secrets", "", "UNNAMED", "x");
89 const request = { repoId: "repo", namespaceId: "ns", environment: "production", access: "repo" as const, approved: false, names: ["TOKEN", "ORG_ONLY", "MISSING"] };
90
91 const held = await openJobSecrets(db, MASTER, request);
92 assert.deepEqual(held.values, { TOKEN: "repo", ORG_ONLY: "org-only" });
93 assert.equal(held.notes.length, 2);
94 assert.deepEqual((await openJobSecrets(db, MASTER, { ...request, approved: true })).values, { TOKEN: "prod", ORG_ONLY: "org-only" });
95 assert.deepEqual((await openJobSecrets(db, MASTER, { ...request, access: "all" })).values, { TOKEN: "prod", ORG_ONLY: "org-only" });
96 assert.deepEqual((await openJobSecrets(db, MASTER, { ...request, access: "none", approved: true })).values, {});
97 await assert.rejects(openJobSecrets(db, undefined, request), /CI_SECRETS_KEY/);
98});