From 0990cab5de5cc2383c7a36d416f1dd9d7d6847fb Mon Sep 17 00:00:00 2001 From: salman Date: Sun, 25 Oct 2026 08:05:00 +0100 Subject: [PATCH] Add CGI run-file metadata and streaming downloads Resolve instrument, year, and run identities using server-owned startup templates and data roots. Return exact filenames, relative destination paths, formats, sizes, and version tokens for desktop data-directory downloads. Stream binary files with explicit lengths and HTTP error responses. Cover current and historical layouts, HAL/HIFI naming, changed metadata, and path containment with integration tests, and document the API. --- cgi/README.md | 62 ++++++++++ cgi/musrfit_startup.xml | 3 +- cgi/single_musrfit.cgi | 181 +++++++++++++++++++++++++++++- package.json | 2 +- tests/cgi-run-download.test.js | 199 +++++++++++++++++++++++++++++++++ 5 files changed, 443 insertions(+), 4 deletions(-) create mode 100644 tests/cgi-run-download.test.js diff --git a/cgi/README.md b/cgi/README.md index 32a5bf5..c54a157 100644 --- a/cgi/README.md +++ b/cgi/README.md @@ -101,6 +101,68 @@ curl -s -X POST -d 'action=health' \ http://localhost/cgi-bin/single_musrfit.cgi ``` +### `action=run_file_info` and `action=download_run_file` + +These read-only actions supply run data to desktop clients. They work without +musrfit executables or a writable temporary directory and do not create web +jobs. Both take the same run identity: + +- `instrument`: `LEM`, `GPS`, `FLAME`, `LTF`, `Dolly`, `VMS`, `GPD`, `HAL`, + `ALC`, or `MISC` (case-insensitive; `HIFI` is an alias for `HAL`). +- `year`: a four-digit year, at least 1900. +- `run`: a positive run number of up to nine digits. + +The server expands its own startup-XML templates in configured order, searching +the configured `` roots and +`//his`. For each template it tries the exact +layout, followed by `/` for the flattened public data tree. +The original filename, including historical campaign names, is preserved. +Clients cannot select a server filesystem path. File and directory symlinks +must resolve inside the configured root that is being searched. + +`run_file_info` returns JSON with `ok`, `code`, `message`, `instrument`, `year`, +`run`, and these file properties: + +| Property | Meaning | +| --- | --- | +| `filename` | Exact data filename, including its extension | +| `relative_path` | Destination relative to the desktop Data directory, for example `lem/his/2026/lem26_his_2615.root` | +| `format` | MSR format: `MUSR-ROOT`, `PSI-BIN`, or `PSI-MDU` | +| `size` | File length in bytes | +| `mtime` | Modification time as Unix seconds | +| `version` | Opaque token identifying the observed file metadata; not a content checksum | + +```bash +curl --fail --silent --show-error \ + --data 'action=run_file_info&instrument=LEM&year=2026&run=2615' \ + http://musruser.psi.ch/cgi-bin/single_musrfit.cgi +``` + +`download_run_file` returns the binary file with `Content-Length`, +`Content-Disposition: attachment`, and `Cache-Control: no-store`. Transfers use +a fixed-size buffer, and raw files are never copied into the web temporary +directory or encoded as JSON/base64. + +The client should pass `version` from the metadata response when downloading. +If the file changed between requests, the CGI returns HTTP 409 and JSON with +`reason=run-file-changed`; fetch metadata again before retrying. Omitting +`version` downloads the file found at the start of the request: + +```bash +curl --fail --show-error --output lem26_his_2615.root \ + --data 'action=download_run_file&instrument=LEM&year=2026&run=2615' \ + http://musruser.psi.ch/cgi-bin/single_musrfit.cgi +``` + +Download errors use both an HTTP error status and a JSON diagnostic: 400 for +invalid identifiers, 404 with `outcome=missing-data` for missing files, 409 for +empty files or changed versions, and 503 for unconfigured templates or +unreadable files. Server paths are not included in these responses. A file +that grows during streaming is limited to its initial advertised length; if +it is truncated or a read fails, the response is incomplete and the server +logs the failure. The desktop must finish and validate a temporary download +before publishing it under the Data directory. + ### `action=write` Write an MSR file: diff --git a/cgi/musrfit_startup.xml b/cgi/musrfit_startup.xml index 06d39cd..8e13145 100644 --- a/cgi/musrfit_startup.xml +++ b/cgi/musrfit_startup.xml @@ -48,6 +48,7 @@ d%yyyy%/tdc/deltat_tdc_dolly_%rrrr%.bin d%yyyy%/tdc/mdu/deltat_tdc_dolly_%rrrr%.mdu + %yyyy%/flame%yy%_his_%rrrrr%.root d%yyyy%/tdc/root/deltat_tdc_flame_%yyyy%_%rrrr%.root d%yyyy%/tdc/deltat_tdc_flame_%rrrr%.bin d%yyyy%/tdc/mdu/deltat_tdc_flame_%yyyy%_%rrrr%.mdu @@ -95,4 +96,4 @@ d%yyyy%/tdc/lem%yy%_his_%rrrr%.root %yyyy%/lem%yy%_his_%rrrrr%.root d%yyyy%/tdc/lem%yy%_his_%rrrrr%.root - \ No newline at end of file + diff --git a/cgi/single_musrfit.cgi b/cgi/single_musrfit.cgi index 8356ca4..791dab9 100755 --- a/cgi/single_musrfit.cgi +++ b/cgi/single_musrfit.cgi @@ -10,7 +10,8 @@ use warnings; use CGI; use JSON::PP qw(encode_json decode_json); use File::Path qw(make_path); -use Fcntl qw(:DEFAULT :flock); +use Cwd qw(abs_path); +use Fcntl qw(:DEFAULT :flock :mode); use Digest::SHA qw(sha256_hex); my $WEBROOT = $ENV{MUSRFIT_WEBROOT} || "/var/www/html"; @@ -68,7 +69,16 @@ my $fn = $in->param("fn") || ""; # JSON response and health-check helpers. sub respond_json { - my ($payload_ref) = @_; + my ($payload_ref, $http_status) = @_; + # Existing fit actions keep their response contract. Download clients also + # need an HTTP error so JSON diagnostics cannot be mistaken for raw data. + if (defined($http_status)) { + my %status_text = ( + 400 => "Bad Request", 404 => "Not Found", 409 => "Conflict", + 500 => "Internal Server Error", 503 => "Service Unavailable" + ); + print "Status: $http_status $status_text{$http_status}\n"; + } print "Content-type: application/json\n"; print "Cache-Control: no-store\n\n"; print encode_json($payload_ref); @@ -1430,6 +1440,122 @@ sub read_startup_config { return $cfg; } +# Resolve a public run identity to an open data file. Metadata and download use +# the same descriptor/stat snapshot; no client path or executable is involved. +sub resolve_run_file { + my ($instrument, $year, $run) = @_; + my %instrument_names = ( + lem => "LEM", gps => "GPS", flame => "FLAME", ltf => "LTF", + dolly => "Dolly", vms => "VMS", gpd => "GPD", hal => "HAL", + hifi => "HAL", alc => "ALC", misc => "MISC" + ); + my $inst = lc($instrument || ""); + if (!exists($instrument_names{$inst})) { + return ({ ok => JSON::PP::false, code => 400, + message => "Invalid instrument", reason => "invalid-instrument" }); + } + if (!defined($year) || $year !~ /\A[0-9]{4}\z/ || $year < 1900) { + return ({ ok => JSON::PP::false, code => 400, + message => "Invalid year", reason => "invalid-year" }); + } + if (!defined($run) || $run !~ /\A[0-9]{1,9}\z/ || $run <= 0) { + return ({ ok => JSON::PP::false, code => 400, + message => "Invalid run number", reason => "invalid-run" }); + } + my $name = $instrument_names{$inst}; + my $folder = lc($name); + my $xml_inst = $folder eq "hal" ? "hifi" : $folder; + my $cfg = read_startup_config(); + my @templates = @{$cfg->{templates}{$xml_inst} || []}; + if (!@templates) { + return ({ ok => JSON::PP::false, code => 503, + message => "Run file templates are not configured for $name", + reason => "run-file-unconfigured" }); + } + + # Startup roots may be mounts or symlinks. Resolve them before checking + # containment, so a file symlink cannot escape a configured data tree. + my @roots = (); + my %seen_roots = (); + for my $root (@{$cfg->{data_paths}}, "$DATA_ROOT/$folder/his") { + next if $root !~ m{\A/}; + my $canonical = abs_path($root); + next if !defined($canonical) || !-d $canonical || $seen_roots{$canonical}++; + push(@roots, $canonical); + } + + my %seen_candidates = (); + my $unavailable; + for my $template (@templates) { + my $sub_path = $template; + my $short_year = substr($year, 2); + $sub_path =~ s/%yyyy%/$year/g; + $sub_path =~ s/%yy%/$short_year/g; + $sub_path =~ s/%(r+)%/sprintf("%0*d", length($1), $run)/egi; + $sub_path =~ s{\A\./}{}; + next if $sub_path !~ m{\A[A-Za-z0-9_+.-]+(?:/[A-Za-z0-9_+.-]+)*\z}; + next if $sub_path =~ m{(?:\A|/)\.{1,2}(?:/|\z)}; + my ($filename, $extension) = $sub_path =~ m{([^/]+\.([A-Za-z0-9]+))\z}; + next if !defined($extension); + my %formats = (root => "MUSR-ROOT", bin => "PSI-BIN", mdu => "PSI-MDU"); + my $format = $formats{lc($extension)}; + next if !defined($format); + $format = "PSI-MDU" if $xml_inst eq "hifi" && lc($extension) eq "bin"; + + # The public data tree also stores historical template names flattened + # under /. Keep the original filename and try the exact XML + # layout first rather than renaming a campaign-specific data file. + my @sub_paths = ($sub_path); + push(@sub_paths, "$year/$filename") if $sub_path ne "$year/$filename"; + for my $candidate (@sub_paths) { + for my $root (@roots) { + my $path = "$root/$candidate"; + next if $seen_candidates{$path}++; + my $canonical = abs_path($path); + next if !defined($canonical); + my $prefix = $root eq "/" ? "/" : "$root/"; + next if index($canonical, $prefix) != 0 || !-f $canonical; + my $fh; + if (!sysopen($fh, $canonical, O_RDONLY | O_NONBLOCK)) { + $unavailable = { ok => JSON::PP::false, code => 503, + message => "Run data file is not readable", + reason => "run-file-unreadable" }; + next; + } + my @stats = stat($fh); + if (!@stats || !S_ISREG($stats[2])) { + close($fh); + next; + } + if ($stats[7] == 0) { + close($fh); + $unavailable = { ok => JSON::PP::false, code => 409, + message => "Run data file is empty; try again later", + reason => "run-file-empty" }; + next; + } + binmode($fh, ":raw"); + # This describes the file snapshot, not a content checksum. A + # desktop sends it back to avoid downloading changed metadata. + my $version = sha256_hex(join("\0", $name, $year, 0 + $run, + $candidate, @stats[0, 1, 7, 9, 10])); + return ({ + ok => JSON::PP::true, code => 0, message => "Run data file found", + instrument => $name, year => 0 + $year, run => 0 + $run, + filename => $filename, + relative_path => "$folder/his/$candidate", + format => $format, size => 0 + $stats[7], + mtime => 0 + $stats[9], version => $version + }, $fh); + } + } + } + return ($unavailable) if defined($unavailable); + return ({ ok => JSON::PP::false, code => 404, + message => "Data file not found for $name $year run " . (0 + $run), + reason => "run-file-not-found", outcome => "missing-data" }); +} + # Resolve the single MUSRFULLDATAPATH musrfit/musrview uses to find relative # RUN data files. An admin-supplied value wins. Otherwise prefer a # from the startup XML that exists on disk (matching what the portable-RUN @@ -1803,6 +1929,57 @@ sub apply_add_runs_to_msr { return (join("\n", @output), \@missing); } +# These read-only actions are independent of web jobs and the musrfit tools. +# Electron uses metadata first, then streams the chosen file to its data root. +if ($action eq "run_file_info" || $action eq "download_run_file") { + my ($info, $data_fh) = resolve_run_file( + scalar($in->param("instrument")), scalar($in->param("year")), + scalar($in->param("run")) + ); + respond_json($info, $info->{code}) if !$info->{ok}; + if ($action eq "run_file_info") { + close($data_fh); + respond_json($info); + } + + my $version = $in->param("version"); + if (defined($version) && $version !~ /\A[0-9a-f]{64}\z/) { + close($data_fh); + respond_json({ ok => JSON::PP::false, code => 400, + message => "Invalid file version", reason => "invalid-file-version" }, 400); + } + if (defined($version) && $version ne $info->{version}) { + close($data_fh); + respond_json({ ok => JSON::PP::false, code => 409, + message => "Run data file changed; request its metadata again", + reason => "run-file-changed" }, 409); + } + + binmode(STDOUT, ":raw"); + print "Content-Type: application/octet-stream\n"; + print "Content-Disposition: attachment; filename=\"$info->{filename}\"\n"; + print "Content-Length: $info->{size}\n"; + print "Cache-Control: no-store\n"; + print "X-Content-Type-Options: nosniff\n\n"; + + # Bound both memory use and the response length. A run still acquiring + # counts may grow during a transfer; never send beyond the advertised size. + # After binary headers, failures go to Apache's error log, not into the file. + my $remaining = $info->{size}; + while ($remaining > 0) { + my $length = $remaining > 65536 ? 65536 : $remaining; + my $read = read($data_fh, my $buffer, $length); + next if !defined($read) && $!{EINTR}; + die "Run data download could not be read\n" if !defined($read); + die "Run data file was truncated during download\n" if $read == 0; + print STDOUT $buffer or die "Run data download connection closed\n"; + $remaining -= $read; + } + close($data_fh); + close(STDOUT) or die "Run data download could not be flushed\n"; + exit(0); +} + if ($action eq "health") { my $musrfit_status = executable_status($MUSRFIT_BIN); my $musrview_status = executable_status($MUSRVIEW_BIN); diff --git a/package.json b/package.json index 3858841..bab1050 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "main": "main.js", "scripts": { "start": "electron .", - "test": "npm run test:electron && node tests/cgi-hardening.test.js && node tests/cgi-startup.test.js && node tests/cgi-integration.test.js && node tests/integration-musrfit.test.js", + "test": "npm run test:electron && node tests/cgi-hardening.test.js && node tests/cgi-startup.test.js && node tests/cgi-run-download.test.js && node tests/cgi-integration.test.js && node tests/integration-musrfit.test.js", "dist:linux": "npm run test:electron && npx --yes electron-builder@26.15.3 --linux rpm AppImage --x64 && bash packaging/runtime/build-runtime-appimage.sh && bash packaging/runtime/build-runtime-rpm.sh", "dist:rpm": "npm run test:electron && npx --yes electron-builder@26.15.3 --linux rpm --x64 && bash packaging/runtime/build-runtime-rpm.sh", "dist:appimage": "npm run test:electron && npx --yes electron-builder@26.15.3 --linux AppImage --x64 && bash packaging/runtime/build-runtime-appimage.sh", diff --git a/tests/cgi-run-download.test.js b/tests/cgi-run-download.test.js new file mode 100644 index 0000000..0eaaadf --- /dev/null +++ b/tests/cgi-run-download.test.js @@ -0,0 +1,199 @@ +// Copyright (C) 2026 Zaher Salman +// SPDX-License-Identifier: GPL-3.0-or-later + +// Exercise the real Perl endpoint with temporary data trees. Inspect binary +// stdout as bytes so NULs, newlines, and invalid UTF-8 cannot hide corruption. +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-download-")); +const dataRoot = path.join(testRoot, "data"); +const firstRoot = path.join(testRoot, "first"); +const secondRoot = path.join(testRoot, "second"); +const tmpRoot = path.join(testRoot, "unused-web-tmp"); +const startupPath = path.join(testRoot, "musrfit_startup.xml"); +const binary = Buffer.alloc(180003); +for (let index = 0; index < binary.length; index++) { + binary[index] = index % 256; +} + +function writeData(root, relativePath, content) { + const target = path.join(root, relativePath); + fs.mkdirSync(path.dirname(target), { recursive: true }); + fs.writeFileSync(target, content === undefined ? binary : content); + return target; +} + +function request(action, params, extraEnv) { + const body = new URLSearchParams(Object.assign({ + action: action, instrument: "LEM", year: "2026", run: "514" + }, params)).toString(); + const result = childProcess.spawnSync("perl", [path.join(repoRoot, "cgi", "single_musrfit.cgi")], { + cwd: repoRoot, + input: body, + timeout: 10000, + maxBuffer: 2 * 1024 * 1024, + env: Object.assign({}, process.env, { + REQUEST_METHOD: "POST", + CONTENT_TYPE: "application/x-www-form-urlencoded", + CONTENT_LENGTH: String(Buffer.byteLength(body)), + PERL5LIB: path.join(repoRoot, "tests", "perl"), + MUSRFIT_WEBROOT: path.join(testRoot, "unused-web-root"), + MUSRFIT_TMPROOT: tmpRoot, + MUSRFIT_DATA_ROOT: dataRoot, + MUSRFIT_STARTUP_XML: startupPath, + // Downloads must work without any musrfit installation. + MUSRFIT_BIN: "/no/musrfit", MUSRVIEW_BIN: "/no/musrview", + MSR2DATA_BIN: "/no/msr2data", DUMP_HEADER_BIN: "/no/dump_header" + }, extraEnv) + }); + assert.ifError(result.error); + assert.strictEqual(result.status, 0, result.stderr.toString()); + const separator = result.stdout.indexOf("\n\n"); + assert.ok(separator > 0, "CGI headers precede the response body"); + const headers = Object.fromEntries(result.stdout.subarray(0, separator).toString("ascii") + .split("\n").map(function(line) { + const colon = line.indexOf(":"); + return [line.slice(0, colon).toLowerCase(), line.slice(colon + 1).trim()]; + })); + const responseBody = result.stdout.subarray(separator + 2); + return { + headers: headers, body: responseBody, + json: headers["content-type"] === "application/json" + ? JSON.parse(responseBody.toString("utf8")) : null + }; +} + +try { + const lemFile = writeData(firstRoot, "2026/lem26_his_0514.root"); + writeData(secondRoot, "d2026/tdc/lem26_his_0600.root"); + writeData(secondRoot, "2026/lem26_his_00601.root"); + writeData(dataRoot, "gps/his/2007/deltat_ccr_0007.bin"); + writeData(secondRoot, "d2024/tdc/root/deltatbc_tdc_gps_2024_0008.root"); + writeData(secondRoot, "d2015/tdc/tdc_hifi_2015_00009.bin"); + fs.writeFileSync(startupPath, ` +${firstRoot}/ +${secondRoot} +%yyyy%/lem%yy%_his_%rrrr%.root +d%yyyy%/tdc/lem%yy%_his_%rrrr%.root +%yyyy%/lem%yy%_his_%rrrrr%.root +d%yyyy%/tdc/root/deltatbc_tdc_gps_%yyyy%_%rrrr%.root +d%yyyy%/deltat_ccr_%rrrr%.bin +d%yyyy%/tdc/tdc_hifi_%yyyy%_%rrrrr%.bin +`); + + const info = request("run_file_info").json; + assert.strictEqual(info.ok, true); + assert.strictEqual(info.code, 0); + assert.strictEqual(info.instrument, "LEM"); + assert.strictEqual(info.year, 2026); + assert.strictEqual(info.run, 514); + assert.strictEqual(info.filename, "lem26_his_0514.root"); + assert.strictEqual(info.relative_path, "lem/his/2026/lem26_his_0514.root"); + assert.strictEqual(info.format, "MUSR-ROOT"); + assert.strictEqual(info.size, binary.length); + assert.strictEqual(info.mtime, Math.floor(fs.statSync(lemFile).mtimeMs / 1000)); + assert.match(info.version, /^[0-9a-f]{64}$/); + assert.ok(!JSON.stringify(info).includes(testRoot), "metadata does not expose server paths"); + assert.strictEqual(request("run_file_info", { instrument: "lem", run: "000514" }).json.version, + info.version, "equivalent run identities have the same version"); + + const download = request("download_run_file", { version: info.version }); + assert.strictEqual(download.headers["content-type"], "application/octet-stream"); + assert.strictEqual(download.headers["content-disposition"], 'attachment; filename="lem26_his_0514.root"'); + assert.strictEqual(download.headers["content-length"], String(binary.length)); + assert.strictEqual(download.headers["cache-control"], "no-store"); + assert.strictEqual(download.headers["x-content-type-options"], "nosniff"); + assert.deepStrictEqual(download.body, binary, "multi-chunk download preserves every byte"); + + // Template and root priority are the same for both actions, including + // older campaign filenames and the HAL/HIFI format exception. + const cases = [ + [{ run: "600" }, "LEM", "lem/his/d2026/tdc/lem26_his_0600.root", "MUSR-ROOT"], + [{ run: "601" }, "LEM", "lem/his/2026/lem26_his_00601.root", "MUSR-ROOT"], + [{ instrument: "GPS", year: "2007", run: "7" }, "GPS", "gps/his/2007/deltat_ccr_0007.bin", "PSI-BIN"], + [{ instrument: "gps", year: "2024", run: "8" }, "GPS", "gps/his/d2024/tdc/root/deltatbc_tdc_gps_2024_0008.root", "MUSR-ROOT"], + [{ instrument: "HAL", year: "2015", run: "9" }, "HAL", "hal/his/d2015/tdc/tdc_hifi_2015_00009.bin", "PSI-MDU"], + [{ instrument: "hifi", year: "2015", run: "9" }, "HAL", "hal/his/d2015/tdc/tdc_hifi_2015_00009.bin", "PSI-MDU"] + ]; + for (const [params, instrument, relativePath, format] of cases) { + const metadata = request("run_file_info", params).json; + assert.strictEqual(metadata.ok, true, JSON.stringify(metadata)); + assert.strictEqual(metadata.instrument, instrument); + assert.strictEqual(metadata.relative_path, relativePath); + assert.strictEqual(metadata.format, format); + assert.deepStrictEqual(request("download_run_file", params).body, binary); + } + writeData(firstRoot, "2026/lem26_his_0600.root", "preferred template"); + assert.strictEqual(request("download_run_file", { run: "600" }).body.toString(), + "preferred template", "an earlier matching template takes precedence"); + + const shippedXmlPath = path.join(testRoot, "shipped-startup.xml"); + const shippedXml = fs.readFileSync(path.join(repoRoot, "cgi", "musrfit_startup.xml"), "utf8") + .replace(/[^<]*<\/data_path>/g, + "" + path.join(testRoot, "unused-shipped-root") + ""); + fs.writeFileSync(shippedXmlPath, shippedXml); + const flame = request("run_file_info", { instrument: "FLAME", run: "1285" }, { + MUSRFIT_STARTUP_XML: shippedXmlPath, + MUSRFIT_DATA_ROOT: path.join(testRoot, "shipped-layout") + }); + assert.strictEqual(flame.json.code, 404, "shipped templates recognize the modern FLAME layout"); + writeData(dataRoot, "flame/his/2026/flame26_his_01285.root"); + const shippedFlame = request("run_file_info", { instrument: "FLAME", run: "1285" }, { + MUSRFIT_STARTUP_XML: shippedXmlPath + }).json; + assert.strictEqual(shippedFlame.relative_path, "flame/his/2026/flame26_his_01285.root"); + + // Stale metadata returns JSON with an HTTP error, never a binary response + // carrying bytes different from the file the desktop agreed to download. + fs.appendFileSync(lemFile, "new counts"); + const changed = request("download_run_file", { version: info.version }); + assert.strictEqual(changed.headers.status, "409 Conflict"); + assert.strictEqual(changed.json.reason, "run-file-changed"); + assert.strictEqual(request("download_run_file", { version: "bogus" }).json.code, 400); + const refreshed = request("run_file_info").json; + assert.notStrictEqual(refreshed.version, info.version); + assert.strictEqual(request("download_run_file", { version: refreshed.version }).body.length, + refreshed.size); + + for (const action of ["run_file_info", "download_run_file"]) { + for (const params of [ + { instrument: "../lem" }, { instrument: "unknown" }, + { year: "26" }, { year: "2026/.." }, { run: "0" }, + { run: "-1" }, { run: "1.5" }, { run: "1e3" }, + { run: "514\n" }, { run: "1/../../etc/passwd" }, { run: "1234567890" } + ]) { + const response = request(action, params); + assert.strictEqual(response.headers.status, "400 Bad Request", JSON.stringify(params)); + assert.strictEqual(response.json.ok, false); + } + const missing = request(action, { run: "9999", fn: lemFile }); + assert.strictEqual(missing.headers.status, "404 Not Found"); + assert.strictEqual(missing.json.outcome, "missing-data"); + assert.strictEqual(request(action, {}, { + MUSRFIT_STARTUP_XML: path.join(testRoot, "missing.xml") + }).json.reason, "run-file-unconfigured"); + } + writeData(firstRoot, "2026/lem26_his_0602.root", ""); + assert.strictEqual(request("download_run_file", { run: "602" }).json.reason, "run-file-empty"); + + // Resolve legitimate mount symlinks, but reject a data-file or directory + // symlink that escapes the configured roots. + const outside = writeData(testRoot, "private/outside.root"); + fs.symlinkSync(outside, path.join(firstRoot, "2026/lem26_his_0603.root")); + assert.strictEqual(request("download_run_file", { run: "603" }).json.code, 404); + writeData(testRoot, "private/lem27_his_0604.root"); + fs.symlinkSync(path.join(testRoot, "private"), path.join(firstRoot, "2027")); + assert.strictEqual(request("download_run_file", { run: "604", year: "2027" }).json.code, 404); + fs.symlinkSync(lemFile, path.join(firstRoot, "2026/lem26_his_0605.root")); + assert.strictEqual(request("run_file_info", { run: "605" }).json.ok, true); + + assert.strictEqual(fs.existsSync(tmpRoot), false, "read-only actions never create web jobs"); + console.log("CGI run metadata and binary download tests passed"); +} finally { + fs.rmSync(testRoot, { recursive: true, force: true }); +}