Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): compare resolved paths in the self-invocation guards

Yudhi Armyndharis06fb300e3691
diff --git a/package.json b/package.json
index e55d02e96..648e9dbfd 100644
--- a/package.json
+++ b/package.json
@@ -41,7 +41,7 @@
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
- "test:scripts": "node --test scripts/postinstall.spec.js scripts/patch-wwebjs-newsletter-preview.spec.js scripts/patch-wwebjs-status.spec.js scripts/patch-wwebjs-ready-sync.spec.js scripts/patch-wwebjs-participant-arity.spec.js scripts/patch-wwebjs-block.spec.js scripts/patch-wwebjs-group-description.spec.js scripts/patch-baileys-appstate.spec.js scripts/patch-baileys-newsletter-create.spec.js scripts/dockerfile-patchers.spec.js scripts/check-upstream-surface.spec.mjs scripts/check-sdk-docs.spec.mjs scripts/check-audit.spec.mjs scripts/check-contract-shapes.spec.mjs",
+ "test:scripts": "node --test scripts/postinstall.spec.js scripts/patch-wwebjs-newsletter-preview.spec.js scripts/patch-wwebjs-status.spec.js scripts/patch-wwebjs-ready-sync.spec.js scripts/patch-wwebjs-participant-arity.spec.js scripts/patch-wwebjs-block.spec.js scripts/patch-wwebjs-group-description.spec.js scripts/patch-baileys-appstate.spec.js scripts/patch-baileys-newsletter-create.spec.js scripts/dockerfile-patchers.spec.js scripts/check-upstream-surface.spec.mjs scripts/check-sdk-docs.spec.mjs scripts/check-audit.spec.mjs scripts/check-contract-shapes.spec.mjs scripts/cli-entrypoint-guard.spec.mjs",
"test:docs": "jest src/common/changelog-sections.spec.ts src/common/ci-privilege-gate.spec.ts src/common/docs-ci-jobs.spec.ts src/common/docs-coverage-policy.spec.ts src/common/docs-metrics-list.spec.ts src/common/openapi/docs-06-contract.spec.ts src/common/test-lane-partition.spec.ts src/config/docs-env-example.spec.ts src/config/openapi-contract.spec.ts src/config/openapi-export-pins.spec.ts src/config/wa-web-version-example-pin.spec.ts src/engine/docs-29-counts.spec.ts src/engine/engine-inventory-parity.spec.ts src/engine/engine-parity.spec.ts src/engine/inventory-mapping-direction.spec.ts src/engine/adapters/baileys-voice-waveform.spec.ts src/engine/adapters/wwebjs-newsletter-preview.spec.ts src/modules/audit/audit-coverage.spec.ts src/modules/auth/global-route-fence-coverage.spec.ts src/modules/auth/query-session-scope-coverage.spec.ts src/modules/docker/compose-network.spec.ts src/modules/queue/bull-board-mount-coverage.spec.ts src/common/release-gate-parity.spec.ts src/database/migrations/__tests__/migration-drift.spec.ts --testPathIgnorePatterns=/node_modules/",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
diff --git a/scripts/check-audit.mjs b/scripts/check-audit.mjs
index 342855976..f615516c7 100644
--- a/scripts/check-audit.mjs
+++ b/scripts/check-audit.mjs
@@ -22,6 +22,8 @@
* Run locally: `npm run check:audit`.
*/
import { execFileSync } from 'node:child_process';
+import { resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
/**
* Advisories CI will not stop on. Every entry states why, and what removes it.
@@ -124,7 +126,14 @@ export function evaluate(report, allowlist = ALLOWLIST) {
}
// Guarded so the spec can import the two functions above without running a real audit.
-if (import.meta.url === `file://${process.argv[1]}`) {
+//
+// Compare RESOLVED PATHS, not a hand-built file URL. `import.meta.url` is percent-encoded, so any
+// checkout path needing escaping (a space, a `#`, non-ASCII) made `file://${process.argv[1]}`
+// differ and the gate exited 0 having run no audit at all. On Windows it never matched: argv[1] is
+// a native path with backslashes and a drive letter. `fileURLToPath` decodes the URL to a native
+// path and `resolve` normalises argv[1], which is the comparison check-sdk-docs.mjs and
+// check-upstream-surface.mjs already use.
+if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
const report = runAudit();
const errors = evaluate(report);
diff --git a/scripts/check-contract-shapes.mjs b/scripts/check-contract-shapes.mjs
index 66da4ac64..85a14879d 100644
--- a/scripts/check-contract-shapes.mjs
+++ b/scripts/check-contract-shapes.mjs
@@ -44,6 +44,7 @@
*/
import { readFileSync, readdirSync } from 'node:fs';
+import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
// Resolve from the script's own location, not process.cwd() — same reason check-sdk-coverage.mjs
@@ -1068,7 +1069,11 @@ export function parseJavaTypes(sources) {
// ── CLI driver ──
-const isDirectRun = process.argv[1] && import.meta.url.endsWith(process.argv[1].split('/').pop());
+// Resolved-path comparison, not a basename match: splitting on `/` finds no separator in a Windows
+// path so the whole native path became the "basename" and never matched, and a bare `endsWith` on a
+// basename would also fire for any other script sharing this file's name. Same comparison as
+// check-sdk-docs.mjs and check-upstream-surface.mjs.
+const isDirectRun = Boolean(process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url));
if (isDirectRun) {
const openapi = JSON.parse(readFileSync(`${REPO_ROOT}openapi.json`, 'utf8'));
const schemas = openapi.components.schemas;
diff --git a/scripts/cli-entrypoint-guard.spec.mjs b/scripts/cli-entrypoint-guard.spec.mjs
new file mode 100644
index 000000000..1c54e8b7a
--- /dev/null
+++ b/scripts/cli-entrypoint-guard.spec.mjs
@@ -0,0 +1,74 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { readFileSync, readdirSync } from 'node:fs';
+import { dirname, join, resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+/**
+ * Structural guard for the "gate that never ran" class of bug.
+ *
+ * Every CLI script here is BOTH a module its spec imports and a command CI runs, so each needs a
+ * self-invocation guard. Get that guard wrong and the failure is silent in the worst direction: the
+ * script exits 0 having executed nothing, and the job it backs reports a pass.
+ *
+ * Two forms were wrong. `import.meta.url === \`file://${process.argv[1]}\`` compares a
+ * percent-encoded URL against a raw native path, so any checkout path needing escaping (a space, a
+ * `#`, non-ASCII) silently disabled the gate, and on Windows it never matched at all. A basename
+ * match built with `process.argv[1].split('/')` finds no separator in a Windows path and is loose
+ * enough to fire for an unrelated script of the same name.
+ *
+ * The correct comparison is resolved path against decoded URL. This test pins it for every script
+ * rather than for the two that were caught.
+ */
+const SCRIPTS_DIR = dirname(fileURLToPath(import.meta.url));
+
+/** Anything that looks like a main-module check, however it is spelled. */
+const GUARD_LINE = /process\.argv\[1\]|import\.meta\.url\s*===/;
+
+/** The one shape that is correct on every platform and every path. */
+const CORRECT_GUARD = /resolve\(process\.argv\[1\]\)\s*===\s*fileURLToPath\(import\.meta\.url\)/;
+
+const BANNED = [
+ { pattern: /import\.meta\.url\s*===\s*`file:\/\/\$\{process\.argv\[1\]\}`/, why: 'compares a percent-encoded URL against a raw native path' },
+ { pattern: /process\.argv\[1\]\.split\(['"]\/['"]\)/, why: 'splits a path on "/", which finds no separator on Windows' },
+ { pattern: /import\.meta\.url\.endsWith\(/, why: 'a basename match also fires for an unrelated script of the same name' },
+];
+
+const cliScripts = readdirSync(SCRIPTS_DIR)
+ .filter(name => name.endsWith('.mjs') && !name.endsWith('.spec.mjs'))
+ .map(name => ({ name, source: readFileSync(join(SCRIPTS_DIR, name), 'utf8') }));
+
+/** Scripts that actually carry a main-module check; the rest are pure modules or pure commands. */
+const guarded = cliScripts.filter(s => GUARD_LINE.test(s.source));
+
+test('the scan sees the scripts it is meant to police', () => {
+ // Non-vacuity: a refactor that renamed the directory or the extension would otherwise leave this
+ // suite passing over an empty set, which is the same silent-pass failure it exists to prevent.
+ assert.ok(cliScripts.length >= 8, `expected the CLI scripts to be found, saw ${cliScripts.length}`);
+ assert.ok(guarded.length >= 4, `expected several self-invocation guards, saw ${guarded.length}`);
+});
+
+test('no script uses a self-invocation guard that can silently disable it', () => {
+ const offenders = [];
+ for (const { name, source } of guarded) {
+ for (const { pattern, why } of BANNED) {
+ if (pattern.test(source)) offenders.push(`${name}: ${why}`);
+ }
+ }
+ assert.deepEqual(offenders, [], `banned self-invocation guard:\n${offenders.join('\n')}`);
+});
+
+test('every guarded script compares a resolved path against the decoded module URL', () => {
+ const offenders = guarded.filter(s => !CORRECT_GUARD.test(s.source)).map(s => s.name);
+ assert.deepEqual(offenders, [], `missing the resolved-path guard: ${offenders.join(', ')}`);
+});
+
+test('the correct guard holds for a path that needs URL escaping', () => {
+ // The concrete case the audit gate silently failed on. Proven here rather than asserted: a URL
+ // built by hand from this path does not equal import.meta.url, while the resolved comparison does.
+ const spaced = '/tmp/openwa probe/check.mjs';
+ const asUrl = new URL(`file://${encodeURI(spaced)}`).href;
+
+ assert.notEqual(asUrl, `file://${spaced}`, 'the hand-built URL should differ once the path is escaped');
+ assert.equal(resolve(fileURLToPath(asUrl)), spaced, 'the resolved comparison should still match');
+});