Files
wekan/tests/ucsDockerExec.test.mjs
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

178 lines
9.3 KiB
JavaScript

// The UCS migration's `docker exec` without the Docker CLI.
//
// Debian's docker-cli was in the WeKan image only so the UCS migration worker
// could run `mongo` and `mongoexport` inside the old database container. It is
// a Go program, and its Go 1.24 standard library was most of the image's Quay
// vulnerability scan. releases/ucs-migration/docker-exec.mjs speaks the Docker
// Engine API over the socket instead; this drives it against a fake engine on a
// unix socket and checks the image no longer installs any Docker CLI.
//
// Run: node tests/ucsDockerExec.test.mjs
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import net from 'node:net';
import os from 'node:os';
import path from 'node:path';
import { spawn, spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
const SCRIPT = path.join(ROOT, 'releases/ucs-migration/docker-exec.mjs');
const { createDemuxer, socketPath } = await import(SCRIPT);
const frame = (type, text) => {
const payload = Buffer.from(text);
const head = Buffer.alloc(8);
head[0] = type;
head.writeUInt32BE(payload.length, 4);
return Buffer.concat([head, payload]);
};
// A minimal Docker Engine: exec create, an upgraded exec start that writes the
// given frames in tiny pieces, and exec inspect.
function fakeEngine({ container = 'wekan-db', frames = [], exitCode = 0, startStatus = 101 } = {}) {
const dir = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'docker-exec-'));
const sock = path.join(dir, 'docker.sock');
const seen = [];
const server = net.createServer((conn) => {
let buf = Buffer.alloc(0);
conn.on('data', (chunk) => {
buf = Buffer.concat([buf, chunk]);
const end = buf.indexOf('\r\n\r\n');
if (end < 0) return;
const head = buf.subarray(0, end).toString();
const length = Number(/content-length: (\d+)/i.exec(head)?.[1] || 0);
if (buf.length < end + 4 + length) return;
const [method, url] = head.split(' ');
const body = buf.subarray(end + 4, end + 4 + length).toString();
seen.push({ method, url, body: body ? JSON.parse(body) : null, head });
buf = Buffer.alloc(0);
const json = (status, obj) => conn.end(`HTTP/1.1 ${status} X\r\nContent-Type: application/json\r\n` +
`Content-Length: ${Buffer.byteLength(JSON.stringify(obj))}\r\nConnection: close\r\n\r\n${JSON.stringify(obj)}`);
if (method === 'POST' && url === `/containers/${container}/exec`) return json(201, { Id: 'exec1' });
if (method === 'POST' && url.startsWith('/containers/')) return json(404, { message: `No such container: ${decodeURIComponent(url.split('/')[2])}` });
if (method === 'POST' && url === '/exec/exec1/start') {
if (startStatus !== 101) return json(startStatus, { message: 'start refused' });
const all = Buffer.concat([Buffer.from('HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\n' +
'Connection: Upgrade\r\nUpgrade: tcp\r\n\r\n'), ...frames]);
// Three bytes at a time: headers and payloads split across reads.
let i = 0;
const pump = () => { if (i >= all.length) return conn.end(); conn.write(all.subarray(i, i += 3)); setImmediate(pump); };
return pump();
}
if (method === 'GET' && url === '/exec/exec1/json') return json(200, { ExitCode: exitCode, Running: false });
return json(500, { message: 'unexpected' });
});
});
return new Promise((resolve) => server.listen(sock, () => resolve({
sock, seen, close: () => new Promise((r) => server.close(() => { fs.rmSync(dir, { recursive: true, force: true }); r(); })),
})));
}
function run(args, env) {
return new Promise((resolve) => {
const child = spawn(process.execPath, [SCRIPT, ...args], { env: { ...process.env, ...env } });
let stdout = '', stderr = '';
child.stdout.on('data', (d) => { stdout += d; });
child.stderr.on('data', (d) => { stderr += d; });
child.on('close', (code) => resolve({ code, stdout, stderr }));
});
}
test('runs the command in the container and keeps stdout, stderr and the exit code apart', async () => {
const big = 'x'.repeat(70000);
const engine = await fakeEngine({
frames: [frame(1, '{"a":1}\n'), frame(2, 'connected to: 127.0.0.1\n'), frame(1, `${big}\n`), frame(1, '{"b":2}\n')],
exitCode: 3,
});
try {
const r = await run(['wekan-db', 'mongoexport', '--db', 'wekan', '-c', 'cards'], { DOCKER_HOST: `unix://${engine.sock}` });
assert.equal(r.code, 3, r.stderr);
assert.equal(r.stdout, `{"a":1}\n${big}\n{"b":2}\n`);
assert.equal(r.stderr, 'connected to: 127.0.0.1\n');
const create = engine.seen[0];
assert.equal(create.url, '/containers/wekan-db/exec');
assert.deepEqual(create.body.Cmd, ['mongoexport', '--db', 'wekan', '-c', 'cards']);
assert.equal(create.body.Tty, false);
assert.equal(create.body.AttachStdin, false);
assert.match(engine.seen[1].head, /Upgrade: tcp/);
} finally { await engine.close(); }
});
test('exit status 0 passes through', async () => {
const engine = await fakeEngine({ frames: [frame(1, '1\n')], exitCode: 0 });
try {
const r = await run(['wekan-db', 'mongo', '--quiet', 'wekan', '--eval', 'db.cards.count()'], { DOCKER_HOST: `unix://${engine.sock}` });
assert.equal(r.code, 0);
assert.equal(r.stdout, '1\n');
} finally { await engine.close(); }
});
test('Docker refusals exit 125 with the reason, never 0 (negative)', async () => {
const engine = await fakeEngine({ container: 'other' });
try {
let r = await run(['wekan-db', 'mongo'], { DOCKER_HOST: `unix://${engine.sock}` });
assert.equal(r.code, 125);
assert.match(r.stderr, /HTTP 404 No such container: wekan-db/);
r = await run(['wekan-db', 'mongo'], { DOCKER_HOST: `unix://${engine.sock}.missing` });
assert.equal(r.code, 125);
assert.match(r.stderr, /ENOENT/);
r = await run(['wekan-db'], {});
assert.equal(r.code, 125);
assert.match(r.stderr, /usage/);
r = await run(['wekan-db', 'mongo'], { DOCKER_HOST: 'tcp://127.0.0.1:2375' });
assert.equal(r.code, 125);
assert.match(r.stderr, /only a unix:\/\/ DOCKER_HOST/);
} finally { await engine.close(); }
const refused = await fakeEngine({ startStatus: 409 });
try {
const r = await run(['wekan-db', 'mongo'], { DOCKER_HOST: `unix://${refused.sock}` });
assert.equal(r.code, 125);
assert.match(r.stderr, /exec start: HTTP 409/);
} finally { await refused.close(); }
});
test('the demuxer reassembles frames split anywhere', () => {
const out = [], err = [];
const demux = createDemuxer((b) => out.push(b.toString()), (b) => err.push(b.toString()));
const all = Buffer.concat([frame(1, 'ab'), frame(2, 'cd'), frame(1, '')]);
for (const byte of all) demux(Buffer.from([byte]));
assert.deepEqual(out, ['ab', '']);
assert.deepEqual(err, ['cd']);
assert.equal(socketPath({}), '/var/run/docker.sock');
assert.equal(socketPath({ DOCKER_HOST: 'unix:///x/d.sock' }), '/x/d.sock');
});
test('the image installs no Docker CLI or other Go client, and the migration tools use docker-exec.mjs', () => {
const dockerfile = fs.readFileSync(path.join(ROOT, 'Dockerfile'), 'utf8');
const installs = [...dockerfile.matchAll(/^[^#\n]*apt-get install[^\n]*/gm)].map((m) => m[0]).join('\n');
assert.doesNotMatch(installs, /\b(docker-cli|docker\.io|docker-ce|docker-ce-cli|containerd|podman|golang)\b/);
// The image is distroless - no shell, no /usr/bin/env - so the tools are
// Node.js with an absolute interpreter, which the kernel runs directly.
for (const tool of ['mongo', 'mongoexport']) {
const file = path.join(ROOT, 'releases/ucs-migration/tools', tool);
const src = fs.readFileSync(file, 'utf8');
assert.match(src, /^#!\/usr\/local\/bin\/node\n/);
assert.ok(src.includes(`[path.join(here, '..', 'docker-exec.mjs'), 'wekan-db', '${tool}', ...process.argv.slice(2)]`));
assert.doesNotMatch(src.split('\n').filter(l => !/^\s*\/\//.test(l)).join('\n'), /\brequire\(|^import |__dirname/m, 'CommonJS in the image, ESM in the repository: neither may be assumed');
assert.doesNotMatch(src, /\bdocker exec\b|#!\/bin\/sh/);
assert.ok(fs.statSync(file).mode & 0o111, `${tool} is executable`);
// For real: no Docker socket answers, so the status is Docker's own 125.
const r = spawnSync(process.execPath, [file, '--quiet'], { encoding: 'utf8',
env: { ...process.env, DOCKER_HOST: `unix://${path.join(ROOT, '.tools', 'tmp', 'no-such-docker.sock')}` } });
assert.equal(r.status, 125, r.stderr);
}
const start = fs.readFileSync(path.join(ROOT, 'releases/ucs-migration/start.mjs'), 'utf8');
assert.ok(!fs.existsSync(path.join(ROOT, 'releases/ucs-migration/start.sh')), 'no shell launcher: there is no shell');
assert.doesNotMatch(start, /docker --version/, 'the --check must not need a Docker CLI');
assert.match(start, /for \(const script of \['run\.mjs', 'importer\.mjs', 'verify\.mjs', 'docker-exec\.mjs'\]\)/);
assert.match(start, /\['--check', path\.join\(root, script\)\]/);
// Nothing else in the migration calls a docker binary either.
for (const f of ['run.mjs', 'importer.mjs', 'verify.mjs', 'start.mjs']) {
const src = fs.readFileSync(path.join(ROOT, 'releases/ucs-migration', f), 'utf8');
assert.doesNotMatch(src, /spawn(Sync)?\(\s*['"]docker['"]|exec docker |^\s*docker /m, f);
}
});