Heimdall/src/custody.ts
Meta Vens 0178d071c1 readSecretInput: never print the path, refuse relative paths and blank secrets
Soul F1: the unreadable-file error carried the NAME_FILE value, and Idunn
admits any string for a declared name, so a URL bound into
GC_ACCESS_DATABASE_URL_FILE by mistake was printed in full at startup. Errors
now name only the variable and an error code, and a NAME_FILE that is not an
absolute path is refused naming the variable alone.

Soul F2 and F6: a blank database URL file selected memory storage silently,
and a whitespace one selected postgres with a URL of " ". The reader now
refuses an empty or whitespace-only value from either form, so custody's own
"set but empty" check is redundant and is deleted; a short key still fails its
length check. The legacy service.env holds no blank secret-class value
(checked as name/blank booleans), so the legacy unit is unaffected.

Soul F3: tests pin that an empty NAME_FILE is refused rather than unset (S1),
and that each call site (token key, provider secret, app shared secret,
database URL, patron secret) refuses both forms, so no site can read the
plaintext name ahead of the reader (S6, S7, S8).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 13:50:53 +02:00

76 lines
2.8 KiB
TypeScript

import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
import { type HeimdallConfig } from "./config.js";
const TOKEN_ENVELOPE_PREFIX = "gc_tok_v1";
const TOKEN_KEY_BYTES = 32;
const TOKEN_IV_BYTES = 12;
export interface TokenCustody {
source: "configured" | "ephemeral_dev";
encrypt(plaintext: string): string;
decrypt(ciphertext: string): string;
}
function decodeConfiguredKey(base64Value: string): Buffer {
// readSecretInput refuses a blank key; a short one fails the length check.
const key = Buffer.from(base64Value.trim(), "base64");
if (key.byteLength !== TOKEN_KEY_BYTES) {
throw new Error(
`GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64_FILE must decode to exactly ${TOKEN_KEY_BYTES} bytes for AES-256-GCM.`
);
}
return key;
}
function decodeEnvelope(ciphertext: string): { iv: Buffer; sealed: Buffer; authTag: Buffer } {
const [prefix, payload] = ciphertext.split(":", 2);
if (prefix !== TOKEN_ENVELOPE_PREFIX || !payload) {
throw new Error("Managed provider credential is not in a supported encrypted format.");
}
const [ivEncoded, sealedEncoded, authTagEncoded] = payload.split(".");
if (!ivEncoded || !sealedEncoded || !authTagEncoded) {
throw new Error("Managed provider credential envelope is malformed.");
}
return {
iv: Buffer.from(ivEncoded, "base64url"),
sealed: Buffer.from(sealedEncoded, "base64url"),
authTag: Buffer.from(authTagEncoded, "base64url"),
};
}
export function createTokenCustody(config: HeimdallConfig): TokenCustody {
const key = config.tokenEncryptionKeyBase64
? decodeConfiguredKey(config.tokenEncryptionKeyBase64)
: (() => {
if (config.storage.backend === "postgres") {
throw new Error(
"GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64_FILE is required when Heimdall uses persistent postgres storage."
);
}
return randomBytes(TOKEN_KEY_BYTES);
})();
const source: TokenCustody["source"] = config.tokenEncryptionKeyBase64 ? "configured" : "ephemeral_dev";
return {
source,
encrypt(plaintext: string): string {
const iv = randomBytes(TOKEN_IV_BYTES);
const cipher = createCipheriv("aes-256-gcm", key, iv);
const sealed = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
const authTag = cipher.getAuthTag();
return `${TOKEN_ENVELOPE_PREFIX}:${iv.toString("base64url")}.${sealed.toString("base64url")}.${authTag.toString("base64url")}`;
},
decrypt(ciphertext: string): string {
const envelope = decodeEnvelope(ciphertext);
const decipher = createDecipheriv("aes-256-gcm", key, envelope.iv);
decipher.setAuthTag(envelope.authTag);
const plaintext = Buffer.concat([decipher.update(envelope.sealed), decipher.final()]);
return plaintext.toString("utf8");
},
};
}