Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(security): refuse a short API_MASTER_KEY at production boot

Yudhi Armyndharis12dc4f9ee41b
diff --git a/.env.example b/.env.example
index c0982e180..3183430f9 100644
--- a/.env.example
+++ b/.env.example
@@ -684,7 +684,8 @@ PLUGINS_DIR=./data/plugins # Plugin directory (default: ./data/plugins)
# =============================================================================
# SECURITY
# =============================================================================
-# Master API key (leave empty to disable, or set to secure value)
+# Master API key (leave empty to disable, or set to secure value). In production the
+# boot refuses a value shorter than 32 characters or one that is a known default.
# API_MASTER_KEY=
# First-boot default admin key. By default a cryptographically random key is
diff --git a/src/config/bootstrap-security.spec.ts b/src/config/bootstrap-security.spec.ts
index b076439b4..e5b9681eb 100644
--- a/src/config/bootstrap-security.spec.ts
+++ b/src/config/bootstrap-security.spec.ts
@@ -303,6 +303,39 @@ describe('assertNoDefaultSecretsInProduction', () => {
);
});
+ it('refuses prod with an API_MASTER_KEY below the 32-character floor, however unique it looks', () => {
+ expect(() =>
+ assertNoDefaultSecretsInProduction({ nodeEnv: 'production', apiMasterKey: 'short-but-unique' }),
+ ).toThrow(/API_MASTER_KEY/);
+ // One character under the floor still fails; the floor itself passes.
+ expect(() => assertNoDefaultSecretsInProduction({ nodeEnv: 'production', apiMasterKey: 'k'.repeat(31) })).toThrow(
+ /API_MASTER_KEY \(below the 32-character minimum\)/,
+ );
+ expect(() =>
+ assertNoDefaultSecretsInProduction({ nodeEnv: 'production', apiMasterKey: 'k'.repeat(32) }),
+ ).not.toThrow();
+ });
+
+ it('measures the master key trimmed — surrounding whitespace is not key material', () => {
+ // validateApiKey trims before hashing, so a padded 31-char key authenticates as 31 chars.
+ expect(() =>
+ assertNoDefaultSecretsInProduction({ nodeEnv: 'production', apiMasterKey: ` ${'k'.repeat(31)} ` }),
+ ).toThrow(/API_MASTER_KEY/);
+ });
+
+ it('does not length-check API_MASTER_KEY outside production (dev keeps its short keys)', () => {
+ for (const nodeEnv of ['development', 'test', undefined]) {
+ expect(() => assertNoDefaultSecretsInProduction({ nodeEnv, apiMasterKey: 'short' })).not.toThrow();
+ }
+ });
+
+ it('allows prod with a strong generated-shape API_MASTER_KEY', () => {
+ // The shape the first-boot generator emits: `owa_k1_` + 32 bytes hex (71 chars).
+ expect(() =>
+ assertNoDefaultSecretsInProduction({ nodeEnv: 'production', apiMasterKey: `owa_k1_${'ab'.repeat(32)}` }),
+ ).not.toThrow();
+ });
+
it('allows ALLOW_DEV_API_KEY=true outside production (the dev opt-in still works)', () => {
expect(() => assertNoDefaultSecretsInProduction({ nodeEnv: 'development', allowDevApiKey: 'true' })).not.toThrow();
});
@@ -378,7 +411,10 @@ describe('assertNoDefaultSecretsInProduction', () => {
}),
).not.toThrow();
expect(() =>
- assertNoDefaultSecretsInProduction({ nodeEnv: 'production', apiMasterKey: 'root-pw-8821x' }),
+ assertNoDefaultSecretsInProduction({
+ nodeEnv: 'production',
+ apiMasterKey: 'root-pw-8821x-and-the-rest-of-entropy',
+ }),
).not.toThrow();
});
});
diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.ts
index 973c6cf06..23b735e0c 100644
--- a/src/config/bootstrap-security.ts
+++ b/src/config/bootstrap-security.ts
@@ -100,6 +100,13 @@ export function resolveBodyLimit(bodySizeEnv?: string): string {
return trimmed && BODY_LIMIT_PATTERN.test(trimmed) ? trimmed : '25mb';
}
+/**
+ * Minimum length for an explicit API_MASTER_KEY in production. A static master key is the seed of an
+ * ADMIN credential, so below this it is brute-forceable however unique it looks. The first-boot
+ * generator emits 71 chars (`owa_k1_` + 32 bytes hex), so a generated key always passes.
+ */
+const MIN_PROD_MASTER_KEY_LENGTH = 32;
+
/** Known weak/default/placeholder secret values that must never reach production. */
const FORBIDDEN_PROD_SECRETS = new Set([
'openwa',
@@ -173,8 +180,9 @@ export interface SecretCheckEnv {
}
/**
- * Refuse to boot in production when a required secret is empty or a known default/
- * placeholder. Only secrets actually in use are checked: the DB password
+ * Refuse to boot in production when a required secret is empty, a known default/
+ * placeholder, or — for an explicit API_MASTER_KEY — below the length floor.
+ * Only secrets actually in use are checked: the DB password
* when DATABASE_TYPE=postgres, the S3 keys when STORAGE_TYPE=s3, and API_MASTER_KEY
* whenever it is set. Throws with the offending var names so the operator can fix them.
*/
@@ -215,9 +223,16 @@ export function assertNoDefaultSecretsInProduction(env: SecretCheckEnv): void {
if (isWeak(env.s3AccessKey)) problems.push('S3_ACCESS_KEY');
if (isWeak(env.s3SecretKey)) problems.push('S3_SECRET_KEY');
}
- // API_MASTER_KEY is optional, but if provided it must not be a known default.
- if (env.apiMasterKey && FORBIDDEN_PROD_SECRETS.has(env.apiMasterKey.trim().toLowerCase())) {
- problems.push('API_MASTER_KEY');
+ // API_MASTER_KEY is optional, but if provided it must neither be a known default nor fall below
+ // the length floor — both are refused the same way, like the other weak secrets above. Unset stays
+ // allowed: first boot then generates a strong random key (resolveSeedApiKey).
+ if (env.apiMasterKey) {
+ const masterKey = env.apiMasterKey.trim();
+ if (FORBIDDEN_PROD_SECRETS.has(masterKey.toLowerCase())) {
+ problems.push('API_MASTER_KEY');
+ } else if (masterKey.length < MIN_PROD_MASTER_KEY_LENGTH) {
+ problems.push(`API_MASTER_KEY (below the ${MIN_PROD_MASTER_KEY_LENGTH}-character minimum)`);
+ }
}
// Redis auth is optional (passwordless private-network Redis is a supported deployment), so unlike
// DATABASE_PASSWORD this rejects only a known placeholder VALUE — never an empty/unset password.