Files
wekan/tests/documentRuntimeDependencies.test.cjs
T
Lauri Ojansivu 130af564c9 Docker image: distroless, with a Node.js entrypoint and no shell.
Artifact Hub's Trivy report of v13.30 listed 218 vulnerabilities in
ghcr.io/wekan/wekan, 214 of them in Debian packages WeKan never runs
(util-linux, login, tar, coreutils, systemd libraries, sqlite) with no
Debian fix to update to. The image is now gcr.io/distroless/cc-debian13
plus libatomic1, node, WeKan, FerretDB and the MongoDB tools; that base
scans at 55 findings, 2 HIGH. Distroless publishes no linux/386, so for
386 the same Debian 13 packages are assembled into scratch, each with
its dpkg record; the release workflow reads both bases from the
Dockerfile and the image keeps all seven platforms. The wekan user
stays 999:999.

With no shell, releases/ferretdb/wekan-entrypoint.mjs replaces
wekan-entrypoint.sh, with the same decisions, exported and tested. It
runs sqlite-recovery.mjs with its own node: the shell entrypoint ran
/build/node, which the image never had, so every start with the bundled
FerretDB stopped with status 127 since v11.52. It also makes cpu-exec's
qemu-user decision itself. The UCS migration launcher and tools, the
UCS hook's volume preparation (ucs-release.py), the MongoDB tool
launchers (symlinks to mongo-tools) and the Nextcloud capability check
run node instead of sh.

Thanks to xet7 !
2026-11-25 00:32:06 +02:00

74 lines
4.8 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { transformSync } = require('@swc/core');
const root = path.resolve(__dirname, '..');
const temp = fs.mkdtempSync(path.join(root, '.tools/tmp/document-runtime-'));
const server = path.join(temp, 'programs/server');
const npm = path.join(server, 'npm/node_modules');
fs.mkdirSync(npm, { recursive: true });
const read = name => fs.readFileSync(path.join(root, name), 'utf8');
function load(name, extra = '') {
const module = { exports: {} };
const context = { module, exports: module.exports, Buffer, Uint8Array, console,
process: { cwd: () => server },
require(name) {
if (name === 'meteor/mongo') return { Mongo: { Collection: class {} } };
if (name === './imageGif') return {};
return require(name);
},
};
vm.runInNewContext(transformSync(read(name) + extra, { module: { type: 'commonjs' } }).code, context);
return module.exports;
}
(async () => {
try {
// Real archive decompression, with dependencies in the release layout.
fs.cpSync(path.join(root, 'node_modules/fflate'), path.join(npm, 'fflate'), { recursive: true });
fs.mkdirSync(path.join(npm, 'pdfjs-dist/legacy/build'), { recursive: true });
fs.writeFileSync(path.join(npm, 'pdfjs-dist/package.json'), '{}');
fs.writeFileSync(path.join(npm, 'pdfjs-dist/legacy/build/pdf.worker.mjs'), '');
fs.mkdirSync(path.join(npm, 'sharp'), { recursive: true });
fs.writeFileSync(path.join(npm, 'sharp/index.js'), 'module.exports = "release-native-module";');
const api = load('server/lib/documentGif.js', '\nexports.test = { officeSearchText, runtimeResolve };').test;
assert.ok(api.runtimeResolve('fflate').startsWith(npm + path.sep), 'must not accidentally use checkout dependencies');
assert.equal(api.runtimeResolve('pdfjs-dist/legacy/build/pdf.worker.mjs'), path.join(npm, 'pdfjs-dist/legacy/build/pdf.worker.mjs'));
assert.equal(api.runtimeResolve('pdfjs-dist/package.json'), path.join(npm, 'pdfjs-dist/package.json'));
assert.throws(() => api.runtimeResolve('wekan-nonexistent-dependency'), { code: 'MODULE_NOT_FOUND' });
const { zipSync, strToU8 } = require('fflate');
const cases = [
['xlsx', {
'xl/sharedStrings.xml': '<sst><si><t>Cloud spreadsheet</t></si></sst>',
'xl/worksheets/sheet1.xml': '<worksheet><c t="s"><v>0</v></c><c><f>PRIVATE_FORMULA</f><v>42</v></c></worksheet>',
}, 'Cloud spreadsheet 42'],
['docx', { 'word/document.xml': '<w:p>Cloud document &amp; text</w:p>' }, 'Cloud document & text'],
['pptx', { 'ppt/slides/slide1.xml': '<a:t>Cloud slides</a:t>' }, 'Cloud slides'],
];
for (const [extension, entries, expected] of cases) {
const archive = zipSync(Object.fromEntries(Object.entries(entries).map(([name, text]) => [name, strToU8(text)])));
assert.equal(await api.officeSearchText(archive, extension), expected);
}
await assert.rejects(api.officeSearchText(zipSync({ 'ignored.xml': strToU8('not a spreadsheet') }), 'xlsx'), /no readable parts/);
await assert.rejects(api.officeSearchText(zipSync({ 'word/document.xml': strToU8('a'.repeat(2 * 1024 * 1024 + 1)) }), 'docx'), /exceeds limit/);
const images = load('server/lib/imageGif.js', '\nexports.loadForTest = loadSharpAtRuntime;');
assert.equal(images.loadForTest(), 'release-native-module');
// Source development also resolves through the parent node_modules.
const sourceRequire = require('node:module').createRequire(path.join(root, 'npm/package.json'));
assert.equal(sourceRequire.resolve('fflate'), require.resolve('fflate'));
// The Docker image is distroless since the Artifact Hub security report:
// Debian's `file` and its libmagic are no longer in it, and MIME detection
// uses the bundled WASM libmagic (wasmagic) that the portable and Windows
// installs already use - the fallback models/lib/mimeDetection.js takes
// when no native `file` answers (tests/portableMimeDetection.test.cjs).
const docker = read('Dockerfile');
const image = docker.slice(docker.lastIndexOf('\nFROM '));
assert.doesNotMatch(image, /apt-get|\bfile\b(?! ?[a-z.\/-]*\.(json|mjs))/, 'the image installs nothing, `file` included');
const mime = read('models/lib/mimeDetection.js');
assert.match(mime, /if \(native\) return native;\n\s+try \{ return await portableMime\(bytes\); \}/, 'no native file: the WASM engine answers');
assert.match(mime, /runtimeRequire\('wasmagic'\)/);
console.log('Release dependencies: XLSX/DOCX/PPTX extraction, rejected archives, PDF assets, native-image lookup and Docker MIME detection without `file` pass.');
} finally { fs.rmSync(temp, { recursive: true, force: true }); }
})().catch(error => { console.error(error); process.exitCode = 1; });