diff --git a/nixos/services/id-mail/worker/deploy.mjs b/nixos/services/id-mail/worker/deploy.mjs index aaa8524f93..7cf59c628b 100644 --- a/nixos/services/id-mail/worker/deploy.mjs +++ b/nixos/services/id-mail/worker/deploy.mjs @@ -84,7 +84,7 @@ if (dryRun) { } // A hostname can belong to only one Worker, so retired Workers go before the deploy takes their routes. -for (const name of config.retire) { +for (const name of config.retire ?? []) { await retire(name); } wrangler(["deploy"]); @@ -123,6 +123,10 @@ function requireEnv(name) { function validate(c) { const errors = []; + if (c === null || typeof c !== "object" || Array.isArray(c)) { + errors.push("config must be a JSON object"); + return errors; + } const check = (ok, message) => { if (!ok) { errors.push(message); @@ -139,16 +143,21 @@ function validate(c) { check(isStringList(c.retire ?? []) && !(c.retire ?? []).includes(c.worker), "retire must list other Workers"); check(isStringList(c.senders) && c.senders.length > 0 && c.senders.every(isEmail), "senders must be email addresses"); check(isStringList(c.transports) && c.transports.length > 0, "transports must be a non-empty list"); + const transportList = Array.isArray(c.transports) ? c.transports : []; check( - (c.transports ?? []).every((name) => KNOWN_TRANSPORTS.includes(name)), + transportList.every((name) => KNOWN_TRANSPORTS.includes(name)), `transports must be among ${KNOWN_TRANSPORTS.join(", ")}`, ); - check((c.transports ?? []).includes(c.defaultTransport), "defaultTransport must be one of transports"); + check(transportList.includes(c.defaultTransport), "defaultTransport must be one of transports"); check(isPositiveInt(c.limits?.windowSeconds), "limits.windowSeconds must be a positive integer"); check(isPositiveInt(c.limits?.callerPerWindow), "limits.callerPerWindow must be a positive integer"); check(isPositiveInt(c.limits?.recipientPerWindow), "limits.recipientPerWindow must be a positive integer"); check(Array.isArray(c.templates), "templates must be a list"); - for (const template of c.templates ?? []) { + for (const [index, template] of (Array.isArray(c.templates) ? c.templates : []).entries()) { + if (template === null || typeof template !== "object") { + check(false, `templates[${index}] must be an object with subject and body`); + continue; + } check( typeof template.subject === "string" && !/[\r\n]/.test(template.subject), "each template subject must be one line of text", @@ -160,12 +169,20 @@ function validate(c) { } check(c.callers && typeof c.callers === "object" && Object.keys(c.callers).length > 0, "callers must not be empty"); for (const [name, caller] of Object.entries(c.callers ?? {})) { + if (caller === null || typeof caller !== "object") { + check(false, `callers.${name} must be an object`); + continue; + } check(typeof caller.tokenKey === "string", `callers.${name}.tokenKey must be a Bitwarden secret name`); const grants = Object.entries(caller.transports ?? {}); check(grants.length > 0, `callers.${name}.transports must grant at least one transport`); for (const [transport, grant] of grants) { const where = `callers.${name}.transports.${transport}`; - check(c.transports?.includes(transport), `${where} is not an enabled transport`); + if (grant === null || typeof grant !== "object") { + check(false, `${where} must be an object`); + continue; + } + check(transportList.includes(transport), `${where} is not an enabled transport`); check(MODES.includes(grant.mode), `${where}.mode must be ${MODES.join(" or ")}`); check( isStringList(grant.from) && grant.from.length > 0 && grant.from.every((from) => c.senders?.includes(from)), diff --git a/nixos/services/id-mail/worker/deploy.test.mjs b/nixos/services/id-mail/worker/deploy.test.mjs index 35ea0f7653..37ece66907 100644 --- a/nixos/services/id-mail/worker/deploy.test.mjs +++ b/nixos/services/id-mail/worker/deploy.test.mjs @@ -18,19 +18,41 @@ import { promisify } from "node:util"; const run = promisify(execFile); const here = dirname(fileURLToPath(import.meta.url)); -function makeSandbox() { +function makeSandbox(mutateConfig) { const dir = mkdtempSync(join(tmpdir(), "deploy-probe-")); const bin = join(dir, "bin"); mkdirSync(bin); copyFileSync(join(here, "deploy.mjs"), join(dir, "deploy.mjs")); - copyFileSync(join(here, "config.json"), join(dir, "config.json")); + // The sandbox config defaults to the committed one; a test may replace + // it wholesale (a raw string) or mutate the parsed object (#6947). + let configText = readFileSync(join(here, "config.json"), "utf8"); + if (typeof mutateConfig === "string") { + configText = mutateConfig; + } else if (typeof mutateConfig === "function") { + const mutated = JSON.parse(configText); + mutateConfig(mutated); + configText = JSON.stringify(mutated, null, 2); + } + writeFileSync(join(dir, "config.json"), configText); const log = join(dir, "calls.log"); writeFileSync(log, ""); // bws stub: logs the call, returns a stub value for every secret // deploy.mjs looks up (the account token plus each caller's tokenKey, // derived from config.json so the probe survives config edits). - const config = JSON.parse(readFileSync(join(here, "config.json"), "utf8")); - const keys = ["cloudflare.api-token-account-wide", ...Object.values(config.callers).map((caller) => caller.tokenKey)]; + // Malformed configs never reach bws, so unparseable shapes simply + // contribute no caller keys. + let keys = ["cloudflare.api-token-account-wide"]; + try { + const config = JSON.parse(configText); + keys = [ + ...keys, + ...Object.values(config.callers ?? {}) + .filter((caller) => caller && typeof caller.tokenKey === "string") + .map((caller) => caller.tokenKey), + ]; + } catch { + /* the invalid-config cases exit in validation, before any bws call */ + } const secretsJson = JSON.stringify(keys.map((key) => ({ key, value: `stub-${key}` }))); writeFileSync(join(bin, "bws"), `#!/bin/sh\necho "bws $@" >> "${log}"\nprintf '%s' '${secretsJson}'\n`); writeFileSync(join(bin, "wrangler"), `#!/bin/sh\necho "wrangler $@" >> "${log}"\nexit 0\n`); @@ -86,6 +108,72 @@ test("--bogus-flag aborts with a usage error before any external call", async () assert.equal(readFileSync(sb.log, "utf8"), "", "no bws/wrangler call may be logged"); }); +test("null and non-object config entries are validation errors, not TypeErrors (#6947)", async () => { + const cases = [ + ["null", /config must be a JSON object/], + [ + (c) => { + c.templates = [null]; + }, + /templates\[0\] must be an object/, + ], + [ + (c) => { + c.callers.amd = null; + }, + /callers\.amd must be an object/, + ], + [ + (c) => { + c.callers.amd.transports.email = null; + }, + /callers\.amd\.transports\.email must be an object/, + ], + [ + (c) => { + c.transports = "email"; + }, + /transports must be a non-empty list/, + ], + [ + (c) => { + c.transports = 5; + }, + /transports must be a non-empty list/, + ], + [ + (c) => { + c.templates = 5; + }, + /templates must be a list/, + ], + ]; + for (const [mutate, pattern] of cases) { + const sb = makeSandbox(mutate); + const result = await runDeploy(sb, ["--dry-run"]); + assert.notEqual(result.code, 0); + assert.match(result.stderr, /config\.json is invalid:/); + assert.match(result.stderr, pattern); + assert.doesNotMatch(result.stderr, /TypeError/); + assert.equal(readFileSync(sb.log, "utf8"), "", "no bws/wrangler call may be logged"); + } +}); + +test("a config without retire validates and deploys fully against the stubs (#6947)", async () => { + const sb = makeSandbox((c) => { + delete c.retire; + }); + const dry = await runDeploy(sb, ["--dry-run"]); + assert.equal(dry.code, 0, dry.stderr); + assert.match(dry.stdout, /nothing deployed/); + const result = await runDeploy(sb, []); + assert.equal(result.code, 0, result.stderr); + assert.match(result.stdout, /deployed mail with callers/); + const calls = readFileSync(sb.log, "utf8"); + assert.match(calls, /wrangler deploy --config/); + assert.match(calls, /secret put/); +}); + test("no args runs the real deploy sequence against the stubs", async () => { const sb = makeSandbox(); const result = await runDeploy(sb, []);