Files
muSRFlow/tests/cgi-hardening.test.js

392 lines
16 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Hardening tests for the web CGI (rc.1 audit): input validation, command
// timeouts, job cleanup / artifact ownership, and interrupted/error paths.
// These spawn the real Perl CGI with controlled environment variables and fake
// executables, matching the harness used by the other cgi-*.test.js files.
const assert = require("assert");
const childProcess = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const repoRoot = path.resolve(__dirname, "..");
const testRoot = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-cgi-hard-"));
const perlTestLibrary = path.join(repoRoot, "tests", "perl");
const perlLibraryPath = [perlTestLibrary, process.env.PERL5LIB]
.filter(Boolean).join(path.delimiter);
function runCGI(body, extraEnv) {
const result = childProcess.spawnSync(
"perl",
[path.join(repoRoot, "cgi", "single_musrfit.cgi")],
{
cwd: repoRoot,
encoding: "utf8",
input: body,
env: Object.assign({}, process.env, {
REQUEST_METHOD: "POST",
PERL5LIB: perlLibraryPath,
CONTENT_TYPE: "application/x-www-form-urlencoded",
CONTENT_LENGTH: String(Buffer.byteLength(body)),
MUSRFIT_WEBROOT: webRoot,
MUSRFIT_TMPROOT: tmpRoot,
MUSRFIT_TMP_URL: "/tmp",
MUSRFIT_RUNTIME_HOME: tmpRoot,
DUMP_HEADER_BIN: fakeDumpHeader,
...extraEnv
})
}
);
assert.strictEqual(result.status, 0, result.stderr);
const responseBody = result.stdout.split(/\r?\n\r?\n/).slice(1).join("\n\n");
return JSON.parse(responseBody);
}
const webRoot = path.join(testRoot, "web");
const tmpRoot = path.join(webRoot, "tmp");
const jobName = "hardenjob";
const workPath = path.join(tmpRoot, jobName);
const fakeDumpHeader = path.join(testRoot, "dump_header");
fs.mkdirSync(workPath, { recursive: true });
// ---- Fake executables -----------------------------------------------------
// dump_header echoes a header and exits 0; honors a hanging mode for the
// watchdog test by recording its pid and sleeping.
fs.writeFileSync(fakeDumpHeader, [
"#!/bin/sh",
"case \"$MUSRFLOW_TEST_DUMP_MODE\" in",
" hang)",
" echo $$ > \"$MUSRFLOW_TEST_DUMP_PID\"",
" sleep 30",
" ;;",
"esac",
"printf 'RUN 514 0.1 200\\n'",
"exit 0",
""
].join("\n"));
fs.chmodSync(fakeDumpHeader, 0o755);
const standardMsr = [
"FITPARAMETER",
"1 Alpha 1.0 0.01",
"THEORY",
"asymmetry 1",
"GLOBAL",
"fittype 2",
"RUN /psi.ch/group/lmu/public/data/MUE4 PSI MUSR-ROOT",
"COMMANDS",
"MIGRAD",
"PLOT 2",
"runs 1",
""
].join("\n");
fs.writeFileSync(path.join(workPath, "input.msr"), standardMsr);
try {
// 1. Unknown action with a valid fn falls through every handler and must be
// rejected as unsupported rather than silently ignored.
{
const res = runCGI(new URLSearchParams({
action: "garbage_action",
fn: "/tmp/" + jobName + "/input.msr"
}).toString());
assert.strictEqual(res.ok, false);
assert.strictEqual(res.code, 400);
assert.strictEqual(res.message, "Unsupported action");
}
// 2. Invalid fn paths are rejected by the entry-point regex gate: traversal
// in the job name and a non-.msr file name.
for (const badFn of ["/tmp/../escape/input.msr", "/tmp/" + jobName + "/EVIL", "/tmp//input.msr"]) {
const res = runCGI(new URLSearchParams({
action: "write",
fn: badFn
}).toString());
assert.strictEqual(res.ok, false, "fn=" + badFn);
assert.strictEqual(res.code, 400, "fn=" + badFn);
assert.strictEqual(res.message, "Invalid fn path", "fn=" + badFn);
}
// 3. dump_header success path (fake records args) and server-owned data-root
// validation.
{
const icPath = path.join(tmpRoot, "..", "..", "data", "MUE4", "his", "2026", "mue26_his_00514.root");
fs.mkdirSync(path.dirname(icPath), { recursive: true });
fs.writeFileSync(icPath, "dummy");
const res = runCGI(new URLSearchParams({
action: "dump_header",
fn: icPath,
file_format: "MusrRoot"
}).toString(), { MUSRFIT_DATA_ROOT: path.dirname(path.dirname(path.dirname(icPath))) });
assert.strictEqual(res.ok, true);
assert.strictEqual(res.code, 0);
assert.match(res.header, /RUN 514/);
}
// 4. dump_header rejects paths outside the configured data root.
{
const outsidePath = path.join(tmpRoot, "..", "escape.root");
fs.writeFileSync(outsidePath, "dummy");
const res = runCGI(new URLSearchParams({
action: "dump_header",
fn: outsidePath,
file_format: "ROOT"
}).toString());
assert.strictEqual(res.ok, false);
assert.strictEqual(res.code, 400);
assert.strictEqual(res.message, "Invalid data-file path");
}
// 4b. Portable RUN names resolve through administrator-owned startup XML
// data paths, matching abbreviated names used by browser Fit/Plot.
{
const portableRoot = path.join(testRoot, "portable-data");
const portableFile = path.join(portableRoot, "2026", "lem26_his_1200.root");
const startupXml = path.join(testRoot, "musrfit_startup.xml");
fs.mkdirSync(path.dirname(portableFile), { recursive: true });
fs.writeFileSync(portableFile, "dummy");
fs.writeFileSync(startupXml, [
"<musrfit_startup>",
" <data_path>" + portableRoot + "</data_path>",
"</musrfit_startup>",
""
].join("\n"));
const res = runCGI(new URLSearchParams({
action: "dump_header",
fn: "2026/lem26_his_1200.root",
file_format: "MusrRoot"
}).toString(), { MUSRFIT_STARTUP_XML: startupXml });
assert.strictEqual(res.ok, true);
assert.strictEqual(res.code, 0);
assert.match(res.header, /RUN 514/);
}
// 5. dump_header hangs are bounded by the watchdog. The fake records its
// pid and sleeps; the CGI must return a timeout well before the sleep
// finishes and must kill the child process.
{
const icPath = path.join(tmpRoot, "..", "..", "data", "MUE4", "his", "2026", "mue26_his_00514.root");
const pidFile = path.join(testRoot, "dump-pid.txt");
if (fs.existsSync(pidFile)) fs.unlinkSync(pidFile);
const start = Date.now();
const res = runCGI(new URLSearchParams({
action: "dump_header",
fn: icPath,
file_format: "MusrRoot"
}).toString(), {
MUSRFIT_DATA_ROOT: path.dirname(path.dirname(path.dirname(icPath))),
MUSRFIT_CMD_TIMEOUT: "2",
MUSRFLOW_TEST_DUMP_MODE: "hang",
MUSRFLOW_TEST_DUMP_PID: pidFile
});
const elapsed = Date.now() - start;
assert.strictEqual(res.ok, false);
assert.strictEqual(res.code, 124);
assert.strictEqual(res.message, "dump_header timed out");
assert.ok(elapsed < 15000, "timeout ran too long: " + elapsed + "ms");
assert.ok(fs.existsSync(pidFile), "child pid was not recorded");
const childPid = parseInt(fs.readFileSync(pidFile, "utf8").trim(), 10);
assert.ok(Number.isInteger(childPid) && childPid > 0);
// Give the kernel a moment, then require the child is reaped/killed.
const sleepOut = childProcess.spawnSync("sh", ["-c",
"kill -0 " + childPid + " 2>/dev/null && echo alive || echo dead"]);
const state = sleepOut.stdout.toString().trim();
assert.strictEqual(state, "dead", "child process " + childPid + " still alive after timeout");
}
// 6. Malformed MSR content yields a structured error, not a crash.
{
fs.writeFileSync(path.join(workPath, "input.msr"), "this is not a valid msr body");
const res = runCGI(new URLSearchParams({
action: "run",
fn: "/tmp/" + jobName + "/input.msr",
runs: "514",
base_run: "514"
}).toString());
assert.strictEqual(res.ok, false);
assert.ok(typeof res.message === "string" && res.message.length > 0);
fs.writeFileSync(path.join(workPath, "input.msr"), standardMsr);
}
// 7. Missing tool: a non-executable musrfit binary is reported cleanly.
{
const notExec = path.join(testRoot, "not-exec");
fs.writeFileSync(notExec, "# not a real binary\n");
fs.chmodSync(notExec, 0o644);
const res = runCGI(new URLSearchParams({
action: "run",
fn: "/tmp/" + jobName + "/input.msr",
runs: "514",
base_run: "514"
}).toString(), { MUSRFIT_BIN: notExec });
assert.strictEqual(res.ok, false);
assert.strictEqual(res.code, 500);
}
// 8. Oversized ADDRUN JSON is rejected at the size gate.
{
const body = new URLSearchParams({
action: "run",
fn: "/tmp/" + jobName + "/input.msr",
runs: "514",
base_run: "514",
addrun_spec: "x".repeat(100001)
}).toString();
const res = runCGI(body);
assert.strictEqual(res.ok, false);
assert.strictEqual(res.code, 400);
assert.strictEqual(res.message, "ADDRUN specification is too large");
}
// 9. Job-directory TTL pruning removes stale dirs and keeps fresh ones.
{
const staleDir = path.join(tmpRoot, "oldjob");
const freshDir = path.join(tmpRoot, "freshjob");
fs.mkdirSync(staleDir, { recursive: true });
fs.mkdirSync(freshDir, { recursive: true });
const tenMinAgo = new Date(Date.now() - 10 * 60 * 1000);
fs.utimesSync(staleDir, tenMinAgo, tenMinAgo);
const res = runCGI(new URLSearchParams({
action: "write",
fn: "/tmp/" + jobName + "/input.msr",
line0: "FITPARAMETER"
}).toString(), { MUSRFIT_JOB_MAXAGE: "60" });
assert.strictEqual(res.ok, true);
assert.strictEqual(fs.existsSync(staleDir), false, "stale dir should be pruned");
assert.strictEqual(fs.existsSync(freshDir), true, "fresh dir should survive");
}
// 10. A job path that resolves to a symlink must be refused before any write.
{
const linkPath = path.join(tmpRoot, "linkjob");
fs.symlinkSync(tmpRoot, linkPath);
try {
const res = runCGI(new URLSearchParams({
action: "write",
fn: "/tmp/linkjob/input.msr",
line0: "FITPARAMETER"
}).toString());
assert.strictEqual(res.ok, false);
assert.strictEqual(res.code, 500);
assert.match(res.message, /not a plain directory/);
} finally {
fs.unlinkSync(linkPath);
}
}
// ---- Fake musrfit for error-path scenarios -----------------------------
// Modes: "missing" prints the unresolved-data diagnostic, "hang" exercises
// the command watchdog, and "fail" prints a generic error.
const fakeMusrfit = path.join(testRoot, "musrfit");
fs.writeFileSync(fakeMusrfit, [
"#!/bin/sh",
"case \"$MUSRFLOW_TEST_MUSRFIT_MODE\" in",
" hang)",
" echo $$ > \"$MUSRFLOW_TEST_MUSRFIT_PID\"",
" sleep 30",
" ;;",
" missing)",
" echo \"Couldn't find 'psi2026/514.root' in any standard path\"",
" ;;",
" *)",
" echo \"musrfit: fit failed for an unrelated reason\"",
" ;;",
"esac",
"exit 1",
""
].join("\n"));
fs.chmodSync(fakeMusrfit, 0o755);
// 11. Repeated actions on the same job are safe and leave the job intact:
// the second write succeeds without clobbering or leaking the dir.
{
const body = new URLSearchParams({
action: "write",
fn: "/tmp/" + jobName + "/input.msr",
line0: "FITPARAMETER"
});
const first = runCGI(body.toString());
const second = runCGI(body.toString());
assert.strictEqual(first.ok, true);
assert.strictEqual(second.ok, true);
assert.strictEqual(fs.statSync(workPath + "/input.msr").isFile(), true);
assert.strictEqual(fs.readFileSync(workPath + "/input.msr", "utf8"), "FITPARAMETER\n");
}
// 12. Missing data: a musrfit diagnostic about an unresolved RUN file is
// surfaced as a structured "missing-data" outcome rather than a crash.
{
fs.writeFileSync(path.join(workPath, "input.msr"), standardMsr);
const res = runCGI(new URLSearchParams({
action: "run",
fn: "/tmp/" + jobName + "/input.msr",
runs: "514",
base_run: "514"
}).toString(), { MUSRFIT_BIN: fakeMusrfit, MUSRFLOW_TEST_MUSRFIT_MODE: "missing" });
assert.strictEqual(res.ok, false);
assert.strictEqual(res.code, 1);
assert.strictEqual(res.outcome, "missing-data");
assert.strictEqual(res.message, "Data file not found: 514.root");
assert.deepStrictEqual(res.missing_files, ["psi2026/514.root"]);
}
// 13. Stale msr2data parameter output is removed at the start of a new job
// so a fresh fit never serves a previous job's parameter data.
{
const parPath = path.join(workPath, "input_par.dat");
fs.writeFileSync(parPath, "stale parameter data from an earlier job\n");
const res = runCGI(new URLSearchParams({
action: "run",
fn: "/tmp/" + jobName + "/input.msr",
runs: "514",
base_run: "514"
}).toString(), { MUSRFIT_BIN: fakeMusrfit, MUSRFLOW_TEST_MUSRFIT_MODE: "fail" });
assert.strictEqual(res.ok, false);
assert.strictEqual(res.code, 1);
assert.ok(res.message.length > 0);
assert.strictEqual(fs.existsSync(parPath), false, "stale _par.dat was not removed");
}
// 14. run_bounded_command must preserve its documented timeout result. If it
// leaks the child's SIGKILL status, run_logged_command reports signal 9
// instead of the actionable timeout message.
{
const pidFile = path.join(testRoot, "musrfit-pid.txt");
if (fs.existsSync(pidFile)) fs.unlinkSync(pidFile);
fs.writeFileSync(path.join(workPath, "input.msr"), standardMsr);
const start = Date.now();
const res = runCGI(new URLSearchParams({
action: "run",
fn: "/tmp/" + jobName + "/input.msr",
runs: "514",
base_run: "514"
}).toString(), {
MUSRFIT_BIN: fakeMusrfit,
MUSRFIT_CMD_TIMEOUT: "1",
MUSRFLOW_TEST_MUSRFIT_MODE: "hang",
MUSRFLOW_TEST_MUSRFIT_PID: pidFile
});
const elapsed = Date.now() - start;
assert.strictEqual(res.ok, false);
assert.ok(elapsed < 15000, "musrfit timeout ran too long: " + elapsed + "ms");
const outPath = path.join(webRoot, res.out_url.replace(/^\//, ""));
const output = fs.readFileSync(outPath, "utf8");
assert.match(output, /\[timed out after 1s\]/);
assert.doesNotMatch(output, /terminated by signal 9/);
assert.ok(fs.existsSync(pidFile), "musrfit child pid was not recorded");
const childPid = parseInt(fs.readFileSync(pidFile, "utf8").trim(), 10);
const childState = childProcess.spawnSync("sh", ["-c",
"kill -0 " + childPid + " 2>/dev/null && echo alive || echo dead"]
).stdout.toString().trim();
assert.strictEqual(childState, "dead",
"musrfit child " + childPid + " still alive after timeout");
}
console.log("CGI hardening tests passed");
} finally {
fs.rmSync(testRoot, { recursive: true, force: true });
}