Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(scripts): prevent EALLOWSCRIPTS in nested dashboard install under npm 11
Mauricio Sevilla Britto5b14d406782c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex dbafd8c9f..927c19c28 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- **Voice notes on the Baileys engine now carry a waveform.**- The webhook producer enqueues idempotently, and the bundled Redis is pinned `--maxmemory-policy noeviction` so queued jobs are not silently dropped.- A media-storage root the app cannot write to is caught at boot with a clear error, instead of failing on the first write (#1066).+- The `postinstall` hook no longer aborts with `EALLOWSCRIPTS` under npm 11 when the user's `.npmrc` sets `allow-scripts=true`: npm exports that as `npm_config_allow_scripts`, which npm 11 refuses in the nested dashboard install. The hook now strips it from the environment passed to each nested step. Thanks @configurowebmax.- The bundled `docker-compose.yml` now forwards the `SEND_PACING_*`, `MEDIA_CONVERSION_*`/`FFMPEG_PATH` and session-ownership (`NODE_ID`, `NODE_URL`, lease/sweep) variables — previously these features could not be enabled from `.env` in a compose deployment at all.- The four label write tools (`LabelUpsert`, `LabelDelete`, `LabelAddToChat`, `LabelRemoveFromChat`) answered `Internal error` over MCP even though the write had succeeded on WhatsApp, prompting agents to retry a completed operation; they now return `{ success: true }`.- **Boot validation covers the lease and routing knobs.** A heartbeat that does not fit inside the lease TTL (renewals landing after the claim lapsed, so peers adopt sessions from a healthy node), a `NODE_URL` without a scheme, and a non-positive media-conversion knob are now boot errors instead of silent fallbacks or a 500 on the first forward. Forwarded responses also relay the owner's `Retry-After`/`X-RateLimit-*`, and an unusable owner URL answers 503 rather than 500.diff --git a/scripts/postinstall.js b/scripts/postinstall.jsindex 237acbb54..973f5fa00 100644--- a/scripts/postinstall.js+++ b/scripts/postinstall.js@@ -4,11 +4,13 @@* Five conditional steps, each skipped when its target is absent so the hook is a no-op where the* piece is missing (the Docker builder stage copies package*.json long before any source):*- * 1. `npm run dashboard:ci` when dashboard/ exists — the dashboard carries its own lockfile and- * the root install would otherwise leave it without dependencies. A failure here MUST abort- * the install: the old inline hook swallowed spawnSync's exit status, so a red dashboard- * install still reported `npm install` success and the breakage surfaced only at build/run- * time.+ * 1. `npm ci` inside dashboard/ when dashboard/ exists — the dashboard carries its own lockfile and+ * the root install would otherwise leave it without dependencies. We run `npm ci` directly in+ * dashboard/ with any `npm_config_allow_scripts` environment variable stripped so that npm 11+ * does not reject `allow-scripts=true` from user `.npmrc` with EALLOWSCRIPTS. A failure here+ * MUST abort the install: the old inline hook swallowed spawnSync's exit status, so a red+ * dashboard install still reported `npm install` success and the breakage surfaced only at+ * build/run time.* 2. `node scripts/patch-wwebjs-201832.js --best-effort` when the patcher exists. The patcher* itself decides fatality: under --best-effort it warns and exits 0 for a pristine-but-* unpatched tree (no `patch` binary, Baileys-only user), but exits 1 for a HALF-patched@@ -31,14 +33,35 @@ const { spawnSync } = require('child_process');const ROOT = path.join(__dirname, '..');+/**+ * Sanitize an environment object for child invocations.+ *+ * npm 11 rejects `--allow-scripts` in project-scoped installs (`npm ci`, `npm install`) when it+ * originates from the environment (`npm_config_allow_scripts`) rather than `.npmrc` or+ * `package.json`. When a root install is invoked with `allow-scripts=true` in `.npmrc`, npm+ * exports that setting to the lifecycle environment as `npm_config_allow_scripts`. Stripping it+ * prevents nested npm executions (`npm run dashboard:ci` -> `cd dashboard && npm ci`) from+ * failing with EALLOWSCRIPTS while preserving the user's `.npmrc` configuration.+ */+function sanitizeEnv(env = process.env) {+ const clean = { ...env };+ for (const key of Object.keys(clean)) {+ if (/^npm_config_allow[_-]scripts$/i.test(key)) {+ delete clean[key];+ }+ }+ return clean;+}+/** The steps to run for a given repo root, in order. */-function planSteps(root) {+function planSteps(root, env = process.env) {+ const cleanEnv = sanitizeEnv(env);const steps = [];if (fs.existsSync(path.join(root, 'dashboard'))) {steps.push({- name: 'dashboard dependencies (npm run dashboard:ci)',- command: 'npm run dashboard:ci',- options: { stdio: 'inherit', shell: true, cwd: root },+ name: 'dashboard dependencies (npm ci)',+ command: 'npm ci',+ options: { stdio: 'inherit', shell: true, cwd: path.join(root, 'dashboard'), env: cleanEnv },});}const patcher = path.join(root, 'scripts', 'patch-wwebjs-201832.js');@@ -47,7 +70,7 @@ function planSteps(root) {name: 'whatsapp-web.js backport (scripts/patch-wwebjs-201832.js --best-effort)',command: process.execPath,args: [patcher, '--best-effort'],- options: { stdio: 'inherit', cwd: root },+ options: { stdio: 'inherit', cwd: root, env: cleanEnv },});}const previewPatcher = path.join(root, 'scripts', 'patch-wwebjs-newsletter-preview.js');@@ -56,7 +79,7 @@ function planSteps(root) {name: 'whatsapp-web.js newsletter preview backport (scripts/patch-wwebjs-newsletter-preview.js --best-effort)',command: process.execPath,args: [previewPatcher, '--best-effort'],- options: { stdio: 'inherit', cwd: root },+ options: { stdio: 'inherit', cwd: root, env: cleanEnv },});}const statusPatcher = path.join(root, 'scripts', 'patch-wwebjs-status.js');@@ -65,7 +88,7 @@ function planSteps(root) {name: 'whatsapp-web.js status send repair (scripts/patch-wwebjs-status.js --best-effort)',command: process.execPath,args: [statusPatcher, '--best-effort'],- options: { stdio: 'inherit', cwd: root },+ options: { stdio: 'inherit', cwd: root, env: cleanEnv },});}const readySyncPatcher = path.join(root, 'scripts', 'patch-wwebjs-ready-sync.js');@@ -74,7 +97,7 @@ function planSteps(root) {name: 'whatsapp-web.js ready-sync repair (scripts/patch-wwebjs-ready-sync.js --best-effort)',command: process.execPath,args: [readySyncPatcher, '--best-effort'],- options: { stdio: 'inherit', cwd: root },+ options: { stdio: 'inherit', cwd: root, env: cleanEnv },});}return steps;@@ -92,8 +115,8 @@ function failureReason(res) {* Run the planned steps, stopping at the first failure. Returns the process exit code (0 = all* steps ran clean or were absent). `spawn` is injectable for tests.*/-function run(root = ROOT, spawn = spawnSync) {- const steps = planSteps(root);+function run(root = ROOT, spawn = spawnSync, env = process.env) {+ const steps = planSteps(root, env);if (!steps.length) {console.log('postinstall: no dashboard/ or patch script present — nothing to do.');return 0;@@ -116,4 +139,4 @@ if (require.main === module) {process.exit(run());}-module.exports = { planSteps, failureReason, run, ROOT };+module.exports = { sanitizeEnv, planSteps, failureReason, run, ROOT };diff --git a/scripts/postinstall.spec.js b/scripts/postinstall.spec.jsindex f95392036..aad11d8d0 100644--- a/scripts/postinstall.spec.js+++ b/scripts/postinstall.spec.js@@ -13,7 +13,7 @@ const fs = require('fs');const os = require('os');const path = require('path');-const { planSteps, failureReason, run } = require('./postinstall.js');+const { sanitizeEnv, planSteps, failureReason, run } = require('./postinstall.js');const OK = { status: 0, signal: null, error: null };@@ -61,11 +61,13 @@ test('planSteps: empty root plans nothing', () => {});test('planSteps: dashboard only plans the dashboard install (shell, inherited stdio)', () => {- const steps = planSteps(makeRoot({ dashboard: true }));+ const root = makeRoot({ dashboard: true });+ const steps = planSteps(root);assert.equal(steps.length, 1);- assert.equal(steps[0].command, 'npm run dashboard:ci');+ assert.equal(steps[0].command, 'npm ci');assert.equal(steps[0].options.shell, true);assert.equal(steps[0].options.stdio, 'inherit');+ assert.equal(steps[0].options.cwd, path.join(root, 'dashboard'));});test('planSteps: patcher only plans the best-effort backport via the current node', () => {@@ -87,7 +89,7 @@ test('planSteps: newsletter preview patcher plans its own best-effort backport',test('planSteps: both present plans dashboard first, patcher second', () => {const steps = planSteps(makeRoot({ dashboard: true, patcher: true }));assert.equal(steps.length, 2);- assert.equal(steps[0].command, 'npm run dashboard:ci');+ assert.equal(steps[0].command, 'npm ci');assert.equal(steps[1].command, process.execPath);});@@ -112,7 +114,7 @@ test('planSteps: dashboard and all patchers run in stable order', () => {makeRoot({ dashboard: true, patcher: true, previewPatcher: true, statusPatcher: true, readySyncPatcher: true }),);assert.equal(steps.length, 5);- assert.equal(steps[0].command, 'npm run dashboard:ci');+ assert.equal(steps[0].command, 'npm ci');assert.match(steps[1].args[0], /patch-wwebjs-201832\.js$/);assert.match(steps[2].args[0], /patch-wwebjs-newsletter-preview\.js$/);assert.match(steps[3].args[0], /patch-wwebjs-status\.js$/);@@ -135,7 +137,7 @@ test('run: non-zero dashboard install exits 1 and never reaches the patcher (faiconst { calls, spawn } = fakeSpawn([{ status: 1, signal: null, error: null }]);assert.equal(run(makeRoot({ dashboard: true, patcher: true }), spawn), 1);assert.equal(calls.length, 1);- assert.equal(calls[0].command, 'npm run dashboard:ci');+ assert.equal(calls[0].command, 'npm ci');});test('run: spawn error (npm not found) exits 1', () => {@@ -160,3 +162,47 @@ test('failureReason: maps each spawnSync outcome to a cause (null = success)', (assert.equal(failureReason({ status: null, signal: 'SIGKILL', error: null }), 'killed by SIGKILL');assert.match(failureReason({ status: null, signal: null, error: new Error('boom') }), /failed to start — boom/);});++test('sanitizeEnv: strips allow-scripts environment variables across case and separator variations', () => {+ const input = {+ PATH: '/usr/bin',+ npm_config_allow_scripts: 'true',+ NPM_CONFIG_ALLOW_SCRIPTS: 'true',+ 'npm_config_allow-scripts': 'true',+ 'NPM_CONFIG_ALLOW-SCRIPTS': 'true',+ npm_config_allow_scripts_extra: 'keep',+ };+ const result = sanitizeEnv(input);+ assert.deepEqual(result, {+ PATH: '/usr/bin',+ npm_config_allow_scripts_extra: 'keep',+ });+ assert.equal(input.npm_config_allow_scripts, 'true');+});++test('planSteps: strips npm_config_allow_scripts from step options.env to avoid EALLOWSCRIPTS in npm 11', () => {+ const env = {+ PATH: '/usr/bin',+ npm_config_allow_scripts: 'true',+ NPM_CONFIG_ALLOW_SCRIPTS: 'true',+ };+ const steps = planSteps(+ makeRoot({ dashboard: true, patcher: true, previewPatcher: true, statusPatcher: true, readySyncPatcher: true }),+ env,+ );+ assert.equal(steps.length, 5);+ for (const step of steps) {+ assert.equal('npm_config_allow_scripts' in step.options.env, false);+ assert.equal('NPM_CONFIG_ALLOW_SCRIPTS' in step.options.env, false);+ assert.equal(step.options.env.PATH, '/usr/bin');+ }+});++test('run: passes sanitized environment to spawn calls', () => {+ const { calls, spawn } = fakeSpawn([OK]);+ const env = { PATH: '/usr/bin', npm_config_allow_scripts: 'true' };+ run(makeRoot({ dashboard: true }), spawn, env);+ assert.equal(calls.length, 1);+ assert.equal('npm_config_allow_scripts' in calls[0].options.env, false);+ assert.equal(calls[0].options.env.PATH, '/usr/bin');+});