Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(security): make CSP upgrade-insecure-requests configurable (#611) (#612)
Yudhi Armyndharisc1b9c1e174b0
diff --git a/.env.example b/.env.exampleindex 472300dd2..aef2788be 100644--- a/.env.example+++ b/.env.example@@ -276,3 +276,5 @@ API_MASTER_KEY=# =============================================================================ENABLE_SWAGGER=true # Enable API documentation at /api/docs (set false to disable on exposed deployments)BODY_SIZE_LIMIT=25mb # Max request body size (base64 media sends ride in the JSON body)+# CSP_UPGRADE_INSECURE_REQUESTS=false # Set false for an HTTP-only deployment on a trusted private network+ # (default: on in production). Stops the browser upgrading the dashboard to https.diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3fe8f4d82..a6445e3fa 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++- **`CSP_UPGRADE_INSECURE_REQUESTS` env var** to control the CSP `upgrade-insecure-requests` directive. It defaults to the existing behaviour (on in production, off elsewhere); set it to `false` for an HTTP-only deployment on a trusted private network, where the browser would otherwise upgrade the dashboard to `https` and make it unreachable. Set it to `true` to force it on. (#611)+## [0.8.3] - 2026-07-03### Addeddiff --git a/src/config/bootstrap-security.spec.ts b/src/config/bootstrap-security.spec.tsindex c61f29338..320a44420 100644--- a/src/config/bootstrap-security.spec.ts+++ b/src/config/bootstrap-security.spec.ts@@ -1,6 +1,7 @@import {resolveCorsPolicy,isSwaggerEnabled,+ isUpgradeInsecureRequestsEnabled,resolveBodyLimit,assertNoDefaultSecretsInProduction,isApiKeyPepperMissingInProduction,@@ -44,6 +45,24 @@ describe('resolveCorsPolicy', () => {});});+describe('isUpgradeInsecureRequestsEnabled', () => {+ it('keeps the legacy default: on in production, off elsewhere (unset)', () => {+ expect(isUpgradeInsecureRequestsEnabled(undefined, 'production')).toBe(true);+ expect(isUpgradeInsecureRequestsEnabled(undefined, 'development')).toBe(false);+ expect(isUpgradeInsecureRequestsEnabled(undefined)).toBe(false);+ });+ it('lets an explicit value override NODE_ENV', () => {+ // HTTP-only private-network prod opts OUT so the dashboard stays reachable (#611)+ expect(isUpgradeInsecureRequestsEnabled('false', 'production')).toBe(false);+ // and it can be forced on outside production+ expect(isUpgradeInsecureRequestsEnabled('true', 'development')).toBe(true);+ });+ it('treats any non-"true"/"false" value as unset (falls back to NODE_ENV)', () => {+ expect(isUpgradeInsecureRequestsEnabled('', 'production')).toBe(true);+ expect(isUpgradeInsecureRequestsEnabled('1', 'development')).toBe(false);+ });+});+describe('isSwaggerEnabled', () => {it('is on by default (unset)', () => {expect(isSwaggerEnabled(undefined)).toBe(true);diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.tsindex 9ec273b88..2da78bd9f 100644--- a/src/config/bootstrap-security.ts+++ b/src/config/bootstrap-security.ts@@ -44,6 +44,19 @@ export function isSwaggerEnabled(enableSwaggerEnv?: string, nodeEnv?: string): breturn nodeEnv !== 'production';}+/**+ * Whether to emit the CSP `upgrade-insecure-requests` directive (browsers auto-upgrade HTTP→HTTPS).+ * An explicit CSP_UPGRADE_INSECURE_REQUESTS wins ('true'/'false'). When unset it keeps the legacy+ * behaviour — ON in production only (the secure default for Internet-facing TLS deployments), OFF+ * elsewhere. Set CSP_UPGRADE_INSECURE_REQUESTS=false for an HTTP-only deployment on a trusted private+ * network, where the upgrade would otherwise force the dashboard to https and make it unreachable. (#611)+ */+export function isUpgradeInsecureRequestsEnabled(cspEnv?: string, nodeEnv?: string): boolean {+ if (cspEnv === 'true') return true;+ if (cspEnv === 'false') return false;+ return nodeEnv === 'production';+}+/** Request body-size cap (DoS hardening). Default is media-aware (base64 sends ride in the JSON body). */export function resolveBodyLimit(bodySizeEnv?: string): string {const trimmed = bodySizeEnv?.trim();diff --git a/src/main.ts b/src/main.tsindex d1a0b4d30..610b90bd4 100644--- a/src/main.ts+++ b/src/main.ts@@ -14,6 +14,7 @@ import { createSwaggerConfig } from './config/swagger.config';import {resolveCorsPolicy,isSwaggerEnabled,+ isUpgradeInsecureRequestsEnabled,resolveBodyLimit,assertNoDefaultSecretsInProduction,isApiKeyPepperMissingInProduction,@@ -121,7 +122,14 @@ async function bootstrap() {connectSrc: ["'self'"],fontSrc: ["'self'", 'https://fonts.gstatic.com'],objectSrc: ["'none'"],- upgradeInsecureRequests: process.env.NODE_ENV === 'production' ? [] : null,+ // Auto-upgrade HTTP→HTTPS in production, unless CSP_UPGRADE_INSECURE_REQUESTS opts out for an+ // HTTP-only private-network deployment (otherwise the browser forces the dashboard to https). (#611)+ upgradeInsecureRequests: isUpgradeInsecureRequestsEnabled(+ process.env.CSP_UPGRADE_INSECURE_REQUESTS,+ process.env.NODE_ENV,+ )+ ? []+ : null,},},hsts: {