Files
hyperframes/packages/cli/src/share/render.ts
T
WaterrrForeverandClaude Opus 5.5 061ff5bbb0 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* chore(cli): drop doc comments the comment ratchet counts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:21:31 +00:00

113 lines
4.1 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { existsSync, readFileSync, realpathSync } from "node:fs";
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;
export interface ShareRender {
path: string;
id: string;
fingerprint: string;
contentFingerprint?: string;
}
export function latestShareRender(dir: string): ShareRender | null {
try {
const value: unknown = JSON.parse(
readFileSync(join(dir, ".hyperframes", "share-render.json"), "utf8"),
);
if (
!value ||
typeof value !== "object" ||
!("path" in value) ||
!("id" in value) ||
!("fingerprint" in value)
)
return null;
if (
typeof value.path !== "string" ||
typeof value.id !== "string" ||
typeof value.fingerprint !== "string" ||
!/^[a-f0-9-]{36}$/.test(value.id)
)
return null;
const expected = join(realpathSync(dir), ".hyperframes", "share-renders", `${value.id}.mp4`);
if (value.path !== expected || !existsSync(expected) || realpathSync(expected) !== expected)
return null;
const contentFingerprint =
"contentFingerprint" in value && typeof value.contentFingerprint === "string"
? value.contentFingerprint
: undefined;
return {
path: expected,
id: value.id,
fingerprint: value.fingerprint,
...(contentFingerprint && { contentFingerprint }),
};
} catch {
return null;
}
}
export async function prepareShareRender(dir: string): Promise<void> {
const [before, content] = await Promise.all([
projectFingerprint(dir),
shareContentFingerprint(dir),
]);
const previous = latestShareRender(dir);
if (content && previous?.fingerprint === before && previous.contentFingerprint === content)
return;
const stateFolder = join(dir, ".hyperframes");
if (existsSync(stateFolder) && realpathSync(stateFolder) !== stateFolder)
throw new Error("Sharing state must stay inside this project, without symlinks.");
await mkdir(stateFolder, { recursive: true });
const folder = join(stateFolder, "share-renders");
if (existsSync(folder) && realpathSync(folder) !== folder)
throw new Error("Sharing state must stay inside this project, without symlinks.");
await mkdir(folder, { recursive: true });
if (realpathSync(folder) !== folder)
throw new Error("Sharing state must stay inside this project, without symlinks.");
const id = randomUUID();
const output = join(folder, `${id}.mp4`);
const [{ createRenderPlan }, { executeRenderPlan }, render] = await Promise.all([
import("../commands/render/plan.js"),
import("../commands/render/execute.js"),
import("../commands/render.js"),
]);
try {
const plan = createRenderPlan({ dir, output, format: "mp4", quiet: true, strict: true });
await executeRenderPlan(plan, {
renderLocal: (project, output, options, cancellation) =>
render.renderLocal(
project,
output,
{ ...options, embedded: true, exitAfterComplete: false },
cancellation,
),
renderDocker: async () => {
throw new Error("Share renders locally.");
},
checkResolution: render.checkRenderResolutionPreflight,
});
if (!existsSync(output))
throw new Error("The render did not produce a video. Run hyperframes render to diagnose it.");
const [after, afterContent] = await Promise.all([
projectFingerprint(dir),
shareContentFingerprint(dir),
]);
if (after !== before || afterContent !== content)
throw new Error(
"The project changed while rendering. Share again to capture the current project.",
);
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 rm(output, { force: true });
throw error;
}
}