Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,105 @@
|
||||
"use strict";
|
||||
|
||||
// Hardening tests for the panel-metrics pm2 resolution: the binary must come
|
||||
// from a fixed list of absolute, trust-boundary directories — never from the
|
||||
// inherited process PATH — and the child must run with that same sanitized
|
||||
// PATH (pm2 is a `#!/usr/bin/env node` script, so its own node resolution is
|
||||
// PATH-driven too).
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const fs = require("fs");
|
||||
const os = require("os");
|
||||
const path = require("path");
|
||||
|
||||
const { PM2_LOOKUP_DIRS, resolvePm2Binary, pm2List } = require("./panel-metrics.js");
|
||||
|
||||
function tmpDir() {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), "panel-metrics-"));
|
||||
}
|
||||
|
||||
// Await the body before removing the temp dir: a test body that returns a
|
||||
// promise (the fake-pm2 spawn) must finish while its files still exist —
|
||||
// removing early would race the child's own re-open of the script by path.
|
||||
function withCleanup(dir, fn) {
|
||||
return Promise.resolve(fn()).finally(() => {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
}
|
||||
|
||||
test("fixed lookup dirs are absolute paths inside the panel trust boundary", () => {
|
||||
for (const dir of PM2_LOOKUP_DIRS) {
|
||||
assert.ok(path.isAbsolute(dir), dir);
|
||||
}
|
||||
// The panel's own pm2 dependency (package.json: pm2 ^7.0.1) and the running
|
||||
// node's dir are both part of the trust boundary; the system dirs close the
|
||||
// list for global installs.
|
||||
assert.ok(PM2_LOOKUP_DIRS.includes(path.join(__dirname, "..", "node_modules", ".bin")));
|
||||
assert.ok(PM2_LOOKUP_DIRS.includes(path.dirname(process.execPath)));
|
||||
assert.ok(PM2_LOOKUP_DIRS.includes("/usr/local/bin"));
|
||||
assert.ok(PM2_LOOKUP_DIRS.includes("/usr/bin"));
|
||||
});
|
||||
|
||||
test("resolvePm2Binary finds an executable pm2 file in the trusted dirs", () => {
|
||||
const dir = tmpDir();
|
||||
withCleanup(dir, () => {
|
||||
const candidate = path.join(dir, "pm2");
|
||||
fs.writeFileSync(candidate, "#!/bin/sh\n");
|
||||
fs.chmodSync(candidate, 0o755);
|
||||
assert.strictEqual(resolvePm2Binary([dir]), candidate);
|
||||
});
|
||||
});
|
||||
|
||||
test("resolvePm2Binary resolves node_modules/.bin symlinks to the real script", () => {
|
||||
const binDir = tmpDir();
|
||||
const pkgDir = tmpDir();
|
||||
withCleanup(binDir, () => withCleanup(pkgDir, () => {
|
||||
const realScript = path.join(pkgDir, "bin", "pm2");
|
||||
fs.mkdirSync(path.dirname(realScript), { recursive: true });
|
||||
fs.writeFileSync(realScript, "#!/bin/sh\n");
|
||||
fs.chmodSync(realScript, 0o755);
|
||||
fs.symlinkSync(realScript, path.join(binDir, "pm2"));
|
||||
assert.strictEqual(resolvePm2Binary([binDir]), path.join(binDir, "pm2"));
|
||||
}));
|
||||
});
|
||||
|
||||
test("resolvePm2Binary skips non-executable files and directories named pm2", () => {
|
||||
const dirA = tmpDir();
|
||||
const dirB = tmpDir();
|
||||
withCleanup(dirA, () => withCleanup(dirB, () => {
|
||||
fs.mkdirSync(path.join(dirA, "pm2")); // a directory, not a file
|
||||
fs.writeFileSync(path.join(dirB, "pm2"), "#!/bin/sh\n");
|
||||
fs.chmodSync(path.join(dirB, "pm2"), 0o644); // not executable
|
||||
assert.strictEqual(resolvePm2Binary([dirA, dirB]), null);
|
||||
}));
|
||||
});
|
||||
|
||||
test("resolvePm2Binary ignores non-absolute and empty dirs defensively", () => {
|
||||
assert.strictEqual(resolvePm2Binary(["", "relative/dir", "/nonexistent-adpanel-test/pm2-dir"]), null);
|
||||
});
|
||||
|
||||
test("resolvePm2Binary never falls back to the inherited PATH", () => {
|
||||
// An empty lookup list resolves to null even if a `pm2` exists somewhere on
|
||||
// the inherited PATH.
|
||||
assert.strictEqual(resolvePm2Binary([]), null);
|
||||
});
|
||||
|
||||
test("pm2List runs the binary with a sanitized child PATH", async () => {
|
||||
const dir = tmpDir();
|
||||
withCleanup(dir, () => {
|
||||
// A fake pm2 that echoes back the PATH it was executed with.
|
||||
const fakePm2 = path.join(dir, "pm2");
|
||||
fs.writeFileSync(fakePm2, '#!/bin/sh\nprintf \'{"childPath":"%s"}\' "$PATH"\n');
|
||||
fs.chmodSync(fakePm2, 0o755);
|
||||
return (async () => {
|
||||
const result = await pm2List(fakePm2);
|
||||
assert.ok(result && typeof result === "object");
|
||||
assert.strictEqual(result.childPath, PM2_LOOKUP_DIRS.join(":"));
|
||||
})();
|
||||
});
|
||||
});
|
||||
|
||||
test("pm2List degrades to null for a missing or unusable binary", async () => {
|
||||
assert.strictEqual(await pm2List(null), null);
|
||||
assert.strictEqual(await pm2List("/nonexistent-adpanel-test/pm2"), null);
|
||||
});
|
||||
Reference in new issue
Block a user