Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): refuse a BODY_SIZE_LIMIT of 0 or an unknown unit at boot

Yudhi Armyndharis46b9206f268b
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index 7b3e99a7d..5458ecba1 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -198,6 +198,15 @@ describe('validateEnv', () => {
expect(() => validateEnv({ INFLIGHT_BODY_BUDGET_BYTES: '104857600' })).not.toThrow();
});
+ it('rejects a BODY_SIZE_LIMIT that refuses every body or would silently fall back to the default', () => {
+ for (const value of ['0', '0kb', '0.5', '50M', '50MiB', '50 MiB', 'abc']) {
+ expect(() => validateEnv({ BODY_SIZE_LIMIT: value })).toThrow(/BODY_SIZE_LIMIT must be a positive size/);
+ }
+ for (const value of ['25mb', '1.5gb', '1048576', '50 MB']) {
+ expect(() => validateEnv({ BODY_SIZE_LIMIT: value })).not.toThrow();
+ }
+ });
+
it('rejects a negative/non-integer webhook fan-out knob (0 is a documented escape hatch)', () => {
expect(() => validateEnv({ WEBHOOK_MAX_PER_SESSION: '-1' })).toThrow(/WEBHOOK_MAX_PER_SESSION/);
expect(() => validateEnv({ WEBHOOK_MAX_PER_SESSION: '1.5' })).toThrow(/WEBHOOK_MAX_PER_SESSION/);
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index a4aa5703a..c026fd69a 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -1,5 +1,7 @@
import { resolve } from 'path';
import { normalizeS3KeyPrefix } from '../common/storage/s3-key-prefix';
+import { resolveBodyLimit } from './bootstrap-security';
+import { parseBodyLimitBytes } from './inflight-body-budget';
type EnvConfig = Record<string, unknown>;
@@ -398,6 +400,16 @@ export function validateEnv(config: EnvConfig): EnvConfig {
checkPositiveInt(key);
}
+ // The body cap takes a unit string. A spelling the parser does not know (`50M`, `50MiB`) silently
+ // becomes the 25mb default, and a value that parses to 0 bytes refuses every request carrying a
+ // body, the same self-DoS INFLIGHT_BODY_BUDGET_BYTES is refused for above.
+ const bodyLimit = str('BODY_SIZE_LIMIT');
+ if (bodyLimit !== undefined && (resolveBodyLimit(bodyLimit) !== bodyLimit || parseBodyLimitBytes(bodyLimit) < 1)) {
+ errors.push(
+ `BODY_SIZE_LIMIT must be a positive size such as 25mb or 1048576 (units b, kb, mb, gb, tb, pb; got "${bodyLimit}")`,
+ );
+ }
+
// The ceiling matters for the same reason from the other side: the docs forbid 0, so an operator
// who wants an effectively unlimited budget reaches for a row of nines. Rejected at boot rather
// than clamped, so they learn the value they wrote is not the value they would have got.