From 071a2da0b5b1f3d278650fb432da79b32726990d Mon Sep 17 00:00:00 2001 From: WaterrrForever Date: Mon, 16 Nov 2026 15:21:35 +0000 Subject: [PATCH] fix(cli): own render-slot lock dir; refuse cache files other accounts can write (#7951) * fix(cli): own render-slot lock dir; refuse cache files other accounts can write Sharing one render slot across accounts no longer means sharing HYPERFRAMES_CACHE_DIR, which holds the browser, packages and helpers each account runs. HYPERFRAMES_RENDER_LOCK_DIR names a folder of lock records only, created sticky and writable by all (1777); the per-user lock folder is created 0700. Help text, errors and docs no longer suggest sharing the cache or chmod 777. foreignWritablePath (core/cache-dir) refuses a cached file whose path up to the cache root is owned by another account or is group/world-writable. Chrome (CLI, engine, inspector), optional packages (incl. the Sherpa runtime), the whisper.cpp build, the TTS script, the Apple audio helpers, opencv.js and the Lambda handler reinstall or rebuild instead of running such a file, and trustedCacheRoot moves their installs to ~/.cache/hyperframes when another account can write HYPERFRAMES_CACHE_DIR. Co-Authored-By: Claude Opus 5.5 * fix(cli): seal own cache installs against umask 002 and repair old lock folder modes sealCacheInstall (core/cache-dir) takes group and world write off what this account just installed and the folders above it up to the cache root, so an install made under umask 002 passes foreignWritablePath on the next run instead of being refused and fetched again. Chrome, optional packages, the whisper.cpp build, opencv.js, the TTS script, the Apple audio helpers and the Lambda handler call it after installing. ensureLockDir makes an existing per-user lock folder 0700 again when an older release left it open, and adds the sticky bit to an existing HYPERFRAMES_RENDER_LOCK_DIR folder, warning once with the folder's name when it cannot. Co-Authored-By: Claude Opus 5.5 * refactor(cli): drop added comments to pass the comment ratchet The explanations move into names: the mode bits in core/cache-dir are GROUP_OR_WORLD_WRITE and WITHOUT_GROUP_OR_WORLD_WRITE, and the owner check is anotherAccountCanWrite. No change in behaviour. Co-Authored-By: Claude Opus 5.5 * fix(cli): verify the lock folder through a no-follow handle and stage installs owner-only The render lock folder is opened with O_NOFOLLOW | O_DIRECTORY and checked and chmod-ed through that descriptor: a link, or a folder another account owns (root may own a shared one), is refused with RenderLockFolderRefused instead of being made world-writable or used as is. Chrome staging, the optional-package staging folder, the whisper.cpp build folder and the opencv, TTS, Apple helper and Lambda handler folders are created 0700, and Chrome's staged tree is sealed before it leaves staging, so nothing is group-writable while it installs. foreignWritablePath accepts a sticky folder owned by this account or root, as /tmp is, and tolerates a statSync without options; tests whose cache sat directly in /tmp, and tests that mock statSync, failed on both. Docs note that a record naming a live process of another account holds the shared slot. Co-Authored-By: Claude Opus 5.5 * test(cli): skip the uid-based Chrome cache refusal cases where there is no uid Windows has no process.getuid, so the ownership check never runs there and the two cases that expect a refusal cannot hold. Co-Authored-By: Claude Opus 5.5 * chore(core): raise the unpacked size budget for the cache ownership checks core grew to 4418013 bytes unpacked with foreignWritablePath, sealCacheInstall and trustedCacheRoot; the budget follows the usual 5% headroom over that size. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- docs/packages/cli.mdx | 15 +- .../cli/src/audio/analysis/providers/apple.ts | 15 +- packages/cli/src/browser/manager.test.ts | 58 +++++++- packages/cli/src/browser/manager.ts | 30 +++- .../src/commands/lambda/prebuiltHandler.ts | 17 ++- packages/cli/src/commands/render.ts | 4 +- .../cli/src/commands/render/renderSlot.ts | 14 +- packages/cli/src/measure/opencv.ts | 17 ++- packages/cli/src/tts/synthesize.ts | 14 +- .../cli/src/utils/optionalPackages.test.ts | 84 +++++++++++ packages/cli/src/utils/optionalPackages.ts | 25 +++- packages/cli/src/utils/renderLock.test.ts | 137 ++++++++++++++++-- packages/cli/src/utils/renderLock.ts | 17 ++- packages/cli/src/utils/renderLockClaim.ts | 97 +++++++++++-- packages/cli/src/whisper/manager.ts | 16 +- packages/cli/src/whisper/sherpa.ts | 4 +- packages/core/src/cacheDir.test.ts | 107 +++++++++++++- packages/core/src/cacheDir.ts | 64 +++++++- .../engine/src/services/browserManager.ts | 11 +- packages/inspector/src/service.ts | 7 +- scripts/package-size-budgets.json | 2 +- 21 files changed, 668 insertions(+), 87 deletions(-) diff --git a/docs/packages/cli.mdx b/docs/packages/cli.mdx index ab29c29fa8..39d7f8eb74 100644 --- a/docs/packages/cli.mdx +++ b/docs/packages/cli.mdx @@ -1934,9 +1934,18 @@ landing, so a second local or `--docker` render from the same user account waits for the first and says which render it is waiting for (pid and project). The slot is a lock file in that account's HyperFrames cache folder, so renders by different user accounts do not queue behind each other; set -`HYPERFRAMES_CACHE_DIR` to a folder they share to give them one slot. A render -whose process died frees its slot on its own. `--workers` is unaffected: it sets -the parallelism inside the one render that runs. +`HYPERFRAMES_RENDER_LOCK_DIR` to the same folder in each account to give them +one slot. That folder holds lock records only: the first render creates it +writable by every account but sticky, so no account can delete or replace +another's records. A record that names a live process of another account +counts as a running render, so an account in the group can hold the slot by +naming a process that never ends; `HYPERFRAMES_RENDER_LOCK=off` renders without +the slot. Do not share `HYPERFRAMES_CACHE_DIR` between accounts: it holds the +browser, packages and helpers each account runs. HyperFrames never +runs a cached one that another account can write: it installs its own copy, +under `~/.cache/hyperframes` when another account can write the cache folder +itself. A render whose process died frees its slot on its own. `--workers` is +unaffected: it sets the parallelism inside the one render that runs. Low-memory detection reads **host** RAM, not cgroup or container limits, so containerised callers — including `--docker` — should set diff --git a/packages/cli/src/audio/analysis/providers/apple.ts b/packages/cli/src/audio/analysis/providers/apple.ts index be0ee26fbe..878553ec80 100644 --- a/packages/cli/src/audio/analysis/providers/apple.ts +++ b/packages/cli/src/audio/analysis/providers/apple.ts @@ -5,7 +5,11 @@ import { mkdir, rename, writeFile } from "node:fs/promises"; import { join } from "node:path"; import type { AudioAnalysisProvider, AudioSignal } from "@hyperframes/core/audio-analysis"; import { AVAILABLE, runFile, unavailable, withWavFile } from "./shared.js"; -import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { + foreignWritablePath, + sealCacheInstall, + trustedCacheRoot, +} from "@hyperframes/core/cache-dir"; const SOUND_ANALYSIS = "/System/Library/Frameworks/SoundAnalysis.framework"; const MUSIC_UNDERSTANDING_MACOS = 27; @@ -63,7 +67,7 @@ function atLeastOneWindow(signal: AudioSignal): AudioSignal { }; } -const helperDir = () => hyperframesCacheDir("audio-helpers"); +const helperDir = () => join(trustedCacheRoot(), "audio-helpers"); function helperPath(name: string, source: string): string { const hash = createHash("sha256").update(source).digest("hex").slice(0, 12); @@ -105,11 +109,13 @@ const builds = new Map>(); /** The helper compiled from `source`, built once per source into the cache; Swift is never fetched. */ function compiled(name: string, source: string): Promise { const binary = helperPath(name, source); - if (existsSync(binary)) return Promise.resolve(binary); + if (existsSync(binary) && !foreignWritablePath(binary, trustedCacheRoot())) { + return Promise.resolve(binary); + } let build = builds.get(binary); if (!build) { build = (async () => { - await mkdir(helperDir(), { recursive: true }); + await mkdir(helperDir(), { recursive: true, mode: 0o700 }); const file = `${binary}.swift`; await writeFile(file, source); const args = swiftcArgs(file, `${binary}.tmp`); @@ -117,6 +123,7 @@ function compiled(name: string, source: string): Promise { throw new Error(`could not build the ${name} helper: ${firstLine(error)}`); }); await rename(`${binary}.tmp`, binary); + sealCacheInstall(binary, trustedCacheRoot()); return binary; })(); builds.set(binary, build); diff --git a/packages/cli/src/browser/manager.test.ts b/packages/cli/src/browser/manager.test.ts index 2ae5321af4..31c8d50e9a 100644 --- a/packages/cli/src/browser/manager.test.ts +++ b/packages/cli/src/browser/manager.test.ts @@ -126,6 +126,8 @@ interface FsMockOptions { osHost?: { platform: string; release: string }; /** paths whose stat reports 0 bytes */ emptyFiles?: ReadonlySet; + /** paths whose stat reports another account as owner */ + otherAccounts?: ReadonlySet; } function installFsMocks({ @@ -135,6 +137,7 @@ function installFsMocks({ initialMtimeMs = 0, osHost = { platform: "linux", release: "24.0.0" }, emptyFiles, + otherAccounts, }: FsMockOptions) { // Mutable, and returned, so tests can pre-seed a "lock already held" path or // assert the lock dir doesn't leak after ensureBrowser resolves. @@ -193,8 +196,24 @@ function installFsMocks({ (err as NodeJS.ErrnoException).code = "ENOENT"; throw err; } - return { mtimeMs: mtimes.get(p) ?? 0, size: emptyFiles?.has(p) ? 0 : 1, isFile: () => true }; + return { + mtimeMs: mtimes.get(p) ?? 0, + size: emptyFiles?.has(p) ? 0 : 1, + isFile: () => true, + uid: otherAccounts?.has(p) ? 4242 : (process.getuid?.() ?? 0), + mode: 0o755, + }; }, + lstatSync: (p: string) => + paths.has(p) + ? { + uid: otherAccounts?.has(p) ? 4242 : (process.getuid?.() ?? 0), + mode: 0o755, + isSymbolicLink: () => false, + isDirectory: () => false, + } + : undefined, + chmodSync: () => {}, utimesSync: (p: string, _atime: Date, mtime: Date) => { if (touchError) throw touchError; if (!paths.has(p)) { @@ -526,7 +545,7 @@ describe("findBrowser — cache resolution", () => { source: "download", }); expect(warnSpy).toHaveBeenCalledWith( - expect.stringContaining("Cached binary missing or not the pinned build"), + expect.stringContaining("Cached binary missing, not the pinned build"), ); // The partial extract is replaced wholesale, not merged into (install() would // otherwise throw "folder exists but exe missing", the bug both feedback reports hit). @@ -534,6 +553,41 @@ describe("findBrowser — cache resolution", () => { expect(paths.has(redownloadedBinary)).toBe(true); }); + it.skipIf(!process.getuid).each([ + ["the binary", HF_BINARY], + ["a folder above it", HF_CACHE], + ])( + "re-downloads instead of launching a cached build when another account owns %s", + async (_what, foreign) => { + const redownloadedBinary = join(HF_CACHE, "chrome-headless-shell", "redownloaded"); + installFsMocks({ + existing: new Set([HF_CACHE, HF_BINARY]), + otherAccounts: new Set([foreign]), + }); + installPuppeteerBrowsersMock({ + installedInHfCache: [ + { + browser: "chrome-headless-shell", + executablePath: HF_BINARY, + buildId: CHROME_VERSION, + }, + ], + installResult: { executablePath: redownloadedBinary }, + }); + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const { findBrowser } = await import("./manager.js"); + + expect(await findBrowser()).toEqual({ + executablePath: redownloadedBinary, + source: "download", + }); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining(`writable by another account at ${HF_BINARY}`), + ); + }, + ); + it("ensureBrowser({force: true}) re-downloads without purging the cache, bypassing any cache/system shortcut", async () => { const staleInstallDir = stagedVersionDir(HF_CACHE, "131.0.6778.85"); const downloadedBinary = join(HF_CACHE, "chrome-headless-shell", "force-downloaded"); diff --git a/packages/cli/src/browser/manager.ts b/packages/cli/src/browser/manager.ts index f5d33d7153..9776f113cf 100644 --- a/packages/cli/src/browser/manager.ts +++ b/packages/cli/src/browser/manager.ts @@ -26,7 +26,11 @@ import { BrowserDownloadStalledError, installInChild } from "./installInChild.js import { normalizeErrorMessage } from "../utils/errorMessage.js"; import { runRenderSetupWorker } from "../utils/cancellableProcess.js"; import { unzipArchive } from "./unzipArchive.js"; -import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { + foreignWritablePath, + sealCacheInstall, + trustedCacheRoot, +} from "@hyperframes/core/cache-dir"; type PuppeteerBrowsers = typeof import("@puppeteer/browsers"); @@ -42,8 +46,8 @@ async function loadPuppeteerBrowsers(): Promise { } } -const CACHE_ROOT_DIR = hyperframesCacheDir(); -const CACHE_DIR = hyperframesCacheDir("chrome"); +const CACHE_ROOT_DIR = trustedCacheRoot(); +const CACHE_DIR = join(CACHE_ROOT_DIR, "chrome"); const ARCHIVE_PLUS_UNPACKED_SHELL_MB = 512; // Puppeteer's managed cache — where `@puppeteer/browsers install // chrome-headless-shell` (and `puppeteer install`) drop binaries. The engine's @@ -334,7 +338,11 @@ async function findFromHyperframesCache(): Promise { b.buildId === managedChromeVersion() && b.platform === hostPlatform, ); - if (match && isPinnedHeadlessShell(match.executablePath)) { + if ( + match && + isPinnedHeadlessShell(match.executablePath) && + !foreignWritablePath(match.executablePath, CACHE_ROOT_DIR) + ) { return { result: { executablePath: match.executablePath, source: "cache" } }; } if (match) { @@ -453,7 +461,7 @@ export async function findBrowser(): Promise { if (fromCache.result) return fromCache.result; if (fromCache.staleHyperframesCachePath) { console.warn( - `[browser] Cached binary missing or not the pinned build at ${fromCache.staleHyperframesCachePath} — re-downloading...`, + `[browser] Cached binary missing, not the pinned build, or writable by another account at ${fromCache.staleHyperframesCachePath} — re-downloading...`, ); try { return await withInstallLock(() => downloadBrowser()); @@ -584,7 +592,7 @@ async function ensureBrowserInCurrentProcess( if (fromCache.result) return fromCache.result; if (fromCache.staleHyperframesCachePath) { console.warn( - `[browser] Cached binary missing or not the pinned build at ${fromCache.staleHyperframesCachePath} — re-downloading...`, + `[browser] Cached binary missing, not the pinned build, or writable by another account at ${fromCache.staleHyperframesCachePath} — re-downloading...`, ); return withInstallLock(() => downloadBrowser(options)); } @@ -845,7 +853,11 @@ async function downloadBrowser(options?: EnsureBrowserOptions): Promise { + mkdirSync(stagingDir, { recursive: true, mode: 0o700 }); + return install(); + }; try { const staged = await retryWhileLocked( @@ -904,7 +916,8 @@ function moveStagedInstallIntoCache( ): string { const target = join(CACHE_DIR, relative(stagingDir, staged.path)); const aside = join(CACHE_ROOT_DIR, `${REPLACED_PREFIX}${randomUUID()}`); - mkdirSync(dirname(target), { recursive: true }); + sealCacheInstall(staged.path, stagingDir); + mkdirSync(dirname(target), { recursive: true, mode: 0o755 }); let replaced = false; try { renameSync(target, aside); @@ -919,6 +932,7 @@ function moveStagedInstallIntoCache( throw err; } if (replaced) removeLeftover(aside); + sealCacheInstall(target, CACHE_ROOT_DIR); return join(target, relative(staged.path, staged.executablePath)); } diff --git a/packages/cli/src/commands/lambda/prebuiltHandler.ts b/packages/cli/src/commands/lambda/prebuiltHandler.ts index 481b6098d5..6fdd5aedf6 100644 --- a/packages/cli/src/commands/lambda/prebuiltHandler.ts +++ b/packages/cli/src/commands/lambda/prebuiltHandler.ts @@ -15,11 +15,15 @@ import { dirname, join } from "node:path"; import { downloadFile } from "../../utils/download.js"; import { isMissingPackageError } from "../../utils/missingPackage.js"; import { VERSION } from "../../version.js"; -import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { + foreignWritablePath, + sealCacheInstall, + trustedCacheRoot, +} from "@hyperframes/core/cache-dir"; import { cliSourced } from "../../utils/sourceTag.js"; const DOCS_URL = cliSourced("https://hyperframes.heygen.com/deploy/aws-lambda"); -const HANDLER_CACHE_DIR = hyperframesCacheDir("lambda-handler"); +const HANDLER_CACHE_DIR = join(trustedCacheRoot(), "lambda-handler"); type AdapterSdk = typeof import("@hyperframes/aws-lambda/sdk"); @@ -71,14 +75,18 @@ export async function fetchPinned( ): Promise { const dir = join(deps.cacheDir, `${pinned.version}-${pinned.sha256}`); const file = join(dir, asset.fileName); - if (existsSync(file) && (await sha256File(file)) === pinned.sha256) { + if ( + existsSync(file) && + !foreignWritablePath(file, deps.cacheDir) && + (await sha256File(file)) === pinned.sha256 + ) { deps.log(`→ Using verified ${asset.label} ${pinned.version} from ${dir}`); return file; } // Override for mirrors and pre-release testing; the pinned sha256 still decides. const url = asset.urlOverride ?? pinned.url; deps.log(`→ Downloading ${asset.label} ${pinned.version} (${pinned.bytes} bytes)`); - mkdirSync(dir, { recursive: true }); + mkdirSync(dir, { recursive: true, mode: 0o700 }); const partial = `${file}.download`; await deps.download(url, partial, { maxBytes: pinned.bytes }); const actual = await sha256File(partial); @@ -91,6 +99,7 @@ export async function fetchPinned( ); } renameSync(partial, file); + sealCacheInstall(file, deps.cacheDir); deps.log(`→ Verified sha256 ${pinned.sha256}`); return file; } diff --git a/packages/cli/src/commands/render.ts b/packages/cli/src/commands/render.ts index a19e0f0084..5b6c3251be 100644 --- a/packages/cli/src/commands/render.ts +++ b/packages/cli/src/commands/render.ts @@ -337,8 +337,8 @@ export default defineCommand({ description: "Wait while another render of this user account runs, then start (default): renders side by " + "side starve each other. Use --no-wait to fail at once instead. Env: HYPERFRAMES_RENDER_LOCK=off " + - "lets renders run side by side; HYPERFRAMES_CACHE_DIR set to a shared folder shares the slot " + - "across user accounts.", + "lets renders run side by side; HYPERFRAMES_RENDER_LOCK_DIR set to one folder in every account " + + "shares the slot across user accounts.", default: true, }, strict: { diff --git a/packages/cli/src/commands/render/renderSlot.ts b/packages/cli/src/commands/render/renderSlot.ts index c06d84e17d..880f182ad3 100644 --- a/packages/cli/src/commands/render/renderSlot.ts +++ b/packages/cli/src/commands/render/renderSlot.ts @@ -7,7 +7,11 @@ import { RenderLockBusy, type RenderLockOptions, } from "../../utils/renderLock.js"; -import { RenderLockStaleBreak, RenderLockUnwritable } from "../../utils/renderLockClaim.js"; +import { + RenderLockFolderRefused, + RenderLockStaleBreak, + RenderLockUnwritable, +} from "../../utils/renderLockClaim.js"; import type { RenderPlan } from "./plan.js"; /** One local render at a time per user account: a second waits for the slot, or fails under --no-wait. */ @@ -31,7 +35,11 @@ export async function acquireRenderSlot( ...lock, }); } catch (error) { - if (error instanceof RenderLockStaleBreak || error instanceof RenderLockUnwritable) { + if ( + error instanceof RenderLockStaleBreak || + error instanceof RenderLockUnwritable || + error instanceof RenderLockFolderRefused + ) { errorBox("The render slot cannot be taken", error.message); return failCommand(1, error); } @@ -40,7 +48,7 @@ export async function acquireRenderSlot( "Another render is running for this user account", describeRenderLockHolder(error.holder, Date.now()), "Rerun without --no-wait to start when it finishes, or set HYPERFRAMES_RENDER_LOCK=off to render side by side. " + - "Set HYPERFRAMES_CACHE_DIR to a shared folder to share the slot across user accounts.", + "Set HYPERFRAMES_RENDER_LOCK_DIR to one folder in every account to share the slot across user accounts.", ); return failCommand(1, error); } diff --git a/packages/cli/src/measure/opencv.ts b/packages/cli/src/measure/opencv.ts index 654571e136..47f591d53d 100644 --- a/packages/cli/src/measure/opencv.ts +++ b/packages/cli/src/measure/opencv.ts @@ -4,13 +4,18 @@ import { mkdirSync, readFileSync, renameSync, rmSync } from "node:fs"; import { createRequire } from "node:module"; import { join } from "node:path"; -import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { + foreignWritablePath, + sealCacheInstall, + trustedCacheRoot, +} from "@hyperframes/core/cache-dir"; import { downloadToFile } from "../cloud/download.js"; import { verifies, type ModelFile } from "../whisper/modelFile.js"; import { Fix } from "./media.js"; const VERSION = "4.10.0"; -export const OPENCV_DIR = hyperframesCacheDir("opencv", VERSION); +const CODE_ROOT = trustedCacheRoot(); +export const OPENCV_DIR = join(CODE_ROOT, "opencv", VERSION); interface Pinned extends ModelFile { url: string; @@ -120,9 +125,12 @@ const isCv = (v: unknown): v is Cv => // fallow-ignore-next-line complexity async function ensureOpenCv(onStatus?: (message: string) => void): Promise { const missing: Pinned[] = []; - for (const f of FILES) if (!(await verifies(join(OPENCV_DIR, f.name), f))) missing.push(f); + for (const f of FILES) { + const file = join(OPENCV_DIR, f.name); + if (foreignWritablePath(file, CODE_ROOT) || !(await verifies(file, f))) missing.push(f); + } if (!missing.length) return; - mkdirSync(OPENCV_DIR, { recursive: true }); + mkdirSync(OPENCV_DIR, { recursive: true, mode: 0o700 }); for (const f of missing) { onStatus?.(`Downloading ${f.name} (${Math.round(f.bytes / 1e5) / 10} MB, once)...`); const temp = join(OPENCV_DIR, `${f.name}.part-${process.pid}`); @@ -144,6 +152,7 @@ async function ensureOpenCv(onStatus?: (message: string) => void): Promise rmSync(temp, { force: true }); } } + sealCacheInstall(OPENCV_DIR, CODE_ROOT); } let loaded: Promise | null = null; diff --git a/packages/cli/src/tts/synthesize.ts b/packages/cli/src/tts/synthesize.ts index 500b8e0a20..2b1acb14e1 100644 --- a/packages/cli/src/tts/synthesize.ts +++ b/packages/cli/src/tts/synthesize.ts @@ -12,7 +12,11 @@ import { } from "./manager.js"; import { findPython, hasPythonPackage, workingPythonEnv } from "./python.js"; import { writeNewFileSync } from "../utils/writeNewFile.js"; -import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { + foreignWritablePath, + sealCacheInstall, + trustedCacheRoot, +} from "@hyperframes/core/cache-dir"; // --------------------------------------------------------------------------- // Inline Python script for Kokoro synthesis @@ -116,13 +120,17 @@ const ESPEAK_LANG_OVERRIDES: Partial> = { // Cache the script to avoid rewriting it on every invocation. // The filename carries a version suffix so older installs automatically // upgrade when the script body changes (e.g., adding the `lang` kwarg). -const SCRIPT_DIR = hyperframesCacheDir("tts"); +const CODE_ROOT = trustedCacheRoot(); +const SCRIPT_DIR = join(CODE_ROOT, "tts"); const SCRIPT_PATH = join(SCRIPT_DIR, "synth-v4.py"); function ensureSynthScript(): string { + if (existsSync(SCRIPT_PATH) && foreignWritablePath(SCRIPT_PATH, CODE_ROOT)) + unlinkSync(SCRIPT_PATH); if (!existsSync(SCRIPT_PATH)) { - mkdirSync(SCRIPT_DIR, { recursive: true }); + mkdirSync(SCRIPT_DIR, { recursive: true, mode: 0o700 }); writeNewFileSync(SCRIPT_PATH, SYNTH_SCRIPT); + sealCacheInstall(SCRIPT_PATH, CODE_ROOT); // Best-effort: delete older versioned scripts left behind by previous // CLI releases so users don't accumulate stale files in ~/.cache. const currentName = basename(SCRIPT_PATH); diff --git a/packages/cli/src/utils/optionalPackages.test.ts b/packages/cli/src/utils/optionalPackages.test.ts index c98bfcdd2f..85b3d41b22 100644 --- a/packages/cli/src/utils/optionalPackages.test.ts +++ b/packages/cli/src/utils/optionalPackages.test.ts @@ -1,12 +1,15 @@ // fallow-ignore-file code-duplication import { spawnSync } from "node:child_process"; import { + chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, + realpathSync, renameSync, rmSync, + statSync, symlinkSync, utimesSync, writeFileSync, @@ -21,6 +24,8 @@ import { OPTIONAL_PACKAGES, install, installedOptionalPackageVersion, + installedPackagePath, + isInstalled, loadBesideCli, loadInstalledOptionalPackage, loadOptionalPackage, @@ -620,6 +625,85 @@ describe("install", () => { return { cache, dir, stubNpm }; } + describe.skipIf(process.platform === "win32")( + "a cached copy another account could have written", + () => { + const writePackage = (prefix: string, body: string) => { + mkdirSync(join(prefix, "node_modules", name), { recursive: true, mode: 0o755 }); + writeFileSync(join(prefix, "node_modules", name, "package.json"), "{}"); + writeFileSync(join(prefix, "node_modules", name, "index.js"), body); + }; + + it("is installed in a folder no other account can reach, even under umask 002", async () => { + const { cache, dir } = setup(); + const previous = process.umask(0o002); + const modes: number[] = []; + try { + await install(dir, name, version, async (_args, prefix) => { + modes.push(statSync(prefix).mode & 0o777); + writePackage(prefix, "1"); + }); + } finally { + process.umask(previous); + rmSync(cache, { recursive: true, force: true }); + } + expect(modes).toEqual([0o700]); + }); + + it("is loaded on the next run when this account installed it under umask 002", async () => { + const { cache, dir } = setup(); + const previous = process.umask(0o002); + try { + await install(dir, name, version, async (_args, prefix) => writePackage(prefix, "1")); + } finally { + process.umask(previous); + } + try { + expect(installedPackagePath(dir, name)).toBe( + realpathSync(join(dir, "node_modules", name, "index.js")), + ); + } finally { + rmSync(cache, { recursive: true, force: true }); + } + }); + + it("is never loaded when another account owns it", () => { + const { cache, dir } = setup(); + const previous = process.umask(0o022); + try { + writePackage(dir, "module.exports = 'planted'"); + expect(installedPackagePath(dir, name)).toBe( + realpathSync(join(dir, "node_modules", name, "index.js")), + ); + vi.spyOn(process, "getuid").mockReturnValue(process.getuid!() + 1); + expect(installedPackagePath(dir, name)).toBeNull(); + } finally { + process.umask(previous); + vi.restoreAllMocks(); + rmSync(cache, { recursive: true, force: true }); + } + }); + + it("is replaced by a fresh install when a folder of it is group- or world-writable", async () => { + const { cache, dir } = setup(); + try { + writePackage(dir, "module.exports = 'planted'"); + chmodSync(join(dir, "node_modules"), 0o777); + expect(isInstalled(dir, name)).toBe(false); + expect(installedPackagePath(dir, name)).toBeNull(); + + await install(dir, name, version, async (_args, prefix) => writePackage(prefix, "1")); + + const entry = installedPackagePath(dir, name); + expect(entry).toBe(realpathSync(join(dir, "node_modules", name, "index.js"))); + expect(readFileSync(entry!, "utf8")).toBe("1"); + } finally { + rmSync(cache, { recursive: true, force: true }); + } + }); + }, + ); + it("runs npm inside the staging dir, with no path on its command line", async () => { const { cache, dir } = setup(); const seen: { args: string[]; cwd: string }[] = []; diff --git a/packages/cli/src/utils/optionalPackages.ts b/packages/cli/src/utils/optionalPackages.ts index afd971000d..62f2bc9673 100644 --- a/packages/cli/src/utils/optionalPackages.ts +++ b/packages/cli/src/utils/optionalPackages.ts @@ -14,7 +14,11 @@ import { import { createRequire } from "node:module"; import { basename, dirname, join, sep } from "node:path"; import { buildNpmCommand } from "./npxCommand.js"; -import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { + foreignWritablePath, + sealCacheInstall, + trustedCacheRoot, +} from "@hyperframes/core/cache-dir"; /** Module type of each optional package; the keys are the only names the loader accepts. */ export interface OptionalPackageModules { @@ -37,7 +41,7 @@ export const PINNED_PACKAGES = { type PinnedPackage = keyof typeof PINNED_PACKAGES; -export const CACHE_DIR = hyperframesCacheDir("optional"); +export const OPTIONAL_CACHE_DIR = join(trustedCacheRoot(), "optional"); /** Installed on first use only when a host bundled the CLI without its sharp dependency. */ export const SHARP_VERSION = "0.35.5"; @@ -55,7 +59,7 @@ export interface OptionalPackageDeps { } /** One directory per package and version, so a version bump never reads a stale install. */ -export function optionalPackageDir(name: FirstUsePackage, cacheDir = CACHE_DIR): string { +export function optionalPackageDir(name: FirstUsePackage, cacheDir = OPTIONAL_CACHE_DIR): string { return join(cacheDir, `${name.replace("/", "__")}@${versionOf(name)}`); } @@ -66,7 +70,7 @@ function versionOf(name: FirstUsePackage): string { /** Installed version, or null. Reads the manifest only, so it never loads a native binding. */ export function installedOptionalPackageVersion( name: OptionalPackage, - cacheDir = CACHE_DIR, + cacheDir = OPTIONAL_CACHE_DIR, cliUrl = import.meta.url, ): string | null { if (pinnedPackageBesideCli(name, cliUrl)?.resolution === "entry") return OPTIONAL_PACKAGES[name]; @@ -234,13 +238,17 @@ function manifestPath(dir: string, name: string): string { return join(dir, "node_modules", name, "package.json"); } +const writableByOthers = (dir: string, file: string) => !!foreignWritablePath(file, dirname(dir)); + export function isInstalled(dir: string, name: string): boolean { - return existsSync(manifestPath(dir, name)); + const manifest = manifestPath(dir, name); + return existsSync(manifest) && !writableByOthers(dir, manifest); } export function installedPackagePath(dir: string, name: string): string | null { if (!isInstalled(dir, name)) return null; - return createRequire(join(dir, "package.json")).resolve(name); + const entry = createRequire(join(dir, "package.json")).resolve(name); + return writableByOthers(dir, entry) ? null : entry; } function loadInstalled(dir: string, name: string): unknown | null { @@ -344,7 +352,7 @@ export async function install( ): Promise { sweepStaleStaging(dir); const staging = `${dir}.tmp-${process.pid}-${randomUUID().slice(0, 8)}`; - mkdirSync(staging, { recursive: true }); + mkdirSync(staging, { recursive: true, mode: 0o700 }); writeFileSync(join(staging, "package.json"), "{}"); try { await run( @@ -362,6 +370,7 @@ export async function install( staging, ); if (isInstalled(dir, name)) return; + sealCacheInstall(staging, dirname(dir)); rmSync(dir, { recursive: true, force: true }); renameSync(staging, dir); } catch (err) { @@ -372,7 +381,7 @@ export async function install( } const defaultDeps: OptionalPackageDeps = { - cacheDir: CACHE_DIR, + cacheDir: OPTIONAL_CACHE_DIR, loadBesideCli: (name) => (name === "sharp" ? null : loadBesideCli(name)), loadInstalled, install, diff --git a/packages/cli/src/utils/renderLock.test.ts b/packages/cli/src/utils/renderLock.test.ts index e31813d587..fe1adff6ed 100644 --- a/packages/cli/src/utils/renderLock.test.ts +++ b/packages/cli/src/utils/renderLock.test.ts @@ -10,6 +10,9 @@ import { readFileSync, rmSync, statSync, + symlinkSync, + unlinkSync, + fchmodSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; @@ -22,6 +25,13 @@ import { RenderLockStaleBreak } from "./renderLockClaim.js"; import type { Holder } from "./projectLock.js"; import { acquireRenderSlot } from "../commands/render/renderSlot.js"; +vi.mock("node:fs", async (importOriginal) => { + const fs = await importOriginal(); + return { ...fs, unlinkSync: vi.fn(fs.unlinkSync), fchmodSync: vi.fn(fs.fchmodSync) }; +}); +const { unlinkSync: realUnlinkSync, fchmodSync: realFchmodSync } = + await vi.importActual("node:fs"); + let dir: string; let path: string; @@ -196,30 +206,134 @@ describe("acquireRenderLock", () => { }, ); - describe.skipIf(process.platform === "win32")("a cache folder shared by accounts", () => { - it("creates its locks folder writable for every account", async () => { + describe.skipIf(process.platform === "win32")("the lock folder", () => { + it("is private to this account when it is the account's own", async () => { const release = await acquireRenderLock({ project: "/work/a", wait: false, path, env: {} }); - expect(statSync(join(dir, "locks")).mode & 0o777).toBe(0o777); + expect(statSync(join(dir, "locks")).mode & 0o7777).toBe(0o700); release(); }); + it("named by HYPERFRAMES_RENDER_LOCK_DIR is writable by every account but sticky, and outside the cache", async () => { + vi.stubEnv("HYPERFRAMES_CACHE_DIR", join(dir, "cache")); + const shared = join(dir, "shared-locks"); + const env = { HYPERFRAMES_RENDER_LOCK_DIR: shared }; + try { + const release = await acquireRenderLock({ project: "/work/a", wait: false, env }); + expect(statSync(shared).mode & 0o7777).toBe(0o1777); + expect(existsSync(join(shared, "render.lock"))).toBe(true); + expect(existsSync(join(dir, "cache"))).toBe(false); + release(); + } finally { + vi.unstubAllEnvs(); + } + }); + + it("is made private again when an older release left it writable by every account", async () => { + mkdirSync(join(dir, "locks")); + chmodSync(join(dir, "locks"), 0o777); + const release = await acquireRenderLock({ project: "/work/a", wait: false, path, env: {} }); + expect(statSync(join(dir, "locks")).mode & 0o7777).toBe(0o700); + release(); + }); + + it("named by HYPERFRAMES_RENDER_LOCK_DIR gets the sticky bit when it already exists without it", async () => { + const shared = join(dir, "shared-locks"); + mkdirSync(shared); + chmodSync(shared, 0o777); + const env = { HYPERFRAMES_RENDER_LOCK_DIR: shared }; + const release = await acquireRenderLock({ project: "/work/a", wait: false, env }); + expect(statSync(shared).mode & 0o7777).toBe(0o1777); + release(); + }); + + it("named by HYPERFRAMES_RENDER_LOCK_DIR is refused when it is a link, and the folder it names is left as it was", async () => { + const victim = join(dir, "private"); + mkdirSync(victim, { mode: 0o700 }); + const shared = join(dir, "planted"); + symlinkSync(victim, shared); + const env = { HYPERFRAMES_RENDER_LOCK_DIR: shared }; + + await expect(acquireRenderLock({ project: "/work/a", wait: false, env })).rejects.toThrow( + `${shared} is not a folder that this account or root owns`, + ); + expect(statSync(victim).mode & 0o7777).toBe(0o700); + expect(readdirSync(victim)).toEqual([]); + }); + + it("named by HYPERFRAMES_RENDER_LOCK_DIR is refused when another account owns it", async () => { + const shared = join(dir, "theirs"); + mkdirSync(shared); + chmodSync(shared, 0o777); + vi.spyOn(process, "getuid").mockReturnValue(process.getuid!() + 1); + const env = { HYPERFRAMES_RENDER_LOCK_DIR: shared }; + + await expect(acquireRenderLock({ project: "/work/a", wait: false, env })).rejects.toThrow( + `${shared} is not a folder that this account or root owns`, + ); + expect(statSync(shared).mode & 0o7777).toBe(0o777); + expect(readdirSync(shared)).toEqual([]); + }); + + it("is refused when another account owns this account's own lock folder", async () => { + mkdirSync(join(dir, "locks")); + vi.spyOn(process, "getuid").mockReturnValue(process.getuid!() + 1); + + await expect( + acquireRenderLock({ project: "/work/a", wait: false, path, env: {} }), + ).rejects.toThrow(`${join(dir, "locks")} is not a folder this account owns`); + }); + + it("says so when another account's stale lock cannot be removed from a sticky shared folder", async () => { + const stale: RenderLockHolder = { + pid: deadPid(), + command: "render", + startedAt: 1, + project: "/work/other-account", + }; + mkdirSync(join(dir, "locks"), { recursive: true }); + writeFileSync(path, JSON.stringify(stale)); + const sticky = Object.assign(new Error("EPERM: operation not permitted"), { code: "EPERM" }); + vi.mocked(unlinkSync).mockImplementation((file) => { + if (file === path) throw sticky; + realUnlinkSync(file); + }); + + try { + for (const wait of [false, true]) { + await expect( + startAcquire({ project: "/work/next", wait, path, env: {} }), + ).rejects.toThrow(`A render of another account stopped and left ${path}`); + } + } finally { + vi.mocked(unlinkSync).mockImplementation(realUnlinkSync); + } + expect(JSON.parse(readFileSync(path, "utf8")).project).toBe("/work/other-account"); + }); + it.skipIf(process.getuid?.() === 0)( - "says how to fix a locks folder this account cannot write in", + "says how to fix a sticky shared lock folder this account cannot write in", async () => { - mkdirSync(join(dir, "locks"), { mode: 0o755 }); - chmodSync(join(dir, "locks"), 0o555); + const shared = join(dir, "read-only"); + mkdirSync(shared); + chmodSync(shared, 0o1555); + vi.mocked(fchmodSync).mockImplementation(() => {}); try { await expect( - acquireRenderLock({ project: "/work/b", wait: false, path, env: {} }), - ).rejects.toThrow(`chmod 777 ${join(dir, "locks")}`); + acquireRenderLock({ + project: "/work/b", + wait: false, + env: { HYPERFRAMES_RENDER_LOCK_DIR: shared }, + }), + ).rejects.toThrow("point HYPERFRAMES_RENDER_LOCK_DIR in each of them at one folder"); } finally { - chmodSync(join(dir, "locks"), 0o755); + vi.mocked(fchmodSync).mockImplementation(realFchmodSync); + chmodSync(shared, 0o755); } }, ); }); - it("keeps the slot in this user's HyperFrames cache folder, which HYPERFRAMES_CACHE_DIR can share", async () => { + it("keeps the slot in this user's HyperFrames cache folder by default", async () => { vi.stubEnv("HYPERFRAMES_CACHE_DIR", join(dir, "shared")); try { const release = await acquireRenderLock({ project: "/work/brand", wait: false, env: {} }); @@ -389,7 +503,8 @@ describe("acquireRenderSlot", () => { const said = vi.mocked(console.error).mock.calls.flat().join("\n"); expect(said).toContain("Another render is running for this user account"); - expect(said).toContain("HYPERFRAMES_CACHE_DIR to a shared folder"); + expect(said).toContain("Set HYPERFRAMES_RENDER_LOCK_DIR to one folder in every account"); + expect(said).not.toContain("HYPERFRAMES_CACHE_DIR"); expect(said).toContain(`pid ${process.pid}, project /work/brand`); release(); }); diff --git a/packages/cli/src/utils/renderLock.ts b/packages/cli/src/utils/renderLock.ts index 63509a4bdd..14def0f669 100644 --- a/packages/cli/src/utils/renderLock.ts +++ b/packages/cli/src/utils/renderLock.ts @@ -1,5 +1,5 @@ import { existsSync } from "node:fs"; -import { dirname } from "node:path"; +import { dirname, join, resolve } from "node:path"; import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; import { readHolder, release } from "./projectLock.js"; import { @@ -44,7 +44,15 @@ export interface RenderLockOptions { const POLL_MS = 1_000; -const renderLockPath = () => hyperframesCacheDir("locks", "render.lock"); +const sharedLockDir = (env: NodeJS.ProcessEnv) => + env.HYPERFRAMES_RENDER_LOCK_DIR?.trim() || undefined; + +const renderLockPath = (env: NodeJS.ProcessEnv) => { + const shared = sharedLockDir(env); + return shared + ? join(resolve(shared), "render.lock") + : hyperframesCacheDir("locks", "render.lock"); +}; /** `HYPERFRAMES_RENDER_LOCK=off` lets this account's renders run side by side. */ function renderLockDisabled(env: NodeJS.ProcessEnv = process.env): boolean { @@ -111,8 +119,9 @@ interface Contender { } function prepareContender(options: RenderLockOptions): Contender { - const path = options.path ?? renderLockPath(); - ensureLockDir(dirname(path)); + const env = options.env ?? process.env; + const path = options.path ?? renderLockPath(env); + ensureLockDir(dirname(path), sharedLockDir(env) !== undefined); liveOtherOwners(path); const pid = options.pid ?? process.pid; const identity = startToken(pid); diff --git a/packages/cli/src/utils/renderLockClaim.ts b/packages/cli/src/utils/renderLockClaim.ts index 3ce94ee5ab..a3c673f19f 100644 --- a/packages/cli/src/utils/renderLockClaim.ts +++ b/packages/cli/src/utils/renderLockClaim.ts @@ -1,4 +1,14 @@ -import { chmodSync, existsSync, mkdirSync, unlinkSync } from "node:fs"; +import { + closeSync, + constants, + existsSync, + fchmodSync, + fstatSync, + mkdirSync, + openSync, + unlinkSync, + type Stats, +} from "node:fs"; import { createFileAtomically } from "@hyperframes/core/atomic-file"; import { readHolder, type Holder } from "./projectLock.js"; import { liveOtherOwners, removeIfPresent, sameProcess } from "./renderLockOwners.js"; @@ -9,9 +19,15 @@ export interface RenderLockHolder extends Holder { } export class RenderLockStaleBreak extends Error { - constructor(readonly breakPath: string) { + constructor( + readonly breakPath: string, + otherAccount = false, + ) { super( - `A render that was reclaiming the render slot stopped part way and left ${breakPath}. ` + + (otherAccount + ? `A render of another account stopped and left ${breakPath}, which only that account or root ` + + "can delete in a shared lock folder. " + : `A render that was reclaiming the render slot stopped part way and left ${breakPath}. `) + "No render is using it: delete that file, then run the render again.", ); this.name = "RenderLockStaleBreak"; @@ -24,22 +40,64 @@ export class RenderLockUnwritable extends Error { cause: unknown, ) { super( - `This account cannot write to ${dir}, where renders take their turn. If accounts share ` + - `HYPERFRAMES_CACHE_DIR, make the folder writable for all of them (chmod 777 ${dir}); ` + - "otherwise unset HYPERFRAMES_CACHE_DIR.", + `This account cannot write to ${dir}, where renders take their turn. To share one render slot ` + + "across accounts, point HYPERFRAMES_RENDER_LOCK_DIR in each of them at one folder for lock " + + "records only: the first render creates it writable by all and sticky (chmod 1777).", { cause }, ); this.name = "RenderLockUnwritable"; } } -/** Accounts sharing a cache folder take turns in one `locks` folder, so each of them must write in it. */ -export function ensureLockDir(dir: string): void { - if (mkdirSync(dir, { recursive: true }) !== undefined && process.platform !== "win32") { - chmodSync(dir, 0o777); +export class RenderLockFolderRefused extends Error { + constructor( + readonly dir: string, + shared: boolean, + ) { + super( + shared + ? `${dir} is not a folder that this account or root owns with the sticky bit set, so another ` + + "account could delete or replace render lock records in it. Point HYPERFRAMES_RENDER_LOCK_DIR " + + `at a folder that does not exist yet, or have its owner run: chmod 1777 ${dir}` + : `${dir} is not a folder this account owns, so another account could delete or replace render ` + + "lock records in it. Remove it, or point HYPERFRAMES_RENDER_LOCK_DIR at a folder for lock records.", + ); + this.name = "RenderLockFolderRefused"; } } +const OPEN_FOLDER_NOT_LINK = constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW; +const PRIVATE_FOLDER = 0o700; +const SHARED_STICKY_FOLDER = 0o1777; + +const lockFolderAccepted = (stat: Stats, shared: boolean) => + shared ? (stat.mode & 0o1000) !== 0 : (stat.mode & 0o077) === 0; + +export function ensureLockDir(dir: string, shared: boolean): void { + mkdirSync(dir, { recursive: true }); + if (process.platform === "win32") return; + let fd: number; + try { + fd = openSync(dir, OPEN_FOLDER_NOT_LINK); + } catch (error) { + throw Object.assign(new RenderLockFolderRefused(dir, shared), { cause: error }); + } + try { + settleLockFolder(fd, dir, shared); + } finally { + closeSync(fd); + } +} + +function settleLockFolder(fd: number, dir: string, shared: boolean): void { + const stat = fstatSync(fd); + const mine = stat.uid === process.getuid?.(); + if (!mine && !(shared && stat.uid === 0)) throw new RenderLockFolderRefused(dir, shared); + const mode = shared ? SHARED_STICKY_FOLDER : PRIVATE_FOLDER; + if (mine && (stat.mode & 0o7777) !== mode) fchmodSync(fd, mode); + if (!lockFolderAccepted(fstatSync(fd), shared)) throw new RenderLockFolderRefused(dir, shared); +} + const holderAlive = (holder: RenderLockHolder) => sameProcess(holder.pid, holder.identity); const sameRecord = (a: RenderLockHolder | null, b: RenderLockHolder) => @@ -58,6 +116,23 @@ function createExclusive(path: string, holder: Holder): boolean { const writerAbsent = (file: string, lockPath: string, marker: string) => existsSync(file) && !readHolder(file) && liveOtherOwners(lockPath, marker) === 0; +function replaceStale( + path: string, + holder: RenderLockHolder, + observed: RenderLockHolder | null, +): boolean { + removeIfPresent(path); + if (createExclusive(path, holder)) return true; + if ( + process.platform !== "win32" && + observed && + sameRecord(readHolder(path), observed) + ) { + throw new RenderLockStaleBreak(path, true); + } + return false; +} + /** * Replaces a stale lock under `.break`, which only its writer ever removes: a `.break` whose writer is * gone stops the render with what to delete, since removing it on an earlier reading could remove a @@ -81,7 +156,7 @@ async function takeOver( const stillStale = observed ? sameRecord(readHolder(path), observed) : writerAbsent(path, path, marker); - if (stillStale) removeIfPresent(path); + if (stillStale) return replaceStale(path, holder, observed); return createExclusive(path, holder); } finally { if (sameRecord(readHolder(breakPath), holder)) unlinkSync(breakPath); diff --git a/packages/cli/src/whisper/manager.ts b/packages/cli/src/whisper/manager.ts index 7480a8cab7..f08add482e 100644 --- a/packages/cli/src/whisper/manager.ts +++ b/packages/cli/src/whisper/manager.ts @@ -8,7 +8,12 @@ import { findFFmpeg } from "../browser/ffmpeg.js"; import { downloadFile } from "../utils/download.js"; import { stoppableExecFile } from "../utils/stoppableExecFile.js"; import { verifies, type ModelFile } from "./modelFile.js"; -import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { + foreignWritablePath, + hyperframesCacheDir, + sealCacheInstall, + trustedCacheRoot, +} from "@hyperframes/core/cache-dir"; import { findTrustedExecutable } from "@hyperframes/parsers/ff-binaries"; const MODELS_DIR = hyperframesCacheDir("whisper", "models"); @@ -129,7 +134,8 @@ function findFromSystem(): WhisperResult | undefined { // --- Build from source ------------------------------------------------------ -const BUILD_DIR = hyperframesCacheDir("whisper", "whisper.cpp"); +const CODE_ROOT = trustedCacheRoot(); +const BUILD_DIR = join(CODE_ROOT, "whisper", "whisper.cpp"); const WHISPER_REPO = "https://github.com/ggml-org/whisper.cpp.git"; // A reviewed release, checked by commit after the clone, so a moved tag is never built or run. const WHISPER_TAG = "v1.9.5"; @@ -157,7 +163,8 @@ function findBuiltBinary(): WhisperResult | undefined { join(BUILD_DIR, "build", "bin", `whisper-cli${exe}`), join(BUILD_DIR, "build", `whisper-cli${exe}`), ]) { - if (existsSync(p)) return { executablePath: p, source: "build" }; + if (existsSync(p) && !foreignWritablePath(p, CODE_ROOT)) + return { executablePath: p, source: "build" }; } return undefined; } @@ -174,7 +181,7 @@ async function buildFromSource( if (!existsSync(BUILD_DIR)) { onProgress?.("Downloading whisper.cpp..."); - mkdirSync(BUILD_DIR, { recursive: true }); + mkdirSync(BUILD_DIR, { recursive: true, mode: 0o700 }); await stoppableExecFile( git, ["clone", "--depth", "1", "--branch", WHISPER_TAG, WHISPER_REPO, "."], @@ -216,6 +223,7 @@ async function buildFromSource( ); } + sealCacheInstall(BUILD_DIR, CODE_ROOT); const result = findBuiltBinary(); if (!result) throw new Error("Build completed but whisper-cli not found"); return result; diff --git a/packages/cli/src/whisper/sherpa.ts b/packages/cli/src/whisper/sherpa.ts index 2940db8c15..c9a9ae3e5c 100644 --- a/packages/cli/src/whisper/sherpa.ts +++ b/packages/cli/src/whisper/sherpa.ts @@ -9,7 +9,7 @@ import { downloadToFile } from "../cloud/download.js"; import { verifies, type ModelFile } from "./modelFile.js"; import { stoppedByCancelSignal } from "../utils/renderCancellation.js"; import { - CACHE_DIR, + OPTIONAL_CACHE_DIR, PINNED_PACKAGES, pinnedPackageBesideCli, installedPackagePath, @@ -39,7 +39,7 @@ const RUNTIME = "sherpa-onnx-node"; const RUNTIME_VERSION = PINNED_PACKAGES[RUNTIME]; /** Per platform and arch, like the native package inside, so Rosetta never shadows arm64. */ export const SHERPA_RUNTIME_DIR = join( - CACHE_DIR, + OPTIONAL_CACHE_DIR, `${RUNTIME}@${RUNTIME_VERSION}-${process.platform}-${process.arch}`, ); diff --git a/packages/core/src/cacheDir.test.ts b/packages/core/src/cacheDir.test.ts index b8fbf92308..9fe8724543 100644 --- a/packages/core/src/cacheDir.test.ts +++ b/packages/core/src/cacheDir.test.ts @@ -1,10 +1,27 @@ -import { readdirSync, readFileSync } from "node:fs"; +import { + chmodSync, + mkdirSync, + mkdtempSync, + readdirSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; import { homedir, tmpdir } from "node:os"; import { join, resolve } from "node:path"; -import { afterEach, describe, expect, it, vi } from "vitest"; -import { hyperframesCacheDir, namedCacheDir } from "./cacheDir.js"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + foreignWritablePath, + hyperframesCacheDir, + namedCacheDir, + sealCacheInstall, + trustedCacheRoot, +} from "./cacheDir.js"; -afterEach(() => vi.unstubAllEnvs()); +afterEach(() => { + vi.unstubAllEnvs(); + vi.restoreAllMocks(); +}); describe("hyperframesCacheDir", () => { it("defaults to ~/.cache/hyperframes", () => { @@ -30,6 +47,88 @@ describe("hyperframesCacheDir", () => { }); }); +describe.skipIf(process.platform === "win32")("foreignWritablePath", () => { + let root: string; + let binary: string; + beforeEach(() => { + root = mkdtempSync(join(tmpdir(), "hf-trust-")); + mkdirSync(join(root, "chrome", "v1"), { recursive: true, mode: 0o755 }); + binary = join(root, "chrome", "v1", "chrome-headless-shell"); + writeFileSync(binary, "#!/bin/sh\n", { mode: 0o755 }); + }); + afterEach(() => rmSync(root, { recursive: true, force: true })); + + /** Every file the test made now reads as another account's, as it would without root to chown. */ + const asAnotherAccount = () => vi.spyOn(process, "getuid").mockReturnValue(process.getuid!() + 1); + + it("accepts a file that it and every folder up to the cache root are this account's alone", () => { + expect(foreignWritablePath(binary, root)).toBeUndefined(); + }); + + it("names a file or folder another account owns", () => { + asAnotherAccount(); + expect(foreignWritablePath(binary, root)).toBe(binary); + }); + + it.each([ + ["group", 0o775], + ["world", 0o777], + ])("names the first folder up to the cache root that is %s-writable", (_who, mode) => { + chmodSync(join(root, "chrome"), mode); + expect(foreignWritablePath(binary, root)).toBe(join(root, "chrome")); + chmodSync(join(root, "chrome"), 0o755); + chmodSync(root, mode); + expect(foreignWritablePath(binary, root)).toBe(root); + }); + + it("accepts a sticky folder, like /tmp, where others cannot replace this account's entries", () => { + chmodSync(root, 0o1777); + expect(foreignWritablePath(binary, root)).toBeUndefined(); + chmodSync(join(root, "chrome"), 0o1777); + asAnotherAccount(); + expect(foreignWritablePath(join(root, "chrome"), root)).toBe(join(root, "chrome")); + }); + + it("checks the nearest folder above a cache root that does not exist yet", () => { + chmodSync(root, 0o777); + expect(foreignWritablePath(join(root, "cache", "x"), join(root, "cache"))).toBe(root); + }); + + it("is skipped on Windows, which has no uid", () => { + const getuid = process.getuid; + process.getuid = undefined; + try { + chmodSync(binary, 0o777); + expect(foreignWritablePath(binary, root)).toBeUndefined(); + } finally { + process.getuid = getuid; + } + }); + + it("accepts on the next check what this account installed under umask 002, once sealed", () => { + const previous = process.umask(0o002); + let installed: string; + try { + installed = join(root, "optional", "pkg@1", "node_modules", "pkg"); + mkdirSync(installed, { recursive: true }); + writeFileSync(join(installed, "index.js"), "1"); + } finally { + process.umask(previous); + } + const entry = join(installed, "index.js"); + expect(foreignWritablePath(entry, root)).toBe(entry); + sealCacheInstall(join(root, "optional", "pkg@1"), root); + expect(foreignWritablePath(entry, root)).toBeUndefined(); + }); + + it("keeps code out of a HYPERFRAMES_CACHE_DIR another account can write, in this user's own cache", () => { + vi.stubEnv("HYPERFRAMES_CACHE_DIR", root); + expect(trustedCacheRoot()).toBe(root); + chmodSync(root, 0o777); + expect(trustedCacheRoot()).toBe(join(homedir(), ".cache", "hyperframes")); + }); +}); + /** Source files under every package's src, tests and test helpers left out. */ function sources(dir: string): string[] { return readdirSync(dir, { recursive: true, withFileTypes: true }) diff --git a/packages/core/src/cacheDir.ts b/packages/core/src/cacheDir.ts index 4ebc980497..20c254890c 100644 --- a/packages/core/src/cacheDir.ts +++ b/packages/core/src/cacheDir.ts @@ -1,11 +1,71 @@ +import { chmodSync, lstatSync, readdirSync, statSync, type Stats } from "node:fs"; import { homedir } from "node:os"; -import { join, resolve } from "node:path"; +import { dirname, join, resolve, sep } from "node:path"; + +const userCacheRoot = () => join(homedir(), ".cache", "hyperframes"); + +const GROUP_OR_WORLD_WRITE = 0o022; +const WITHOUT_GROUP_OR_WORLD_WRITE = 0o7755; +const STICKY = 0o1000; + +const ownedByAnotherAccount = (stat: Stats, uid: number) => stat.uid !== uid && stat.uid !== 0; +const stickyFolder = (stat: Stats) => stat.isDirectory() && (stat.mode & STICKY) !== 0; + +const anotherAccountCanWrite = (stat: Stats, uid: number) => + ownedByAnotherAccount(stat, uid) || + ((stat.mode & GROUP_OR_WORLD_WRITE) !== 0 && !stickyFolder(stat)); + +function statIfPresent(path: string): Stats | undefined { + try { + return statSync(path); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined; + throw error; + } +} export function hyperframesCacheDir(...parts: string[]): string { const named = process.env["HYPERFRAMES_CACHE_DIR"]; - return join(named ? resolve(named) : join(homedir(), ".cache", "hyperframes"), ...parts); + return join(named ? resolve(named) : userCacheRoot(), ...parts); } export function namedCacheDir(...parts: string[]): string | undefined { return process.env["HYPERFRAMES_CACHE_DIR"] ? hyperframesCacheDir(...parts) : undefined; } + +export function foreignWritablePath(path: string, root: string): string | undefined { + const uid = process.getuid?.(); + if (uid === undefined) return undefined; + const top = resolve(root); + for (let at = resolve(path); ; at = dirname(at)) { + const stat = statIfPresent(at); + if (stat && anotherAccountCanWrite(stat, uid)) return at; + const pastRoot = at === top || !at.startsWith(top + sep); + if ((stat && pastRoot) || at === dirname(at)) return undefined; + } +} + +export function trustedCacheRoot(): string { + const root = hyperframesCacheDir(); + return foreignWritablePath(root, root) ? userCacheRoot() : root; +} + +export function sealCacheInstall(path: string, root: string): void { + const uid = process.getuid?.(); + if (uid === undefined) return; + const seal = (at: string) => { + const stat = lstatSync(at, { throwIfNoEntry: false }); + if (!stat || stat.isSymbolicLink() || stat.uid !== uid) return; + if ((stat.mode & GROUP_OR_WORLD_WRITE) !== 0) + chmodSync(at, stat.mode & WITHOUT_GROUP_OR_WORLD_WRITE); + if (stat.isDirectory()) for (const name of readdirSync(at)) seal(join(at, name)); + }; + seal(resolve(path)); + const top = resolve(root); + for (let at = dirname(resolve(path)); at === top || at.startsWith(top + sep); at = dirname(at)) { + const stat = statIfPresent(at); + if (stat?.uid === uid && (stat.mode & GROUP_OR_WORLD_WRITE) !== 0) { + chmodSync(at, stat.mode & WITHOUT_GROUP_OR_WORLD_WRITE); + } + } +} diff --git a/packages/engine/src/services/browserManager.ts b/packages/engine/src/services/browserManager.ts index 40dc8d04fc..e03460642b 100644 --- a/packages/engine/src/services/browserManager.ts +++ b/packages/engine/src/services/browserManager.ts @@ -56,7 +56,7 @@ export type { BrowserPoolState, CaptureMode, } from "./browserLeasePool.js"; -import { hyperframesCacheDir } from "@hyperframes/core/cache-dir"; +import { foreignWritablePath, trustedCacheRoot } from "@hyperframes/core/cache-dir"; import { findTrustedExecutable } from "@hyperframes/parsers/ff-binaries"; let _puppeteer: PuppeteerNode | undefined; @@ -265,12 +265,15 @@ export function resolveHeadlessShellPath( } return fromEnv.path; } - const ours = hyperframesCacheDir("chrome", "chrome-headless-shell"); + const root = trustedCacheRoot(); + const ours = join(root, "chrome", "chrome-headless-shell"); const shared = join(homedir(), ".cache", "puppeteer", "chrome-headless-shell"); + const trusted = (binary: string | undefined) => + binary && !foreignWritablePath(binary, root) ? binary : undefined; return ( - findManagedHeadlessShell(ours) ?? + trusted(findManagedHeadlessShell(ours)) ?? findSharedHeadlessShell(shared) ?? - findCachedHeadlessShell(ours) + trusted(findCachedHeadlessShell(ours)) ); } diff --git a/packages/inspector/src/service.ts b/packages/inspector/src/service.ts index a4b4e96fb9..ce8b009906 100644 --- a/packages/inspector/src/service.ts +++ b/packages/inspector/src/service.ts @@ -33,7 +33,7 @@ import { import { closeServer, isLoopbackHost, servableProjectFile } from "./pageServer"; import { Sources, fingerprint } from "./sources"; import type { InspectRequest, Snippet } from "./types"; -import { hyperframesCacheDir } from "../../core/src/cacheDir"; +import { foreignWritablePath, trustedCacheRoot } from "../../core/src/cacheDir"; import { browserProxyArgs, createPublicOnlyProfile, @@ -85,7 +85,8 @@ function chromePath() { "Set HF_INSPECT_CHROME to an installed Chrome executable; inspection does not download a browser.", ); } - const root = hyperframesCacheDir("chrome", "chrome-headless-shell"); + const cacheRoot = trustedCacheRoot(); + const root = join(cacheRoot, "chrome", "chrome-headless-shell"); if (existsSync(root)) for (const version of readdirSync(root).sort().reverse()) { const dir = join(root, version); @@ -95,7 +96,7 @@ function chromePath() { platform, process.platform === "win32" ? "chrome-headless-shell.exe" : "chrome-headless-shell", ); - if (existsSync(file)) return file; + if (existsSync(file) && !foreignWritablePath(file, cacheRoot)) return file; } } throw new Error( diff --git a/scripts/package-size-budgets.json b/scripts/package-size-budgets.json index 5c2e4170c1..42c48845d8 100644 --- a/scripts/package-size-budgets.json +++ b/scripts/package-size-budgets.json @@ -9,7 +9,7 @@ }, "@hyperframes/core": { "packed": 1800000, - "unpacked": 4374000 + "unpacked": 4639000 }, "@hyperframes/engine": { "packed": 510000,