Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(auth): trim API_MASTER_KEY before seeding the admin key
Yudhi Armyndharis15c8eccd4c62
diff --git a/docs/04-security-design.md b/docs/04-security-design.mdindex d04caeff0..7b565d074 100644--- a/docs/04-security-design.md+++ b/docs/04-security-design.md@@ -66,7 +66,7 @@ Storage: SHA-256 hash only (never store plain key); `keyPrefix` keeps the first```Every key minted through the API-keys endpoints uses that format. The bootstrap seed key is the only-exception: an explicit `API_MASTER_KEY` is taken verbatim, and `ALLOW_DEV_API_KEY=true` opts into the+exception: an explicit `API_MASTER_KEY` is taken verbatim (surrounding whitespace is stripped), and `ALLOW_DEV_API_KEY=true` opts into thefixed `dev-admin-key`; with neither set, the seed key is generated in the format above.### Permission Modeldiff --git a/src/modules/auth/auth.service.spec.ts b/src/modules/auth/auth.service.spec.tsindex 378782646..6bd5768f4 100644--- a/src/modules/auth/auth.service.spec.ts+++ b/src/modules/auth/auth.service.spec.ts@@ -53,6 +53,18 @@ describe('resolveSeedApiKey (first-boot default admin key)', () => {expect(resolveSeedApiKey()).toBe('my-explicit-master-key');});+ // validateApiKey hashes the trimmed key, so a seed hashed with a trailing newline (a Kubernetes secret+ // made --from-file) could never authenticate, and the non-empty table meant it was never reseeded.+ it('trims surrounding whitespace from API_MASTER_KEY so the seeded key can authenticate', () => {+ process.env.API_MASTER_KEY = ' my-explicit-master-key\n';+ expect(resolveSeedApiKey()).toBe('my-explicit-master-key');+ });++ it('treats a whitespace-only API_MASTER_KEY as unset and generates a key', () => {+ process.env.API_MASTER_KEY = ' ';+ expect(resolveSeedApiKey()).toMatch(/^owa_k1_[a-f0-9]{64}$/);+ });+it('generates a random owa_k1_ key by default (no opt-in)', () => {expect(resolveSeedApiKey()).toMatch(/^owa_k1_[a-f0-9]{64}$/);});diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.tsindex 8af5e0784..32a42bec3 100644--- a/src/modules/auth/auth.service.ts+++ b/src/modules/auth/auth.service.ts@@ -29,8 +29,11 @@ import { EventsGateway, type ApiKeyEvictionReason } from '../events/events.gatew* a developer explicitly opts in with `ALLOW_DEV_API_KEY=true`, never by default.*/export function resolveSeedApiKey(): string {- if (process.env.API_MASTER_KEY) {- return process.env.API_MASTER_KEY;+ // Trimmed because validateApiKey hashes the trimmed key: a seed hashed with a trailing newline could+ // never authenticate. A whitespace-only value counts as unset.+ const masterKey = process.env.API_MASTER_KEY?.trim();+ if (masterKey) {+ return masterKey;}if (process.env.ALLOW_DEV_API_KEY === 'true') {return 'dev-admin-key';