Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): reject a MEDIA_DOWNLOAD_ENABLED value the read site cannot recognise
Yudhi Armyndharis621fc644cb0f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4578dd177..ad88c0d57 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- **A WhatsApp-initiated unlink now clears a session's stored credentials once rather than twice** — whatsapp-web.js can raise the logout event more than once for a single unlink and nothing deduplicated it, so the repeats raced each other and a still-open browser into the `ENOTEMPTY` failure the removal now carries the retry budget to survive.- **A reaction to a message the gateway never stored no longer vanishes** — the `message.reaction` webhook and the dashboard stream were both gated on the stored row, so a reaction to an ephemeral message, or to one that arrived before the session went live, was dropped with no error and no log.+- **`MEDIA_DOWNLOAD_ENABLED` is now validated at boot** — it is read as "anything that is not `false`/`0`/`no`", so a typo left inbound media being decrypted and base64-inlined into every message row instead of disabling it.- **A data import now carries each session's ownership lease by its remaining time instead of its original deadline** — a restore re-applied the deadline unchanged, so one that ran longer than a claim had left committed that claim as expired, including claims held by other nodes whose engines never stopped.- **A refused data import no longer offers a retry that stops live engines** — the one refusal whose retry is destructive now carries `IMPORT_WOULD_ORPHAN_ENGINES` and the dashboard matches it positively, so a refusal that only asks the operator to wait can no longer present a confirm whose OK re-runs the replace-all with `stopOrphans=true`.- **A data import is refused with `409` when another transaction holds the connection** — on SQLite it would otherwise nest inside that transaction and commit into it rather than to disk, so a restore reported as successful could vanish with that transaction's rollback.diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.tsindex 01dcc9fc0..2b72b931c 100644--- a/src/config/env.validation.spec.ts+++ b/src/config/env.validation.spec.ts@@ -179,6 +179,27 @@ describe('validateEnv', () => {},);+ it('rejects a MEDIA_DOWNLOAD_ENABLED typo instead of silently keeping the expensive default on', () => {+ // The odd one out of the boolean family: it is read with `!== 'false' && !== '0' && !== 'no'`+ // (inbound-media-cap.ts), so a typo does not disable a feature — it leaves inbound media being+ // decrypted and base64-inlined into every message row, up to MEDIA_DOWNLOAD_MAX_BYTES apiece.+ // An operator who typed it to turn that OFF gets the most expensive behaviour the gateway has,+ // with no diagnostics anywhere.+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: 'fasle' })).toThrow(/MEDIA_DOWNLOAD_ENABLED/);+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: 'ture' })).toThrow(/MEDIA_DOWNLOAD_ENABLED/);+ // Unlike the strict family this flag is read through a NORMALISING parser — inbound-media-cap.ts+ // trims and lowercases, and inbound-media-cap.spec.ts asserts 'FALSE' / ' false ' disable. Those+ // spellings demonstrably work, so validation must not reject them; only a value the read site+ // cannot recognise at all is a mistake worth failing the boot for.+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: 'False' })).not.toThrow();+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: ' false ' })).not.toThrow();+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: '0' })).not.toThrow();+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: 'no' })).not.toThrow();+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: 'true' })).not.toThrow();+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: 'false' })).not.toThrow();+ expect(() => validateEnv({ MEDIA_DOWNLOAD_ENABLED: '' })).not.toThrow();+ });+it('rejects a SEARCH_PROVIDER typo instead of silently falling back to auto', () => {// A bogus / typo value must fail fast at boot rather than silently selecting the default provider.expect(() => validateEnv({ SEARCH_PROVIDER: 'bogus' })).toThrow(/SEARCH_PROVIDER/);diff --git a/src/config/env.validation.ts b/src/config/env.validation.tsindex 3b2f0e987..68a6c3d5e 100644--- a/src/config/env.validation.ts+++ b/src/config/env.validation.ts@@ -303,6 +303,31 @@ export function validateEnv(config: EnvConfig): EnvConfig {checkBool(key);}+ // MEDIA_DOWNLOAD_ENABLED is the one boolean whose read site NORMALISES before comparing+ // (`inbound-media-cap.ts` trims and lowercases, then treats 'false'/'0'/'no' as off), so the strict+ // check above would reject spellings that demonstrably work — inbound-media-cap.spec.ts asserts+ // 'FALSE' and ' false ' disable. What normalising cannot save it from is a MISSPELLING: every+ // unrecognised value means ENABLED, so `fasle` leaves inbound media being decrypted and+ // base64-inlined into every message row, up to MEDIA_DOWNLOAD_MAX_BYTES apiece — the most+ // expensive behaviour the gateway has, chosen by an operator who asked for the opposite. So accept+ // exactly the vocabulary the read site understands, and fail the boot on anything else.+ const LENIENT_BOOL_VALUES = new Set(['true', '1', 'yes', 'false', '0', 'no']);+ const lenientBoolKey = 'MEDIA_DOWNLOAD_ENABLED';+ const lenientRaw = config[lenientBoolKey];+ if (lenientRaw !== undefined) {+ if (typeof lenientRaw !== 'string') {+ errors.push(`${lenientBoolKey} must be one of true/false/1/0/yes/no`);+ } else {+ const normalized = lenientRaw.trim().toLowerCase();+ if (normalized !== '' && !LENIENT_BOOL_VALUES.has(normalized)) {+ errors.push(+ `${lenientBoolKey} must be one of true/false/1/0/yes/no (got ${JSON.stringify(lenientRaw)}) — ` ++ 'an unrecognised value silently means ENABLED',+ );+ }+ }+ }+// SEARCH_PROVIDER enum: 'auto' selects the built-in DB full-text provider at runtime, 'builtin-fts'// pins it explicitly, 'none' keeps the module and route mounted but registers no provider, so// /search returns 501; use SEARCH_ENABLED=false to omit the module entirely (route 404). Plugin ids