mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
GitHub CodeQL code scanning alerts #549 and #550 (js/shell-command-injection-from-environment): tests/snapSettingsKeys and tests/snapSocketDenied ran `bash -c` with a script that interpolated an absolute path built from __dirname (`source "${configPath}"`, `source "${helper}"`). A checkout path containing a quote, `$(` or a backtick would change the command that runs. tests/linuxPackageManagers and tests/snapCaddyNofile6552 had the same shape without an alert yet. All four now pass the path (and the soft limit, and the wekan-control function under test) in environment variables, so every script text is constant. tests/testShellPathsFromEnv.test.cjs fails on any test that sources an interpolated value, and checks it catches the flagged forms. These are test files run on a developer machine or CI, so no attack can be attempted against a running WeKan and nothing is logged in Admin Panel -> Problems. Thanks to GitHub CodeQL and xet7 !
30 lines
1.6 KiB
JavaScript
30 lines
1.6 KiB
JavaScript
'use strict';
|
|
// CodeQL js/shell-command-injection-from-environment (alerts #549, #550): tests
|
|
// ran `bash -c` with a script that interpolated an absolute path built from
|
|
// __dirname, e.g. `source "${helper}"`. A checkout path with a quote, `$(` or a
|
|
// backtick would change the command. The path goes in through the environment
|
|
// instead (`source "$HELPER"`), 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}`'));
|
|
|
|
console.log('testShellPathsFromEnv: no test sources an interpolated path');
|