Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): clear Redis TLS and username for the built-in Redis

Yudhi Armyndharis0f0c689d8337
diff --git a/src/modules/infra/config-sections.spec.ts b/src/modules/infra/config-sections.spec.ts
index 6c21ee563..931d93ca2 100644
--- a/src/modules/infra/config-sections.spec.ts
+++ b/src/modules/infra/config-sections.spec.ts
@@ -160,6 +160,7 @@ it.each(cases)(
});
else if (!(builtIn === undefined && pin === 'external')) {
Object.assign(expected, { REDIS_HOST: 'redis', REDIS_PORT: '6379' });
+ if (pin !== 'external') expected.REDIS_TLS = 'false';
delete expected.REDIS_PASSWORD;
}
} else {
@@ -376,3 +377,38 @@ it.each(['true', 'false'])('keeps external Redis credentials independently of st
expect(savedValues(existing, ctx).REDIS_PASSWORD).toBe('StoredRedis123456');
});
+
+describe('Redis username ownership', () => {
+ const existing = {
+ REDIS_ENABLED: 'true',
+ REDIS_BUILTIN: 'true',
+ REDIS_HOST: 'redis',
+ REDIS_PORT: '6381',
+ REDIS_USERNAME: 'worker',
+ REDIS_PASSWORD: 'DummyRedisPassword123456',
+ REDIS_TLS: 'false',
+ };
+
+ it.each([true, undefined])('clears a generated username for the bundled preset %s', builtIn => {
+ const ctx = context();
+ applyRedisSection({ enabled: true, builtIn, password: '' }, existing, ctx);
+ expect(savedValues(existing, ctx).REDIS_USERNAME).toBeUndefined();
+ });
+
+ it('preserves the explicit preset tuple beside an external host pin', () => {
+ process.env.REDIS_HOST = 'cache.external.invalid';
+ recordPinnedEnvKeys({ REDIS_HOST: process.env.REDIS_HOST });
+ const ctx = context();
+ applyRedisSection({ enabled: true, builtIn: true, password: '' }, existing, ctx);
+ const saved = savedValues(existing, ctx);
+ expect(saved.REDIS_USERNAME).toBe('worker');
+ expect(saved.REDIS_PORT).toBe('6379');
+ expect(saved.REDIS_PASSWORD).toBeUndefined();
+ });
+
+ it('keeps a named external username when the choice stays external', () => {
+ const ctx = context();
+ applyRedisSection({ enabled: true, builtIn: false, host: 'cache.external.invalid', password: '' }, existing, ctx);
+ expect(savedValues(existing, ctx).REDIS_USERNAME).toBe('worker');
+ });
+});
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index 0cabf22b7..3e6f66b27 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -203,6 +203,12 @@ export function applyRedisSection(
// would make the client AUTH against a passwordless server on the next boot.
updates.REDIS_HOST = 'redis';
updates.REDIS_PORT = '6379';
+ // It also listens in plaintext and defines no ACL user, so TLS and a username kept from an external
+ // server would fail the connection. Beside a host pinned elsewhere the stored values stay with that server.
+ if (!hostPinnedElsewhere('redis')) {
+ updates.REDIS_TLS = 'false';
+ staleKeys.add('REDIS_USERNAME');
+ }
if (!redis.password) staleKeys.add('REDIS_PASSWORD');
} else if (!redisBuiltIn) {
// External Redis (explicit, or inherited when builtIn is absent)
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index ed2f277b6..39f4d3e18 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -43,6 +43,7 @@ import { AuditAction } from '../audit/entities/audit-log.entity';
import { recordOsEnvKeys, recordPinnedEnvKeys } from '../../config/env-precedence';
import { resolveStorageRoot } from '../../config/storage-root';
import { validateEnv } from '../../config/env.validation';
+import { redisConnectionOptions } from '../../config/redis-options';
import {
EXPECTED_MODE,
EXPECTED_MODE_FLAG_PINNED,
@@ -729,6 +730,7 @@ describe('InfraConfigController.saveConfig byte-identical regression (full dashb
REDIS_ENABLED: 'true',
REDIS_HOST: 'redis',
REDIS_PORT: '6379',
+ REDIS_TLS: 'false',
S3_ACCESS_KEY_ID: 'minioadmin',
S3_BUCKET: 'openwa',
S3_ENDPOINT: 'http://minio:9000',
@@ -3002,3 +3004,152 @@ describe('InfraConfigController audit trail (light-dependency handlers)', () =>
expect(calls[0][1].metadata.profiles).toEqual(['postgres']);
});
});
+
+describe('InfraConfigController built-in Redis transport', () => {
+ const external =
+ 'DATABASE_TYPE=sqlite\nSTORAGE_TYPE=local\nREDIS_ENABLED=true\nREDIS_BUILTIN=false\n' +
+ 'REDIS_HOST=cache.external.invalid\nREDIS_PORT=6379\nREDIS_TLS=true\n';
+ const keys = ['REDIS_TLS', 'REDIS_HOST', 'REDIS_ENABLED', 'REDIS_BUILTIN', 'QUEUE_ENABLED', 'CACHE_ENABLED'];
+ let saved: Record<string, string | undefined>;
+
+ beforeEach(() => {
+ saved = Object.fromEntries(keys.map(key => [key, process.env[key]]));
+ for (const key of keys) delete process.env[key];
+ recordOsEnvKeys({});
+ recordPinnedEnvKeys({});
+ (fs.existsSync as jest.Mock).mockReturnValue(true);
+ (fs.readFileSync as jest.Mock).mockReturnValue(external);
+ (fs.writeFileSync as jest.Mock).mockClear();
+ });
+ afterEach(() => {
+ for (const key of keys) {
+ if (saved[key] === undefined) delete process.env[key];
+ else process.env[key] = saved[key];
+ }
+ recordOsEnvKeys({});
+ recordPinnedEnvKeys({});
+ (fs.existsSync as jest.Mock).mockReturnValue(false);
+ (fs.readFileSync as jest.Mock).mockReturnValue('');
+ });
+
+ function write(redis: NonNullable<SaveConfigDto['redis']>): Record<string, string> {
+ const controller = new InfraConfigController({} as never, {} as never, {} as never);
+ expect(controller.saveConfig({ redis }).saved).toBe(true);
+ const content = jest.mocked(fs.writeFileSync).mock.calls.at(-1)![1] as string;
+ return jest.requireActual<typeof import('dotenv')>('dotenv').parse(content);
+ }
+
+ it('resets saved TLS when switching to the plaintext built-in Redis', () => {
+ const env = write({ enabled: true, builtIn: true });
+ expect(env.REDIS_HOST).toBe('redis');
+ expect(env.REDIS_BUILTIN).toBe('true');
+ Object.assign(process.env, { REDIS_HOST: env.REDIS_HOST, REDIS_TLS: env.REDIS_TLS ?? '' });
+ expect(redisConnectionOptions().tls).toBeUndefined();
+ });
+
+ it('keeps TLS for an external Redis configuration', () => {
+ const env = write({ enabled: true, builtIn: false, host: 'cache.external.invalid' });
+ Object.assign(process.env, { REDIS_HOST: env.REDIS_HOST, REDIS_TLS: env.REDIS_TLS });
+ expect(redisConnectionOptions()).toMatchObject({ host: 'cache.external.invalid', tls: {} });
+ });
+
+ it('keeps TLS when an external host pin overrides a saved built-in choice', () => {
+ process.env.REDIS_HOST = 'cache.external.invalid';
+ process.env.REDIS_TLS = 'true';
+ recordPinnedEnvKeys({ REDIS_HOST: 'cache.external.invalid', REDIS_TLS: 'true' });
+ const env = write({ enabled: true, builtIn: true });
+ expect(env.REDIS_TLS).toBe('true');
+ expect(redisConnectionOptions()).toMatchObject({ host: 'cache.external.invalid', tls: {} });
+ });
+});
+
+describe('InfraConfigController built-in Redis username', () => {
+ const keys = [
+ 'REDIS_USERNAME',
+ 'REDIS_PASSWORD',
+ 'REDIS_HOST',
+ 'REDIS_PORT',
+ 'REDIS_TLS',
+ 'REDIS_ENABLED',
+ 'REDIS_BUILTIN',
+ 'QUEUE_ENABLED',
+ 'CACHE_ENABLED',
+ ];
+ let previous: Record<string, string | undefined>;
+ const stored = (values: Record<string, string> = {}) =>
+ Object.entries({
+ DATABASE_TYPE: 'sqlite',
+ STORAGE_TYPE: 'local',
+ REDIS_ENABLED: 'true',
+ REDIS_BUILTIN: 'false',
+ REDIS_HOST: 'cache.external.invalid',
+ REDIS_PORT: '6381',
+ REDIS_USERNAME: 'worker',
+ REDIS_PASSWORD: 'DummyRedisPassword123456',
+ REDIS_TLS: 'false',
+ QUEUE_ENABLED: 'false',
+ CACHE_ENABLED: 'false',
+ ...values,
+ })
+ .map(([key, value]) => `${key}=${value}`)
+ .join('\n') + '\n';
+
+ beforeEach(() => {
+ previous = Object.fromEntries(keys.map(key => [key, process.env[key]]));
+ for (const key of keys) delete process.env[key];
+ recordOsEnvKeys({});
+ recordPinnedEnvKeys({});
+ (fs.existsSync as jest.Mock).mockReturnValue(true);
+ (fs.readFileSync as jest.Mock).mockReturnValue(stored());
+ (fs.writeFileSync as jest.Mock).mockClear();
+ });
+ afterEach(() => {
+ for (const key of keys) {
+ if (previous[key] === undefined) delete process.env[key];
+ else process.env[key] = previous[key];
+ }
+ recordOsEnvKeys({});
+ recordPinnedEnvKeys({});
+ (fs.existsSync as jest.Mock).mockReturnValue(false);
+ (fs.readFileSync as jest.Mock).mockReturnValue('');
+ });
+
+ function write(redis: NonNullable<SaveConfigDto['redis']>): Record<string, string> {
+ const controller = new InfraConfigController({} as never, {} as never, {} as never);
+ expect(controller.saveConfig({ redis }).saved).toBe(true);
+ return jest
+ .requireActual<typeof import('dotenv')>('dotenv')
+ .parse(jest.mocked(fs.writeFileSync).mock.calls.at(-1)![1] as string);
+ }
+
+ it('clears an old generated username when selecting the bundled Redis', () => {
+ expect(write({ enabled: true, builtIn: true }).REDIS_USERNAME).toBeUndefined();
+ });
+
+ it.each(['host', 'project file'])('allows the default username from the %s', origin => {
+ process.env.REDIS_USERNAME = 'default';
+ recordOsEnvKeys(origin === 'host' ? { REDIS_USERNAME: 'default' } : {});
+ recordPinnedEnvKeys({ REDIS_USERNAME: 'default' });
+ expect(write({ enabled: true, builtIn: true }).REDIS_USERNAME).toBeUndefined();
+ });
+
+ it('keeps an external pinned username and the deliberate preset port/password behavior', () => {
+ process.env.REDIS_HOST = 'cache.external.invalid';
+ recordPinnedEnvKeys({ REDIS_HOST: process.env.REDIS_HOST });
+ const saved = write({ enabled: true, builtIn: true });
+ expect(saved.REDIS_USERNAME).toBe('worker');
+ expect(saved.REDIS_PORT).toBe('6379');
+ expect(saved.REDIS_PASSWORD).toBeUndefined();
+ });
+
+ it('accepts a named user for explicit external mode at the bundled host spelling', () => {
+ expect(write({ enabled: true, builtIn: false, host: 'redis' }).REDIS_USERNAME).toBe('worker');
+ });
+
+ it('permits an inactive named username pin', () => {
+ process.env.REDIS_USERNAME = 'worker';
+ recordPinnedEnvKeys({ REDIS_USERNAME: 'worker' });
+ (fs.readFileSync as jest.Mock).mockReturnValue(stored({ REDIS_ENABLED: 'false' }));
+ expect(() => write({ enabled: false, builtIn: true })).not.toThrow();
+ });
+});
diff --git a/src/modules/infra/infra-status.controller.spec.ts b/src/modules/infra/infra-status.controller.spec.ts
index d708bd889..69c796a00 100644
--- a/src/modules/infra/infra-status.controller.spec.ts
+++ b/src/modules/infra/infra-status.controller.spec.ts
@@ -318,6 +318,12 @@ describe('InfraStatusController.getStatus envPinned', () => {
expect((await buildController().getStatus()).envPinned).toEqual(['DATABASE_HOST', 'REDIS_HOST', 'S3_ENDPOINT']);
});
+ it('reports a Redis TLS pin beside the built-in selection', async () => {
+ recordPinnedEnvKeys({ REDIS_TLS: 'true' });
+
+ expect((await buildController().getStatus()).envPinned).toEqual(['REDIS_TLS']);
+ });
+
it('reports an empty list when nothing above data/.env.generated supplies a selection key', async () => {
recordPinnedEnvKeys({});
diff --git a/src/modules/infra/infra-status.controller.ts b/src/modules/infra/infra-status.controller.ts
index 33c88b299..8c17dde19 100644
--- a/src/modules/infra/infra-status.controller.ts
+++ b/src/modules/infra/infra-status.controller.ts
@@ -87,6 +87,8 @@ const DASHBOARD_PINNABLE_ENV_KEYS = [
'DATABASE_SSL_REJECT_UNAUTHORIZED',
'REDIS_ENABLED',
'REDIS_BUILTIN',
+ 'REDIS_TLS',
+ 'REDIS_USERNAME',
'REDIS_HOST',
'REDIS_PORT',
'REDIS_PASSWORD',