Recipe test reads its secret names from the source
Soul pass 3 (R3): the recipe test's list of secret names was written by hand, and its secret-shaped pattern missed TOKEN, API_KEY, PRIVATE_KEY, SALT, CREDENTIAL and DSN. A new secret could be read in the source and declared in plaintext in the recipe without a failure. The test now takes the names from every readSecretInput call under src/: a literal as written, the two computed names over the lists that compute them, and a failure for any argument it cannot expand. Every one of those must be declared as its _FILE and never by its plaintext name. The pattern is wider, with two named exemptions: the signing key's path and its bootstrap flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c626a86d55
commit
ca2e78f00c
1 changed files with 55 additions and 16 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { readFileSync } from "node:fs";
|
||||
import { readdirSync, readFileSync } from "node:fs";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { appProfiles } from "../src/app-profiles.js";
|
||||
import { providers } from "../src/contracts.js";
|
||||
import { appSlugs, providers } from "../src/contracts.js";
|
||||
import { providerCatalog } from "../src/providers.js";
|
||||
|
||||
// The Idunn recipe decides which environment names a binding may set, and
|
||||
|
|
@ -26,20 +26,45 @@ const everyDeclaredName = [...recipe.replace(/\r\n/g, "\n").matchAll(/^\w+_envir
|
|||
(match) => [...match[1]!.matchAll(/"([^"]+)"/g)].map((name) => name[1]!)
|
||||
);
|
||||
|
||||
const serviceSecretNames = [
|
||||
"GC_ACCESS_DATABASE_URL",
|
||||
"GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64",
|
||||
"GC_ACCESS_BIFROST_PATRON_SUPPORT_SECRET",
|
||||
];
|
||||
// The secrets Heimdall reads are whatever its source passes to
|
||||
// readSecretInput, so the set is read from the source: a secret added there
|
||||
// cannot be declared in plaintext here without failing. A literal name is
|
||||
// taken as written; the two computed names expand over the lists that compute
|
||||
// them; any other argument fails, because this test cannot know its names.
|
||||
const srcRoot = new URL("../src/", import.meta.url);
|
||||
const secretArguments = readdirSync(srcRoot, { recursive: true })
|
||||
.map(String)
|
||||
.filter((file) => file.endsWith(".ts"))
|
||||
.flatMap((file) =>
|
||||
[...readFileSync(new URL(file.replace(/\\/g, "/"), srcRoot), "utf8").matchAll(/(?<!function )readSecretInput\(\s*[^,()]+,\s*([^)]+?)\s*\)/g)].map(
|
||||
(call) => call[1]!
|
||||
)
|
||||
);
|
||||
|
||||
const computedSecretNames: Record<string, string[]> = {
|
||||
"`${prefix}_CLIENT_SECRET`": providers.map((provider) => `GC_ACCESS_PROVIDER_${provider.toUpperCase()}_CLIENT_SECRET`),
|
||||
envKey: appSlugs.map((appSlug) => `GC_ACCESS_APP_${appSlug.toUpperCase()}_SHARED_SECRET`),
|
||||
};
|
||||
|
||||
/** Secrets Heimdall reads through readSecretInput, by their plaintext name. */
|
||||
const secretNames = [
|
||||
...serviceSecretNames,
|
||||
...providers.map((provider) => `GC_ACCESS_PROVIDER_${provider.toUpperCase()}_CLIENT_SECRET`),
|
||||
...Object.values(appProfiles).map((profile) => `GC_ACCESS_APP_${profile.slug.toUpperCase()}_SHARED_SECRET`),
|
||||
];
|
||||
const secretNames = secretArguments.flatMap((argument) => {
|
||||
const literal = /^"([A-Z0-9_]+)"$/.exec(argument);
|
||||
if (literal) return [literal[1]!];
|
||||
const computed = computedSecretNames[argument];
|
||||
if (computed) return computed;
|
||||
throw new Error(`recipe-secrets cannot derive the names read by readSecretInput(env, ${argument}); teach it.`);
|
||||
});
|
||||
|
||||
const secretShaped = /SECRET|PASSWORD|DATABASE_URL|ENCRYPTION_KEY|_PEM$/;
|
||||
const secretShaped =
|
||||
/SECRET|PASSWORD|PASSPHRASE|TOKEN|API_KEY|PRIVATE_KEY|ENCRYPTION_KEY|SALT|CREDENTIAL|DATABASE_URL|_DSN|_PEM/;
|
||||
|
||||
/** Secret-shaped names that hold no secret, each for its own reason. */
|
||||
const notSecrets = new Set([
|
||||
// A path to the signing key file, which Heimdall reads (or creates) itself.
|
||||
"GC_ACCESS_SIGNING_PRIVATE_KEY_PATH",
|
||||
// A flag: create that file on first boot.
|
||||
"GC_ACCESS_SIGNING_PRIVATE_KEY_BOOTSTRAP",
|
||||
]);
|
||||
|
||||
describe("Idunn recipe secret declarations", () => {
|
||||
it("reads the service's declared environment", () => {
|
||||
|
|
@ -48,10 +73,24 @@ describe("Idunn recipe secret declarations", () => {
|
|||
});
|
||||
|
||||
it("declares no secret-shaped name except as a _FILE credential path", () => {
|
||||
const plaintext = everyDeclaredName.filter((name) => secretShaped.test(name) && !name.endsWith("_FILE"));
|
||||
const plaintext = everyDeclaredName.filter(
|
||||
(name) => secretShaped.test(name) && !name.endsWith("_FILE") && !notSecrets.has(name)
|
||||
);
|
||||
expect(plaintext).toEqual([]);
|
||||
});
|
||||
|
||||
it("finds every readSecretInput call in the source", () => {
|
||||
expect(secretNames).toEqual(
|
||||
expect.arrayContaining([
|
||||
"GC_ACCESS_DATABASE_URL",
|
||||
"GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64",
|
||||
"GC_ACCESS_BIFROST_PATRON_SUPPORT_SECRET",
|
||||
"GC_ACCESS_PROVIDER_DISCORD_CLIENT_SECRET",
|
||||
"GC_ACCESS_APP_GHOSTLIGHT_SHARED_SECRET",
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it("declares none of the secrets Heimdall reads under its plaintext name", () => {
|
||||
for (const name of secretNames) {
|
||||
expect(everyDeclaredName).not.toContain(name);
|
||||
|
|
@ -70,8 +109,8 @@ describe("Idunn recipe secret declarations", () => {
|
|||
}
|
||||
});
|
||||
|
||||
it("declares the database URL, token key and patron secret files", () => {
|
||||
for (const name of serviceSecretNames) {
|
||||
it("declares the _FILE of every secret Heimdall reads", () => {
|
||||
for (const name of secretNames) {
|
||||
expect(serviceNames).toContain(`${name}_FILE`);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue