From e3ff9c803cd3bccdf39cf84f5bc36e52a61e1345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 14 Nov 2026 20:14:27 -0500 Subject: [PATCH] fix(engine): start nvidia-smi, vm_stat, ps and tasklist from trusted folders only (#7788) * fix(engine): start nvidia-smi, vm_stat, ps and tasklist from trusted folders only The renderer resolves these helpers through the shared trusted lookup and runs the absolute path, so a program with the same name in a project folder is never started in their place. * test(engine): memory and import tests follow helpers started by resolved path --- .../engine/src/services/audioFxImport.test.ts | 3 ++ .../src/services/availableMemory.test.ts | 35 +++++++----- .../engine/src/services/browserManager.ts | 21 +++++--- packages/engine/src/services/systemMemory.ts | 7 ++- .../src/services/trustedHelpers.test.ts | 54 +++++++++++++++++++ packages/engine/src/utils/processRss.ts | 5 +- 6 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 packages/engine/src/services/trustedHelpers.test.ts diff --git a/packages/engine/src/services/audioFxImport.test.ts b/packages/engine/src/services/audioFxImport.test.ts index d2cef97e89..049e29b692 100644 --- a/packages/engine/src/services/audioFxImport.test.ts +++ b/packages/engine/src/services/audioFxImport.test.ts @@ -1,9 +1,11 @@ import { describe, expect, it, vi } from "vitest"; const execSync = vi.hoisted(() => vi.fn(() => "")); +const execFileSync = vi.hoisted(() => vi.fn(() => "")); vi.mock("child_process", async (importActual) => ({ ...(await importActual()), execSync, + execFileSync, })); describe("importing the engine", () => { @@ -11,5 +13,6 @@ describe("importing the engine", () => { await import("./audioFxRender.js"); await import("../index.js"); expect(execSync).not.toHaveBeenCalled(); + expect(execFileSync).not.toHaveBeenCalled(); }, 60_000); }); diff --git a/packages/engine/src/services/availableMemory.test.ts b/packages/engine/src/services/availableMemory.test.ts index 508d4282f4..1872b153c0 100644 --- a/packages/engine/src/services/availableMemory.test.ts +++ b/packages/engine/src/services/availableMemory.test.ts @@ -16,24 +16,31 @@ describe("getAvailableMemoryMb", () => { platform: () => "darwin", freemem: () => 100 * 1024 * 1024, })); + vi.doMock("@hyperframes/parsers/ff-binaries", async () => ({ + ...(await vi.importActual("@hyperframes/parsers/ff-binaries")), + findTrustedExecutable: (name: string) => `/usr/bin/${name}`, + })); + const execFileSync = vi.fn(() => + [ + "Mach Virtual Memory Statistics: (page size of 16384 bytes)", + "Pages free: 5000.", + "Pages active: 200000.", + "Pages inactive: 100000.", + "Pages speculative: 2000.", + "Pages throttled: 0.", + "Pages wired down: 150000.", + "Pages purgeable: 3000.", + ].join("\n"), + ); vi.doMock("node:child_process", async () => ({ ...(await vi.importActual("node:child_process")), - execSync: (_cmd: string) => - [ - "Mach Virtual Memory Statistics: (page size of 16384 bytes)", - "Pages free: 5000.", - "Pages active: 200000.", - "Pages inactive: 100000.", - "Pages speculative: 2000.", - "Pages throttled: 0.", - "Pages wired down: 150000.", - "Pages purgeable: 3000.", - ].join("\n"), + execFileSync, })); const { getAvailableMemoryMb } = await import("./systemMemory.js"); const result = getAvailableMemoryMb(); + expect(execFileSync).toHaveBeenCalledWith("/usr/bin/vm_stat", [], expect.anything()); // (5000 + 100000 + 3000 + 2000) * 16384 = 110000 * 16384 = 1,802,240,000 bytes // 1,802,240,000 / (1024 * 1024) = ~1718 MB expect(result).toBe(Math.trunc((110000 * 16384) / (1024 * 1024))); @@ -45,9 +52,13 @@ describe("getAvailableMemoryMb", () => { platform: () => "darwin", freemem: () => 4 * 1024 * 1024 * 1024, })); + vi.doMock("@hyperframes/parsers/ff-binaries", async () => ({ + ...(await vi.importActual("@hyperframes/parsers/ff-binaries")), + findTrustedExecutable: (name: string) => `/usr/bin/${name}`, + })); vi.doMock("node:child_process", async () => ({ ...(await vi.importActual("node:child_process")), - execSync: () => { + execFileSync: () => { throw new Error("vm_stat not found"); }, })); diff --git a/packages/engine/src/services/browserManager.ts b/packages/engine/src/services/browserManager.ts index 3756338da6..0f6921be37 100644 --- a/packages/engine/src/services/browserManager.ts +++ b/packages/engine/src/services/browserManager.ts @@ -18,7 +18,7 @@ import { } from "./sandboxLaunch.js"; import { scanHtmlOpeningTags } from "@hyperframes/parsers/html-attribute-spans"; import type { Browser, LaunchOptions, Page, PuppeteerNode } from "puppeteer-core"; -import { execSync } from "child_process"; +import { execFileSync } from "child_process"; import { existsSync, readdirSync, statSync } from "fs"; import { join } from "path"; import { homedir } from "os"; @@ -56,6 +56,7 @@ export type { CaptureMode, } from "./browserLeasePool.js"; import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { findTrustedExecutable } from "@hyperframes/parsers/ff-binaries"; let _puppeteer: PuppeteerNode | undefined; @@ -1105,13 +1106,19 @@ let _cachedVramMb: number | null = null; function probeNvidiaVramMb(): number | null { if (_cachedVramMb !== null) return _cachedVramMb; try { + const nvidiaSmi = findTrustedExecutable("nvidia-smi"); + if (!nvidiaSmi) return null; // Synchronous, runs once per process (cached). ~50ms on typical systems. - const out = execSync("nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits", { - timeout: 3000, - encoding: "utf-8", - stdio: ["pipe", "pipe", "pipe"], - windowsHide: true, - }).trim(); + const out = execFileSync( + nvidiaSmi, + ["--query-gpu=memory.total", "--format=csv,noheader,nounits"], + { + timeout: 3000, + encoding: "utf-8", + stdio: ["pipe", "pipe", "pipe"], + windowsHide: true, + }, + ).trim(); const mb = parseInt(out.split("\n")[0] ?? "", 10); if (Number.isFinite(mb) && mb > 0) { _cachedVramMb = mb; diff --git a/packages/engine/src/services/systemMemory.ts b/packages/engine/src/services/systemMemory.ts index 1d39788298..bfce941589 100644 --- a/packages/engine/src/services/systemMemory.ts +++ b/packages/engine/src/services/systemMemory.ts @@ -8,7 +8,8 @@ * it lives here once instead of being re-derived inline. */ -import { execSync } from "child_process"; +import { execFileSync } from "child_process"; +import { findTrustedExecutable } from "@hyperframes/parsers/ff-binaries"; import { readFileSync } from "fs"; import { availableParallelism, cpus, freemem, platform, totalmem } from "os"; @@ -234,7 +235,9 @@ export function getAvailableMemoryMb(): number { if (platform() === "darwin") { try { - const raw = execSync("vm_stat", { encoding: "utf-8", timeout: 5000 }); + const vmStat = findTrustedExecutable("vm_stat"); + if (!vmStat) return fallback; + const raw = execFileSync(vmStat, [], { encoding: "utf-8", timeout: 5000 }); const pageSize = parseInt(raw.match(/page size of (\d+)/)?.[1] ?? "0", 10); if (!pageSize) return fallback; diff --git a/packages/engine/src/services/trustedHelpers.test.ts b/packages/engine/src/services/trustedHelpers.test.ts new file mode 100644 index 0000000000..edaa01024c --- /dev/null +++ b/packages/engine/src/services/trustedHelpers.test.ts @@ -0,0 +1,54 @@ +import { chmodSync, existsSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { delimiter, join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +// The project folder is the working directory and the first PATH entry, as when a render runs from it. +describe.skipIf(process.platform === "win32")("helper programs the renderer starts", () => { + const originalCwd = process.cwd(); + const originalPath = process.env.PATH; + let project: string; + let ran: string; + + beforeEach(() => { + project = realpathSync(mkdtempSync(join(tmpdir(), "hf-helpers-"))); + ran = join(project, "ran"); + for (const name of ["nvidia-smi", "vm_stat", "ps"]) { + const file = join(project, name); + writeFileSync(file, `#!/bin/sh\necho ${name} >> "${ran}"\necho 4096\n`); + chmodSync(file, 0o755); + } + process.chdir(project); + process.env.PATH = `${project}${delimiter}${originalPath}`; + vi.resetModules(); + }); + + afterEach(() => { + process.chdir(originalCwd); + process.env.PATH = originalPath; + vi.doUnmock("os"); + rmSync(project, { recursive: true, force: true }); + }); + + it("never runs an nvidia-smi from the project folder", async () => { + const { buildChromeArgs } = await import("./browserManager.js"); + buildChromeArgs({ width: 1920, height: 1080 }); + expect(existsSync(ran)).toBe(false); + }); + + it("never runs a vm_stat from the project folder", async () => { + vi.doMock("os", async (importOriginal) => { + const os = await importOriginal(); + return { ...os, default: os, platform: () => "darwin" }; + }); + const { getAvailableMemoryMb } = await import("./systemMemory.js"); + getAvailableMemoryMb(); + expect(existsSync(ran)).toBe(false); + }); + + it("never runs a ps from the project folder", async () => { + const { sampleProcessRss } = await import("../utils/processRss.js"); + await sampleProcessRss([process.pid], undefined, "darwin"); + expect(existsSync(ran)).toBe(false); + }); +}); diff --git a/packages/engine/src/utils/processRss.ts b/packages/engine/src/utils/processRss.ts index 6c9b83f6bc..d627cd2d59 100644 --- a/packages/engine/src/utils/processRss.ts +++ b/packages/engine/src/utils/processRss.ts @@ -1,6 +1,7 @@ import { execFile } from "node:child_process"; import { readFile } from "node:fs/promises"; import { promisify } from "node:util"; +import { findTrustedExecutable } from "@hyperframes/parsers/ff-binaries"; export interface ProcessRssSample { pid: number; @@ -19,7 +20,9 @@ function defaultExec(): ExecFileLike { // (same reason psnr.ts does this). const execFileP = promisify(execFile); return async (file, args) => { - const { stdout } = await execFileP(file, [...args], { windowsHide: true, timeout: 5_000 }); + const binary = findTrustedExecutable(file); + if (!binary) throw new Error(`${file} is not in a trusted PATH folder`); + const { stdout } = await execFileP(binary, [...args], { windowsHide: true, timeout: 5_000 }); return { stdout: String(stdout) }; }; }