Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): close three gaps this branch left in its own knobs
Yudhi Armyndharisc352fcb268d4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f012d1845..b0e19fd85 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### 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 one padded with surrounding whitespace is rejected too rather than accepted and then treated as non-production: 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.+- ⚠️ **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 one padded with surrounding whitespace is rejected too rather than accepted and then treated as non-production: 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. A blank `NODE_ENV=` counts as unset for the default-secret guard too, so both halves of the rule agree about the same string.### Fixed@@ -50,7 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- The three group-picture routes now refuse an id that does not name a group. They reuse the account's own profile-picture primitives, and Baileys omits the `target` attribute whenever the jid is the account's own — so `PUT` or `DELETE /sessions/:id/groups/<own-jid>/picture` replaced or permanently deleted the WhatsApp account's own avatar and answered `{success: true}`, while whatsapp-web.js refused the same input with a 404. Both engines now reject it with a 400 before the id reaches either adapter.-- `GET /sessions/:sessionId/messages` and the MCP `MessageList` tool now bound the inline media they return, via `MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES` (8 MiB of encoded base64 by default). The row count was clamped to 1..100, but a row is not a bounded object: each carries its base64 in `metadata.media.data`, so a hundred media rows serialised to well over a hundred megabytes, and a large enough page pushed `JSON.stringify` past its ceiling and failed the read outright — permanently for the dashboard, which requests the maximum page size with no way to ask for less. The budget is spent newest-first; past it a payload is replaced with the `{ omitted: true, sizeBytes }` marker the engine already emits for skipped inbound media, and remains fetchable from the per-message media route. In the dashboard a trimmed message shows the same 📎 Media placeholder it already uses for media the engine skipped on the way in, and `MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES` raises the budget for a deployment that would rather pay the bytes.+- `GET /sessions/:sessionId/messages` and the MCP `MessageList` tool now bound the inline media they return, via `MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES` (8 MiB of encoded base64 by default). The row count was clamped to 1..100, but a row is not a bounded object: each carries its base64 in `metadata.media.data`, so a hundred media rows serialised to well over a hundred megabytes, and a large enough page pushed `JSON.stringify` past its ceiling and failed the read outright — permanently for the dashboard, which requests the maximum page size with no way to ask for less. The budget is spent newest-first; past it a payload is replaced with the `{ omitted: true, sizeBytes }` marker the engine already emits for skipped inbound media, and remains fetchable from the per-message media route. In the dashboard a trimmed message shows the same 📎 Media placeholder it already uses for media the engine skipped on the way in, and `MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES` raises the budget for a deployment that would rather pay the bytes. The knob is checked at boot like its export-side sibling — `8MiB` would otherwise parse to 8 bytes and omit everything — and is forwarded by both compose files.- Bulk send now caps its rendered result at `TEMPLATE_RENDER_MAX_CHARS` (default 64 KiB), the same limit the single-send template path already enforced. `content.text` is length-validated before substitution, so caller-supplied variables inflated each item without bound while the request body stayed far below the in-flight body budget — a few hundred KiB of variables became hundreds of megabytes of heap per item, then went to the engine and the `messages.body` column. An over-cap render now fails that item with a message naming the limit, exactly as a pacing refusal does, rather than being truncated silently or sent.diff --git a/docker-compose.dev.yml b/docker-compose.dev.ymlindex 42dcb7811..09de6c02b 100644--- a/docker-compose.dev.yml+++ b/docker-compose.dev.yml@@ -129,6 +129,7 @@ services:- SEND_PACING_COLD_DAILY_CAP=${SEND_PACING_COLD_DAILY_CAP:-}- SEND_PACING_BREAKER_THRESHOLD=${SEND_PACING_BREAKER_THRESHOLD:-}- SEND_PACING_BREAKER_COOLDOWN_MS=${SEND_PACING_BREAKER_COOLDOWN_MS:-}+ - MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES=${MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES:-}- MEDIA_DOWNLOAD_ENABLED=${MEDIA_DOWNLOAD_ENABLED:-}- MEDIA_DOWNLOAD_MAX_BYTES=${MEDIA_DOWNLOAD_MAX_BYTES:-}- MEDIA_DOWNLOAD_TIMEOUT_MS=${MEDIA_DOWNLOAD_TIMEOUT_MS:-}diff --git a/docker-compose.yml b/docker-compose.ymlindex 2bc200e07..ac2bf3b3e 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -205,6 +205,7 @@ services:- SEND_PACING_BREAKER_COOLDOWN_MS=${SEND_PACING_BREAKER_COOLDOWN_MS:-}# Server-side media conversion (opt-in). The image ships ffmpeg, but without these forwards# the feature could not be switched on from .env at all.+ - MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES=${MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES:-}- MEDIA_DOWNLOAD_ENABLED=${MEDIA_DOWNLOAD_ENABLED:-}- MEDIA_DOWNLOAD_MAX_BYTES=${MEDIA_DOWNLOAD_MAX_BYTES:-}- MEDIA_DOWNLOAD_TIMEOUT_MS=${MEDIA_DOWNLOAD_TIMEOUT_MS:-}diff --git a/src/config/bootstrap-security.spec.ts b/src/config/bootstrap-security.spec.tsindex c5ae5086b..556d36750 100644--- a/src/config/bootstrap-security.spec.ts+++ b/src/config/bootstrap-security.spec.ts@@ -160,6 +160,15 @@ describe('assertNoDefaultSecretsInProduction', () => {).toThrow(/DATABASE_PASSWORD/);});+ // Boot validation treats a blank NODE_ENV as UNSET and lets it through, so this guard must agree:+ // otherwise `NODE_ENV=` boots clean and then gets production-grade enforcement, and the two halves+ // of the same rule disagree about the same string.+ it('treats a blank NODE_ENV the way boot validation does — as unset', () => {+ for (const nodeEnv of ['', ' ']) {+ expect(() => assertNoDefaultSecretsInProduction({ nodeEnv, allowDevApiKey: 'true' })).not.toThrow();+ }+ });+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();diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.tsindex bf9430a7e..793c9a527 100644--- a/src/config/bootstrap-security.ts+++ b/src/config/bootstrap-security.ts@@ -176,7 +176,11 @@ export function assertNoDefaultSecretsInProduction(env: SecretCheckEnv): void {// 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;+ // 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+ // two halves of this guard disagree about the same string.+ const nodeEnv = env.nodeEnv?.trim();+ if (nodeEnv === 'development' || nodeEnv === 'test' || !nodeEnv) return;const isWeak = (value?: string): boolean => !value || FORBIDDEN_PROD_SECRETS.has(value.trim().toLowerCase());const problems: string[] = [];diff --git a/src/config/env-precedence.ts b/src/config/env-precedence.tsindex 6664d38e8..3185056e8 100644--- a/src/config/env-precedence.ts+++ b/src/config/env-precedence.ts@@ -26,6 +26,7 @@ export const BLANK_SHADOWED_ENV_KEYS: string[] = [// Inbound-media knobs. Not dashboard-managed, but every blank compose forward must be cleared or// the empty value shadows .env / data/.env.generated — which is why the gate above requires an// entry for each one.+ 'MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES','MEDIA_DOWNLOAD_ENABLED','MEDIA_DOWNLOAD_MAX_BYTES','MEDIA_DOWNLOAD_TIMEOUT_MS',diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.tsindex 77a08fd92..0d40f44fe 100644--- a/src/config/env.validation.spec.ts+++ b/src/config/env.validation.spec.ts@@ -65,6 +65,17 @@ describe('validateEnv', () => {// 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.+ // The knob that raises the message-list media budget parses with parseInt, so `8MiB` silently+ // means 8 BYTES — every payload omitted — exactly the trap its sibling+ // EXPORT_INLINE_MEDIA_BUDGET_BYTES is boot-checked for.+ it('rejects a non-integer message-list media budget', () => {+ expect(() => validateEnv({ MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES: '8MiB' })).toThrow(+ /MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES/,+ );+ expect(() => validateEnv({ MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES: '8388608' })).not.toThrow();+ expect(() => validateEnv({ MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES: '0' })).not.toThrow();+ });+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/);diff --git a/src/config/env.validation.ts b/src/config/env.validation.tsindex 8601add1b..584f76a21 100644--- a/src/config/env.validation.ts+++ b/src/config/env.validation.ts@@ -187,6 +187,7 @@ export function validateEnv(config: EnvConfig): EnvConfig {'AUTOMATION_MAX_PER_SESSION', // 0 = unlimited'WEBHOOK_MEDIA_INLINE_MAX_BYTES', // 0 = never inline media'EXPORT_INLINE_MEDIA_BUDGET_BYTES', // 0 = a data export carries no inline media at all+ 'MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES', // 0 = a message list carries no inline media at all]) {checkNonNegativeInt(key);}