From af2d29b293043b263577ca37815690b5d39dfcff Mon Sep 17 00:00:00 2001 From: Lauri Ojansivu Date: Wed, 25 Nov 2026 06:22:53 +0200 Subject: [PATCH] Docker entrypoint: log lines say what happens in fixed words, with nothing read from the environment. GitHub CodeQL js/clear-text-logging, alerts 613-618, on releases/ferretdb/wekan-entrypoint.mjs: cpu-exec's messages logged the CPU feature list of WEKAN_REQUIRED_CPU_FEATURES, the program's path and the qemu-user path, and the recovery and rotating-backup messages the backup folder's path, all derived from the environment. None of them is a secret, but the environment can hold one, and the administrator who set them has them already. Now cpu-exec names the architecture and the caller's own word for the program (FerretDB, WeKan (node)), and recovery and backup say backup/, backup/prev/ or continuous-restore/ of the data folder. tests/dockerEntrypoint.test.cjs checks the messages, and a negative test lists what a log or warn line of the entrypoint may interpolate, so a path or the feature list cannot come back; it fails on the old code. Thanks to GitHub CodeQL and xet7 ! --- releases/ferretdb/wekan-entrypoint.mjs | 30 +++++++++++++++----------- tests/dockerEntrypoint.test.cjs | 23 ++++++++++++++++++-- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/releases/ferretdb/wekan-entrypoint.mjs b/releases/ferretdb/wekan-entrypoint.mjs index f5f079533a..8ff4de4a3a 100644 --- a/releases/ferretdb/wekan-entrypoint.mjs +++ b/releases/ferretdb/wekan-entrypoint.mjs @@ -128,9 +128,13 @@ export function wantFerret(env, markerExists) { // that script with. node and ferretdb are baseline builds needing no special // features, so with none declared this is a plain start. // -// Returns [command, args] to spawn. +// Returns [command, args] to spawn. What it logs is in fixed words: the +// feature list, the program and the qemu path all come from the environment +// (CodeQL js/clear-text-logging), and the administrator who set +// WEKAN_REQUIRED_CPU_FEATURES has its value already. `name` is the caller's +// own word for the program, never a path. export function cpuExecPlan(binary, args, { - spec = '', machine = os.machine(), cpuinfo = '', qemuCandidates = [], log = console.error, + spec = '', machine = os.machine(), cpuinfo = '', qemuCandidates = [], log = console.error, name = 'the program', } = {}) { let required = ''; for (const entry of String(spec).split(',')) { @@ -147,12 +151,12 @@ export function cpuExecPlan(binary, args, { if (!missing.length) return [binary, args]; const qemu = qemuCandidates.find(Boolean); if (qemu) { - log(`cpu-exec: this CPU lacks ${missing.join(' ')}; running ${binary} through qemu-user emulation (${qemu}).`); + log(`cpu-exec: this CPU lacks a feature that WEKAN_REQUIRED_CPU_FEATURES requires on ${machine}; running ${name} through qemu-user emulation.`); log('cpu-exec: emulation is slower — consider migrating to FerretDB (no special CPU features needed).'); return [qemu, [binary, ...args]]; } - log(`cpu-exec: ERROR: this CPU lacks ${missing.join(' ')} and no qemu-user (qemu-${machine}) was found;`); - log(`cpu-exec: running ${binary} directly — expect SIGILL (Illegal instruction). Install qemu-user(-static) or migrate to FerretDB.`); + log(`cpu-exec: ERROR: this CPU lacks a feature that WEKAN_REQUIRED_CPU_FEATURES requires on ${machine}, and no qemu-user (qemu-${machine}) was found;`); + log(`cpu-exec: running ${name} directly — expect SIGILL (Illegal instruction). Install qemu-user(-static) or migrate to FerretDB.`); return [binary, args]; } @@ -215,11 +219,13 @@ export function requestedRestore(dir, env, log = console.log, warn = console.err const backup = path.join(dir, 'backup'); const has = sub => fs.existsSync(path.join(sub, 'wekan.sqlite')); let source = ''; - if (mode === 'backup' && has(backup)) source = backup; - else if (mode === 'prev' && has(path.join(backup, 'prev'))) source = path.join(backup, 'prev'); + // Where it came from, said relative to the data folder in fixed words. + let from = ''; + if (mode === 'backup' && has(backup)) { source = backup; from = 'backup/'; } + else if (mode === 'prev' && has(path.join(backup, 'prev'))) { source = path.join(backup, 'prev'); from = 'backup/prev/'; } // Admin Panel / Attachments / Continuous backup stages a rebuilt // database here (server/continuousBackup.js), never over the live one. - else if (mode === 'continuous' && has(path.join(dir, 'continuous-restore'))) source = path.join(dir, 'continuous-restore'); + else if (mode === 'continuous' && has(path.join(dir, 'continuous-restore'))) { source = path.join(dir, 'continuous-restore'); from = 'continuous-restore/'; } try { fs.writeFileSync(path.join(dir, 'RECOVERY_IN_PROGRESS'), `recovery ${mode}\n`); } catch { /* best effort */ } // A continuous-backup restore replaces the live database the administrator // chose to roll back: keep that one first, at rest with its WAL, beside the @@ -233,7 +239,7 @@ export function requestedRestore(dir, env, log = console.log, warn = console.err if (copySqliteFiles(source, dir)) { for (const side of ['wekan.sqlite-wal', 'wekan.sqlite-shm']) fs.rmSync(path.join(dir, side), { force: true }); appendEvent(dir, { type: `restore-${mode}`, severity: 'warning', source: 'startup', detail: 'Restored wekan.sqlite from a backup copy' }); - log(`Recovery: restored text-data database from ${source}.`); + log(`Recovery: restored text-data database from ${from} of the data folder.`); fs.rmSync(request, { force: true }); return 'restored'; } @@ -270,7 +276,7 @@ export function rotatingBackup(dir, log = console.log, warn = console.error) { copySqliteFiles(backup, prev); } if (copySqliteFiles(dir, backup)) { - log(`Backed up text-data database (wekan.sqlite*) to ${backup} (previous kept in ${backup}/prev).`); + log('Backed up text-data database (wekan.sqlite*) to backup/ of the data folder (previous kept in backup/prev/).'); appendEvent(dir, { type: 'backup-created', severity: 'info', source: 'startup', detail: 'Backed up wekan.sqlite to backup/' }); return true; } @@ -372,7 +378,7 @@ export async function main({ build = path.dirname(fileURLToPath(import.meta.url) `--listen-addr=${listen}`, '--telemetry=disable', '--log-level=error', - ], cpuOptions()); + ], { ...cpuOptions(), name: 'FerretDB' }); const db = spawn(cmd, args, { env, stdio: 'inherit' }); db.on('error', e => warn(`FerretDB could not start: ${e.message}`)); db.on('exit', (code, signal) => { if (!stopping) warn(`FerretDB exited (${signal || code}).`); }); @@ -445,7 +451,7 @@ export async function main({ build = path.dirname(fileURLToPath(import.meta.url) // stack, so it never changed what WeKan could do. A deployment that raises // --stack-size in NODE_OPTIONS sets the stack with `docker run --ulimit stack=` // or the compose file's `ulimits:`.) - const [cmd, args] = cpuExecPlan(node, [path.join(build, 'main.js')], cpuOptions()); + const [cmd, args] = cpuExecPlan(node, [path.join(build, 'main.js')], { ...cpuOptions(), name: 'WeKan (node)' }); const wekan = spawn(cmd, args, { env, stdio: 'inherit', cwd: build }); children.push(wekan); for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP']) { diff --git a/tests/dockerEntrypoint.test.cjs b/tests/dockerEntrypoint.test.cjs index 8bf11012ba..081548361c 100644 --- a/tests/dockerEntrypoint.test.cjs +++ b/tests/dockerEntrypoint.test.cjs @@ -103,9 +103,12 @@ test('#6458 cpu-exec: qemu-user only for a declared feature the CPU lacks', asyn assert.deepEqual(cpuExecPlan('/build/ferretdb', ['--x'], { spec: 'aarch64=atomics', machine: 'x86_64', cpuinfo, qemuCandidates: ['/q'], log: quiet }), ['/build/ferretdb', ['--x']], 'another architecture\'s entry is not this one\'s'); const said = []; - assert.deepEqual(cpuExecPlan('/build/ferretdb', ['--x'], { spec: 'x86_64=avx512f', machine: 'x86_64', cpuinfo, qemuCandidates: ['/build/qemu-x86_64'], log: m => said.push(m) }), + assert.deepEqual(cpuExecPlan('/build/ferretdb', ['--x'], { spec: 'x86_64=avx512f', machine: 'x86_64', cpuinfo, qemuCandidates: ['/build/qemu-x86_64'], log: m => said.push(m), name: 'FerretDB' }), ['/build/qemu-x86_64', ['/build/ferretdb', '--x']]); - assert.match(said[0], /lacks avx512f/); + assert.match(said[0], /lacks a feature that WEKAN_REQUIRED_CPU_FEATURES requires on x86_64; running FerretDB through qemu-user/); + // Negative (CodeQL js/clear-text-logging): nothing read from the + // environment - the feature list, a path, the qemu found - is logged. + assert.doesNotMatch(said.join('\n'), /avx512f|\/build\//); // "avx" is not found inside "avx2": a whole word. assert.deepEqual(cpuExecPlan('/n', [], { spec: 'x86_64=avx', machine: 'x86_64', cpuinfo, qemuCandidates: ['/q'], log: quiet }), ['/q', ['/n']]); // ARM lists them on "Features" lines. @@ -114,6 +117,22 @@ test('#6458 cpu-exec: qemu-user only for a declared feature the CPU lacks', asyn said.length = 0; assert.deepEqual(cpuExecPlan('/n', [], { spec: 'x86_64=avx512f', machine: 'x86_64', cpuinfo, qemuCandidates: [], log: m => said.push(m) }), ['/n', []]); assert.match(said.join('\n'), /expect SIGILL/); + assert.doesNotMatch(said.join('\n'), /avx512f|\/n\b/, 'negative: no feature list or path in it either'); +}); + +test('negative: the entrypoint logs no value taken from the environment (CodeQL js/clear-text-logging)', () => { + const src = fs.readFileSync(path.join(__dirname, '..', 'releases', 'ferretdb', 'wekan-entrypoint.mjs'), 'utf8'); + // What a log or warn line may interpolate: counts, the port, the listen + // address, a child's exit, the CPU architecture and the caller's own name + // for a program. A path, the CPU feature list or a qemu path is not on it. + const allowed = new Set(['heapMb', 'rc', 'listen', 'grace', 'max', 'e.message', 'signal || code', 'machine', 'name', + "first.stderr || 'no reason given'", "env.PORT || '8080'", 'from', 'ferretBin']); + const found = []; + for (const m of src.matchAll(/\b(?:log|warn)\(`([^`]*)`/g)) { + for (const v of m[1].matchAll(/\$\{([^}]*)\}/g)) if (!allowed.has(v[1].trim())) found.push(v[1]); + } + assert.deepEqual(found, [], 'a log line interpolates something not on the list'); + assert.doesNotMatch(src, /\b(?:log|warn)\(`[^`]*\$\{(?:source|backup|binary|qemu|missing[^}]*)\}/, 'the six CodeQL found are gone'); }); function tmp() { return fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'entry-')); }