Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): keep a database password the bundled host already uses

Yudhi Armyndharis5cd15889cabc
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index d14cc4414..e138c5437 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -22,6 +22,11 @@ function setSecret(updates: Record<string, string>, key: string, value: string |
if (value) updates[key] = value;
}
+// The host the next boot dials before this save changes it: one the environment pins, else the stored one.
+function nextBootHost(key: string, existing: Record<string, string>): string | undefined {
+ return isEnvPinned(key) ? process.env[key] : existing[key];
+}
+
// What the next boot's validation (env.validation.ts) reads from these keys, refused here instead: the saved
// file is loaded at boot, and a value it rejects crash-loops the instance and the dashboard that could undo it.
function portValue(label: string, value: string): string {
@@ -84,9 +89,14 @@ export function applyDatabaseSection(
// dashboard ALWAYS sends builtIn, so keying the reset on "explicit builtIn:true" reset a
// re-keyed container on every save from the Infrastructure page.
// What decides it is whether the stored password belongs to this same bundled container:
- // it does when the previous mode was already built-in. Coming from external, the stored
- // value is the external DB's credential and must not be carried into the container.
- const storedPassword = existing.POSTGRES_BUILTIN === 'true' ? existing.DATABASE_PASSWORD : undefined;
+ // it does when the previous mode was already built-in, or when the next boot already dials the
+ // bundled host with it (the environment's host, else the file's, a file that names it without the
+ // flag included). Coming from external, the stored value is the external DB's credential and must
+ // not be carried into the container.
+ const storedPassword =
+ existing.POSTGRES_BUILTIN === 'true' || nextBootHost('DATABASE_HOST', existing) === 'postgres'
+ ? existing.DATABASE_PASSWORD
+ : undefined;
updates.DATABASE_PASSWORD = database.password || storedPassword || 'openwa';
updates.DATABASE_NAME = 'openwa';
// Built-in Postgres is initialized with the default 'public' schema (see
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 7dc21117c..734ebc7a1 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -1324,6 +1324,49 @@ describe('InfraConfigController.saveConfig built-in/external mode flips and the
expect(env).not.toContain('ExternalPw!');
});
+ // The stored password belongs to the bundled container whenever the next boot already dials it with that password,
+ // whatever the flag says: an older or hand-edited file names the bundled host without the flag, and a pinned host
+ // outranks the file's.
+ describe('built-in mode over a stored password the next boot already dials the bundled host with', () => {
+ afterEach(() => {
+ delete process.env.DATABASE_HOST;
+ delete process.env.DATABASE_PASSWORD;
+ recordPinnedEnvKeys({});
+ });
+
+ it('keeps it when the file names the bundled host without the flag', () => {
+ const env = written(
+ { database: { type: 'postgres', builtIn: true } },
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=postgres\nDATABASE_PASSWORD=CustomPw123!\nDATABASE_NAME=openwa\n',
+ );
+ expect(env).toContain('POSTGRES_BUILTIN=true');
+ expect(env).toContain('DATABASE_PASSWORD=CustomPw123!');
+ });
+
+ it('keeps it when the environment pins the bundled host over another host in the file', () => {
+ process.env.DATABASE_HOST = 'postgres';
+ recordPinnedEnvKeys({ DATABASE_HOST: 'postgres' });
+ const env = written(
+ { database: { type: 'postgres', builtIn: true } },
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=db.old.example\nDATABASE_PASSWORD=CustomPw123!\n',
+ );
+ expect(env).toContain('DATABASE_PASSWORD=CustomPw123!');
+ });
+
+ // The pinned password keeps that boot past the default-secret guard; the file's is what the rule decides.
+ it('still resets it when the environment pins another host over the bundled host in the file', () => {
+ process.env.DATABASE_HOST = 'db.example.com';
+ process.env.DATABASE_PASSWORD = 'PinnedPw456!';
+ recordPinnedEnvKeys({ DATABASE_HOST: 'db.example.com', DATABASE_PASSWORD: 'PinnedPw456!' });
+ const env = written(
+ { database: { type: 'postgres', builtIn: true } },
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=postgres\nDATABASE_PASSWORD=ExternalPw!\n',
+ );
+ expect(env).toContain('DATABASE_PASSWORD=openwa');
+ expect(env).not.toContain('ExternalPw!');
+ });
+ });
+
// The guard must evaluate what the next boot would SEE: load-env.ts loads with dotenv
// override:false, so a value in the container environment (compose `environment:`) wins over the
// saved file. Snapshot + clear every guard-relevant key so these tests are hermetic.