Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): preserve bundled TLS under host pins

Yudhi Armyndharis95e1c6bff9a9
diff --git a/src/modules/infra/config-sections.spec.ts b/src/modules/infra/config-sections.spec.ts
index 5f14a00e3..c7631f6a6 100644
--- a/src/modules/infra/config-sections.spec.ts
+++ b/src/modules/infra/config-sections.spec.ts
@@ -204,3 +204,49 @@ it.each(cases)(
expect(merged).toEqual(expected);
},
);
+
+function savedValues(existing: Record<string, string>, ctx: ConfigSectionContext): Record<string, string> {
+ const saved = { ...existing, ...ctx.updates };
+ for (const key of ctx.staleKeys) delete saved[key];
+ return saved;
+}
+
+function context(): ConfigSectionContext {
+ return { updates: {}, staleKeys: new Set(), profiles: [] };
+}
+
+it.each(['true', 'false'])(
+ 'preserves stored bundled TLS keys beneath an external host pin with startup flag %s',
+ flag => {
+ process.env.DATABASE_HOST = 'db.external.example';
+ process.env.DATABASE_SSL = 'true';
+ recordPinnedEnvKeys({ DATABASE_HOST: process.env.DATABASE_HOST, DATABASE_SSL: 'true' });
+ const existing = {
+ DATABASE_TYPE: 'postgres',
+ DATABASE_HOST: 'openwa-POSTGRES.',
+ POSTGRES_BUILTIN: flag,
+ DATABASE_PASSWORD: 'StoredPassword123456',
+ DATABASE_SSL: 'false',
+ DATABASE_SSL_REJECT_UNAUTHORIZED: 'false',
+ };
+ const ctx = context();
+
+ applyDatabaseSection({ type: 'postgres', sslEnabled: true, sslRejectUnauthorized: true }, existing, ctx);
+
+ expect(savedValues(existing, ctx)).toEqual(existing);
+ },
+);
+
+it.each([true, false])('writes explicitly chosen database TLS under an external host pin, builtIn %s', builtIn => {
+ process.env.DATABASE_HOST = 'db.pinned.example';
+ recordPinnedEnvKeys({ DATABASE_HOST: process.env.DATABASE_HOST });
+ const existing = { DATABASE_TYPE: 'postgres', DATABASE_HOST: 'postgres', DATABASE_SSL: 'false' };
+ const ctx = context();
+
+ applyDatabaseSection({ builtIn, sslEnabled: true, sslRejectUnauthorized: false }, existing, ctx);
+
+ expect(savedValues(existing, ctx)).toMatchObject({
+ DATABASE_SSL: 'true',
+ DATABASE_SSL_REJECT_UNAUTHORIZED: 'false',
+ });
+});
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index d03a39143..cb703522e 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -80,10 +80,12 @@ export function applyDatabaseSection(
// external host pin preserves the same stored service's connection details instead of applying its preset again.
const dbBuiltIn = database.builtIn ?? namesBundledContainer('postgres', existing.DATABASE_HOST);
if (type === 'postgres') {
- if (
+ const preserveBundledDetails =
dbBuiltIn &&
- (database.builtIn !== undefined || existing.DATABASE_TYPE !== 'postgres' || !hostPinnedElsewhere('postgres'))
- ) {
+ database.builtIn === undefined &&
+ existing.DATABASE_TYPE === 'postgres' &&
+ hostPinnedElsewhere('postgres');
+ if (dbBuiltIn && !preserveBundledDetails) {
// Built-in PostgreSQL - use container name as host
updates.DATABASE_HOST = 'postgres';
updates.DATABASE_PORT = '5432';
@@ -149,11 +151,12 @@ export function applyDatabaseSection(
}
// The bundled container runs without TLS (getContainerSpec sets none up), so a built-in database the next boot
// dials there connects without it: SSL kept from an external server would refuse every connection. Beside a host
- // pinned elsewhere the next boot dials that server, and the setting sent stays.
+ // pinned elsewhere an inherited bundled choice keeps its stored TLS keys with its other connection details;
+ // an explicit choice still writes the setting sent for that pinned server.
if (dbBuiltIn && !hostPinnedElsewhere('postgres')) {
updates.DATABASE_SSL = 'false';
staleKeys.add('DATABASE_SSL_REJECT_UNAUTHORIZED');
- } else if (database.sslEnabled !== undefined) {
+ } else if (!preserveBundledDetails && database.sslEnabled !== undefined) {
updates.DATABASE_SSL = database.sslEnabled ? 'true' : 'false';
if (database.sslEnabled) {
// Default to certificate verification; only relax it when the operator opts out