Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): read a blank project .env value as blank, like boot

Yudhi Armyndharis00f92143f8e1
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 75d295690..764858d64 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -1073,6 +1073,34 @@ describe('InfraConfigController.saveConfig built-in/external mode flips and the
recordPinnedEnvKeys({});
}
});
+
+ it('a blank project .env line keeps the key blank at boot, so fresh S3 keys cannot satisfy the guard', () => {
+ // clearBlankEnv runs on the host env before either snapshot, so a pinned blank can only be a
+ // `KEY=` line in ./.env. dotenv keeps it, data/.env.generated cannot fill it, and the next
+ // production boot refuses the empty credentials: the save must be refused the same way.
+ recordOsEnvKeys({});
+ recordPinnedEnvKeys({ S3_ACCESS_KEY_ID: '', S3_SECRET_ACCESS_KEY: '' });
+ process.env.S3_ACCESS_KEY_ID = '';
+ process.env.S3_SECRET_ACCESS_KEY = '';
+ try {
+ expectRejected(
+ {
+ storage: {
+ type: 's3',
+ builtIn: false,
+ s3Bucket: 'b',
+ s3AccessKey: 'AKIAEXAMPLESTRONG1',
+ s3SecretKey: 'Sup3rSecretS3Key!',
+ s3Endpoint: 'https://s3.example.com',
+ },
+ },
+ 'STORAGE_TYPE=local\n',
+ /S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY/,
+ );
+ } finally {
+ recordPinnedEnvKeys({});
+ }
+ });
});
});
});
@@ -1216,15 +1244,16 @@ describe('InfraConfigController.getConfig reflects environment-pinned values (#1
(fs.readFileSync as jest.Mock).mockReturnValue('');
});
- it('a blank pinned forward counts as unset, so the file value applies', () => {
- // Compose renders `- KEY=${KEY:-}` as an empty value when the operator set nothing; boot's
- // clearBlankEnv treats it as unset, and the read must agree or the blank would shadow the file.
+ it('a blank pinned value hides the file value, as it does at boot', () => {
+ // clearBlankEnv runs on the host env before the snapshot, so a pinned blank can only be an
+ // `ENGINE_TYPE=` line in ./.env. dotenv keeps it and data/.env.generated cannot fill it, so the
+ // runtime falls back to the default engine and the form must report that, not the file value.
recordPinnedEnvKeys({ ENGINE_TYPE: '' });
process.env.ENGINE_TYPE = '';
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue('ENGINE_TYPE=baileys\n');
- expect(newController().getConfig().engine.type).toBe('baileys');
+ expect(newController().getConfig().engine.type).toBe('whatsapp-web.js');
(fs.existsSync as jest.Mock).mockReturnValue(false);
(fs.readFileSync as jest.Mock).mockReturnValue('');
diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.ts
index 79db3734f..a83545681 100644
--- a/src/modules/infra/infra-config.controller.ts
+++ b/src/modules/infra/infra-config.controller.ts
@@ -128,17 +128,11 @@ export class InfraConfigController {
// while the process actually runs baileys/postgres (#1313). isEnvPinned's boot snapshot excludes
// file-sourced keys, so a value that only ever lived in data/.env.generated is NOT pinned and the
// freshly-saved file still wins over process.env's stale boot-time copy — keeping the
- // "saved, pending restart" form state intact until the reboot applies it (#226/#1082). The blank
- // rule is the same one the save guard's bootValue applies: a blank counts as unset only for the
- // blank-forwarded keys boot's clearBlankEnv clears; elsewhere the runtime reads the blank as-is
- // (configuration.ts's `=== 'true'` checks), so the read must not fall through to the file there.
- const effective = (key: string): string | undefined => {
- const envValue = isEnvPinned(key) ? process.env[key] : undefined;
- if (envValue !== undefined && (envValue.trim() !== '' || !BLANK_SHADOWED_ENV_KEYS.includes(key))) {
- return envValue;
- }
- return saved[key];
- };
+ // "saved, pending restart" form state intact until the reboot applies it (#226/#1082). A pinned
+ // blank is read as-is: boot's clearBlankEnv drops blank host forwards before the snapshot, so a
+ // blank that is still pinned is a `KEY=` line in ./.env, which the file can never fill at boot.
+ const effective = (key: string): string | undefined =>
+ (isEnvPinned(key) ? process.env[key] : undefined) ?? saved[key];
// Secrets (passwords, S3 keys) are never returned; the form shows a "set" indicator
// and an empty submission preserves the stored value (see saveConfig). This lets
@@ -309,8 +303,10 @@ export class InfraConfigController {
// loads with dotenv override:false, so a value supplied via the container environment
// (compose `environment:`) wins over this file — the precedence the file header documents.
// Without that, a deployment providing DATABASE_PASSWORD & co. through the environment is
- // 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.
+ // refused on EVERY save even though its boot passes the guard. A pinned blank is kept as-is:
+ // clearBlankEnv drops blank host forwards before either snapshot, so a blank that is still
+ // pinned is a `KEY=` line in ./.env, and boot keeps it. Only without a snapshot (a process that
+ // never ran load-env) does a blank on a blank-forwarded key count as unset, as clearBlankEnv would.
//
// 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
@@ -321,7 +317,8 @@ export class InfraConfigController {
// 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 = isEnvPinned(key) || isOsProvidedEnv(key) ? process.env[key] : undefined;
+ if (isEnvPinned(key) && process.env[key] !== undefined) return process.env[key];
+ const envValue = isOsProvidedEnv(key) ? process.env[key] : undefined;
if (envValue !== undefined && (envValue.trim() !== '' || !BLANK_SHADOWED_ENV_KEYS.includes(key))) {
return envValue;
}