Files
wekan/tests/linuxPackageManagers.test.cjs
T
Lauri Ojansivu 433e58a618 Tests pass shell paths through the environment, not into bash -c scripts.
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 !
2026-10-08 00:16:24 +03:00

107 lines
3.9 KiB
JavaScript

'use strict';
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const ROOT = path.join(__dirname, '..');
const helper = path.join(ROOT, 'releases', 'ensure-tools.sh');
const zshScripts = [
'build.sh', 'releases/up.sh', 'releases/clone-release-repos.sh',
'releases/count-lines-of-code-per-committer.sh',
'releases/rebuild-release.sh', 'releases/rebuild-docs.sh',
'releases/node-update-local.sh', 'releases/install-sandstorm.sh',
'releases/build-bundle-armhf.sh',
'releases/build-bundle-ppc64le.sh',
'releases/build-bundle-ppc64el.sh',
'releases/build-bundle-s390x.sh', 'releases/snap-build.sh',
'releases/release-all.sh', 'releases/create-github-secrets.sh',
];
const fixtures = {
alpine: 'ID=alpine\n',
arch: 'ID=arch\n',
fedora: 'ID=fedora\n',
rhel: 'ID=rhel\n',
oracle: 'ID=ol\nID_LIKE="fedora rhel"\n',
debian: 'ID=ubuntu\nID_LIKE=debian\n',
};
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('linuxPackageManagers:');
for (const [name, contents] of Object.entries(fixtures)) {
test(`${name} is detected from os-release`, () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wekan-os-release-'));
const file = path.join(dir, 'os-release');
fs.writeFileSync(file, contents);
const result = spawnSync('bash', ['-c', '. "$ET_HELPER"; _et_linux_family'], {
encoding: 'utf8', env: { ...process.env, ET_HELPER: helper, WEKAN_OS_RELEASE_FILE: file },
});
fs.rmSync(dir, { recursive: true, force: true });
assert.strictEqual(result.status, 0, result.stderr);
const expected = name === 'oracle' ? 'rhel' : name;
assert.strictEqual(result.stdout.trim(), expected);
});
}
test('macOS is detected without relying on the invoking shell', () => {
const result = spawnSync('bash', ['-c', '. "$ET_HELPER"; _et_os'], {
encoding: 'utf8', env: { ...process.env, ET_HELPER: helper, WEKAN_UNAME_S: 'Darwin' },
});
assert.strictEqual(result.status, 0, result.stderr);
assert.strictEqual(result.stdout.trim(), 'macos');
});
test('zsh invocation hands Bash scripts to the system Bash', () => {
const handoff =
'if [ -n "${ZSH_VERSION:-}" ]; then exec /bin/bash "$0" "$@"; fi';
for (const file of zshScripts) {
const src = fs.readFileSync(path.join(ROOT, file), 'utf8');
assert.ok(src.startsWith('#!') && src.includes(handoff), file);
}
});
test('Homebrew uses macOS formula names', () => {
const src = fs.readFileSync(helper, 'utf8');
for (const mapping of [
'python3|pip3|python3-pip) package=python',
'awk) package=gawk',
'g++) package=gcc',
'7zip) package=sevenzip',
'npm) package=node',
]) {
assert.ok(src.includes(mapping), mapping);
}
});
test('every Linux family has an explicit package-manager command', () => {
const src = fs.readFileSync(helper, 'utf8');
for (const command of ['apk add --no-cache', 'pacman -Sy --needed --noconfirm',
'dnf', 'yum', 'apt-get']) assert.ok(src.includes(command), command);
});
test('host-facing release installers use the shared detector', () => {
for (const file of ['up.sh', 'clone-release-repos.sh',
'count-lines-of-code-per-committer.sh', 'rebuild-release.sh',
'rebuild-docs.sh', 'node-update-local.sh', 'install-sandstorm.sh',
'build-bundle-armhf.sh', 'build-bundle-ppc64le.sh',
'build-bundle-ppc64el.sh', 'build-bundle-s390x.sh', 'snap-build.sh']) {
const src = fs.readFileSync(path.join(ROOT, 'releases', file), 'utf8');
assert.ok(src.includes('ensure-tools.sh'), `${file} must source ensure-tools.sh`);
}
});
test('shared specialized installers cover every Linux family', () => {
const src = fs.readFileSync(helper, 'utf8');
assert.ok(src.includes('ensure_build_toolchain()'));
assert.ok(src.includes('ensure_archive_tools()'));
assert.ok(src.includes('oracle-epel-release-el${major}'));
});
console.log(`\n${passed} tests passed`);