Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): accept a negative AUDIT_RETENTION_DAYS again

Yudhi Armyndharis2097449029d8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b49b8e703..1eae4d891 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Turkish (`tr`) dashboard translation. Thanks @codedByCan.
-- `AUDIT_RETENTION_DAYS` is checked at boot, so a typo fails startup with a named error instead of silently reverting to the 90-day default. It is validated as non-negative rather than positive, because `0` is the documented switch that disables audit-log pruning entirely.
+- `AUDIT_RETENTION_DAYS` is checked at boot, so a typo fails startup with a named error instead of silently reverting to the 90-day default. It is validated as an integer of any sign rather than a positive one, because both `0` and any negative value are documented switches that disable audit-log pruning entirely.
- `message.controller.spec.ts` holds the two headers the stored-media download sends, `X-Content-Type-Options: nosniff` and `Content-Disposition: attachment`; deleting either had passed every suite in the repo.
- Optional `quotedMessageId` on every `send-*` message endpoint and its MCP tool, so a reply can carry media, a location, a contact card or a poll instead of only text. An id the engine cannot resolve now fails the send rather than delivering the message unquoted. Thanks @nirizr for the report.
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index e4c4aa788..3d80e3cbe 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -107,11 +107,14 @@ describe('validateEnv', () => {
expect(() => validateEnv({ WEBHOOK_MAX_PER_SESSION: '0', WEBHOOK_MEDIA_INLINE_MAX_BYTES: '0' })).not.toThrow();
});
- it('rejects a non-integer audit retention (0 is the documented retention-off switch)', () => {
+ it('rejects a non-integer audit retention but accepts every value documented as "disable"', () => {
expect(() => validateEnv({ AUDIT_RETENTION_DAYS: 'ninety' })).toThrow(/AUDIT_RETENTION_DAYS/);
expect(() => validateEnv({ AUDIT_RETENTION_DAYS: '90.5' })).toThrow(/AUDIT_RETENTION_DAYS/);
- // 0 disables pruning in audit.service.ts, so this knob must never become positive-only.
+ expect(() => validateEnv({ AUDIT_RETENTION_DAYS: '30d' })).toThrow(/AUDIT_RETENTION_DAYS/);
+ // audit.service.ts and docs/05 both document `<= 0` as the off switch, so BOTH spellings must
+ // boot. Requiring non-negative here would refuse to start on a configuration the docs advertise.
expect(() => validateEnv({ AUDIT_RETENTION_DAYS: '0' })).not.toThrow();
+ expect(() => validateEnv({ AUDIT_RETENTION_DAYS: '-1' })).not.toThrow();
expect(() => validateEnv({ AUDIT_RETENTION_DAYS: '90' })).not.toThrow();
});
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index 0d9ca179b..1861af427 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -173,11 +173,33 @@ 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
- 'AUDIT_RETENTION_DAYS', // 0 = retention disabled, so this cannot be positive-only
]) {
checkNonNegativeInt(key);
}
+ // Retention knobs whose read site documents `<= 0` as the switch that disables pruning, so a
+ // negative value is a supported spelling of "off" rather than a typo — `audit.service.ts` clamps
+ // with Math.max(0, parsed) and `docs/05-database-design.md` advertises `≤ 0 disables`. The point of
+ // validating them is to reject `30d` / `ninety`, which parse to NaN and silently become the
+ // default; rejecting `-1` would instead refuse to boot a configuration this repo documents.
+ const SIGNED_DECIMAL_INTEGER = /^-?\d+$/;
+ const checkInt = (key: string): void => {
+ const raw = str(key);
+ if (raw === undefined) return;
+ const n = SIGNED_DECIMAL_INTEGER.test(raw) ? Number(raw) : NaN;
+ if (!Number.isInteger(n)) {
+ errors.push(`${key} must be an integer (got "${raw}")`);
+ }
+ };
+ // Keep this an ARRAY LITERAL even at one entry: docs-env-example.spec.ts derives the keys it
+ // requires `.env.example` to list by scanning `'KEY',` array elements in this file. Collapsing it
+ // into a bare checkInt('AUDIT_RETENTION_DAYS') call would silently drop the knob out of that gate.
+ for (const key of [
+ 'AUDIT_RETENTION_DAYS', // <= 0 disables retention
+ ]) {
+ checkInt(key);
+ }
+
// Some knobs are nonsensical at 0 and contradict the "non-negative" intent: a rate-limit LIMIT of 0
// disables that tier's throttling (a self-DoS), and a webhook timeout of 0 aborts every delivery
// immediately. Require a positive integer for these.