Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): let the project .env satisfy the config save guard

Yudhi Armyndharis9df72e935160
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 31754bc9f..75d295690 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -1047,6 +1047,32 @@ describe('InfraConfigController.saveConfig built-in/external mode flips and the
);
expect(env).toContain('DATABASE_HOST=db.example.com');
});
+
+ it('honors a value from the project .env, which also outranks the saved file at boot', () => {
+ // load-env takes the host snapshot before ./.env loads, but ./.env still loads ahead of
+ // data/.env.generated with override:false, so its password is the one the next boot sees.
+ recordOsEnvKeys({});
+ recordPinnedEnvKeys({ DATABASE_PASSWORD: 'Sup3rSecret!' });
+ process.env.DATABASE_PASSWORD = 'Sup3rSecret!';
+ try {
+ const env = written(
+ {
+ database: {
+ type: 'postgres',
+ builtIn: false,
+ host: 'db.example.com',
+ username: 'app',
+ database: 'appdb',
+ password: '',
+ },
+ },
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=db.example.com\n',
+ );
+ expect(env).toContain('DATABASE_HOST=db.example.com');
+ } finally {
+ recordPinnedEnvKeys({});
+ }
+ });
});
});
});
diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.ts
index 513364bee..e93bbd3f7 100644
--- a/src/modules/infra/infra-config.controller.ts
+++ b/src/modules/infra/infra-config.controller.ts
@@ -306,14 +306,16 @@ export class InfraConfigController {
// refused on EVERY save even though its boot passes the guard. A blank compose-forwarded
// value counts as unset exactly like clearBlankEnv treats it at boot.
//
- // Only a HOST-supplied key may win. load-env also merges .env and data/.env.generated into
- // process.env, so reading process.env alone would hand back the very file this save is
- // replacing — the guard would then bless a flip by validating the OLD config (a built-in ->
- // external switch keeping the bundled 'openwa' password would save cleanly and crash-loop the
- // next production boot, the exact case this guard exists for). isOsProvidedEnv separates the
- // two using the snapshot load-env takes before either file is loaded.
+ // Only a key from a layer ABOVE the file may win: the host, or the project .env, which load-env
+ // also loads ahead of data/.env.generated with override:false. load-env merges the generated
+ // file into process.env too, so reading process.env alone would hand back the very file this
+ // save is replacing: the guard would then bless a flip by validating the OLD config (a
+ // built-in -> external switch keeping the bundled 'openwa' password would save cleanly and
+ // crash-loop the next production boot, the exact case this guard exists for). isEnvPinned's
+ // snapshot is taken before that file loads; isOsProvidedEnv keeps the no-snapshot default of
+ // assuming an override.
const bootValue = (key: string): string | undefined => {
- const envValue = isOsProvidedEnv(key) ? process.env[key] : undefined;
+ const envValue = isEnvPinned(key) || isOsProvidedEnv(key) ? process.env[key] : undefined;
if (envValue !== undefined && (envValue.trim() !== '' || !BLANK_SHADOWED_ENV_KEYS.includes(key))) {
return envValue;
}