Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(build): derive dockerignore gate inputs from COPY

Yudhi Armyndharis5153e5bb438f
diff --git a/package.json b/package.json
index 3f7302f0b..e5073a53d 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/proxy-addr.spec.js scripts/music-metadata.spec.js 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-wwebjs-media-id.spec.js scripts/patch-wwebjs-send-error.spec.js scripts/patch-baileys-appstate.spec.js scripts/patch-baileys-newsletter-create.spec.js scripts/patch-baileys-pairing.spec.js scripts/baileys-pairing.integration.spec.mjs scripts/dockerfile-patchers.spec.js scripts/smoke-test-docker-proxy.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/check-sdk-coverage.spec.mjs scripts/cli-entrypoint-guard.spec.mjs",
+ "test:scripts": "node --test scripts/proxy-addr.spec.js scripts/music-metadata.spec.js 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-wwebjs-media-id.spec.js scripts/patch-wwebjs-send-error.spec.js scripts/patch-baileys-appstate.spec.js scripts/patch-baileys-newsletter-create.spec.js scripts/patch-baileys-pairing.spec.js scripts/baileys-pairing.integration.spec.mjs scripts/dockerfile-patchers.spec.js scripts/check-dockerignore.spec.mjs scripts/smoke-test-docker-proxy.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/check-sdk-coverage.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/docs-next-version.spec.ts src/common/docs-sdk-rate-limit.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/docs-27-backfill-engine.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 src/common/docs-governance.spec.ts --testPathIgnorePatterns=/node_modules/",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
diff --git a/scripts/check-dockerignore.mjs b/scripts/check-dockerignore.mjs
index 177a255d2..1e8817976 100644
--- a/scripts/check-dockerignore.mjs
+++ b/scripts/check-dockerignore.mjs
@@ -11,11 +11,12 @@
* 1. Non-build / sensitive material (private working notes, VCS metadata, runtime data, local
* env files, agent/tool workspaces, dependency and build-output dirs) is rejected.
* 2. Every input the multi-stage build actually needs (package manifests, src/, the dashboard
- * source, the backport patcher, docker-entrypoint.sh, TS configs) still passes.
+ * source, docker-entrypoint.sh, TS configs, and every file the Dockerfile COPYs by name,
+ * derived from it) still passes.
*
* Run locally: `npm run check:dockerignore`. Runs in CI (lint job).
*/
-import { readFileSync } from 'node:fs';
+import { readFileSync, readdirSync } from 'node:fs';
const root = new URL('../', import.meta.url);
const text = readFileSync(new URL('.dockerignore', root), 'utf8');
@@ -161,12 +162,44 @@ const mustKeep = [
];
const errors = [];
+
+// Everything else the Dockerfile COPYs out of the build context is an input too. Derive those instead
+// of restating them: a hand-written list lost eight of the patchers, and a rule dropping one of them
+// then broke the image build rather than this check. A copy from another stage, a flag, the
+// destination and `.` (the whole context) are not inputs.
+const dockerfile = readFileSync(new URL('Dockerfile', root), 'utf8').replace(/\\\n/g, ' ');
+const wildcard = /[*?[]/;
+function copyInputs(source) {
+ if (!wildcard.test(source)) return [source];
+ const cut = source.lastIndexOf('/') + 1;
+ const dir = source.slice(0, cut);
+ if (wildcard.test(dir)) {
+ errors.push(`the Dockerfile COPYs '${source}', and this check expands a wildcard only in the file name.`);
+ return [];
+ }
+ const name = globToRegExp(source.slice(cut));
+ return readdirSync(new URL(dir || './', root))
+ .filter(entry => name.test(entry))
+ .map(entry => dir + entry);
+}
+const copied = [];
+for (const [, args] of dockerfile.matchAll(/^COPY\s+(.+)$/gim)) {
+ const words = args.trim().split(/\s+/);
+ if (words.some(word => word.startsWith('--from='))) continue;
+ const sources = words.filter(word => !word.startsWith('--')).slice(0, -1);
+ copied.push(...sources.filter(source => source !== '.').flatMap(copyInputs));
+}
+if (copied.length === 0) {
+ errors.push('no COPY source found in the Dockerfile; the COPY parser in this check needs updating.');
+}
+const keep = [...new Set([...mustKeep, ...copied])];
+
for (const path of mustReject) {
if (!ignored(path)) {
errors.push(`.dockerignore does not reject '${path}' — it would leak into the Docker build context/cache/layers.`);
}
}
-for (const path of mustKeep) {
+for (const path of keep) {
if (ignored(path)) {
errors.push(`.dockerignore rejects '${path}' — the Dockerfile build needs it in the context.`);
}
@@ -178,4 +211,4 @@ if (errors.length) {
console.error('\nFix .dockerignore so non-build material stays out and build inputs stay in, then re-run `npm run check:dockerignore`.\n');
process.exit(1);
}
-console.log(`✓ Docker-context check OK (${rules.length} .dockerignore rules, ${mustReject.length} rejected / ${mustKeep.length} kept paths asserted).`);
+console.log(`✓ Docker-context check OK (${rules.length} .dockerignore rules, ${mustReject.length} rejected / ${keep.length} kept paths asserted).`);
diff --git a/scripts/check-dockerignore.spec.mjs b/scripts/check-dockerignore.spec.mjs
new file mode 100644
index 000000000..067c1f50a
--- /dev/null
+++ b/scripts/check-dockerignore.spec.mjs
@@ -0,0 +1,89 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { spawnSync } from 'node:child_process';
+import { fileURLToPath } from 'node:url';
+
+/**
+ * The gate resolves its root from its own location, so each case copies the script into a throwaway
+ * root next to a Dockerfile and a .dockerignore of its own and runs that copy. The repository's files
+ * are only read.
+ *
+ * The Dockerfile COPYs its patchers by name. A .dockerignore rule that drops one still printed OK
+ * while the gate asserted only a hand-picked few, and the break surfaced later as a COPY failure in
+ * the multi-arch docker job.
+ */
+const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
+const GATE = join(ROOT, 'scripts', 'check-dockerignore.mjs');
+const REAL_DOCKERFILE = readFileSync(join(ROOT, 'Dockerfile'), 'utf8');
+const REAL_DOCKERIGNORE = readFileSync(join(ROOT, '.dockerignore'), 'utf8');
+
+/** Run a copy of the gate in a fresh root holding `files`; `files` maps a relative path to its text. */
+function runGate(files) {
+ const root = mkdtempSync(join(tmpdir(), 'openwa-dockerignore-'));
+ try {
+ mkdirSync(join(root, 'scripts'));
+ copyFileSync(GATE, join(root, 'scripts', 'check-dockerignore.mjs'));
+ for (const [path, text] of Object.entries(files)) {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ writeFileSync(join(root, path), text);
+ }
+ const run = spawnSync(process.execPath, [join(root, 'scripts', 'check-dockerignore.mjs')], { encoding: 'utf8' });
+ return { status: run.status, out: `${run.stdout}${run.stderr}` };
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+}
+
+const withRule = rule => ({ Dockerfile: REAL_DOCKERFILE, '.dockerignore': `${REAL_DOCKERIGNORE}\n${rule}\n` });
+
+test('the repository passes its own gate', () => {
+ const run = runGate({ Dockerfile: REAL_DOCKERFILE, '.dockerignore': REAL_DOCKERIGNORE });
+ assert.equal(run.status, 0, run.out);
+});
+
+test('a rule that drops any patcher the Dockerfile copies fails the gate', () => {
+ const patchers = readdirSync(join(ROOT, 'scripts')).filter(
+ name => name.startsWith('patch-') && name.endsWith('.js') && !name.endsWith('.spec.js'),
+ );
+ // Non-vacuity: an empty directory scan would pass every assertion below.
+ assert.ok(patchers.length >= 10, `expected the patchers to be found, saw ${patchers.length}`);
+ for (const patcher of patchers) {
+ const run = runGate(withRule(`scripts/${patcher}`));
+ assert.equal(run.status, 1, `${patcher} is dropped by a rule and the gate passed`);
+ assert.match(run.out, new RegExp(`rejects 'scripts/${patcher.replaceAll('.', '\\.')}'`), run.out);
+ }
+});
+
+test('a wildcard COPY source is expanded against the tree', () => {
+ const run = runGate({
+ Dockerfile: 'COPY pkg-*.txt ./\n',
+ 'pkg-a.txt': '',
+ 'pkg-b.txt': '',
+ '.dockerignore': `${REAL_DOCKERIGNORE}\npkg-b.txt\n`,
+ });
+ assert.equal(run.status, 1, run.out);
+ assert.match(run.out, /rejects 'pkg-b\.txt'/);
+ assert.doesNotMatch(run.out, /rejects 'pkg-a\.txt'/);
+});
+
+test('a COPY from another stage, a flag and the destination are not build inputs', () => {
+ const dockerfile = [
+ 'COPY --chown=openwa:openwa scripts/lib-env.sh ./scripts/',
+ 'COPY --from=builder /app/dist ./dist',
+ 'COPY . .',
+ '',
+ ].join('\n');
+ // dist/ is rejected by the real .dockerignore, so counting the stage copy's source or the
+ // destination would fail this run.
+ const run = runGate({ Dockerfile: dockerfile, '.dockerignore': REAL_DOCKERIGNORE });
+ assert.equal(run.status, 0, run.out);
+});
+
+test('a Dockerfile with no COPY source fails instead of passing for lack of input', () => {
+ const run = runGate({ Dockerfile: 'FROM scratch\n', '.dockerignore': REAL_DOCKERIGNORE });
+ assert.equal(run.status, 1, run.out);
+ assert.match(run.out, /COPY/);
+});