'use strict'; // Every fix in https://wekan.fi/hall-of-fame/ has a suite of its own, pinning the // place it was found. This one asks the other half of the question: is the same // MISTAKE anywhere else - in WeKan, and in the FerretDB fork WeKan ships as its // default database (.tools/w/FerretDB)? // Run: node tests/fixedVulnerabilityClasses.test.cjs // // A per-site regression test cannot answer that. It knows one file, and the next // occurrence of the pattern is written somewhere else - a new route, a new // importer, a new backend - months later, by somebody who never read the advisory. // So each check here is a CLASS, applied to the whole tree: // // ZipBleed an archive entry names its own destination path // WhereBleed a client-supplied selector carrying $where / mapReduce // TransitBleed TLS verification switched off to make a connection work // SourceBleed a string turned back into code (eval, new Function) // MimeBleed user-supplied SVG served inline, where it is script // RandomBleed a secret from a non-cryptographic random number generator // // The rules for this file, so it stays worth running: // * every check passes RIGHT NOW, and each one FAILED before its fix - so a red // result means somebody has written the old mistake again, not that the check // drifted; // * comments and strings do not count as code, and the scanner strips them, or // the advisory notes that explain a fix would trip the check that enforces it; // * an allowed occurrence is listed WITH ITS REASON, not silently excluded. // A file that appears here without a reason is a bug in this file. // // .tools/w/FerretDB is a separate repository, cloned by build.sh, and may be absent - // then its checks SAY they were skipped rather than passing quietly. const assert = require('assert'); const fs = require('fs'); const path = require('path'); const repoRoot = path.resolve(__dirname, '..'); const FERRETDB = path.join(repoRoot, '.tools/w/FerretDB'); const hasFerretDB = fs.existsSync(path.join(FERRETDB, 'go.mod')); let passed = 0; let skipped = 0; function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); } function ferretTest(name, fn) { if (!hasFerretDB) { skipped += 1; console.log(' -- skipped (no .tools/w/FerretDB clone) -', name); return; } test(name, fn); } console.log('fixedVulnerabilityClasses:'); // ── walking the sources ───────────────────────────────────────────────────── // Everything WeKan itself ships. node_modules, builds and caches are other // people's code and are not ours to guard here. const WEKAN_DIRS = ['client', 'models', 'server', 'imports', 'config', 'packages', 'snap-src', 'sandstorm-src']; const SKIP_DIRS = new Set([ 'node_modules', '.git', '.build', '.meteor', '.tools', 'test-results', 'coverage', 'dist', 'build', 'vendor', 'fonts', 'webfonts', '.goroot', 'gopath', 'gomodcache', 'gocache', ]); function walk(dir, extensions, out = []) { let entries = []; try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return out; } for (const entry of entries) { if (SKIP_DIRS.has(entry.name)) continue; const full = path.join(dir, entry.name); if (entry.isDirectory()) { walk(full, extensions, out); continue; } if (extensions.some(ext => entry.name.endsWith(ext))) out.push(full); } return out; } const jsFiles = WEKAN_DIRS.flatMap(d => walk(path.join(repoRoot, d), ['.js', '.mjs', '.cjs', '.jsx'])); const goFiles = hasFerretDB ? walk(FERRETDB, ['.go']) : []; // Comments and string literals are NOT code. An advisory note quoting the mistake // it fixed, or a rejection list naming the operator it rejects, must not read as // the mistake itself - that is how a guard like this starts lying. function codeOnly(source) { let out = ''; let i = 0; const n = source.length; while (i < n) { const two = source.slice(i, i + 2); if (two === '//') { while (i < n && source[i] !== '\n') i += 1; continue; } if (two === '/*') { i += 2; while (i < n && source.slice(i, i + 2) !== '*/') i += 1; i += 2; continue; } const ch = source[i]; if (ch === '"' || ch === "'" || ch === '`') { const quote = ch; out += ' '; // keep positions roughly sane i += 1; while (i < n) { if (source[i] === '\\') { i += 2; continue; } if (source[i] === quote) { i += 1; break; } i += 1; } continue; } // A regular expression literal is code, but its BODY is a pattern, and a // pattern that names $where is a rejection, not a use. if (ch === '/' && /[=(,:[!&|?{;\n]\s*$/.test(out.slice(-40))) { i += 1; while (i < n && source[i] !== '\n') { if (source[i] === '\\') { i += 2; continue; } if (source[i] === '/') { i += 1; break; } i += 1; } out += ' '; continue; } out += ch; i += 1; } return out; } const cache = new Map(); function codeOf(file) { if (!cache.has(file)) { try { cache.set(file, codeOnly(fs.readFileSync(file, 'utf8'))); } catch { cache.set(file, ''); } } return cache.get(file); } const rel = file => path.relative(repoRoot, file); // Files where an occurrence is CORRECT, each with the reason it is correct. function findAll(files, regex, allowed = {}) { const hits = []; for (const file of files) { const code = codeOf(file); if (!regex.test(code)) { regex.lastIndex = 0; continue; } regex.lastIndex = 0; const relative = rel(file); if (allowed[relative]) continue; hits.push(relative); } return hits; } // ── TransitBleed: TLS verification switched off ───────────────────────────── test('nothing turns off TLS certificate verification (TransitBleed)', () => { // The fix was to TRUST a certificate the admin supplies, not to stop checking: // `rejectUnauthorized: false` accepts ANY certificate, including the one the // machine-in-the-middle just made up, and it is the shortest way to make an // internal SMTP or webhook host "work". const hits = findAll(jsFiles, /rejectUnauthorized\s*:\s*false/); assert.deepStrictEqual(hits, [], 'these files accept any TLS certificate; supply the CA instead ' + '(server/lib/mailTransport.js and server/lib/ssrfGuard.js show how)'); const envHits = findAll( [...jsFiles, ...walk(path.join(repoRoot, 'snap-src'), ['.sh', '']), path.join(repoRoot, 'start-wekan.sh')], /NODE_TLS_REJECT_UNAUTHORIZED/); assert.deepStrictEqual(envHits, [], 'NODE_TLS_REJECT_UNAUTHORIZED=0 turns verification off for the whole process, ' + 'which is the same hole with a wider blast radius'); }); ferretTest('the FerretDB fork does not skip TLS verification either', () => { const hits = findAll(goFiles, /InsecureSkipVerify\s*:\s*true/); assert.deepStrictEqual(hits, [], 'Go\'s spelling of the same mistake - the database WeKan ships speaks TLS to ' + 'PostgreSQL/MySQL/HANA backends, and this would accept any certificate'); }); // ── SourceBleed: a string turned back into code ───────────────────────────── test('no string is turned back into code on the server (SourceBleed)', () => { const serverish = jsFiles.filter(f => /\/(server|models|imports)\//.test(f) && !/\/client\//.test(f)); const hits = findAll(serverish, /(^|[^.\w$])eval\s*\(|new\s+Function\s*\(/m, { // None today. An entry here needs a reason, not a shrug. }); assert.deepStrictEqual(hits, [], 'eval() and new Function() execute whatever reaches them; every WeKan use of ' + 'them has been replaced by a parser or a lookup table'); }); // ── WhereBleed: a client selector carrying executable JavaScript ──────────── test('no publication runs a client selector with $where or mapReduce (WhereBleed)', () => { // A selector is DATA sent by the client, and $where makes it CODE the database // runs. The fix was to build selectors server-side from validated ids, and to // reject a client selector that carries $where at all. const pubs = jsFiles.filter(f => /\/server\/publications\//.test(f)); const hits = findAll(pubs, /\$where|mapReduce/, { // The rejection itself has to name what it rejects. 'server/publications/cardsWindow.js': 'defines hasWhere(), the rejection', }); assert.deepStrictEqual(hits, [], 'a publication naming $where in code is either running one or has grown its ' + 'own copy of the rejection; the shared one is models/lib/mongoSelectorSafety.js'); const guard = fs.readFileSync(path.join(repoRoot, 'models/lib/mongoSelectorSafety.js'), 'utf8'); assert.ok(/\$where/.test(guard) && /mapReduce|\$function|\$accumulator/.test(guard), 'and the shared guard must still know every operator that executes code'); }); // ── ZipBleed: an archive entry naming its own destination ─────────────────── test('an archive entry cannot choose where it is written (ZipBleed)', () => { // `path.join(dest, entry.path)` RESOLVES `..`, so an entry called // `x/../../../etc/cron.d/wekan` was written outside the destination entirely. // An ARCHIVE reader, not a directory listing: `fs.readdirSync(...).name` is one // path component and cannot escape anything, while a zip or tar entry carries a // whole path chosen by whoever built the file. const ARCHIVE_READER = /unzipper|adm-?zip|AdmZip|yauzl|tar-stream|node-stream-zip|extract-zip/i; const extractors = jsFiles.filter(f => { const code = codeOf(f); return ARCHIVE_READER.test(code) || /entry\.entryName|entry\.path/.test(code); }); assert.ok(extractors.length > 0, 'the scanner must actually find the extract paths'); for (const file of extractors) { const code = codeOf(file); if (!/path\.join|path\.resolve|createWriteStream|mkdirSync/.test(code)) continue; assert.ok(/safeEntryPath|isPathInside|storagePathContainment/.test(code), `${rel(file)} builds a filesystem path from an archive entry without a ` + 'containment check; safeEntryPath() refuses anything that escapes'); } }); // ── MimeBleed: user-supplied SVG is script ───────────────────────────────── test('a stored SVG is never served inline unsanitized (MimeBleed)', () => { // An SVG is an XML document that may carry