Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): let the plugin registry be redirected off the shared data dir
Yudhi Armyndharis47399d9d81a0
diff --git a/.env.example b/.env.exampleindex fc68209ff..0ff0a312b 100644--- a/.env.example+++ b/.env.example@@ -683,6 +683,11 @@ WEBHOOK_SSRF_PROTECT=true# plugin code kept elsewhere (e.g. in a container layer) is lost while its config and secrets# survive, leaving registry entries that point at code that no longer exists.PLUGINS_DIR=./data/plugins # Plugin directory (default: ./data/plugins)+# Where the plugin REGISTRY and per-plugin storage live (default: ./data). Distinct from+# PLUGINS_DIR, which is where plugin packages are installed. It moves plugin state only: the+# databases, sessions, media and auth dirs each carry their own path knob and none of them follow+# this one.+# PLUGIN_STATE_DIR=./data# Cap on a plugin .zip downloaded by install-from-URL (matches the 5 MB upload limit). A non-positive# or non-numeric value falls back to the default.# PLUGIN_DOWNLOAD_MAX_BYTES=5242880 # default 5 MiBdiff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6be08db98..05f300d06 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- The dashboard CSP nonce is substituted at every occurrence in the served document, not only the first. One placeholder exists today, so a second would have been left reading the literal text and its script refused by the browser.- Outbound webhook deliveries survive a hard crash. Fan-out is fire-and-forget, so a crash between persisting a message and completing its POST lost the delivery with no record in either mode, while the documented contract promises at-least-once. Each delivery is now recorded before it is attempted, retired once the queue or the inline send owns it, and a bounded sweep replays whatever is left stranded using the stored idempotency key so the retry stays deduplicable at the receiver.- Settled outbound delivery records are pruned after `WEBHOOK_OUTBOX_RETENTION_DAYS` (default 7). A record that can still be replayed is never pruned on age, and a non-positive window falls back to the default rather than letting the table grow without bound.+- `PLUGIN_STATE_DIR` moves the plugin registry and per-plugin storage off the default `./data`. It was the one piece of state with no path knob, so a test run rewrote the developer's own registry and every suite in that run inherited whatever the previous one left in it.### Testsdiff --git a/docker-compose.dev.yml b/docker-compose.dev.ymlindex e4d65f8a7..5d1596483 100644--- a/docker-compose.dev.yml+++ b/docker-compose.dev.yml@@ -83,6 +83,7 @@ services:# docker-compose.yml and the application default; set explicitly so the path stays correct even# if the volume moves.- PLUGINS_DIR=${PLUGINS_DIR:-/app/data/plugins}+ - PLUGIN_STATE_DIR=${PLUGIN_STATE_DIR:-}# Plugin install-from-URL redirect policy (ssrf-guard): off unless the exact string 'true'.- PLUGIN_DOWNLOAD_ALLOW_INSECURE_REDIRECTS=${PLUGIN_DOWNLOAD_ALLOW_INSECURE_REDIRECTS:-false}- WEBHOOK_TIMEOUT=${WEBHOOK_TIMEOUT:-10000}diff --git a/docker-compose.yml b/docker-compose.ymlindex f601d94d7..f4906ee04 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -257,6 +257,7 @@ services:- BODY_SIZE_LIMIT=${BODY_SIZE_LIMIT:-}# Plugins- PLUGINS_DIR=${PLUGINS_DIR:-/app/data/plugins}+ - PLUGIN_STATE_DIR=${PLUGIN_STATE_DIR:-}# Plugin install-from-URL redirect policy (ssrf-guard): off unless the exact string 'true'.- PLUGIN_DOWNLOAD_ALLOW_INSECURE_REDIRECTS=${PLUGIN_DOWNLOAD_ALLOW_INSECURE_REDIRECTS:-false}# Securitydiff --git a/src/config/configuration.ts b/src/config/configuration.tsindex c0b7ba4a6..db0da74ce 100644--- a/src/config/configuration.ts+++ b/src/config/configuration.ts@@ -14,10 +14,16 @@ import { readWsRateLimitConfig } from '../modules/events/ws-rate-limit';* from an unrelated string. Deriving both from one value is what keeps a plugin's code and its* registry entry in the same tree.*- * Deliberately NOT env-overridable: every other data path (DATABASE_NAME, MAIN_DATABASE_NAME,+ * There is deliberately no DATA_DIR knob: every other data path (DATABASE_NAME, MAIN_DATABASE_NAME,* SESSION_DATA_PATH, BAILEYS_AUTH_DIR, STORAGE_LOCAL_PATH) carries its own override and none of them- * would follow a DATA_DIR knob, so such a knob would move part of the state while looking like it- * moved all of it.+ * would follow it, so a knob by that name would move part of the state while looking like it moved+ * all of it.+ *+ * PLUGIN_STATE_DIR is that objection answered rather than repeated: this value reaches exactly one+ * consumer, PluginStorageService, so the knob is named for the registry and per-plugin storage it+ * actually moves and claims nothing about the rest of the tree. Without it the plugin registry is+ * the one piece of state a test lane cannot redirect, which is why every e2e suite in a run shares+ * one registry file and rewrites the developer's copy of it.*/export const DEFAULT_DATA_DIR = './data';@@ -74,7 +80,7 @@ export default () => ({// Root of the persistent state tree (see DEFAULT_DATA_DIR). Read by PluginStorageService for the// plugin registry and per-plugin storage; the other data paths keep their own env-specific keys.- dataDir: DEFAULT_DATA_DIR,+ dataDir: process.env.PLUGIN_STATE_DIR || DEFAULT_DATA_DIR,// HTTP server timeouts (Node http.Server). Pinned explicitly so they are operator-tunable and// observable at boot rather than left at Node's implicit defaults. requestTimeout defaults todiff --git a/src/config/env-precedence.ts b/src/config/env-precedence.tsindex c52786bf0..e2c09ea50 100644--- a/src/config/env-precedence.ts+++ b/src/config/env-precedence.ts@@ -178,6 +178,7 @@ export const BLANK_SHADOWED_ENV_KEYS: string[] = ['WEBHOOK_RECONCILE_BATCH_SIZE','WEBHOOK_RECONCILE_MAX_ATTEMPTS','WEBHOOK_OUTBOX_RETENTION_DAYS',+ 'PLUGIN_STATE_DIR','INGRESS_WORKER_CONCURRENCY','WS_RATE_LIMIT_FRAME_PER_SECOND','WS_RATE_LIMIT_FRAME_BURST',diff --git a/test/setup-e2e.ts b/test/setup-e2e.tsindex 0ba2d232f..dfebb85c8 100644--- a/test/setup-e2e.ts+++ b/test/setup-e2e.ts@@ -1,10 +1,14 @@// e2e boot environment. Set BEFORE AppModule is imported (setupFiles phase) so the// app boots against local SQLite with no Redis/queue and no production boot guard.//-// These suites run ONE AT A TIME (`maxWorkers: 1` in jest-e2e.json), and that is load-bearing-// rather than a performance choice. Each suite boots a real application, and not all of an-// application's state is redirected here: `dataDir` is a hard-coded `./data` with no environment-// lever, so every worker's plugin loader read-modify-writes the same `data/plugins/registry.json`.+// These suites run ONE AT A TIME (`maxWorkers: 1` in jest-e2e.json). That began as a fix for+// parallel workers fighting over one plugin registry, and the measurement below records it. Serial+// execution did not close the whole hole: the registry was shared SEQUENTIALLY too. Each suite+// boots a real application, and closing it disables every enabled plugin, which writes+// `status: disabled` into the registry the NEXT suite then boots against. Measured directly:+// set the whatsapp-web.js engine to `enabled`, run one AppModule-booting suite, and it comes back+// `disabled`. The same write landed in the DEVELOPER's `./data/plugins/registry.json`.+// PLUGIN_STATE_DIR below redirects it, the way the databases and the key file are redirected.// Measured on one parallel run: 52 writes from 12 processes, 30 of them landing within 500ms of a// write by a different process. Each individual write is atomic, but the read-modify-write cycle// is not, so workers overwrote and re-read each other's plugin state.@@ -20,6 +24,10 @@ import { tmpdir } from 'os';import { rmSync } from 'fs';process.env.NODE_ENV = 'test';+// The plugin registry + per-plugin storage: the last state a suite could not isolate.+const e2ePluginState = join(tmpdir(), `openwa-e2e-plugins-${process.pid}`);+rmSync(e2ePluginState, { force: true, recursive: true });+process.env.PLUGIN_STATE_DIR = e2ePluginState;process.env.DATABASE_TYPE = 'sqlite';// Isolate the e2e data DB to a throwaway temp file so suites never pollute the developer's// ./data/openwa.sqlite. e2e creates sessions/webhooks and doesn't self-clean, so without this they