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

153 lines
5.6 KiB
TypeScript

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";
// Point the module's ~/.hyperframes config dir at a throwaway home so tests use real fs
// without touching the developer's actual home directory.
const osState = vi.hoisted(() => ({ home: "" }));
vi.mock("node:os", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:os")>();
return { ...actual, homedir: () => osState.home };
});
describe("projectLink", () => {
let projectsPath: string;
let projectDirs: string[];
let ensureProjectId: typeof import("./projectLink.js").ensureProjectId;
let readProjectLink: typeof import("./projectLink.js").readProjectLink;
let writeProjectLink: typeof import("./projectLink.js").writeProjectLink;
let readTeamProject: typeof import("./projectLink.js").readTeamProject;
let writeTeamProject: typeof import("./projectLink.js").writeTeamProject;
beforeEach(async () => {
osState.home = mkdtempSync(join(tmpdir(), "hf-home-"));
projectsPath = join(osState.home, ".hyperframes", "projects.json");
projectDirs = [];
vi.resetModules();
({ ensureProjectId, readProjectLink, writeProjectLink, readTeamProject, writeTeamProject } =
await import("./projectLink.js"));
});
afterEach(() => {
rmSync(osState.home, { recursive: true, force: true });
for (const dir of projectDirs) rmSync(dir, { recursive: true, force: true });
});
function makeProjectDir(): string {
const dir = mkdtempSync(join(tmpdir(), "hf-proj-"));
projectDirs.push(dir);
return dir;
}
it("mints one project id per directory and reuses it", () => {
const first = makeProjectDir();
const second = makeProjectDir();
const firstId = ensureProjectId(first);
expect(ensureProjectId(first)).toBe(firstId);
expect(ensureProjectId(second)).not.toBe(firstId);
});
it("round-trips a project link", () => {
const dir = makeProjectDir();
const link = { projectId: "hfp_123", url: "https://hyperframes.dev/p/hfp_123" };
writeProjectLink(dir, link);
expect(readProjectLink(dir)).toEqual(link);
});
it("persists only the project id and URL", () => {
const dir = makeProjectDir();
const link = { projectId: "hfp_123", url: "https://hyperframes.dev/p/hfp_123", secret: "nope" };
writeProjectLink(dir, link);
expect(readFileSync(projectsPath, "utf-8")).not.toContain("nope");
});
it("treats a missing projects file as empty and creates it on ensure", () => {
const dir = makeProjectDir();
expect(readProjectLink(dir)).toBeNull();
const projectId = ensureProjectId(dir);
expect(projectId).toBeTruthy();
expect(readProjectLink(dir)).toEqual({ projectId, url: "" });
});
it("treats corrupt JSON as empty and rewrites it on ensure", () => {
const dir = makeProjectDir();
writeProjectLink(dir, { projectId: "x", url: "y" });
writeFileSync(projectsPath, "{not valid json", "utf-8");
expect(readProjectLink(dir)).toBeNull();
const projectId = ensureProjectId(dir);
expect(readProjectLink(dir)).toEqual({ projectId, url: "" });
expect(() => JSON.parse(readFileSync(projectsPath, "utf-8"))).not.toThrow();
});
it("uses the resolved directory as the storage key", () => {
const dir = makeProjectDir();
const projectId = ensureProjectId(dir);
expect(ensureProjectId(resolve(dir))).toBe(projectId);
expect(Object.keys(JSON.parse(readFileSync(projectsPath, "utf-8")))).toEqual([resolve(dir)]);
});
it("reads and writes a committed team project (id + optional space)", () => {
const dir = makeProjectDir();
expect(readTeamProject(dir)).toBeNull();
// Personal-space project: id only, no spaceId key.
const soloFile = writeTeamProject(dir, { projectId: "hfp_solo" });
expect(readTeamProject(dir)).toEqual({ projectId: "hfp_solo" });
const soloBody = readFileSync(soloFile, "utf-8");
expect(soloBody).not.toContain("spaceId");
// Never a secret.
expect(soloBody).not.toContain("token");
// Team-space project: id + shared space id round-trip.
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");
});
});