Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
+75
-3
@@ -20,6 +20,8 @@
|
||||
*/
|
||||
|
||||
const os = require("os");
|
||||
const path = require("path");
|
||||
const fsSync = require("fs");
|
||||
const fsp = require("fs/promises");
|
||||
const { execFile } = require("child_process");
|
||||
|
||||
@@ -110,10 +112,80 @@ async function readNetCounters() {
|
||||
}
|
||||
}
|
||||
|
||||
function pm2List() {
|
||||
// ---------------------------------------------------------------------------
|
||||
// pm2 process metrics (aggregate monit counters via `pm2 jlist`).
|
||||
//
|
||||
// The pm2 binary is resolved against a FIXED list of absolute directories —
|
||||
// never the inherited process PATH. execFile("pm2", ...) would look the name
|
||||
// up through whatever PATH this process inherited, and a login-shell launch
|
||||
// routinely includes user-writable entries (~/.local/bin, nvm/npm user bins);
|
||||
// a local attacker who can plant a fake `pm2` in one of those would get it
|
||||
// executed by this process on every metrics poll (the persist loop polls even
|
||||
// when nobody watches the page).
|
||||
//
|
||||
// The lookup list therefore contains only directories inside the panel's own
|
||||
// trust boundary: the panel's node_modules/.bin (pm2 is a panel dependency —
|
||||
// whoever can write there can already rewrite the panel itself, so this adds
|
||||
// no exposure and is what makes metrics work under the stock systemd unit,
|
||||
// whose minimal PATH does not include node_modules/.bin), the directory of
|
||||
// the running node binary (same argument — it is this process's interpreter;
|
||||
// covers nvm/container installs), and the root-owned system bin dirs. If pm2
|
||||
// is not found there, PM2 metrics degrade to null and the inherited PATH is
|
||||
// never searched as a fallback.
|
||||
// ---------------------------------------------------------------------------
|
||||
const PM2_LOOKUP_DIRS = [
|
||||
...new Set([
|
||||
path.join(__dirname, "..", "node_modules", ".bin"),
|
||||
path.dirname(process.execPath),
|
||||
"/usr/local/bin",
|
||||
"/usr/bin",
|
||||
].filter(Boolean)),
|
||||
];
|
||||
|
||||
function resolvePm2Binary(lookupDirs = PM2_LOOKUP_DIRS) {
|
||||
for (const dir of lookupDirs) {
|
||||
if (!dir || !path.isAbsolute(dir)) continue;
|
||||
const candidate = path.join(dir, "pm2");
|
||||
try {
|
||||
// statSync follows symlinks: node_modules/.bin/pm2 is a symlink into
|
||||
// the pm2 package, which is exactly the file we want.
|
||||
if (!fsSync.statSync(candidate).isFile()) continue;
|
||||
fsSync.accessSync(candidate, fsSync.constants.X_OK);
|
||||
return candidate;
|
||||
} catch (_) { /* not here / not usable; try the next directory */ }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
let _pm2Path = null;
|
||||
let _pm2PathResolved = false;
|
||||
|
||||
// execPath is a test hook; production callers take the resolved, memoized
|
||||
// default. Resolution is deliberately once-per-process: a pm2 that appears
|
||||
// mid-run is picked up on restart rather than re-scanned on every poll.
|
||||
function pm2List(execPath) {
|
||||
if (execPath === undefined) {
|
||||
if (!_pm2PathResolved) {
|
||||
_pm2Path = resolvePm2Binary();
|
||||
_pm2PathResolved = true;
|
||||
if (!_pm2Path) {
|
||||
console.warn(`[panel-metrics] pm2 not found in fixed lookup paths (${PM2_LOOKUP_DIRS.join(", ")}) — PM2 metrics disabled; the inherited PATH is never searched`);
|
||||
}
|
||||
}
|
||||
execPath = _pm2Path;
|
||||
}
|
||||
if (!execPath) return Promise.resolve(null);
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
execFile("pm2", ["jlist"], { timeout: 4000, maxBuffer: 4 * 1024 * 1024 }, (err, stdout) => {
|
||||
execFile(execPath, ["jlist"], {
|
||||
timeout: 4000,
|
||||
maxBuffer: 4 * 1024 * 1024,
|
||||
// pm2 is a Node script launched through `#!/usr/bin/env node`, so give
|
||||
// the child the same sanitized lookup list as its PATH — its own node
|
||||
// resolution then also stays inside the trusted directories. The env
|
||||
// spread keeps PM2_HOME / HOME intact for daemon socket discovery.
|
||||
env: { ...process.env, PATH: PM2_LOOKUP_DIRS.join(":") },
|
||||
}, (err, stdout) => {
|
||||
if (err || !stdout) return resolve(null);
|
||||
try {
|
||||
resolve(JSON.parse(stdout));
|
||||
@@ -498,4 +570,4 @@ class PanelMetricsCollector {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { PanelMetricsCollector };
|
||||
module.exports = { PanelMetricsCollector, PM2_LOOKUP_DIRS, resolvePm2Binary, pm2List };
|
||||
Reference in new issue
Block a user