Files
wekan/tests/imageBuildOnlyModules.test.cjs
T
Lauri Ojansivu 6157094ab8 Stop shipping npm and node-gyp, and raise the bundle's own npm packages.
A container scan of ghcr.io/wekan/wekan:v10.91 reported 80 findings against
"Node.js", three of them CRITICAL, and not one of them was in code WeKan
runs.

Three CRITICAL and most of the HIGH findings were three copies of one
package - tar 6.2.1 twice, tar 7.5.11 once - carried by two BUILD tools
left in the finished image:

- node-gyp and @mapbox/node-pre-gyp are meteor-dev-bundle dependencies that
  compile native modules during the npm install in programs/server. WeKan
  compiles nothing at run time; uWebSockets.js, bcrypt and argon2 all ship
  prebuilt .node files that node-gyp-build picks at require time. Their tree
  is 83 of the 120 packages that install leaves behind, and it brought tar
  6.2.1 (node-gyp, cacache), brace-expansion, minimatch and npm's networking
  stack with it.
- npm itself runs exactly once, for that install. The container starts bash
  /build/wekan-entrypoint.sh and never calls it, so its bundled tar 7.5.11,
  sigstore 4.1.0, @sigstore/*, ip-address 10.1.0 and brace-expansion 5.0.4
  were image content no code path could reach.

releases/prune-build-only-modules.mjs removes the first. It is a
REACHABILITY walk, not a list of 83 names - start from every dependency of
programs/server/package.json except those two, follow each package's own
dependencies, keep the closure - so it does not go stale when Meteor
changes its dev-bundle. The Dockerfile removes npm and npx in cleanup;
node stays. Verified by booting the pruned bundle: identical failure at the
database, no missing module.

The rest of the report was the npm packages Meteor's own packages bundle -
nodemailer 8.0.3, openpgp 5.11.1, svgo 2.8.2, postcss 8.5.1, nanoid 3.3.15,
qs 6.13.0, cookie 0.4.1, on-headers 1.0.2, tmp 0.2.3, lodash 4.17.21, diff
3.5.0, body-parser 1.20.3, @babel/runtime 7.20.7, underscore 1.13.7.
Npm.depends names an exact version and meteor build copies it into
programs/server/npm/node_modules/meteor/<package>/node_modules, where no
package.json or overrides entry here is consulted - so the bundle is the
only place they can be raised.
releases/bump-bundle-npm-deps.mjs does that from the minimums in
releases/bundle-npm-security-bumps.json, with --ignore-scripts so no
prebuilt native is rebuilt, replacing only copies below the minimum.

A minimum stays inside the major the package was built against, and that
was learned rather than assumed: uuid's lowest fixed release, 11.1.1, moved
its entry point to dist/cjs/index.js and the bundle - which recorded
dist/index.js at build time - died on boot with "Cannot find module
.../uuid/dist/index.js". It is in notFixable with that error, beside
lodash.template, which has no fixed release at all.

Both run in every place a bundle is made: the amd64 build, the arm64
container, three Windows legs, two macOS legs, install-node-for-arch.sh for
the emulated arches, and the Dockerfile - each reinstalls programs/server,
which is also why each bumps again (meteor-dev-bundle pins underscore
1.13.7, CVE-2026-27601, over the bumped copy).

tests/imageBuildOnlyModules.test.cjs and
tests/bundleNpmSecurityBumps.test.cjs pin both, and buildScriptParity's
skip list records why these two scripts are not menu entries. The debian
13.6 target's 176 findings are all "Fixed in: -" and no upgrade closes
them; the build/<tool> and build/ferretdb targets are fixed in
wekan/mongo-tools-patches and wekan/FerretDB.

Thanks to xet7 !
2026-08-13 23:51:23 +03:00

111 lines
5.5 KiB
JavaScript

'use strict';
// What is BUILT WITH does not have to be SHIPPED.
// Run: node tests/imageBuildOnlyModules.test.cjs
//
// A container scan of ghcr.io/wekan/wekan:v10.91 reported 80 findings against
// "Node.js", three of them CRITICAL, and not one of them was in code WeKan runs:
//
// tar 6.2.1 x2 node-gyp's and cacache's own copies, in the bundle
// tar 7.5.11 npm's own copy, in /usr/local/lib/node_modules/npm
// sigstore, @sigstore/*, ip-address, brace-expansion npm's, same place
//
// Two build tools, shipped by accident. `npm` runs exactly once in the image -
// the `npm install` that populates programs/server - and the container starts
// bash + wekan-entrypoint.sh, which never calls it. node-gyp and
// @mapbox/node-pre-gyp are there to COMPILE native modules during that install,
// and the bundle compiles none: uWebSockets.js, bcrypt and argon2 all ship
// prebuilt .node files that node-gyp-build picks at require time.
//
// So both go, after the install that needs them:
// - releases/prune-build-only-modules.mjs walks what is reachable from
// programs/server/package.json without those two, and removes the rest - 83
// of 120 packages, verified by booting the pruned bundle.
// - the Dockerfile deletes npm and npx in its cleanup step.
//
// This suite pins that both still happen, in the image AND in every .zip.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const repoRoot = path.resolve(__dirname, '..');
const read = f => fs.readFileSync(path.join(repoRoot, f), 'utf8');
const dockerfile = read('Dockerfile');
const workflow = read('.github/workflows/release-all.yml');
const installNode = read('releases/install-node-for-arch.sh');
const pruner = read('releases/prune-build-only-modules.mjs');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('imageBuildOnlyModules:');
test('the pruner exists and is driven by reachability, not by a list', () => {
// A hardcoded list of 83 package names would be wrong the first time Meteor
// changed a dev-bundle dependency, and wrong silently.
assert.ok(/BUILD_ONLY = \['node-gyp', '@mapbox\/node-pre-gyp'\]/.test(pruner),
'the two build-only roots are named');
assert.ok(/keep\.add\(name\)/.test(pruner) && /depsOf\(name\)/.test(pruner),
'and everything else is kept or dropped by walking dependencies from them');
});
test('it never touches the Meteor packages\' own npm dependencies', () => {
// programs/server/npm/node_modules/meteor/<package>/node_modules is loaded at
// run time. Only the top level of programs/server/node_modules is pruned.
assert.ok(/does NOT touch/.test(pruner) && /npm\/node_modules/.test(pruner),
'the script says so');
assert.ok(!/rmSync\([^)]*npm[/\\]node_modules/.test(pruner),
'and removes nothing under npm/node_modules');
});
test('the image prunes after its install, and before the bundle is moved', () => {
const install = dockerfile.indexOf('npm install --prefix ./bundle/programs/server');
const prune = dockerfile.indexOf('node /tmp/prune-build-only-modules.mjs');
const move = dockerfile.indexOf('mv /home/wekan/app/bundle /build');
assert.ok(install > 0 && prune > install && move > prune,
'install, then prune, then move');
assert.ok(/COPY --chmod=755 releases\/prune-build-only-modules\.mjs/.test(dockerfile),
'and the script is copied in, or the RUN dies on a missing file');
});
test('the image ships no npm and no npx', () => {
assert.ok(/rm -rf \/usr\/local\/lib\/node_modules\/npm \/usr\/local\/bin\/npm \/usr\/local\/bin\/npx/
.test(dockerfile), 'npm, npx and npm\'s own node_modules are removed');
const remove = dockerfile.indexOf('rm -rf /usr/local/lib/node_modules/npm');
const install = dockerfile.indexOf('npm install --prefix ./bundle/programs/server');
assert.ok(remove > install,
'and removed AFTER the install that needs it, not before');
});
test('node itself stays - that is what runs WeKan (negative)', () => {
// The guard against over-correcting: this is about npm, not about node.
assert.ok(!/rm -rf [^\n]*\/usr\/local\/bin\/node\b/.test(dockerfile),
'the node binary is never removed');
assert.ok(/ln -s "\/usr\/local\/bin\/node" "\/usr\/local\/bin\/nodejs"/.test(dockerfile),
'and is still linked as nodejs');
});
test('every leg that installs programs/server prunes afterwards', () => {
// A leg that installs and does not prune ships the tree again in its .zip.
// `bash "$SRC/releases/npm-retry.sh" npm install` and the unquoted container
// form; the bcrypt replacements use --ignore-scripts and are not these.
const installs = workflow.split('\n')
.filter(l => /npm-retry\.sh"? npm install/.test(l) && !/--ignore-scripts/.test(l));
assert.ok(installs.length >= 6, `expected the per-arch installs, found ${installs.length}`);
const prunes = (workflow.match(/prune-build-only-modules\.mjs/g) || []).length;
assert.ok(prunes >= installs.length - 1,
`${installs.length} bundle installs but only ${prunes} prunes`);
assert.ok(/prune-build-only-modules\.mjs/.test(installNode),
'and the extra-arch containers prune in install-node-for-arch.sh');
});
test('the container legs prune INSIDE the container, where node is', () => {
// /releases is mounted read-only and /bundle read-write; the runner's own node
// is not what those legs run.
assert.ok(/node \/releases\/prune-build-only-modules\.mjs \/bundle/.test(workflow),
'the arm64 leg prunes through the mounted path');
});
console.log(`\nimageBuildOnlyModules: ${passed} tests passed`);