From 061ff5bbb04f71432f0d97277f4140dc85b5d738 Mon Sep 17 00:00:00 2001 From: WaterrrForever Date: Mon, 16 Nov 2026 15:21:31 +0000 Subject: [PATCH] fix(cli): never write project records through symlinks under .hyperframes (#7950) * fix(cli): never write project records through symlinks under .hyperframes The committed team descriptor (.hyperframes/project.json) and the HeyGen job log (.hyperframes/heygen-jobs.jsonl) were written with path-based mkdir/write/ append calls that follow links, so a cloned project could point either file, or .hyperframes itself, at a file outside the project and have publish or a paid HeyGen job overwrite or append to it. - projectRecords: add appendRecord (O_NOFOLLOW open, same plain file checked by fstat/lstat, real folders from .hyperframes) and recordKind. - writeTeamProject goes through writeRecord (exclusive temp + rename), so a link at project.json is replaced, never written through, and a linked .hyperframes is refused. - readTeamProject refuses a linked or non-plain descriptor (or a linked .hyperframes) instead of reading it as absent, which stops publish before it would write one. - The job log and project voice picks go through appendRecord and fail with a Fix when refused. Co-Authored-By: Claude Opus 5.5 * fix(cli): route every remaining .hyperframes write through link-refusing helpers Writers that still made or wrote into a project's .hyperframes with path-based mkdir/write calls now refuse a linked or non-folder .hyperframes and never write through a link: - project lock: folder via recordFolder (the exclusive lock create stays) - lambda stack state: writeRecord - received-files record: recordFolder + writeRecord, so a link to another project file is replaced instead of overwritten - check reports: folder via recordFolder (the wx create stays) - check and snapshot frame folders: new projectFolder, which holds a folder inside .hyperframes to recordFolder and makes one elsewhere as asked - template pull staging: recordFolder - share render record: writeRecord - studio server state folder: recordFolder - share storage: the atomic JSON temp file is opened with wx Co-Authored-By: Claude Opus 5.5 * chore(cli): drop doc comments the comment ratchet counts Co-Authored-By: Claude Opus 5.5 * fix(cli): refuse to append to a project record with more than one hard link A hard link at .hyperframes/heygen-jobs.jsonl names an outside file as a plain file, so the O_NOFOLLOW open and the same-file check let the job log append to it. The append now refuses a file whose link count is above one. Whole-file record writes go through a temp file renamed into place, which replaces the name rather than writing to a linked inode. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- packages/cli/src/commands/check.ts | 12 +++- packages/cli/src/commands/lambda/state.ts | 11 +-- packages/cli/src/commands/snapshot.ts | 6 +- packages/cli/src/commands/templates.ts | 8 ++- packages/cli/src/heygen/v3.test.ts | 67 +++++++++++++++++- packages/cli/src/heygen/v3.ts | 26 +++++-- packages/cli/src/server/studioServer.ts | 9 +-- packages/cli/src/share/render.ts | 15 ++-- packages/cli/src/utils/checkBrowser.ts | 3 +- packages/cli/src/utils/checkPipeline.ts | 3 +- .../cli/src/utils/checkSnapshotDirectory.ts | 3 + packages/cli/src/utils/linkedWrites.test.ts | 61 ++++++++++++++++ packages/cli/src/utils/privateProjectFile.ts | 22 ++++-- packages/cli/src/utils/projectLink.test.ts | 38 +++++++++- packages/cli/src/utils/projectLink.ts | 33 +++++++-- packages/cli/src/utils/projectLock.ts | 10 ++- packages/cli/src/utils/projectRecords.ts | 69 +++++++++++++++++-- packages/share/src/storage.mjs | 2 +- 18 files changed, 335 insertions(+), 63 deletions(-) diff --git a/packages/cli/src/commands/check.ts b/packages/cli/src/commands/check.ts index 2c016f6621..5e55e6924f 100644 --- a/packages/cli/src/commands/check.ts +++ b/packages/cli/src/commands/check.ts @@ -2,7 +2,7 @@ import { terminalText } from "@hyperframes/core"; import { captureArtifactRevision } from "../utils/artifactCapture.js"; import { pngDimensions } from "../utils/pngDimensions.js"; import { randomUUID } from "node:crypto"; -import { mkdirSync, writeFileSync } from "node:fs"; +import { writeFileSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { recordArtifacts, @@ -54,6 +54,7 @@ import { readCachedPass, storePass, } from "../utils/checkCache.js"; +import { recordFolder } from "../utils/projectRecords.js"; import { VERSION } from "../version.js"; export const examples: Example[] = [ @@ -113,6 +114,13 @@ export function checkReportFile(projectDir: string, id: string): string { return join(projectDir, ".hyperframes", "artifacts", "checks", `${id}.json`); } +function makeReportFolder(path: string, projectDir: string): void { + if (!recordFolder(projectDir, "artifacts/checks")) + throw new Error( + `${dirname(path)} is reached through a link or a file; remove it and run again`, + ); +} + async function saveCheckReport( project: ProjectDir, options: CheckOptions, @@ -121,7 +129,7 @@ async function saveCheckReport( id: string, ): Promise { const path = checkReportFile(project.dir, id); - mkdirSync(dirname(path), { recursive: true }); + makeReportFolder(path, project.dir); writeFileSync(path, JSON.stringify(report, null, 2) + "\n", { flag: "wx" }); const reportScope = "scope" in report && typeof report.scope === "object" && report.scope !== null diff --git a/packages/cli/src/commands/lambda/state.ts b/packages/cli/src/commands/lambda/state.ts index 2242ae1fcd..849895d012 100644 --- a/packages/cli/src/commands/lambda/state.ts +++ b/packages/cli/src/commands/lambda/state.ts @@ -11,9 +11,9 @@ import { failCommand } from "../../utils/commandResult.js"; * can symlink the directory. */ -import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync } from "node:fs"; -import { dirname, join } from "node:path"; -import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; +import { existsSync, readdirSync, readFileSync, rmSync } from "node:fs"; +import { join } from "node:path"; +import { writeRecord } from "../../utils/projectRecords.js"; export interface StackOutputs { /** Project-name prefix passed to `deploy`. Used as CloudFormation stack-name suffix. */ @@ -52,8 +52,9 @@ export function stateFilePath( export function writeStackOutputs(outputs: StackOutputs, cwd: string = process.cwd()): string { const path = stateFilePath(outputs.stackName, cwd); - mkdirSync(dirname(path), { recursive: true }); - replaceFileAtomically(path, JSON.stringify(outputs, null, 2) + "\n"); + const name = `${STATE_FILE_PREFIX}${outputs.stackName}.json`; + if (!writeRecord(cwd, name, JSON.stringify(outputs, null, 2) + "\n")) + throw new Error(`Could not write ${path}: it, or .hyperframes, is a link or not a plain file`); return path; } diff --git a/packages/cli/src/commands/snapshot.ts b/packages/cli/src/commands/snapshot.ts index d107e6fff0..87f87f613a 100644 --- a/packages/cli/src/commands/snapshot.ts +++ b/packages/cli/src/commands/snapshot.ts @@ -11,6 +11,7 @@ import { sdrCanvasColourFilters, } from "@hyperframes/engine"; import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { projectFolder } from "../utils/projectRecords.js"; import { screenshotOnceMore } from "../utils/screenshotOnceMore.js"; import { parseTimeoutMs } from "../utils/timeoutArg.js"; import type { Page } from "puppeteer-core"; @@ -1172,7 +1173,10 @@ export async function captureSnapshots( : null; const snapshotDir = opts.outputDir ?? artifactDirectory(projectDir, "snapshots"); - mkdirSync(snapshotDir, { recursive: true }); + if (!projectFolder(projectDir, snapshotDir)) + throw new Error( + `${snapshotDir} is reached through a link or a file; remove it and run again`, + ); if (!opts.keep) replaced.push(...clearEarlierStills(snapshotDir)); if (replaced.length > 0) { log(` ${c.warn("⚠")} ${clearedNote(replaced.length, snapshotDir)}`); diff --git a/packages/cli/src/commands/templates.ts b/packages/cli/src/commands/templates.ts index 59ac08d241..e1dd48d871 100644 --- a/packages/cli/src/commands/templates.ts +++ b/packages/cli/src/commands/templates.ts @@ -17,6 +17,7 @@ import { writeFileSync, } from "node:fs"; import { makeOutputFolder } from "../utils/outputFolder.js"; +import { recordFolder } from "../utils/projectRecords.js"; import { processIsAlive } from "../utils/processTree.js"; import { basename, dirname, join, resolve } from "node:path"; import { resolveCloudAuthHeaders } from "../cloud/auth.js"; @@ -209,7 +210,6 @@ async function unpack( pack: TemplatePack, ): Promise { makeOutputFolder(dirname(dest)); - makeOutputFolder(stage); removeAbandonedParts(stage, dest); const work = createStagingDir(stage, `${partPrefix(dest)}${process.pid}-`); const stopWatching = removeOnStop(work); @@ -269,7 +269,11 @@ export async function pullTemplate(slug: string, dir: string): Promise { ); const dest = resolve(dir, "templates", slug); if (alreadyPulled(dest, pack)) return dest; - const stage = resolve(dir, ".hyperframes", "template-pulls"); + const stage = recordFolder(resolve(dir), "template-pulls"); + if (!stage) + throw new PullRefused( + `${resolve(dir, ".hyperframes")} is a link or not a folder; remove it and pull again.`, + ); const aside = await unpack(await download(await bundleUrl(pack), pack), dest, stage, pack); if (aside) console.log( diff --git a/packages/cli/src/heygen/v3.test.ts b/packages/cli/src/heygen/v3.test.ts index dd0613a3d7..de97cd92ce 100644 --- a/packages/cli/src/heygen/v3.test.ts +++ b/packages/cli/src/heygen/v3.test.ts @@ -1,4 +1,12 @@ -import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { + linkSync, + lstatSync, + mkdirSync, + mkdtempSync, + readFileSync, + symlinkSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; @@ -14,7 +22,7 @@ vi.mock("../cloud/auth.js", () => ({ resolveCloudBaseUrl: () => "https://api.test", })); -const { idempotencyKey, ownVoices, runJob } = await import("./v3.js"); +const { idempotencyKey, keepVoice, ownVoices, runJob } = await import("./v3.js"); /** A job route through the generated client: the create and the status GET it polls. */ const X: JobRoute = { @@ -583,3 +591,58 @@ describe("ownVoices", () => { await expect(ownVoices(5)).rejects.toMatchObject({ fix: "npx hyperframes auth login" }); }); }); + +describe("a project's job log in place of which a link was planted", () => { + const VICTIM = "victim bytes\n"; + const planted = (linkJobLog: boolean) => { + const projectDir = mkdtempSync(join(tmpdir(), "hf-v3-planted-")); + const outside = mkdtempSync(join(tmpdir(), "hf-v3-outside-")); + const victim = join(outside, "heygen-jobs.jsonl"); + writeFileSync(victim, VICTIM); + if (linkJobLog) { + mkdirSync(join(projectDir, ".hyperframes")); + symlinkSync(victim, join(projectDir, ".hyperframes/heygen-jobs.jsonl")); + } else { + symlinkSync(outside, join(projectDir, ".hyperframes")); + } + return { projectDir, victim }; + }; + + for (const [what, linkJobLog] of [ + ["the job log", true], + ["the .hyperframes folder", false], + ] as const) { + it(`never appends a job or a voice pick through a link in place of ${what}`, async () => { + const { projectDir, victim } = planted(linkJobLog); + vi.stubGlobal("fetch", async () => reply(200, { data: { id: "j1", status: "completed" } })); + await expect(runJob(X, {}, "k1", { log: { projectDir, what: "a line" } })).rejects.toThrow( + /never written through/, + ); + expect(() => keepVoice(projectDir, "en", { voice_id: "v1", name: "Ann" })).toThrow( + /never written through/, + ); + expect(readFileSync(victim, "utf8")).toBe(VICTIM); + expect( + lstatSync( + join(projectDir, linkJobLog ? ".hyperframes/heygen-jobs.jsonl" : ".hyperframes"), + ).isSymbolicLink(), + ).toBe(true); + }); + } + + it("never appends to a job log hard-linked to a file outside the project", async () => { + const projectDir = mkdtempSync(join(tmpdir(), "hf-v3-planted-")); + mkdirSync(join(projectDir, ".hyperframes")); + const victim = join(mkdtempSync(join(tmpdir(), "hf-v3-outside-")), "victim"); + writeFileSync(victim, VICTIM); + linkSync(victim, join(projectDir, ".hyperframes/heygen-jobs.jsonl")); + vi.stubGlobal("fetch", async () => reply(200, { data: { id: "j1", status: "completed" } })); + await expect(runJob(X, {}, "k1", { log: { projectDir, what: "a line" } })).rejects.toThrow( + /never written through/, + ); + expect(() => keepVoice(projectDir, "en", { voice_id: "v1", name: "Ann" })).toThrow( + /never written through/, + ); + expect(readFileSync(victim, "utf8")).toBe(VICTIM); + }); +}); diff --git a/packages/cli/src/heygen/v3.ts b/packages/cli/src/heygen/v3.ts index 9b995ec21f..7db3a3f595 100644 --- a/packages/cli/src/heygen/v3.ts +++ b/packages/cli/src/heygen/v3.ts @@ -10,6 +10,7 @@ import { resolveCredential } from "../auth/resolver.js"; import { refreshIfNeeded, resolveCloudAuthHeaders, resolveCloudBaseUrl } from "../cloud/auth.js"; import { HyperframesApiError, HyperframesCloudClient } from "../cloud/_gen/client.js"; import { Fix } from "../measure/media.js"; +import { appendRecord } from "../utils/projectRecords.js"; import { creditLimit, creditsSpent } from "../media-use/lib/credit-limit.mjs"; import { cliCheckoutLink, pageUrl } from "./plan.js"; @@ -237,14 +238,24 @@ export interface JobLog { what: string; } +const JOB_LOG = "heygen-jobs.jsonl"; + +function appendJobLog(projectDir: string, line: Record): void { + if ( + !appendRecord( + projectDir, + JOB_LOG, + `${JSON.stringify({ at: new Date().toISOString(), ...line })}\n`, + ) + ) + throw new Fix( + `${join(projectDir, ".hyperframes", JOB_LOG)} could not be added to; a link or anything but a plain file there, or in place of .hyperframes, is never written through`, + "remove that link or file and run the same command again", + ); +} + function log(entry: JobLog, record: Record) { - if (!entry.projectDir) return; - const dir = join(entry.projectDir, ".hyperframes"); - mkdirSync(dir, { recursive: true }); - appendFileSync( - join(dir, "heygen-jobs.jsonl"), - `${JSON.stringify({ at: new Date().toISOString(), what: entry.what, ...record })}\n`, - ); + if (entry.projectDir) appendJobLog(entry.projectDir, { what: entry.what, ...record }); } export interface RunOptions { @@ -420,6 +431,7 @@ export function keptVoice(projectDir: string | null, request: string): Voice | u } export function keepVoice(projectDir: string | null, request: string, voice: Voice): void { + if (projectDir) return appendJobLog(projectDir, { voice_pick: request, voice }); const file = picksFile(projectDir); mkdirSync(dirname(file), { recursive: true }); appendFileSync( diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts index d3e589ca4e..712a1ac13a 100644 --- a/packages/cli/src/server/studioServer.ts +++ b/packages/cli/src/server/studioServer.ts @@ -10,7 +10,7 @@ import type { HttpBindings } from "@hono/node-server"; import { Hono, type Context } from "hono"; import { streamSSE } from "hono/streaming"; import { realpath } from "@hyperframes/core"; -import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync, unlinkSync } from "node:fs"; +import { existsSync, readFileSync, statSync, writeFileSync, unlinkSync } from "node:fs"; import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; @@ -45,6 +45,7 @@ import { } from "@hyperframes/studio-server/request-identity"; import { mountShareRoutes } from "./shareRoutes.js"; import { isDevMode } from "../utils/env.js"; +import { recordFolder } from "../utils/projectRecords.js"; import { outsideAssetsAllowed } from "../utils/receivedRender.js"; import { insideHostApp } from "../utils/desktopApp.js"; import { runRenderSetupWorker } from "../utils/cancellableProcess.js"; @@ -467,11 +468,7 @@ function rewriteWrittenToHostViewport(projectDir: string, written: string[]): vo } function createStateFolderBeforeWatching(projectDir: string): void { - try { - mkdirSync(join(projectDir, ".hyperframes"), { recursive: true }); - } catch { - return; - } + recordFolder(projectDir, ""); } export function createStudioServer(options: StudioServerOptions): StudioServer { diff --git a/packages/cli/src/share/render.ts b/packages/cli/src/share/render.ts index fc9bb17550..4ad048c629 100644 --- a/packages/cli/src/share/render.ts +++ b/packages/cli/src/share/render.ts @@ -1,8 +1,9 @@ import { randomUUID } from "node:crypto"; import { existsSync, readFileSync, realpathSync } from "node:fs"; -import { mkdir, writeFile, rename, rm } from "node:fs/promises"; +import { mkdir, rm } from "node:fs/promises"; import { join } from "node:path"; import { projectFingerprint, shareSourceFingerprint } from "@hyperframes/share"; +import { writeRecord } from "../utils/projectRecords.js"; export const shareContentFingerprint = shareSourceFingerprint; @@ -76,8 +77,6 @@ export async function prepareShareRender(dir: string): Promise { import("../commands/render/execute.js"), import("../commands/render.js"), ]); - const target = join(stateFolder, "share-render.json"); - const temporary = `${target}.${id}.tmp`; try { const plan = createRenderPlan({ dir, output, format: "mp4", quiet: true, strict: true }); await executeRenderPlan(plan, { @@ -103,13 +102,11 @@ export async function prepareShareRender(dir: string): Promise { throw new Error( "The project changed while rendering. Share again to capture the current project.", ); - await writeFile( - temporary, - JSON.stringify({ id, path: output, fingerprint: before, contentFingerprint: content }), - ); - await rename(temporary, target); + const record = { id, path: output, fingerprint: before, contentFingerprint: content }; + if (!writeRecord(dir, "share-render.json", JSON.stringify(record))) + throw new Error("Sharing state must stay inside this project, without symlinks."); } catch (error) { - await Promise.allSettled([rm(output, { force: true }), rm(temporary, { force: true })]); + await rm(output, { force: true }); throw error; } } diff --git a/packages/cli/src/utils/checkBrowser.ts b/packages/cli/src/utils/checkBrowser.ts index 1d3de8ca40..7e56c33672 100644 --- a/packages/cli/src/utils/checkBrowser.ts +++ b/packages/cli/src/utils/checkBrowser.ts @@ -5,7 +5,7 @@ import { logCheckProgress } from "./checkProgress.js"; import { BrowserCleanupError, closeManagedBrowser, onStopSignal } from "../browser/launch.js"; import { collectMotionBlurDrafts, dropMotionBlurConsoleEcho } from "./checkMotionBlur.js"; import { checkSnapshotDirectory } from "./checkSnapshotDirectory.js"; -import { existsSync, mkdirSync, writeFileSync } from "node:fs"; +import { existsSync, writeFileSync } from "node:fs"; import os from "node:os"; import { join, resolve } from "node:path"; import type { Browser, Page } from "puppeteer-core"; @@ -789,7 +789,6 @@ async function withFindingCapturePage( chromeBrowser = session.browser; await waitForPreferredSeekTarget(session.page); const snapshotDir = checkSnapshotDirectory(project.dir); - mkdirSync(snapshotDir, { recursive: true }); return await capture(session.page, snapshotDir, server); } catch (error) { failure = { kind: "failed", error }; diff --git a/packages/cli/src/utils/checkPipeline.ts b/packages/cli/src/utils/checkPipeline.ts index 9c721bd460..71edf74cc0 100644 --- a/packages/cli/src/utils/checkPipeline.ts +++ b/packages/cli/src/utils/checkPipeline.ts @@ -24,7 +24,7 @@ import { findingFrameRequests, } from "./checkFindingFrames.js"; export { selectFindingFrames } from "./checkFindingFrames.js"; -import { mkdirSync, writeFileSync } from "node:fs"; +import { writeFileSync } from "node:fs"; import { readFile } from "node:fs/promises"; import type { PrelaunchedBrowser } from "../capture/captureCompositionFrame.js"; import { join, relative } from "node:path"; @@ -2415,7 +2415,6 @@ async function writeSnapshot( pngBase64: string, ): Promise { const snapshotDir = checkSnapshotDirectory(projectDir); - mkdirSync(snapshotDir, { recursive: true }); const filename = `frame-${String(index).padStart(2, "0")}-at-${time.toFixed(1)}s.png`; const path = join(snapshotDir, filename); writeFileSync(path, Buffer.from(pngBase64, "base64")); diff --git a/packages/cli/src/utils/checkSnapshotDirectory.ts b/packages/cli/src/utils/checkSnapshotDirectory.ts index 7b971e1cbe..057ec315bb 100644 --- a/packages/cli/src/utils/checkSnapshotDirectory.ts +++ b/packages/cli/src/utils/checkSnapshotDirectory.ts @@ -1,5 +1,6 @@ import { resolve } from "node:path"; import { artifactDirectory, finishSnapshotRun } from "@hyperframes/studio-server/history"; +import { projectFolder } from "./projectRecords.js"; const runDirectories = new Map(); const activeChecks = new Map(); @@ -16,6 +17,8 @@ export function checkSnapshotDirectory(projectDir: string): string { directory = artifactDirectory(project, "snapshots"); runDirectories.set(project, directory); } + if (!projectFolder(project, directory)) + throw new Error(`${directory} is reached through a link or a file; remove it and run again`); return directory; } diff --git a/packages/cli/src/utils/linkedWrites.test.ts b/packages/cli/src/utils/linkedWrites.test.ts index 9d25cab4fd..b4fde16239 100644 --- a/packages/cli/src/utils/linkedWrites.test.ts +++ b/packages/cli/src/utils/linkedWrites.test.ts @@ -13,10 +13,15 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { writeStackOutputs } from "../commands/lambda/state.js"; +import { pullTemplate } from "../commands/templates.js"; import { outDir, writeImage } from "../measure/media.js"; import { writeParakeetTranscript } from "../whisper/parakeet.js"; +import { checkSnapshotDirectory } from "./checkSnapshotDirectory.js"; import { writeTurn, type Turn } from "./historyOwner.js"; +import { recordReceivedFiles } from "./privateProjectFile.js"; import { recordProjectRegistryItems, seedProjectAuthoringSkill } from "./projectConfig.js"; +import { withProjectLock } from "./projectLock.js"; +import { projectFolder } from "./projectRecords.js"; const roots: string[] = []; afterEach(() => { @@ -146,3 +151,59 @@ describe("fixed-name outputs", () => { expect(readFileSync(clip).subarray(1, 4).toString()).toBe("PNG"); }); }); + +describe("every writer under a linked .hyperframes folder", () => { + const writers: [string, (project: string) => unknown][] = [ + [ + "the project lock", + (project) => + withProjectLock(project, "test", async () => {}, { projectLock: { waitMs: 50 } }), + ], + [ + "the lambda stack file", + (project) => writeStackOutputs({ stackName: "demo", region: "us-east-1" } as never, project), + ], + ["the received-files record", (project) => recordReceivedFiles(project, ["a.png"])], + ["a check's snapshot folder", (project) => checkSnapshotDirectory(project)], + [ + "a snapshot --output inside .hyperframes", + (project) => { + if (!projectFolder(project, join(records(project), "shots"))) throw new Error("refused"); + }, + ], + [ + "a template pull's staging folder", + (project) => { + vi.stubGlobal("fetch", async () => { + throw new Error("offline"); + }); + return pullTemplate("finance-explainer", project).finally(() => vi.unstubAllGlobals()); + }, + ], + ]; + + for (const [what, write] of writers) { + it(`refuses ${what}`, async () => { + const { project, outside } = setup(); + linkRecordsFolderOut(project, outside); + await expect(Promise.resolve().then(() => write(project))).rejects.toThrow( + /link|refused|symlinks/, + ); + expect(readdirSync(outside)).toEqual(["victim.txt"]); + }); + } + + it("replaces a linked received-files record instead of overwriting the project file it points at", () => { + const { project } = setup(); + const data = join(project, "data.json"); + writeFileSync(data, '{"files":[]}'); + mkdirSync(records(project)); + const record = join(records(project), "received-files.json"); + symlinkSync("../data.json", record); + + recordReceivedFiles(project, ["a.png"]); + + expect(readFileSync(data, "utf8")).toBe('{"files":[]}'); + expect(isLink(record)).toBe(false); + }); +}); diff --git a/packages/cli/src/utils/privateProjectFile.ts b/packages/cli/src/utils/privateProjectFile.ts index 434f98b74d..36fbc0d204 100644 --- a/packages/cli/src/utils/privateProjectFile.ts +++ b/packages/cli/src/utils/privateProjectFile.ts @@ -8,16 +8,16 @@ import { rmSync, statSync, } from "node:fs"; -import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; +import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; import { isEnvOrGitPath, isPrivateOutsideFile, linksOutside, } from "@hyperframes/studio-server/outside-file"; import { isPrivateProjectFile, realFilePath } from "@hyperframes/studio-server/safe-path"; -import { mkdirWithinProject } from "@hyperframes/core"; import { isCredentialOrToolStatePath } from "@hyperframes/core/safe-path"; -import { publishRegistryFile, registryRoot, registryTargetPath } from "../registry/publication.js"; +import { registryRoot } from "../registry/publication.js"; +import { recordFolder, writeRecord } from "./projectRecords.js"; import { walkBudget } from "./walkBudget.js"; export { isEnvOrGitPath, isPrivateOutsideFile, linksOutside }; @@ -89,15 +89,25 @@ function takeRecordLock(lock: string): void { function changeReceivedRecord(projectDir: string, change: (files: Set) => boolean): void { const root = registryRoot(projectDir); - const lock = registryTargetPath(root, RECEIVED_RECORD_LOCK); - mkdirWithinProject(root, dirname(lock)); + if (!recordFolder(root, "")) + throw new Error( + `${join(root, ".hyperframes")} is a link or not a folder; remove it and try again.`, + ); + const lock = join(root, RECEIVED_RECORD_LOCK); takeRecordLock(lock); try { const record = readRecord(root, true); if (record && !Array.isArray(record.listed)) return; const files = new Set(((record?.listed ?? []) as unknown[]).map(String)); if (!change(files)) return; - publishRegistryFile(root, RECEIVED_RECORD, `${JSON.stringify({ files: [...files].sort() })}\n`); + if ( + !writeRecord( + root, + basename(RECEIVED_RECORD), + `${JSON.stringify({ files: [...files].sort() })}\n`, + ) + ) + throw new Error(`Could not write ${join(root, RECEIVED_RECORD)}.`); } finally { rmSync(lock, { force: true }); } diff --git a/packages/cli/src/utils/projectLink.test.ts b/packages/cli/src/utils/projectLink.test.ts index 37cbefd19b..0a43b32ddd 100644 --- a/packages/cli/src/utils/projectLink.test.ts +++ b/packages/cli/src/utils/projectLink.test.ts @@ -1,4 +1,12 @@ -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { + lstatSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; @@ -113,4 +121,32 @@ describe("projectLink", () => { writeTeamProject(dir, { projectId: "hfp_team", spaceId: "space-42" }); expect(readTeamProject(dir)).toEqual({ projectId: "hfp_team", spaceId: "space-42" }); }); + + it("refuses a team descriptor that is a link, and replaces the link rather than writing through it", () => { + const dir = makeProjectDir(); + const victim = join(makeProjectDir(), ".bashrc"); + writeFileSync(victim, "victim bytes\n"); + mkdirSync(join(dir, ".hyperframes")); + const descriptor = join(dir, ".hyperframes", "project.json"); + symlinkSync(victim, descriptor); + + expect(() => readTeamProject(dir)).toThrow(/is a link or not a plain file/); + writeTeamProject(dir, { projectId: "hfp_new" }); + + expect(readFileSync(victim, "utf-8")).toBe("victim bytes\n"); + expect(lstatSync(descriptor).isFile()).toBe(true); + expect(readTeamProject(dir)).toEqual({ projectId: "hfp_new" }); + }); + + it("refuses to read or write the team descriptor through a linked .hyperframes folder", () => { + const dir = makeProjectDir(); + const outside = makeProjectDir(); + const victim = join(outside, "project.json"); + writeFileSync(victim, "victim bytes\n"); + symlinkSync(outside, join(dir, ".hyperframes")); + + expect(() => readTeamProject(dir)).toThrow(/is a link or not a plain file/); + expect(() => writeTeamProject(dir, { projectId: "hfp_new" })).toThrow(/Could not write/); + expect(readFileSync(victim, "utf-8")).toBe("victim bytes\n"); + }); }); diff --git a/packages/cli/src/utils/projectLink.ts b/packages/cli/src/utils/projectLink.ts index 3afc6cdc5b..578d69d78c 100644 --- a/packages/cli/src/utils/projectLink.ts +++ b/packages/cli/src/utils/projectLink.ts @@ -1,7 +1,9 @@ import { randomUUID } from "node:crypto"; import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { join, resolve } from "node:path"; +import { CliRuntimeError } from "./commandResult.js"; import { hyperframesConfigDir, seededConfigFile } from "./configDir.js"; +import { readRecord, recordKind, writeRecord } from "./projectRecords.js"; interface ProjectLink { projectId: string; @@ -13,11 +15,10 @@ type ProjectLinks = Record; const configDir = () => hyperframesConfigDir(); const projectsFile = () => seededConfigFile("projects.json"); -/** Read + parse a JSON object file, or null if it's missing, unreadable, or not an object. */ -function readJsonRecord(path: string): Record | null { +/** Parse a JSON object, or null if it's not valid JSON or not an object. */ +function jsonObject(text: string): Record | null { try { - if (!existsSync(path)) return null; - const parsed: unknown = JSON.parse(readFileSync(path, "utf-8")); + const parsed: unknown = JSON.parse(text); if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null; return parsed as Record; } catch { @@ -25,6 +26,14 @@ function readJsonRecord(path: string): Record | null { } } +function readJsonRecord(path: string): Record | null { + try { + return existsSync(path) ? jsonObject(readFileSync(path, "utf-8")) : null; + } catch { + return null; + } +} + function isProjectLink(value: unknown): value is ProjectLink { if (typeof value !== "object" || value === null || Array.isArray(value)) return false; const link = value as Record; @@ -92,8 +101,18 @@ function teamProjectPath(projectDir: string): string { return join(resolve(projectDir), TEAM_PROJECT_DIR, TEAM_PROJECT_FILE); } +const MAX_TEAM_PROJECT_BYTES = 64 * 1024; + export function readTeamProject(projectDir: string): TeamProject | null { - const record = readJsonRecord(teamProjectPath(projectDir)); + const kind = recordKind(resolve(projectDir), TEAM_PROJECT_FILE); + if (kind === "absent") return null; + if (kind === "other") + throw new CliRuntimeError( + `${teamProjectPath(projectDir)} is a link or not a plain file, or ${TEAM_PROJECT_DIR} is not a real folder. Remove it and publish again.`, + ); + const record = jsonObject( + readRecord(resolve(projectDir), TEAM_PROJECT_FILE, MAX_TEAM_PROJECT_BYTES), + ); if (!record || typeof record.projectId !== "string" || record.projectId.length === 0) return null; const spaceId = typeof record.spaceId === "string" && record.spaceId.length > 0 ? record.spaceId : undefined; @@ -107,7 +126,7 @@ export function writeTeamProject(projectDir: string, team: TeamProject): string projectId: team.projectId, ...(team.spaceId ? { spaceId: team.spaceId } : {}), }; - mkdirSync(join(resolve(projectDir), TEAM_PROJECT_DIR), { recursive: true }); - writeFileSync(file, `${JSON.stringify(body, null, 2)}\n`); + if (!writeRecord(resolve(projectDir), TEAM_PROJECT_FILE, `${JSON.stringify(body, null, 2)}\n`)) + throw new Error(`Could not write ${file}`); return file; } diff --git a/packages/cli/src/utils/projectLock.ts b/packages/cli/src/utils/projectLock.ts index 6057fdcc02..0a2015ed4e 100644 --- a/packages/cli/src/utils/projectLock.ts +++ b/packages/cli/src/utils/projectLock.ts @@ -1,7 +1,8 @@ -import { mkdirSync, readFileSync, unlinkSync } from "node:fs"; +import { readFileSync, unlinkSync } from "node:fs"; import { createFileAtomically } from "@hyperframes/core/atomic-file"; -import { dirname, join } from "node:path"; +import { join } from "node:path"; import { setTimeout as sleep } from "node:timers/promises"; +import { recordFolder } from "./projectRecords.js"; import { readToolContext, type ToolContext } from "./toolContext.js"; export class ProjectLockTimeout extends Error {} @@ -90,7 +91,10 @@ export async function withProjectLock( ): Promise { if (!context.projectLock) return run(); const path = projectLockPath(projectDir); - mkdirSync(dirname(path), { recursive: true }); + if (!recordFolder(projectDir, "")) + throw new Error( + `${join(projectDir, ".hyperframes")} is a link or not a folder; remove it and run again`, + ); const holder: Holder = { pid: process.pid, command, startedAt: Date.now() }; const deadline = holder.startedAt + context.projectLock.waitMs; let nextNoticeAt = holder.startedAt + FIRST_NOTICE_MS; diff --git a/packages/cli/src/utils/projectRecords.ts b/packages/cli/src/utils/projectRecords.ts index a7563f417a..6a7b73dff7 100644 --- a/packages/cli/src/utils/projectRecords.ts +++ b/packages/cli/src/utils/projectRecords.ts @@ -7,8 +7,9 @@ import { openSync, readSync, unlinkSync, + writeSync, } from "node:fs"; -import { join } from "node:path"; +import { isAbsolute, join, relative, resolve, sep } from "node:path"; import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; // The records the CLI and the desktop app keep in a project's .hyperframes/. A project can be a clone of someone @@ -37,9 +38,17 @@ function recordsDir(projectDir: string, name: string, create = false): string | const recordFile = (name: string) => name.split("/").at(-1) ?? name; +function plainFileAt(fd: number, path: string) { + const stats = fstatSync(fd); + const named = lstatSync(path); + return stats.isFile() && named.isFile() && named.ino === stats.ino && named.dev === stats.dev + ? stats + : null; +} + /** The last `maxBytes` of a plain file, and whether that is all of it; null for a link, a device, or a missing file. - * The file is opened first and read only when `lstat` then finds the same plain file at the path, so this holds - * where O_NOFOLLOW doesn't exist (Windows), and nothing path-based follows the check. */ + * The file is opened first and read only when `lstat` then finds the same plain file at the path, so nothing + * path-based follows the check. */ function readPlainTail(path: string, maxBytes: number): { text: string; whole: boolean } | null { let fd: number; try { @@ -48,10 +57,8 @@ function readPlainTail(path: string, maxBytes: number): { text: string; whole: b return null; } try { - const stats = fstatSync(fd); - const named = lstatSync(path); - if (!stats.isFile() || !named.isFile() || named.ino !== stats.ino || named.dev !== stats.dev) - return null; + const stats = plainFileAt(fd, path); + if (!stats) return null; const length = Math.min(stats.size, maxBytes); const buffer = Buffer.alloc(length); readSync(fd, buffer, 0, length, stats.size - length); @@ -108,10 +115,58 @@ export function writeRecord(projectDir: string, name: string, data: string): boo return dir !== null && replaceFile(join(dir, recordFile(name)), data, 0o600); } +export function appendRecord(projectDir: string, name: string, data: string): boolean { + const dir = recordsDir(projectDir, name, true); + if (!dir) return false; + const path = join(dir, recordFile(name)); + if (recordKind(projectDir, name) === "other") return false; + let fd: number; + try { + const flags = constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT; + fd = openSync(path, flags | NO_FOLLOW | NON_BLOCK, 0o600); + } catch { + return false; + } + try { + if ((plainFileAt(fd, path)?.nlink ?? 2) > 1) return false; + writeSync(fd, data); + return true; + } catch { + return false; + } finally { + closeSync(fd); + } +} + +export function recordKind(projectDir: string, name: string): "absent" | "file" | "other" { + const parts = [".hyperframes", ...name.split("/")]; + let path = projectDir; + for (const [index, part] of parts.entries()) { + path = join(path, part); + try { + const stats = lstatSync(path, { throwIfNoEntry: false }); + if (!stats) return "absent"; + if (!(index === parts.length - 1 ? stats.isFile() : stats.isDirectory())) return "other"; + } catch { + return "other"; + } + } + return "file"; +} + export function recordFolder(projectDir: string, folder: string): string | null { return recordsDir(projectDir, `${folder.replaceAll("\\", "/")}/`, true); } +export function projectFolder(projectDir: string, folder: string): string | null { + const inside = relative(join(resolve(projectDir), ".hyperframes"), resolve(folder)); + if (inside === ".." || inside.startsWith(`..${sep}`) || isAbsolute(inside)) { + mkdirSync(folder, { recursive: true }); + return folder; + } + return recordFolder(projectDir, inside); +} + /** Deletes a record that is a plain file; false when there is none to delete. */ export function removeRecord(projectDir: string, name: string): boolean { const dir = recordsDir(projectDir, name); diff --git a/packages/share/src/storage.mjs b/packages/share/src/storage.mjs index b9560f7a9a..c5dc0247bb 100644 --- a/packages/share/src/storage.mjs +++ b/packages/share/src/storage.mjs @@ -59,7 +59,7 @@ export function writeJsonAtomic(path, data) { const save = (queued.get(path) ?? Promise.resolve()).then(async () => { const temp = `${path}.${randomUUID()}.tmp`; try { - await writeFile(temp, text, { mode: 0o600 }); + await writeFile(temp, text, { mode: 0o600, flag: "wx" }); await rename(temp, path); } finally { await rm(temp, { force: true });