Files
wekan/tests/macosSandbox.test.cjs
T
Lauri Ojansivu 2b65b713a0 Tests: no path handed to a shell at all, CodeQL alerts #602 and #603.
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 !
2026-11-11 03:44:13 +02:00

67 lines
3.6 KiB
JavaScript

'use strict';
// docs/Security/Sandboxes/macOS/sandbox.sh starts VSCodium under sandbox-exec
// so that it, its terminals and its extensions reach only this checkout - the
// macOS counterpart of the Linux Flatpak sandbox (README.md beside it).
// Run: node tests/macosSandbox.test.cjs
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 script = fs.readFileSync(path.join(root, 'docs/Security/Sandboxes/macOS/sandbox.sh'), 'utf8');
let passed = 0;
const test = (name, fn) => { const r = fn(); if (r === 'skip') { console.log(' skip -', name); return; } passed += 1; console.log(' ok -', name); };
test('the profile refuses every user\'s files and opens only the checkout', () => {
assert.match(script, /\(deny file-read\* file-write\* \(subpath "\/Users"\) \(subpath "\/Volumes"\)\)/);
assert.match(script, /\(allow file-read\* file-write\* \(subpath \$\(sb_string "\$REPO"\)\)\)/);
assert.match(script, /com\\\\\.apple\\\\\.launchd/, 'and the ssh-agent socket');
});
test('VSCodium runs with a clean environment, its data and temporary files in .tools', () => {
assert.match(script, /sandbox-exec -f "\$PROFILE" \/usr\/bin\/env -i/);
assert.match(script, /HOME="\$TOOLS\/home" TMPDIR="\$TOOLS\/tmp"/);
assert.match(script, /--user-data-dir "\$TOOLS\/vscodium\/user-data"/);
assert.match(script, /--extensions-dir "\$TOOLS\/vscodium\/extensions"/);
// Chromium's own helper sandbox cannot start inside sandbox-exec.
assert.match(script, /VSCodium" --no-sandbox/);
});
test('the commit author is copied into the checkout, as ~/.gitconfig is out of reach', () => {
assert.match(script, /git -C "\$REPO" config --local "\$key" "\$value"/);
});
test('on macOS: the profile really refuses the home directory and allows the checkout', () => {
if (process.platform !== 'darwin' || spawnSync('which', ['sandbox-exec']).status !== 0) return 'skip';
const tmp = fs.mkdtempSync(path.join(root, '.tools', 'tmp', 'macos-sandbox-'));
try {
// Inside the sandbox this test is about (VSCodium started by the script),
// macOS refuses to apply a second profile at all: "sandbox_apply:
// Operation not permitted", even for (allow default). Nothing can be
// measured there, so it is a skip - but only when that probe fails.
const probe = path.join(tmp, 'probe.sb');
fs.writeFileSync(probe, '(version 1)\n(allow default)\n');
const nested = spawnSync('sandbox-exec', ['-f', probe, '/usr/bin/true'], { encoding: 'utf8' });
if (nested.status !== 0 && /sandbox_apply/.test(nested.stderr)) return 'skip';
const homeDir = os.homedir();
const profile = path.join(tmp, 'test.sb');
fs.writeFileSync(profile, `(version 1)\n(allow default)\n(deny file-read* file-write* (subpath "/Users") (subpath "/Volumes"))\n`
+ `(allow file-read* file-write* (subpath ${JSON.stringify(root)}))\n`);
// No shell at all (CodeQL alerts #601-#603): each probe is its own
// program under the profile, its path an argument.
const home = spawnSync('sandbox-exec', ['-f', profile, '/bin/ls', homeDir + '/Desktop'], { encoding: 'utf8' });
assert.notStrictEqual(home.status, 0, 'the home directory is refused');
const written = spawnSync('sandbox-exec', ['-f', profile, '/usr/bin/touch', tmp + '/ok'], { encoding: 'utf8' });
assert.strictEqual(written.status, 0, written.stderr);
assert.ok(fs.existsSync(tmp + '/ok'), 'the checkout is written');
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
});
console.log(`\nmacosSandbox: ${passed} passed`);