Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): cap SSRF_DNS_TIMEOUT_MS and the Postgres timeouts at boot
Yudhi Armyndharisc1bb6d5b28de
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.tsindex d5553d40f..d9e97cf34 100644--- a/src/config/env.validation.spec.ts+++ b/src/config/env.validation.spec.ts@@ -720,6 +720,25 @@ describe('validateEnv', () => {},);+ it('rejects a non-positive or overflowing SSRF_DNS_TIMEOUT_MS', () => {+ expect(() => validateEnv({ SSRF_DNS_TIMEOUT_MS: '0' })).toThrow(/SSRF_DNS_TIMEOUT_MS must be a positive integer/);+ expect(() => validateEnv({ SSRF_DNS_TIMEOUT_MS: '10s' })).toThrow(/SSRF_DNS_TIMEOUT_MS must be a positive integer/);+ expect(() => validateEnv({ SSRF_DNS_TIMEOUT_MS: '2147483648' })).toThrow(+ /SSRF_DNS_TIMEOUT_MS must not exceed 2147483647 ms/,+ );+ expect(() => validateEnv({ SSRF_DNS_TIMEOUT_MS: '2147483647' })).not.toThrow();+ });++ // pg arms these with setTimeout and sends statement_timeout to a server capped at INT_MAX; 0 still disables.+ it.each(['DATABASE_CONNECTION_TIMEOUT_MS', 'DATABASE_IDLE_TIMEOUT_MS', 'DATABASE_STATEMENT_TIMEOUT_MS'])(+ 'rejects a %s above 2147483647 ms and keeps 0',+ key => {+ expect(() => validateEnv({ [key]: '99999999999' })).toThrow(new RegExp(`${key} must not exceed 2147483647 ms`));+ expect(() => validateEnv({ [key]: '2147483647' })).not.toThrow();+ expect(() => validateEnv({ [key]: '0' })).not.toThrow();+ },+ );+it('rejects a SESSION_LEASE_HEARTBEAT_MS above the Node timer ceiling even inside a longer lease', () => {const lease = { SESSION_LEASE_TTL_MS: '5000000000' };expect(() => validateEnv({ ...lease, SESSION_LEASE_HEARTBEAT_MS: '2147483648' })).toThrow(diff --git a/src/config/env.validation.ts b/src/config/env.validation.tsindex 09f86cebd..f2689eedb 100644--- a/src/config/env.validation.ts+++ b/src/config/env.validation.ts@@ -372,6 +372,8 @@ 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 read fell back to its default on garbage, so `10s` silently meant 10000.+ 'SSRF_DNS_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@@ -445,10 +447,14 @@ export function validateEnv(config: EnvConfig): EnvConfig {['RATE_LIMIT_MEDIUM_TTL', 'each hit expires after 1 ms and that rate-limit tier never blocks'],['RATE_LIMIT_LONG_TTL', 'each hit expires after 1 ms and that rate-limit tier never blocks'],['INGRESS_INSTANCE_TTL', 'each hit expires after 1 ms and the ingress rate limits never block'],+ ['SSRF_DNS_TIMEOUT_MS', 'every guarded DNS lookup times out, failing webhook deliveries and URL downloads'],// 0 still disables these three, so they carry only the ceiling, not the positive-only check.['MESSAGE_REAPER_INTERVAL_MS', 'the pending message reaper reruns every millisecond'],['WEBHOOK_RECONCILE_INTERVAL_MS', 'the webhook reconciler reruns every millisecond'],['INGRESS_RECONCILE_INTERVAL_MS', 'the ingress reconciler reruns every millisecond'],+ // 0 disables these two as well; pg arms both with a plain setTimeout.+ ['DATABASE_CONNECTION_TIMEOUT_MS', 'every pool connect times out'],+ ['DATABASE_IDLE_TIMEOUT_MS', 'each idle pool connection is closed 1 ms after release'],]) {const raw = str(key);const n = raw !== undefined && DECIMAL_INTEGER.test(raw) ? Number(raw) : NaN;@@ -459,6 +465,19 @@ export function validateEnv(config: EnvConfig): EnvConfig {);}}+ // Not a Node timer, but the same ceiling: PostgreSQL's statement_timeout is an int capped at+ // 2147483647, and a larger startup value is refused on every runtime connection. 0 still disables.+ const statementTimeout = str('DATABASE_STATEMENT_TIMEOUT_MS');+ if (+ statementTimeout !== undefined &&+ DECIMAL_INTEGER.test(statementTimeout) &&+ Number(statementTimeout) > MAX_TIMER_MS+ ) {+ errors.push(+ `DATABASE_STATEMENT_TIMEOUT_MS must not exceed ${MAX_TIMER_MS} ms (got "${statementTimeout}"): ` ++ 'PostgreSQL refuses a larger statement_timeout, so every runtime connection fails',+ );+ }// Knobs armed at a multiple of their value get that fraction of the timer's ceiling. Send verbs arm// four times PLUGIN_CAP_TIMEOUT_MS (SEND_CAP_TIMEOUT_FACTOR in plugin-worker-host.ts); a direct// (queue-off) webhook delivery doubles WEBHOOK_RETRY_DELAY on each retry, up to 2^3 for the maximum