mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
The fix for #601 passed the paths to `bash -c` as arguments ("$1", "$2") instead of pasting them into the script. That is safe in itself, but it still hands a temporary or checkout path to a shell, and CodeQL flagged js/shell-command-injection-from-environment again on the two umask tests in tests/releaseDownloads.test.cjs. Those tests no longer use a shell: fetch.sh runs as bash's program with its arguments, the umask is set on the test's own process around the call and put back, and `chmod +x` is fs.chmodSync. tests/macosSandbox.test.cjs, fixed the same way for #601, now runs ls and touch as programs of sandbox-exec, each path an argument. The guard (tests/testShellPathsFromEnv.test.cjs) now also refuses any value given to a shell's -c script as an argument - spawned or as a wrapper's program - and the one other test that did so, tests/dbConformanceWiring.test.cjs, passes its value in the environment. Run against the files as they were, it names all three; its pattern takes a backslash only as an escape, so a long file cannot make it backtrack. The findings were in tests, not in WeKan, so there is no attempt to show in Admin Panel -> Problems. Thanks to GitHub CodeQL and xet7 !
84 lines
5.2 KiB
JavaScript
84 lines
5.2 KiB
JavaScript
'use strict';
|
|
// CodeQL js/shell-command-injection-from-environment (alerts #549, #550, #601,
|
|
// #602, #603):
|
|
// tests ran `bash -c` with a script that interpolated an absolute path built
|
|
// from __dirname or the temporary directory, e.g. `source "${helper}"`,
|
|
// `node_bin=${JSON.stringify(prog)}` or `bash "${fetchSh}" -o "${out}"`. A path
|
|
// with a quote, `$(` or a backtick would change the command - JSON's quotes are
|
|
// not the shell's. The path goes in through the environment (`"$HELPER"`) or
|
|
// as an argument (`"$1"`), so the script text is constant.
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const root = path.resolve(__dirname, '..');
|
|
// A shell `source`/`.` of an interpolated JavaScript value inside a template.
|
|
const SHAPE = /(?:^|[\s;&|(`'"])(?:source|\.)\s+"?\$\{[^}]+\}/m;
|
|
|
|
const offenders = fs.readdirSync(path.join(root, 'tests'))
|
|
.filter(name => /\.(c?js|mjs)$/.test(name) && name !== path.basename(__filename))
|
|
.filter(name => SHAPE.test(fs.readFileSync(path.join(root, 'tests', name), 'utf8')))
|
|
.sort();
|
|
assert.deepEqual(offenders, [], 'pass the path in an environment variable: source "$HELPER"');
|
|
|
|
// Negative: the guard does catch the shapes that were flagged.
|
|
assert.ok(SHAPE.test('spawnSync(\'bash\', [\'-c\', `source "${helper}"; socket_denied`])'));
|
|
assert.ok(SHAPE.test('spawnSync(\'bash\', [\'-c\', `. "${helper}"; _et_os`])'));
|
|
assert.ok(SHAPE.test('`\n source "${configPath}"\n`'));
|
|
// ...and not the fixed form, nor a JavaScript template that merely mentions a dot.
|
|
assert.ok(!SHAPE.test('[\'-c\', \'source "$SOCKET_DENIED_HELPER"; socket_denied\']'));
|
|
assert.ok(!SHAPE.test('`${name}. ${detail}`'));
|
|
|
|
// #601: any value quoted into a `-c` script - "${...}" or ${JSON.stringify(...)}
|
|
// - is data pasted into code. A script built from the test's own snippets
|
|
// (${functions}, ${body}) is code, and stays allowed.
|
|
const DASH_C = /['"]-c['"],\s*`([^`]*)`/g;
|
|
const QUOTED_VALUE = /"\$\{|\$\{JSON\.stringify\(/;
|
|
function quotedValues(src) {
|
|
const found = [];
|
|
for (const m of src.matchAll(DASH_C)) if (QUOTED_VALUE.test(m[1])) found.push(m[1].slice(0, 80));
|
|
return found;
|
|
}
|
|
const quoted = fs.readdirSync(path.join(root, 'tests'))
|
|
.filter(name => /\.(c?js|mjs)$/.test(name) && name !== path.basename(__filename))
|
|
.flatMap(name => quotedValues(fs.readFileSync(path.join(root, 'tests', name), 'utf8')).map(s => `${name}: ${s}`))
|
|
.sort();
|
|
assert.deepEqual(quoted, [], 'pass the value as an argument ("$1") or in the environment ("$NAME")');
|
|
// Negative: the guard catches #601's shapes...
|
|
assert.deepEqual(quotedValues("spawnSync('bash', ['-c', `set -u; node_bin=${JSON.stringify(prog)}; npm`])").length, 1);
|
|
assert.deepEqual(quotedValues("spawnSync('bash', ['-c', `umask 022; bash \"${fetchSh}\" -o \"${out}\"`])").length, 1);
|
|
// ...and not the fixed forms, nor a script built from the test's own code.
|
|
assert.deepEqual(quotedValues("spawnSync('bash', ['-c', 'bash \"$1\" -o \"$2\"', '_', fetchSh, out])").length, 0);
|
|
assert.deepEqual(quotedValues("spawnSync('bash', ['-c', `set -u; node_bin=\"$NODE_BIN\"; ${body}`])").length, 0);
|
|
assert.deepEqual(quotedValues("spawnSync('bash', ['-c', `${functions}\ntool_repo_dir ${args}`])").length, 0);
|
|
|
|
// #602, #603: a value given to a shell's -c script as an argument ("$1") is
|
|
// still a path handed to a shell, so it is flagged too. A test runs the
|
|
// program itself with its arguments, or passes the value in the environment.
|
|
// A shell - spawned, or as the program of a wrapper such as sandbox-exec.
|
|
const SHELL_C_ARGS = /['"](?:\/bin\/|\/usr\/bin\/)?(?:ba|da|z)?sh['"]\s*,\s*\[?\s*['"]-c['"]\s*,\s*(?:'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|`[^`]*`)\s*,(?!\s*\])/;
|
|
function shellArgs(src) {
|
|
const found = [];
|
|
for (const line of src.split(/(?=spawnSync\(|spawn\(|execFileSync\(|execFile\()/)) {
|
|
const m = line.match(SHELL_C_ARGS);
|
|
if (m) found.push(m[0].slice(0, 80));
|
|
}
|
|
return found;
|
|
}
|
|
const withArgs = fs.readdirSync(path.join(root, 'tests'))
|
|
.filter(name => /\.(c?js|mjs)$/.test(name) && name !== path.basename(__filename))
|
|
.flatMap(name => shellArgs(fs.readFileSync(path.join(root, 'tests', name), 'utf8')).map(s => `${name}: ${s}`))
|
|
.sort();
|
|
assert.deepEqual(withArgs, [], 'run the program itself with its arguments, or pass the value in the environment');
|
|
// Negative: the guard catches #602's and #603's shape...
|
|
assert.equal(shellArgs("spawnSync('bash', ['-c', 'umask 022; bash \"$1\" -o \"$2\"', '_', fetchSh, out])").length, 1);
|
|
assert.equal(shellArgs("spawnSync('/bin/sh', ['-c', `x \"$1\"`, '_', dir])").length, 1);
|
|
assert.equal(shellArgs("spawnSync('sandbox-exec', ['-f', profile, '/bin/bash', '-c', 'ls \"$1\"', '_', home])").length, 1);
|
|
// ...and not a script alone, the program run itself, or another tool's -c.
|
|
assert.equal(shellArgs("spawnSync('bash', ['-c', 'conformance_result \"$COMPARISON\"'], { env })").length, 0);
|
|
assert.equal(shellArgs("spawnSync('bash', [fetchSh, '-o', out, url])").length, 0);
|
|
assert.equal(shellArgs("spawnSync('python3', ['-c', 'import sys', file])").length, 0);
|
|
assert.equal(shellArgs("spawnSync('bash', ['-c', `x`, ], { env })").length, 0); // a trailing comma is no argument
|
|
|
|
console.log('testShellPathsFromEnv: no test sources an interpolated path, quotes a value into a -c script, or hands one to it');
|