Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(security): warn at boot when NODE_ENV is unset, cap MCP fallback body parser

Yudhi Armyndharis2c8eb5bc2766
diff --git a/src/config/bootstrap-security.spec.ts b/src/config/bootstrap-security.spec.ts
index 556d36750..b076439b4 100644
--- a/src/config/bootstrap-security.spec.ts
+++ b/src/config/bootstrap-security.spec.ts
@@ -7,6 +7,7 @@ import {
resolveBodyLimit,
assertNoDefaultSecretsInProduction,
isApiKeyPepperMissingInProduction,
+ isNodeEnvUnset,
} from './bootstrap-security';
describe('resolveCorsPolicy', () => {
@@ -398,3 +399,17 @@ describe('isApiKeyPepperMissingInProduction', () => {
expect(isApiKeyPepperMissingInProduction(undefined, undefined)).toBe(false);
});
});
+
+describe('isNodeEnvUnset', () => {
+ it('is true when NODE_ENV is unset or blank (incl. whitespace-only)', () => {
+ expect(isNodeEnvUnset(undefined)).toBe(true);
+ expect(isNodeEnvUnset('')).toBe(true);
+ expect(isNodeEnvUnset(' ')).toBe(true);
+ });
+
+ it('is false for any set value, including an unrecognised one (no warning noise)', () => {
+ expect(isNodeEnvUnset('development')).toBe(false);
+ expect(isNodeEnvUnset('production')).toBe(false);
+ expect(isNodeEnvUnset('staging')).toBe(false);
+ });
+});
diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.ts
index 8f30ec748..8fad87f2d 100644
--- a/src/config/bootstrap-security.ts
+++ b/src/config/bootstrap-security.ts
@@ -128,6 +128,16 @@ export function isApiKeyPepperMissingInProduction(nodeEnv?: string, apiKeyPepper
return nodeEnv === 'production' && !apiKeyPepper?.trim();
}
+/**
+ * Whether NODE_ENV is unset or blank. That is the deliberate local-dev default, but it silently
+ * degrades four controls to their dev posture: the default-secret assert is skipped, a wildcard
+ * CORS origin is allowed, Swagger UI is served, and validation error detail is exposed. Boot warns
+ * (without changing any default) so a production deployment that merely forgot the variable can tell.
+ */
+export function isNodeEnvUnset(nodeEnv?: string): boolean {
+ return !nodeEnv?.trim();
+}
+
/** A built-in S3 endpoint is the bundled MinIO (host `minio`). An unset endpoint means the standard
* AWS regional endpoint, so it is external and its credentials must never be exempted. */
function isInternalS3Endpoint(endpoint?: string): boolean {
diff --git a/src/config/inflight-body-budget.spec.ts b/src/config/inflight-body-budget.spec.ts
index 90749a702..76b152583 100644
--- a/src/config/inflight-body-budget.spec.ts
+++ b/src/config/inflight-body-budget.spec.ts
@@ -584,7 +584,7 @@ describe('body-parser inflate backstop', () => {
expect(source.match(/^\s+inflate: false,$/gm)).toHaveLength(2);
});
- it('disables inflate on the MCP route-level fallback parser', () => {
- expect(read('../modules/mcp/mcp.server.ts')).toContain('express.json({ inflate: false })');
+ it('mirrors the global cap and disabled inflate on the MCP route-level fallback parser', () => {
+ expect(read('../modules/mcp/mcp.server.ts')).toContain('express.json({ limit: bodyLimit, inflate: false })');
});
});
diff --git a/src/main.ts b/src/main.ts
index f858af09c..467fe0969 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -27,6 +27,7 @@ import {
resolveBodyLimit,
assertNoDefaultSecretsInProduction,
isApiKeyPepperMissingInProduction,
+ isNodeEnvUnset,
} from './config/bootstrap-security';
import { BullBoardAuthMiddleware } from './common/security/bull-board-auth.middleware';
import { AuthService } from './modules/auth/auth.service';
@@ -59,6 +60,18 @@ async function bootstrap() {
// logger WITHOUT swallowing the exception, so the crash-and-restart posture is unchanged (see the helper).
registerUncaughtExceptionMonitor(bootstrapLogger);
+ // Advisory (not enforced): an unset/blank NODE_ENV is the deliberate local-dev default, but it
+ // silently degrades four controls to their dev posture (the default-secret guard, wildcard CORS,
+ // Swagger UI, validation error detail) — warn so a production deployment that simply forgot the
+ // variable can tell. The defaults themselves stay unchanged.
+ if (isNodeEnvUnset(process.env.NODE_ENV)) {
+ bootstrapLogger.warn(
+ 'NODE_ENV is not set: running with development defaults — the default-secret guard is skipped, ' +
+ 'wildcard CORS is allowed, Swagger UI is served, and validation error detail is exposed. ' +
+ 'Set NODE_ENV=production for a production deployment.',
+ );
+ }
+
// Fail fast: never start production with default/placeholder secrets.
assertNoDefaultSecretsInProduction({
nodeEnv: process.env.NODE_ENV,
diff --git a/src/modules/mcp/mcp.server.ts b/src/modules/mcp/mcp.server.ts
index 40fbf0b21..bde30cf3a 100644
--- a/src/modules/mcp/mcp.server.ts
+++ b/src/modules/mcp/mcp.server.ts
@@ -14,6 +14,7 @@ import { AuditAction } from '../audit/entities/audit-log.entity';
import { handleToolError, jsonToolResult, smartToolResult } from './tool-result';
import type { KeyRateLimiter } from './mcp-rate-limit';
import { resolveClientIp } from '../../common/utils/ip';
+import { resolveBodyLimit } from '../../config/bootstrap-security';
const logger = new Logger('McpServer');
@@ -247,5 +248,8 @@ export function mountMcpServer(
// `inflate: false` matches the global parsers: a compressed body is refused by the budget
// middleware long before this runs, and this keeps the fallback from becoming the one parser that
// would still gunzip an unaccounted body if that ordering ever changed.
- adapter.post(basePath, createIpThrottle(ipRateLimiter), express.json({ inflate: false }), handler);
+ // `limit` mirrors the same global cap for the same reason: without it a middleware reorder would
+ // silently leave this mount uncapped.
+ const bodyLimit = resolveBodyLimit(process.env.BODY_SIZE_LIMIT);
+ adapter.post(basePath, createIpThrottle(ipRateLimiter), express.json({ limit: bodyLimit, inflate: false }), handler);
}