mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* 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>
117 lines
4.3 KiB
JavaScript
117 lines
4.3 KiB
JavaScript
// @ts-check
|
|
import {
|
|
lstat,
|
|
mkdir,
|
|
readFile,
|
|
writeFile,
|
|
rename as renameFile,
|
|
rm as remove,
|
|
} from "node:fs/promises";
|
|
import { basename, join, sep } from "node:path";
|
|
import { randomUUID } from "node:crypto";
|
|
|
|
/** @param {unknown} error */
|
|
export const errorCode = (error) =>
|
|
error instanceof Error && "code" in error ? error.code : undefined;
|
|
|
|
/** @template T @param {() => Promise<T>} run */
|
|
async function retryBusy(run) {
|
|
const waits = [10, 25, 50, 100, 200];
|
|
for (let attempt = 0; ; attempt++) {
|
|
try {
|
|
return await run();
|
|
} catch (error) {
|
|
const delay = waits[attempt];
|
|
if (
|
|
process.platform !== "win32" ||
|
|
!["EPERM", "EBUSY", "EACCES", "ENOTEMPTY"].includes(String(errorCode(error))) ||
|
|
delay === undefined
|
|
)
|
|
throw error;
|
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
}
|
|
}
|
|
}
|
|
/** @param {string} from @param {string} to */
|
|
export const rename = (from, to) => retryBusy(() => renameFile(from, to));
|
|
/** @param {string} path @param {import("node:fs").RmOptions} [options] */
|
|
export const rm = (path, options) => retryBusy(() => remove(path, options));
|
|
|
|
/** Create metadata folders only inside an existing project. @param {string} root @param {string} rel */
|
|
export async function mkdirInside(root, rel) {
|
|
let dir = root;
|
|
for (const part of rel.split(sep).filter(Boolean)) {
|
|
if (part === ".." || part === ".") throw new Error("Invalid metadata directory.");
|
|
dir = join(dir, part);
|
|
await mkdir(dir).catch((error) => {
|
|
if (errorCode(error) !== "EEXIST") throw error;
|
|
});
|
|
if (!(await lstat(dir)).isDirectory())
|
|
throw new Error("The destination must be a real folder, not a link.");
|
|
}
|
|
return dir;
|
|
}
|
|
/** @type {Map<string, Promise<void>>} */
|
|
const queued = new Map();
|
|
/** @param {string} path @param {unknown} data */
|
|
export function writeJsonAtomic(path, data) {
|
|
const text = JSON.stringify(data, null, 2);
|
|
const save = (queued.get(path) ?? Promise.resolve()).then(async () => {
|
|
const temp = `${path}.${randomUUID()}.tmp`;
|
|
try {
|
|
await writeFile(temp, text, { mode: 0o600, flag: "wx" });
|
|
await rename(temp, path);
|
|
} finally {
|
|
await rm(temp, { force: true });
|
|
}
|
|
});
|
|
const settled = save.catch(() => {});
|
|
queued.set(path, settled);
|
|
void settled.then(() => queued.get(path) === settled && queued.delete(path));
|
|
return save;
|
|
}
|
|
export class SharingStateError extends Error {
|
|
/** @param {string} path @param {unknown} [cause] */
|
|
constructor(path, cause) {
|
|
super(
|
|
`Sharing state in .hyperframes/${basename(path)} could not be read. Restore this file from a backup, or contact support to reconnect the existing share. Keep the file; deleting it can lose recovery state or create a duplicate share.`,
|
|
{ cause },
|
|
);
|
|
this.name = "SharingStateError";
|
|
}
|
|
}
|
|
|
|
/** @template T @param {string} path @param {T} fallback @returns {Promise<Record<string, unknown> | Exclude<T, object>>} */
|
|
export async function readJsonOr(path, fallback) {
|
|
try {
|
|
return JSON.parse(await readFile(path, "utf8"));
|
|
} catch (error) {
|
|
if (errorCode(error) !== "ENOENT") throw new SharingStateError(path, error);
|
|
return /** @type {Record<string, unknown> | Exclude<T, object>} */ (fallback);
|
|
}
|
|
}
|
|
|
|
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
export const isJsonObject = (value) =>
|
|
value !== null && typeof value === "object" && !Array.isArray(value);
|
|
|
|
/** @param {unknown} value @returns {value is string} */
|
|
export const isSavedId = (value) => typeof value === "string" && value.trim() !== "";
|
|
|
|
/** @param {unknown} value @returns {value is string | null} */
|
|
export const isNullableSavedId = (value) => value === null || isSavedId(value);
|
|
|
|
/** @param {unknown} value @returns {value is string | null | undefined} */
|
|
export const isOptionalSavedId = (value) => value === undefined || isNullableSavedId(value);
|
|
|
|
/** @param {unknown} value @returns {value is boolean | undefined} */
|
|
export const isOptionalBoolean = (value) => value === undefined || typeof value === "boolean";
|
|
|
|
/** An absent file is the only source of an empty record. @param {string} path */
|
|
export async function readJsonRecord(path) {
|
|
const value = await readJsonOr(path, undefined);
|
|
if (value === undefined) return null;
|
|
if (!isJsonObject(value)) throw new SharingStateError(path);
|
|
return value;
|
|
}
|