Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): refuse TLS or a named user beside the built-in Redis
Yudhi Armyndharisfc0b67222bfc
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.tsindex 8ef4be316..11d058839 100644--- a/src/config/env.validation.spec.ts+++ b/src/config/env.validation.spec.ts@@ -852,3 +852,54 @@ describe('validateEnv', () => {expect(() => validateEnv({ WEBHOOK_RETRY_DELAY: '268435455' })).not.toThrow();});});++// The bundled Redis container runs a plaintext listener with only its default user. A connection that+// asks for TLS, or authenticates as a named user, fails on every attempt, as the bundled PostgreSQL does+// with DATABASE_SSL=true.+describe('validateEnv built-in Redis connection settings', () => {+ const builtin = { REDIS_ENABLED: 'true', REDIS_BUILTIN: 'true', REDIS_HOST: 'redis' };++ describe('REDIS_TLS', () => {+ it.each(['redis', 'REDIS.', 'openwa-redis'])('rejects TLS against the bundled host %s', host => {+ expect(() => validateEnv({ ...builtin, REDIS_HOST: host, REDIS_TLS: 'true' })).toThrow(/REDIS_TLS/);+ });++ it.each(['QUEUE_ENABLED', 'CACHE_ENABLED'])('rejects it when only %s uses Redis', consumer => {+ expect(() => validateEnv({ ...builtin, REDIS_ENABLED: 'false', [consumer]: 'true', REDIS_TLS: 'true' })).toThrow(+ /REDIS_TLS/,+ );+ });++ it('keeps TLS to an external host, and accepts plaintext or a Redis nothing uses', () => {+ expect(() => validateEnv({ ...builtin, REDIS_HOST: 'cache.example.com', REDIS_TLS: 'true' })).not.toThrow();+ expect(() => validateEnv({ ...builtin, REDIS_TLS: 'false' })).not.toThrow();+ expect(() => validateEnv({ ...builtin, REDIS_ENABLED: 'false', REDIS_TLS: 'true' })).not.toThrow();+ expect(() => validateEnv({ ...builtin, REDIS_BUILTIN: 'false', REDIS_TLS: 'true' })).not.toThrow();+ });+ });++ describe('REDIS_USERNAME', () => {+ it.each(['REDIS_ENABLED', 'QUEUE_ENABLED', 'CACHE_ENABLED'])('rejects a named user for %s', consumer => {+ expect(() =>+ validateEnv({ ...builtin, REDIS_ENABLED: 'false', [consumer]: 'true', REDIS_USERNAME: 'worker' }),+ ).toThrow(/REDIS_USERNAME/);+ });++ // The username is passed to the server as written, so only the exact word `default` names the default user.+ it.each(['DEFAULT', 'default ', ' '])('does not normalize the user name %j', username => {+ expect(() => validateEnv({ ...builtin, REDIS_USERNAME: username })).toThrow(/REDIS_USERNAME/);+ });++ it.each([undefined, '', 'default'])('accepts the default user as %j', username => {+ expect(() => validateEnv({ ...builtin, REDIS_USERNAME: username })).not.toThrow();+ });++ it('keeps a named user for an external host, and accepts one when Redis is not the bundled one in use', () => {+ expect(() =>+ validateEnv({ ...builtin, REDIS_HOST: 'cache.example.com', REDIS_USERNAME: 'worker' }),+ ).not.toThrow();+ expect(() => validateEnv({ ...builtin, REDIS_BUILTIN: 'false', REDIS_USERNAME: 'worker' })).not.toThrow();+ expect(() => validateEnv({ ...builtin, REDIS_ENABLED: 'false', REDIS_USERNAME: 'worker' })).not.toThrow();+ });+ });+});diff --git a/src/config/env.validation.ts b/src/config/env.validation.tsindex 3a42a5872..7b5b6f16c 100644--- a/src/config/env.validation.ts+++ b/src/config/env.validation.ts@@ -60,6 +60,37 @@ export function postgresSchemaError(schema: string): string | null {return null;}+const REDIS_CONSUMER_FLAGS = ['REDIS_ENABLED', 'QUEUE_ENABLED', 'CACHE_ENABLED'];++/** The bundled Redis is the one this deployment connects to: it is selected, in use, and named as the host. */+function usesBundledRedis(config: EnvConfig): boolean {+ const host = typeof config.REDIS_HOST === 'string' ? config.REDIS_HOST : undefined;+ return (+ REDIS_CONSUMER_FLAGS.some(key => config[key] === 'true') &&+ config.REDIS_BUILTIN === 'true' &&+ namesBundledContainer('redis', host)+ );+}++/** The bundled Redis listens in plaintext; a TLS handshake against it fails on every connect. */+export function builtinRedisTlsError(config: EnvConfig): string | null {+ if (!usesBundledRedis(config) || config.REDIS_TLS !== 'true') return null;+ return (+ 'REDIS_TLS=true cannot reach the built-in Redis container, which runs without TLS; ' ++ 'set REDIS_TLS=false where it is configured'+ );+}++/** The bundled Redis has only its default user; ioredis authenticates as any other name and is refused. */+export function builtinRedisUsernameError(config: EnvConfig): string | null {+ const username = typeof config.REDIS_USERNAME === 'string' ? config.REDIS_USERNAME : undefined;+ if (!usesBundledRedis(config) || !username || username === 'default') return null;+ return (+ 'REDIS_USERNAME must be unset or default for the built-in Redis container; ' ++ 'remove the named username where it is configured'+ );+}+/*** Fail-fast environment validation. Wired as ConfigModule's `validate`* callback so a misconfigured deployment is rejected at BOOT instead of silently@@ -214,6 +245,11 @@ export function validateEnv(config: EnvConfig): EnvConfig {}}+ const redisTlsError = builtinRedisTlsError(config);+ if (redisTlsError) errors.push(redisTlsError);+ const redisUsernameError = builtinRedisUsernameError(config);+ if (redisUsernameError) errors.push(redisUsernameError);+// Plain decimal digits only: these numeric knobs are read downstream with parseInt(raw, 10),// which silently truncates spellings Number() would accept (`1e6` → 1, `0x100` → 0) — the boot// would validate one value and then configure another. Requiring digits keeps the validated