Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): validate and cap the reaper and reconciler grace windows

Yudhi Armyndharis118059984956
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index a1720e9a9..d5553d40f 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -706,6 +706,20 @@ describe('validateEnv', () => {
},
);
+ // A huge grace builds a cutoff whose year SQLite binds in wrapped form, matching fresh rows.
+ it.each(['MESSAGE_REAPER_GRACE_MS', 'WEBHOOK_RECONCILE_GRACE_MS', 'INGRESS_RECONCILE_GRACE_MS'])(
+ 'rejects a malformed or oversized %s',
+ key => {
+ expect(() => validateEnv({ [key]: '1h' })).toThrow(new RegExp(`${key} must be a non-negative integer`));
+ expect(() => validateEnv({ [key]: '-1' })).toThrow(new RegExp(`${key} must be a non-negative integer`));
+ expect(() => validateEnv({ [key]: '999999999999999' })).toThrow(
+ new RegExp(`${key} must be at most 3153600000000`),
+ );
+ expect(() => validateEnv({ [key]: '3153600000000' })).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.ts
index 6fbe0a174..09f86cebd 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -236,6 +236,10 @@ export function validateEnv(config: EnvConfig): EnvConfig {
'MESSAGE_REAPER_INTERVAL_MS',
'WEBHOOK_RECONCILE_INTERVAL_MS',
'INGRESS_RECONCILE_INTERVAL_MS',
+ // 0 = act on a row as soon as it is seen; the cap below keeps the cutoff inside the safe range.
+ 'MESSAGE_REAPER_GRACE_MS',
+ 'WEBHOOK_RECONCILE_GRACE_MS',
+ 'INGRESS_RECONCILE_GRACE_MS',
]) {
checkNonNegativeInt(key);
}
@@ -287,6 +291,13 @@ export function validateEnv(config: EnvConfig): EnvConfig {
errors.push(`${key} must be at most 36500 (got "${days}")`);
}
}
+ // The grace windows build the same kind of cutoff (now minus the grace), so they share the cap.
+ for (const key of ['MESSAGE_REAPER_GRACE_MS', 'WEBHOOK_RECONCILE_GRACE_MS', 'INGRESS_RECONCILE_GRACE_MS']) {
+ const raw = str(key);
+ if (raw !== undefined && DECIMAL_INTEGER.test(raw) && Number(raw) > 36500 * 86_400_000) {
+ errors.push(`${key} must be at most ${36500 * 86_400_000} ms, 36500 days (got "${raw}")`);
+ }
+ }
// BAILEYS_WA_VERSION: optional version pin for the Baileys engine (e.g. 2.3000.1045340097 or 2,3000,1045340097)
for (const key of ['BAILEYS_WA_VERSION']) {