Add server-side safeguards so heavy web workflows cannot crash Apache: - Run-concurrency gate: whole run/series_run_candidate requests lease flock slots under TMPROOT/.musrflow-run-slots (MUSRFIT_MAX_RUNS=2 default, MUSRFIT_MAX_RUNS_PER_IP optional per-client pool); saturated servers answer 503 server-busy and leases are released when the CGI process exits. - Per-child resource wrapper: every external command runs with RLIMIT_AS (MUSRFIT_CHILD_MEM_KB, 16 GiB), optional RLIMIT_CPU, and exec nice (MUSRFIT_CHILD_NICE=10) so a runaway child cannot starve Apache workers. - Retune MUSRFIT_CMD_TIMEOUT default 400 -> 360 (deploy knob). - Size/job backstops: POST body cap before CGI->new (413), write MSR cap (413), and MUSRFIT_MAX_JOBS refusal of new job folders (never deletes). - Exclude the .musrflow-run-slots dir from prune_stale_jobs and job counting. - Document the new knobs in cgi/README.md and add CGI hardening tests.
613 lines
24 KiB
JavaScript
613 lines
24 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 cgiEnv(body, extraEnv) {
|
|
return 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
|
|
});
|
|
}
|
|
|
|
function runCGI(body, extraEnv) {
|
|
const result = childProcess.spawnSync(
|
|
"perl",
|
|
[path.join(repoRoot, "cgi", "single_musrfit.cgi")],
|
|
{
|
|
cwd: repoRoot,
|
|
encoding: "utf8",
|
|
input: body,
|
|
env: cgiEnv(body, 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);
|
|
}
|
|
|
|
// Like runCGI, but hands the CGI's lifetime back to the caller so a request can
|
|
// stay in flight (holding admission slots) while other requests are fired.
|
|
function spawnCGI(body, extraEnv) {
|
|
const child = childProcess.spawn(
|
|
"perl",
|
|
[path.join(repoRoot, "cgi", "single_musrfit.cgi")],
|
|
{ cwd: repoRoot, env: cgiEnv(body, extraEnv) }
|
|
);
|
|
child.stdin.end(body);
|
|
return child;
|
|
}
|
|
|
|
function waitForFile(filePath, timeoutMs) {
|
|
const start = Date.now();
|
|
while (Date.now() - start < timeoutMs) {
|
|
if (fs.existsSync(filePath)) return true;
|
|
childProcess.execFileSync("sleep", ["0.1"]);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function waitForExitAsync(child, timeoutMs) {
|
|
if (child.exitCode !== null || child.signalCode !== null) {
|
|
return Promise.resolve(true);
|
|
}
|
|
return new Promise((resolve) => {
|
|
const timer = setTimeout(() => resolve(false), timeoutMs);
|
|
child.once("exit", () => { clearTimeout(timer); resolve(true); });
|
|
});
|
|
}
|
|
|
|
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);
|
|
|
|
(async () => {
|
|
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");
|
|
}
|
|
|
|
// 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");
|
|
}
|
|
|
|
// 15. Heavy-run admission (need to keep the slot pool irreducible): a fit in
|
|
// flight leases the single global slot, a concurrent second fit is
|
|
// deferred with 503 server-busy, and the slot is reclaimed as soon as
|
|
// the first request finishes and its process exits.
|
|
let gateChild = null;
|
|
try {
|
|
const pidFile = path.join(testRoot, "gate-run-pid.txt");
|
|
if (fs.existsSync(pidFile)) fs.unlinkSync(pidFile);
|
|
fs.writeFileSync(path.join(workPath, "input.msr"), standardMsr);
|
|
const body = new URLSearchParams({
|
|
action: "run",
|
|
fn: "/tmp/" + jobName + "/input.msr",
|
|
runs: "514",
|
|
base_run: "514"
|
|
}).toString();
|
|
gateChild = spawnCGI(body, {
|
|
MUSRFIT_BIN: fakeMusrfit,
|
|
MUSRFIT_MAX_RUNS: "1",
|
|
MUSRFIT_MAX_RUNS_PER_IP: "0",
|
|
MUSRFLOW_TEST_MUSRFIT_MODE: "hang",
|
|
MUSRFLOW_TEST_MUSRFIT_PID: pidFile,
|
|
REMOTE_ADDR: "198.51.100.7"
|
|
});
|
|
assert.ok(waitForFile(pidFile, 10000), "first fit never started");
|
|
const runningPid = parseInt(fs.readFileSync(pidFile, "utf8").trim(), 10);
|
|
assert.ok(Number.isInteger(runningPid) && runningPid > 0);
|
|
|
|
const second = runCGI(body, {
|
|
MUSRFIT_BIN: fakeMusrfit,
|
|
MUSRFIT_MAX_RUNS: "1",
|
|
MUSRFIT_MAX_RUNS_PER_IP: "0",
|
|
MUSRFLOW_TEST_MUSRFIT_MODE: "fail",
|
|
REMOTE_ADDR: "198.51.100.7"
|
|
});
|
|
assert.strictEqual(second.ok, false);
|
|
assert.strictEqual(second.code, 503);
|
|
assert.strictEqual(second.reason, "server-busy");
|
|
|
|
// Free the slot by killing the hung fit; the next request must then run.
|
|
try { process.kill(runningPid, "SIGKILL"); } catch (_e) { /* already gone */ }
|
|
const released = await waitForExitAsync(gateChild, 10000);
|
|
assert.ok(released, "first request never exited with its slot released");
|
|
gateChild = null;
|
|
|
|
const third = runCGI(body, {
|
|
MUSRFIT_BIN: fakeMusrfit,
|
|
MUSRFIT_MAX_RUNS: "1",
|
|
MUSRFIT_MAX_RUNS_PER_IP: "0",
|
|
MUSRFLOW_TEST_MUSRFIT_MODE: "fail",
|
|
REMOTE_ADDR: "198.51.100.7"
|
|
});
|
|
assert.notStrictEqual(third.code, 503, "slot was not released");
|
|
} finally {
|
|
if (gateChild) gateChild.kill("SIGKILL");
|
|
}
|
|
|
|
// 16. Per-client admission: the global pool still has capacity, but a second
|
|
// concurrent fit from the same client IP is deferred, while a different
|
|
// client may start one.
|
|
let ipChild = null;
|
|
try {
|
|
const pidFile = path.join(testRoot, "ipgate-run-pid.txt");
|
|
if (fs.existsSync(pidFile)) fs.unlinkSync(pidFile);
|
|
const body = new URLSearchParams({
|
|
action: "run",
|
|
fn: "/tmp/" + jobName + "/input.msr",
|
|
runs: "514",
|
|
base_run: "514"
|
|
}).toString();
|
|
ipChild = spawnCGI(body, {
|
|
MUSRFIT_BIN: fakeMusrfit,
|
|
MUSRFIT_MAX_RUNS: "4",
|
|
MUSRFIT_MAX_RUNS_PER_IP: "1",
|
|
MUSRFLOW_TEST_MUSRFIT_MODE: "hang",
|
|
MUSRFLOW_TEST_MUSRFIT_PID: pidFile,
|
|
REMOTE_ADDR: "198.51.100.8"
|
|
});
|
|
assert.ok(waitForFile(pidFile, 10000), "per-ip first fit never started");
|
|
const ipRunningPid = parseInt(fs.readFileSync(pidFile, "utf8").trim(), 10);
|
|
|
|
const sameIp = runCGI(body, {
|
|
MUSRFIT_BIN: fakeMusrfit,
|
|
MUSRFIT_MAX_RUNS: "4",
|
|
MUSRFIT_MAX_RUNS_PER_IP: "1",
|
|
MUSRFLOW_TEST_MUSRFIT_MODE: "fail",
|
|
REMOTE_ADDR: "198.51.100.8"
|
|
});
|
|
assert.strictEqual(sameIp.code, 503, "same client bypassed the per-IP pool");
|
|
assert.strictEqual(sameIp.reason, "server-busy");
|
|
|
|
const otherIp = runCGI(body, {
|
|
MUSRFIT_BIN: fakeMusrfit,
|
|
MUSRFIT_MAX_RUNS: "4",
|
|
MUSRFIT_MAX_RUNS_PER_IP: "1",
|
|
MUSRFLOW_TEST_MUSRFIT_MODE: "fail",
|
|
REMOTE_ADDR: "198.51.100.9"
|
|
});
|
|
assert.notStrictEqual(otherIp.code, 503,
|
|
"a different client was blocked by the per-IP pool");
|
|
try { process.kill(ipRunningPid, "SIGKILL"); } catch (_e) { /* already gone */ }
|
|
assert.ok(await waitForExitAsync(ipChild, 10000),
|
|
"per-ip request never exited with its slot released");
|
|
} finally {
|
|
if (ipChild) ipChild.kill("SIGKILL");
|
|
}
|
|
|
|
// 17. Child resource hooks: the fake musrfit records the virtual-memory
|
|
// rlimit and the scheduling priority it actually runs under, proving the
|
|
// ulimit/nice wrapper is applied to every forked external tool.
|
|
{
|
|
const probe = path.join(testRoot, "musrfit-probe");
|
|
fs.writeFileSync(probe, [
|
|
"#!/bin/sh",
|
|
"echo \"vmem:$(ulimit -v)\" > \"$MUSRFLOW_TEST_MUSRFIT_PROBE\"",
|
|
"echo \"nice:$(ps -o ni= -p $$ 2>/dev/null | tr -d ' ')\" >> \"$MUSRFLOW_TEST_MUSRFIT_PROBE\"",
|
|
"exit 1",
|
|
""
|
|
].join("\n"));
|
|
fs.chmodSync(probe, 0o755);
|
|
const probeFile = path.join(testRoot, "musrfit-probe.txt");
|
|
if (fs.existsSync(probeFile)) fs.unlinkSync(probeFile);
|
|
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: probe,
|
|
MUSRFIT_CHILD_MEM_KB: "8192",
|
|
MUSRFIT_CHILD_NICE: "6",
|
|
MUSRFLOW_TEST_MUSRFIT_PROBE: probeFile
|
|
});
|
|
assert.strictEqual(res.ok, false);
|
|
assert.strictEqual(res.code, 1);
|
|
const recorded = {};
|
|
for (const line of fs.readFileSync(probeFile, "utf8").trim().split("\n")) {
|
|
const sep = line.indexOf(":");
|
|
if (sep < 0) continue;
|
|
recorded[line.slice(0, sep)] = line.slice(sep + 1);
|
|
}
|
|
assert.strictEqual(recorded.vmem, "8192", "RLIMIT_AS was not applied");
|
|
if (recorded.nice !== undefined && recorded.nice !== "") {
|
|
assert.strictEqual(recorded.nice, "6", "child ran at an unexpected priority");
|
|
}
|
|
}
|
|
|
|
// 18. Oversized POST bodies are refused before CGI parameter parsing, so a
|
|
// huge request cannot balloon a worker's memory.
|
|
{
|
|
const oversized = new URLSearchParams({
|
|
action: "write",
|
|
fn: "/tmp/" + jobName + "/input.msr",
|
|
line0: "FITPARAMETER",
|
|
line1: "1 Alpha 1.0 0.01"
|
|
}).toString();
|
|
const res = runCGI(oversized, { MUSRFIT_MAX_REQUEST_BYTES: "2" });
|
|
assert.strictEqual(res.ok, false);
|
|
assert.strictEqual(res.code, 413);
|
|
assert.strictEqual(res.reason, "request-too-large");
|
|
}
|
|
|
|
// 19. Oversized MSR content for action=write is refused without touching the
|
|
// existing file.
|
|
{
|
|
const before = fs.readFileSync(path.join(workPath, "input.msr"), "utf8");
|
|
const res = runCGI(new URLSearchParams({
|
|
action: "write",
|
|
fn: "/tmp/" + jobName + "/input.msr",
|
|
line0: "FITPARAMETER",
|
|
line1: "1 Alpha 1.0 0.01"
|
|
}).toString(), { MUSRFIT_MAX_MSR_BYTES: "4" });
|
|
assert.strictEqual(res.ok, false);
|
|
assert.strictEqual(res.code, 413);
|
|
assert.strictEqual(res.reason, "msr-too-large");
|
|
assert.strictEqual(fs.readFileSync(path.join(workPath, "input.msr"), "utf8"), before,
|
|
"oversized MSR clobbered the existing file");
|
|
}
|
|
|
|
// 20. Job ceiling: new job directories are refused once the ceiling is hit,
|
|
// while existing sessions keep writing and nothing is ever deleted.
|
|
{
|
|
const limitRoot = path.join(testRoot, "joblimit");
|
|
fs.mkdirSync(path.join(limitRoot, "existingjob"), { recursive: true });
|
|
const newRes = runCGI(new URLSearchParams({
|
|
action: "write",
|
|
fn: "/tmp/newjob/input.msr",
|
|
line0: "FITPARAMETER"
|
|
}).toString(), { MUSRFIT_TMPROOT: limitRoot, MUSRFIT_MAX_JOBS: "1" });
|
|
assert.strictEqual(newRes.ok, false);
|
|
assert.strictEqual(newRes.code, 500);
|
|
assert.match(newRes.message, /job limit/i);
|
|
assert.strictEqual(fs.existsSync(path.join(limitRoot, "newjob")), false,
|
|
"refused job directory was still created");
|
|
|
|
const keepRes = runCGI(new URLSearchParams({
|
|
action: "write",
|
|
fn: "/tmp/existingjob/input.msr",
|
|
line0: "FITPARAMETER"
|
|
}).toString(), { MUSRFIT_TMPROOT: limitRoot, MUSRFIT_MAX_JOBS: "1" });
|
|
assert.strictEqual(keepRes.ok, true);
|
|
assert.strictEqual(fs.readFileSync(path.join(limitRoot, "existingjob", "input.msr"), "utf8"),
|
|
"FITPARAMETER\n", "existing session could not write");
|
|
}
|
|
|
|
console.log("CGI hardening tests passed");
|
|
} finally {
|
|
fs.rmSync(testRoot, { recursive: true, force: true });
|
|
}
|
|
})().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|