mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
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
This commit is contained in:
@@ -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<typeof import("child_process")>()),
|
||||
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);
|
||||
});
|
||||
|
||||
@@ -16,24 +16,31 @@ describe("getAvailableMemoryMb", () => {
|
||||
platform: () => "darwin",
|
||||
freemem: () => 100 * 1024 * 1024,
|
||||
}));
|
||||
vi.doMock("@hyperframes/parsers/ff-binaries", async () => ({
|
||||
...(await vi.importActual<object>("@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<typeof import("node:child_process")>("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<object>("@hyperframes/parsers/ff-binaries")),
|
||||
findTrustedExecutable: (name: string) => `/usr/bin/${name}`,
|
||||
}));
|
||||
vi.doMock("node:child_process", async () => ({
|
||||
...(await vi.importActual<typeof import("node:child_process")>("node:child_process")),
|
||||
execSync: () => {
|
||||
execFileSync: () => {
|
||||
throw new Error("vm_stat not found");
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<typeof import("os")>();
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -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) };
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user