Files
hyperframes/packages/cli/src/utils/projectLink.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

133 lines
4.6 KiB
TypeScript

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;
url: string;
}
type ProjectLinks = Record<string, ProjectLink>;
const configDir = () => hyperframesConfigDir();
const projectsFile = () => seededConfigFile("projects.json");
/** Parse a JSON object, or null if it's not valid JSON or not an object. */
function jsonObject(text: string): Record<string, unknown> | null {
try {
const parsed: unknown = JSON.parse(text);
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
return parsed as Record<string, unknown>;
} catch {
return null;
}
}
function readJsonRecord(path: string): Record<string, unknown> | 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<string, unknown>;
return (
typeof link.projectId === "string" && link.projectId.length > 0 && typeof link.url === "string"
);
}
function readProjectLinks(): ProjectLinks {
const record = readJsonRecord(projectsFile());
if (!record) return {};
const links: ProjectLinks = {};
for (const [path, value] of Object.entries(record)) {
if (isProjectLink(value)) {
links[path] = { projectId: value.projectId, url: value.url };
}
}
return links;
}
function writeProjectLinks(links: ProjectLinks): void {
try {
mkdirSync(configDir(), { recursive: true, mode: 0o700 });
writeFileSync(projectsFile(), `${JSON.stringify(links, null, 2)}\n`, { mode: 0o600 });
} catch {
// Project links must never prevent local CLI commands from running.
}
}
export function readProjectLink(absDir: string): ProjectLink | null {
return readProjectLinks()[resolve(absDir)] ?? null;
}
export function writeProjectLink(absDir: string, link: ProjectLink): void {
const links = readProjectLinks();
links[resolve(absDir)] = { projectId: link.projectId, url: link.url };
writeProjectLinks(links);
}
export function ensureProjectId(absDir: string): string {
const links = readProjectLinks();
const path = resolve(absDir);
const existing = links[path];
if (existing) return existing.projectId;
const projectId = randomUUID();
links[path] = { projectId, url: "" };
writeProjectLinks(links);
return projectId;
}
// A committed, in-project descriptor so a whole team publishes to one shared link. Holds
// the project id and (for team spaces) the space id — never a secret; ownership is
// enforced server-side by the authenticated space's membership.
const TEAM_PROJECT_DIR = ".hyperframes";
const TEAM_PROJECT_FILE = "project.json";
export interface TeamProject {
projectId: string;
/** Shared team space id. Absent for a personal-space project (resolves per-user). */
spaceId?: string;
}
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 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;
return { projectId: record.projectId, ...(spaceId ? { spaceId } : {}) };
}
/** Write the committed team descriptor and return its path (for a "commit this" hint). */
export function writeTeamProject(projectDir: string, team: TeamProject): string {
const file = teamProjectPath(projectDir);
const body: TeamProject = {
projectId: team.projectId,
...(team.spaceId ? { spaceId: team.spaceId } : {}),
};
if (!writeRecord(resolve(projectDir), TEAM_PROJECT_FILE, `${JSON.stringify(body, null, 2)}\n`))
throw new Error(`Could not write ${file}`);
return file;
}