Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): refuse a unit-suffixed media knob at boot

Yudhi Armyndharis99aeeae72da0
diff --git a/.env.example b/.env.example
index dd1977ea3..476ef70de 100644
--- a/.env.example
+++ b/.env.example
@@ -454,11 +454,13 @@ WEBHOOK_SSRF_PROTECT=true
# STORE_EPHEMERAL_MESSAGES=true # Set to false to skip persisting and dispatching incoming
# # WhatsApp disappearing messages (ephemeralDuration > 0).
# # Default: true (backward compatible — store everything).
+# The four media knobs below take a RAW NUMBER, unlike BODY_SIZE_LIMIT further down which takes a
+# unit string. Writing `50mb` here is refused at boot rather than read as its leading digits.
# MEDIA_DOWNLOAD_MAX_BYTES=52428800 # cap remote-URL sends, inbound media, AND outbound base64 sends (default 50 MiB; oversized inbound media is dropped, message kept; oversized base64 is rejected with 413)
-# MEDIA_DOWNLOAD_TIMEOUT_MS=30000 # abort a slow media download (default 30s)
+# MEDIA_DOWNLOAD_TIMEOUT_MS=30000 # abort a slow media download (default 30s), in milliseconds
# Inbound media download parallelism (how many inbound media items download at once). Each concurrent
-# download holds memory up to MEDIA_DOWNLOAD_MAX_BYTES; this bounds N. A non-positive/garbage value
-# falls back to the default.
+# download holds memory up to MEDIA_DOWNLOAD_MAX_BYTES; this bounds N. Unset takes the default; a
+# value that is not a positive integer fails the boot.
# INBOUND_MEDIA_CONCURRENCY=4 # default 4
# ---------------------------------------------------------------------------
diff --git a/src/common/media/load-remote-media.ts b/src/common/media/load-remote-media.ts
index b57155ce8..6288bffd4 100644
--- a/src/common/media/load-remote-media.ts
+++ b/src/common/media/load-remote-media.ts
@@ -5,6 +5,11 @@ const DEFAULT_MEDIA_MAX_BYTES = 50 * 1024 * 1024;
/** Default timeout for a server-side media download: 30s (overridable via MEDIA_DOWNLOAD_TIMEOUT_MS). */
const DEFAULT_MEDIA_TIMEOUT_MS = 30_000;
+/**
+ * Read a positive-integer knob. Boot validation rejects a malformed value for every key read here,
+ * so the fallback covers unset and empty only: `parseInt` accepts the leading digits of a
+ * unit-suffixed value and would silently take `50mb` as 50.
+ */
function positiveIntFromEnv(name: string, fallback: number): number {
const parsed = Number.parseInt(process.env[name] ?? '', 10);
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index cc14f17e4..685aa4528 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -37,6 +37,34 @@ describe('validateEnv', () => {
expect(() => validateEnv({ DATABASE_TYPE: 'sqlite', POSTGRES_SCHEMA: '1bad' })).not.toThrow();
});
+ /**
+ * The media knobs take RAW numbers while `BODY_SIZE_LIMIT` beside them in .env.example takes a
+ * unit string. Their read sites use `Number.parseInt`, which takes the leading digits and drops
+ * the unit, so `50mb` was accepted as 50: a 50-byte cap, and `30s` a 30 ms timeout. Both are
+ * positive integers, so the "garbage falls back to the default" those helpers promise never
+ * fired. Boot has to be the place this stops.
+ */
+ it('rejects a unit-suffixed media knob instead of reading its leading digits', () => {
+ expect(() => validateEnv({ MEDIA_DOWNLOAD_MAX_BYTES: '50mb' })).toThrow(/MEDIA_DOWNLOAD_MAX_BYTES/);
+ expect(() => validateEnv({ MEDIA_DOWNLOAD_TIMEOUT_MS: '30s' })).toThrow(/MEDIA_DOWNLOAD_TIMEOUT_MS/);
+ expect(() => validateEnv({ CHAT_HISTORY_MEDIA_BUDGET_BYTES: '25mb' })).toThrow(/CHAT_HISTORY_MEDIA_BUDGET_BYTES/);
+ expect(() => validateEnv({ INBOUND_MEDIA_CONCURRENCY: '4x' })).toThrow(/INBOUND_MEDIA_CONCURRENCY/);
+ });
+
+ it('rejects a non-positive media knob and accepts a plain byte count', () => {
+ expect(() => validateEnv({ MEDIA_DOWNLOAD_MAX_BYTES: '0' })).toThrow(/MEDIA_DOWNLOAD_MAX_BYTES/);
+ expect(() => validateEnv({ MEDIA_DOWNLOAD_MAX_BYTES: '-1' })).toThrow(/MEDIA_DOWNLOAD_MAX_BYTES/);
+ expect(() => validateEnv({ INBOUND_MEDIA_CONCURRENCY: 'abc' })).toThrow(/INBOUND_MEDIA_CONCURRENCY/);
+ expect(() => validateEnv({ MEDIA_DOWNLOAD_MAX_BYTES: '52428800' })).not.toThrow();
+ expect(() => validateEnv({ MEDIA_DOWNLOAD_TIMEOUT_MS: '30000' })).not.toThrow();
+ });
+
+ // Unset and empty stay the operator's way of taking the default; compose forwards a blank.
+ it('leaves an unset or blank media knob alone', () => {
+ expect(() => validateEnv({})).not.toThrow();
+ expect(() => validateEnv({ MEDIA_DOWNLOAD_MAX_BYTES: '', MEDIA_DOWNLOAD_TIMEOUT_MS: ' ' })).not.toThrow();
+ });
+
it('rejects a non-integer / out-of-range port', () => {
expect(() => validateEnv({ DATABASE_PORT: 'abc' })).toThrow(/DATABASE_PORT/);
expect(() => validateEnv({ PORT: '70000' })).toThrow(/PORT/);
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index 2a8fe8ecc..e6a38c0b0 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -291,6 +291,17 @@ export function validateEnv(config: EnvConfig): EnvConfig {
// Positive-only is the POINT here, not a convention: 0 arms no Puppeteer timer at all, so a
// wedged renderer holds the request forever (see wwebjs-lifecycle.ts).
'PUPPETEER_PROTOCOL_TIMEOUT_MS',
+ // The media knobs take RAW numbers while their neighbours in .env.example and docs/12 take unit
+ // strings (`BODY_SIZE_LIMIT=25mb`), and their read sites parse with `Number.parseInt`. That
+ // accepts the leading digits of a unit-suffixed value and discards the unit, so
+ // `MEDIA_DOWNLOAD_MAX_BYTES=50mb` became a 50 BYTE cap and `MEDIA_DOWNLOAD_TIMEOUT_MS=30s`
+ // became 30 ms: every download fails, and the "garbage falls back to the default" the helpers
+ // promise never fires because 50 is a perfectly good positive integer. Reject at boot instead,
+ // which is what the two inline-media budgets below already do.
+ 'MEDIA_DOWNLOAD_MAX_BYTES',
+ 'MEDIA_DOWNLOAD_TIMEOUT_MS',
+ 'INBOUND_MEDIA_CONCURRENCY',
+ 'CHAT_HISTORY_MEDIA_BUDGET_BYTES',
]) {
checkPositiveInt(key);
}
diff --git a/src/engine/adapters/inbound-media-cap.ts b/src/engine/adapters/inbound-media-cap.ts
index 86431da79..b4deca39f 100644
--- a/src/engine/adapters/inbound-media-cap.ts
+++ b/src/engine/adapters/inbound-media-cap.ts
@@ -3,7 +3,13 @@ import type { IncomingMessage } from '../interfaces/whatsapp-engine.interface';
/** Default inbound media cap: 50 MiB. Shares MEDIA_DOWNLOAD_MAX_BYTES with the outbound download cap. */
const DEFAULT_INBOUND_MEDIA_MAX_BYTES = 50 * 1024 * 1024;
-/** Resolved inbound media byte cap; a non-positive/garbage override falls back to the default. */
+/**
+ * Resolved inbound media byte cap.
+ *
+ * The fallback below only covers unset and empty: boot validation rejects anything else, including
+ * the unit-suffixed form its neighbours accept. `parseInt` would read `50mb` as 50 and this helper
+ * would take it, since 50 is a positive integer, capping every download at 50 bytes.
+ */
export function inboundMediaMaxBytes(): number {
const parsed = Number.parseInt(process.env.MEDIA_DOWNLOAD_MAX_BYTES ?? '', 10);
return Number.isInteger(parsed) && parsed > 0 ? parsed : DEFAULT_INBOUND_MEDIA_MAX_BYTES;
@@ -15,7 +21,8 @@ const DEFAULT_INBOUND_MEDIA_CONCURRENCY = 4;
/**
* Max inbound media downloads processed concurrently. Each download materialises a full decrypted
* buffer in heap, so an unbounded fire-and-forget loop lets a sender flood the gateway with N parallel
- * multi-MB allocations; this bounds N. Override via INBOUND_MEDIA_CONCURRENCY; garbage falls back.
+ * multi-MB allocations; this bounds N. Override via INBOUND_MEDIA_CONCURRENCY; boot validation
+ * rejects a malformed value, so the fallback below only covers unset and empty.
*/
export function inboundMediaConcurrency(): number {
const parsed = Number.parseInt(process.env.INBOUND_MEDIA_CONCURRENCY ?? '', 10);
@@ -25,7 +32,11 @@ export function inboundMediaConcurrency(): number {
/** Default inbound media download timeout: 30s. Shares MEDIA_DOWNLOAD_TIMEOUT_MS with the outbound download. */
const DEFAULT_INBOUND_MEDIA_TIMEOUT_MS = 30_000;
-/** Resolved per-download wall-clock timeout; a non-positive/garbage override falls back to the default. */
+/**
+ * Resolved per-download wall-clock timeout. Boot validation rejects a malformed override, so the
+ * fallback only covers unset and empty: `parseInt` would read `30s` as 30 and this helper would take
+ * it, timing out every download after 30 ms.
+ */
export function inboundMediaTimeoutMs(): number {
const parsed = Number.parseInt(process.env.MEDIA_DOWNLOAD_TIMEOUT_MS ?? '', 10);
return Number.isInteger(parsed) && parsed > 0 ? parsed : DEFAULT_INBOUND_MEDIA_TIMEOUT_MS;
@@ -81,7 +92,8 @@ const DEFAULT_CHAT_HISTORY_MEDIA_BUDGET_BYTES = 25 * 1024 * 1024;
/**
* Aggregate budget (counted in base64 characters, the actual response/heap payload) for media inlined
* by one getChatHistory pass. Once the running total crosses it, remaining media messages carry the
- * usual `omitted` marker instead of a download. A non-positive/garbage override falls back to the default.
+ * usual `omitted` marker instead of a download. Boot validation rejects a malformed override, so the
+ * fallback covers unset and empty only.
*/
export function chatHistoryMediaBudgetBytes(): number {
const parsed = Number.parseInt(process.env.CHAT_HISTORY_MEDIA_BUDGET_BYTES ?? '', 10);