Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): reject a padded DATABASE_TYPE, ENGINE_TYPE or STORAGE_TYPE
Yudhi Armyndhariscc60a91baba3
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.tsindex e2256faa0..c88d19faa 100644--- a/src/config/env.validation.spec.ts+++ b/src/config/env.validation.spec.ts@@ -97,6 +97,22 @@ describe('validateEnv', () => {expect(() => validateEnv({ STORAGE_TYPE: 's3' })).not.toThrow();});+ // The runtime compares these raw (`=== 'postgres'`, the engine plugin lookup, `=== 's3'`), so a+ // padded value that only matches after trimming would validate here and then take the default branch.+ it.each([+ ['DATABASE_TYPE', 'postgres '],+ ['DATABASE_TYPE', 'sqlite\r'],+ ['ENGINE_TYPE', 'baileys '],+ ['STORAGE_TYPE', ' s3'],+ ])('rejects a padded %s %j instead of validating the trimmed value', (key, value) => {+ const pg = { DATABASE_HOST: 'db', DATABASE_USERNAME: 'u', DATABASE_PASSWORD: 'p' };+ expect(() => validateEnv({ ...pg, [key]: value })).toThrow(new RegExp(`${key} must be`));+ });++ it('still treats a whitespace-only enum (a blank compose forward) as unset', () => {+ expect(() => validateEnv({ DATABASE_TYPE: ' ', ENGINE_TYPE: '', STORAGE_TYPE: ' ' })).not.toThrow();+ });+// Every production hardening in the repo compares NODE_ENV against the exact string 'production',// so an unrecognised value silently selects the permissive branch of each one — including the// ALLOW_DEV_API_KEY rejection that stops the public `dev-admin-key` being seeded as an ADMINdiff --git a/src/config/env.validation.ts b/src/config/env.validation.tsindex 0993658a3..9944f6a54 100644--- a/src/config/env.validation.ts+++ b/src/config/env.validation.ts@@ -72,18 +72,26 @@ export function validateEnv(config: EnvConfig): EnvConfig {return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined;};- const dbType = str('DATABASE_TYPE');+ // The engine/storage/database selectors are checked RAW, like NODE_ENV below: every reader compares+ // process.env verbatim, so a padded 'postgres ' that only matched after trimming validated clean and+ // then booted SQLite. Whitespace-only still means unset, as a blank compose forward does everywhere.+ const rawEnum = (key: string): string | undefined => {+ const value = config[key];+ return typeof value === 'string' && value.trim() !== '' ? value : undefined;+ };++ const dbType = rawEnum('DATABASE_TYPE');if (dbType && dbType !== 'sqlite' && dbType !== 'postgres') {- errors.push(`DATABASE_TYPE must be "sqlite" or "postgres" (got "${dbType}")`);+ errors.push(`DATABASE_TYPE must be "sqlite" or "postgres" (got ${JSON.stringify(dbType)})`);}// Whitelist the registered engine/storage ids so a typo fails fast at boot instead of silently// falling back to the default (engine.factory swallows an unknown ENGINE_TYPE → legacy wwebjs;// STORAGE_TYPE → local). Values must match the ids registered in engine.factory / configuration.const checkEnum = (key: string, allowed: string[]): void => {- const value = str(key);+ const value = rawEnum(key);if (value !== undefined && !allowed.includes(value)) {- errors.push(`${key} must be one of ${allowed.map(v => `"${v}"`).join(', ')} (got "${value}")`);+ errors.push(`${key} must be one of ${allowed.map(v => `"${v}"`).join(', ')} (got ${JSON.stringify(value)})`);}};checkEnum('ENGINE_TYPE', ['whatsapp-web.js', 'baileys']);