Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): validate NODE_ENV at boot and fail closed on an unrecognised value

Yudhi Armyndharisf7878a6262f4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c1a97c7a4..93416746e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Added
+
+- ⚠️ **Breaking (config).** `NODE_ENV` is checked at boot, so a value outside `production`, `development` and `test` fails startup with a named error instead of silently selecting the permissive branch of every production hardening — CORS, Swagger, DTO error detail, the default-secret guard and the `ALLOW_DEV_API_KEY` rejection that stops the public `dev-admin-key` being seeded as an ADMIN credential. The value is compared verbatim, so a padded ` production ` is rejected too: every reader compares the raw variable. Action required: a deployment running `NODE_ENV=staging` (or any other value) now refuses to boot where it previously started with development-grade security. Set `production`, or leave the variable unset. Unset remains legal and unchanged.
+
### Fixed
- Two enabled instances of one integration plugin that share a session scope no longer collapse onto a single config, so an ingress delivery authenticated with one instance's secret is no longer handled with the credentials of whichever instance was provisioned last. Dispatch now layers the instance's own config over the base config and any per-session override.
diff --git a/src/config/bootstrap-security.spec.ts b/src/config/bootstrap-security.spec.ts
index a14f9ac3a..c5ae5086b 100644
--- a/src/config/bootstrap-security.spec.ts
+++ b/src/config/bootstrap-security.spec.ts
@@ -147,6 +147,25 @@ describe('assertNoDefaultSecretsInProduction', () => {
).not.toThrow();
});
+ // main.ts runs this guard BEFORE NestFactory.create, i.e. before ConfigModule validates NODE_ENV,
+ // so at this point the value is still arbitrary. Recognising only 'production' made every
+ // unrecognised value skip the guard entirely; it now recognises only the values that are
+ // deliberately NOT production and treats everything else as production.
+ it('treats an unrecognised NODE_ENV as production rather than skipping the guard', () => {
+ expect(() => assertNoDefaultSecretsInProduction({ nodeEnv: 'prod', allowDevApiKey: 'true' })).toThrow(
+ /ALLOW_DEV_API_KEY/,
+ );
+ expect(() =>
+ assertNoDefaultSecretsInProduction({ nodeEnv: 'staging', databaseType: 'postgres', databasePassword: 'openwa' }),
+ ).toThrow(/DATABASE_PASSWORD/);
+ });
+
+ it('still skips the guard for the two environments that are deliberately not production', () => {
+ for (const nodeEnv of ['development', 'test', undefined]) {
+ expect(() => assertNoDefaultSecretsInProduction({ nodeEnv, allowDevApiKey: 'true' })).not.toThrow();
+ }
+ });
+
it('refuses prod with a default Postgres password', () => {
expect(() =>
assertNoDefaultSecretsInProduction({
diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.ts
index 03f606c52..bf9430a7e 100644
--- a/src/config/bootstrap-security.ts
+++ b/src/config/bootstrap-security.ts
@@ -169,7 +169,14 @@ export interface SecretCheckEnv {
* whenever it is set. Throws with the offending var names so the operator can fix them.
*/
export function assertNoDefaultSecretsInProduction(env: SecretCheckEnv): void {
- if (env.nodeEnv !== 'production') return;
+ // Deny-list, not an allow-list: main.ts calls this BEFORE NestFactory.create, so NODE_ENV has not
+ // been through boot validation yet and is still an arbitrary string here. Recognising only
+ // 'production' meant every unrecognised value — a `prod` typo, a `staging` deployment — skipped the
+ // guard silently, including the ALLOW_DEV_API_KEY rejection below. Only the two values that are
+ // deliberately not production (and an unset variable, the standard Node default for local runs)
+ // skip it; everything else is treated as production. Mirrors shutdown.service.ts's drain window,
+ // which already fails toward the production behaviour the same way.
+ if (env.nodeEnv === 'development' || env.nodeEnv === 'test' || env.nodeEnv === undefined) return;
const isWeak = (value?: string): boolean => !value || FORBIDDEN_PROD_SECRETS.has(value.trim().toLowerCase());
const problems: string[] = [];
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index 3d80e3cbe..77a08fd92 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -61,6 +61,26 @@ describe('validateEnv', () => {
expect(() => validateEnv({ STORAGE_TYPE: 's3' })).not.toThrow();
});
+ // Every production hardening in the repo compares NODE_ENV against the exact string 'production',
+ // so an unrecognised value silently selects the permissive branch of each one — including the
+ // ALLOW_DEV_API_KEY rejection that stops the public `dev-admin-key` being seeded as an ADMIN
+ // credential. A typo must fail the boot, not downgrade it.
+ it('rejects a NODE_ENV typo instead of silently selecting the permissive branch', () => {
+ expect(() => validateEnv({ NODE_ENV: 'prod' })).toThrow(/NODE_ENV/);
+ expect(() => validateEnv({ NODE_ENV: 'staging' })).toThrow(/NODE_ENV/);
+ expect(() => validateEnv({ NODE_ENV: 'Production' })).toThrow(/NODE_ENV/);
+ // Padded values must fail too: every reader compares the RAW `process.env.NODE_ENV` against
+ // 'production', so a value that only matches after trimming validates clean and then selects the
+ // permissive branch anyway — the exact silent downgrade this check exists to stop.
+ expect(() => validateEnv({ NODE_ENV: ' production ' })).toThrow(/NODE_ENV/);
+ expect(() => validateEnv({ NODE_ENV: 'production' })).not.toThrow();
+ expect(() => validateEnv({ NODE_ENV: 'development' })).not.toThrow();
+ expect(() => validateEnv({ NODE_ENV: 'test' })).not.toThrow();
+ // Unset stays legal: it is the standard Node default and every shipped deployment sets it
+ // explicitly (Dockerfile, both compose files, the Helm chart, .env.example).
+ expect(() => validateEnv({})).not.toThrow();
+ });
+
it('rejects a non-integer rate-limit / webhook / pool-size / redis-timeout / session-cap value', () => {
expect(() => validateEnv({ RATE_LIMIT_SHORT_LIMIT: 'abc' })).toThrow(/RATE_LIMIT_SHORT_LIMIT/);
expect(() => validateEnv({ WEBHOOK_TIMEOUT: '10s' })).toThrow(/WEBHOOK_TIMEOUT/);
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index 1861af427..8601add1b 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -70,6 +70,20 @@ export function validateEnv(config: EnvConfig): EnvConfig {
};
checkEnum('ENGINE_TYPE', ['whatsapp-web.js', 'baileys']);
checkEnum('STORAGE_TYPE', ['local', 's3']);
+ // Every production hardening in the repo gates on the exact string 'production', so an
+ // unrecognised value silently selects the permissive branch of each one — CORS, Swagger, DTO
+ // error detail, the default-secret guard and the ALLOW_DEV_API_KEY rejection that stops the public
+ // `dev-admin-key` being seeded as ADMIN. Unset stays legal (the standard Node default); every
+ // shipped deployment sets it explicitly.
+ //
+ // Checked RAW rather than through `str()`: the readers compare `process.env.NODE_ENV` verbatim, so
+ // a padded ' production ' that only matches after trimming would validate clean here and still take
+ // the permissive branch at runtime — blessing the very downgrade this check exists to stop.
+ const nodeEnvAllowed = ['production', 'development', 'test'];
+ const rawNodeEnv = config.NODE_ENV;
+ if (typeof rawNodeEnv === 'string' && rawNodeEnv !== '' && !nodeEnvAllowed.includes(rawNodeEnv)) {
+ errors.push(`NODE_ENV must be one of ${nodeEnvAllowed.map(v => `"${v}"`).join(', ')} (got "${rawNodeEnv}")`);
+ }
if (dbType === 'postgres') {
for (const key of ['DATABASE_HOST', 'DATABASE_USERNAME', 'DATABASE_PASSWORD']) {