Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

build: set NODE_ENV=production in the runtime image

Yudhi Armyndharis2349670471be
diff --git a/Dockerfile b/Dockerfile
index 53dd75c56..8b29017cf 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -58,6 +58,13 @@ RUN npm run build && npm run dashboard:ci -- --include=dev && npm run dashboard:
# Same digest-pinned node:22-slim base as the builder stage.
FROM docker.io/node:22-slim@sha256:d649c27dae7ba0137b3cef5dd75baa422c08dc3d9e3fc0c23dfb172dc3cc6436 AS production
+# Run the app with production defaults from the first boot: an unset NODE_ENV selects the
+# development branch of the CORS/Swagger/DTO-error-detail/default-secret hardening (main.ts
+# warns about exactly this case). Both compose files and the Helm chart already set it; a plain
+# `docker run` of this image did not. The npm installs below pin --omit=dev explicitly, so this
+# changes nothing about which dependencies land in the image.
+ENV NODE_ENV=production
+
# Chrome for Testing has no linux-arm64 build, and Puppeteer's chromium snapshot
# is x86_64-only on Linux too. So: amd64 uses Chrome for Testing (downloaded below)
# to avoid the Debian chromium package's K8s SIGTRAP under strict non-root/seccomp;
diff --git a/src/common/services/shutdown.service.spec.ts b/src/common/services/shutdown.service.spec.ts
index 29b36733b..db7a9f380 100644
--- a/src/common/services/shutdown.service.spec.ts
+++ b/src/common/services/shutdown.service.spec.ts
@@ -86,9 +86,9 @@ describe('ShutdownService.shutdown (idempotent, bounded grace)', () => {
expect(cb).toHaveBeenCalledTimes(1);
});
- it('keeps the 3s drain window when NODE_ENV is UNSET (a bare `docker run` / k8s pod)', async () => {
- // Regression guard: the runtime image does not set NODE_ENV, so an unset env must NOT collapse the
- // drain to 0 — only an explicit dev/test does. Otherwise a rolling deploy loses its readiness window.
+ it('keeps the 3s drain window when NODE_ENV is UNSET (an ad-hoc run outside the packaged runtimes)', async () => {
+ // Regression guard: an unset NODE_ENV must NOT collapse the drain to 0 — only an explicit
+ // dev/test does. Otherwise a rolling deploy loses its readiness window.
delete process.env.NODE_ENV;
delete process.env.SHUTDOWN_DELAY_MS;
const { svc, cb } = svcWithCb();
diff --git a/src/common/services/shutdown.service.ts b/src/common/services/shutdown.service.ts
index e3cc2e5ec..c8da8d1c9 100644
--- a/src/common/services/shutdown.service.ts
+++ b/src/common/services/shutdown.service.ts
@@ -80,9 +80,9 @@ export class ShutdownService {
/**
* Bounded, configurable grace (SHUTDOWN_DELAY_MS), capped at 30s. An explicit value always wins.
* When unset, the default is the full 3s drain window (so a load balancer observes the 503 before
- * teardown) for EVERY real deployment — including a bare `docker run` or a Kubernetes pod that never
- * sets NODE_ENV. Only an explicit `development`/`test` skips the window (delay 0), so a
- * `nest start --watch` hot reload or a dev Ctrl+C is not slowed by a grace it does not need.
+ * teardown) for EVERY real deployment — even an ad-hoc run that never sets NODE_ENV. Only an
+ * explicit `development`/`test` skips the window (delay 0), so a `nest start --watch` hot reload
+ * or a dev Ctrl+C is not slowed by a grace it does not need.
*/
private resolveDelay(): number {
const parsed = Number.parseInt(process.env.SHUTDOWN_DELAY_MS ?? '', 10);
diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.ts
index 8fad87f2d..973c6cf06 100644
--- a/src/config/bootstrap-security.ts
+++ b/src/config/bootstrap-security.ts
@@ -186,9 +186,10 @@ export function assertNoDefaultSecretsInProduction(env: SecretCheckEnv): void {
// deliberately not production (and an unset or blank variable, the standard Node default for local
// runs) skip it; everything else is treated as production.
//
- // NOTE: unset skips the guard. That is the pre-existing behaviour and is deliberate — a plain
- // `docker run` sets no NODE_ENV, and refusing to start there would break local use — but it does
- // mean a production deployment must set it. shutdown.service.ts makes the OPPOSITE choice for the
+ // NOTE: unset skips the guard. That is the pre-existing behaviour and is deliberate — a local
+ // `node dist/main` outside the packaged runtimes sets no NODE_ENV, and refusing to start there
+ // would break local use — but it does mean a production deployment must set it (the runtime image
+ // and the chart do so themselves). shutdown.service.ts makes the OPPOSITE choice for the
// same variable (unset keeps the production drain window), so this is not a mirror of it.
// A blank value means the same here as it does to boot validation, which treats '' as unset:
// otherwise `NODE_ENV=` boots clean and then gets production-grade secret enforcement, and the
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index ebe363e27..1aba20862 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -75,11 +75,11 @@ export function validateEnv(config: EnvConfig): EnvConfig {
// 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 because it is the standard Node default AND the published image does not set
- // it: the Dockerfile carries no runtime `ENV NODE_ENV`, so refusing an unset value would stop a
- // plain `docker run` of the image from booting. Note what that means in practice — both compose
- // files set it (`${NODE_ENV:-production}` and a hardcoded `development`), but running the image
- // directly without passing it takes the permissive branch of every hardening listed above.
+ // Unset stays legal because it is the standard Node default for a plain `node dist/main` outside
+ // any packaged runtime — refusing it would break local runs. The packaged runtimes all set it (the
+ // runtime image carries `ENV NODE_ENV=production`, the chart sets it, and both compose files set it
+ // via `${NODE_ENV:-production}` and a hardcoded `development`); only a hand-rolled deployment that
+ // strips it still takes the permissive branch of every hardening listed above.
//
// 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