Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion packages/cli/src/utils/staticProjectServer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync, type ReadStream } from "node:fs";
import {
mkdirSync,
mkdtempSync,
rmSync,
symlinkSync,
writeFileSync,
type ReadStream,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import fs from "node:fs";
Expand Down Expand Up @@ -721,3 +728,36 @@ describe("serveStaticProjectHtml private project files", () => {
},
);
});

function writeToolRecords(projectDir: string): void {
mkdirSync(join(projectDir, ".hyperframes", "prepared-assets"), { recursive: true });
for (const name of ["app-history.jsonl", "agent-handoff.json", "comments.jsonl"]) {
writeFileSync(join(projectDir, ".hyperframes", name), "PRIVATE_RECORD");
}
writeFileSync(join(projectDir, ".hyperframes", "prepared-assets", "clip.txt"), "prepared");
symlinkSync(
join(projectDir, ".hyperframes", "app-history.jsonl"),
join(projectDir, "linked-history.json"),
);
}

describe("serveStaticProjectHtml tool records", () => {
it.each([
".hyperframes/app-history.jsonl",
".hyperframes/agent-handoff.json",
".hyperframes/comments.jsonl",
"%2ehyperframes/agent-handoff.json",
".hyperframes%2fcomments.jsonl",
"linked-history.json",
])("answers %s with 404 and still serves prepared assets", async (requestPath) => {
dir = mkdtempSync(join(tmpdir(), "hf-static-records-"));
writeToolRecords(dir);
server = await serveStaticProjectHtml(dir, "<html></html>");

const res = await fetch(`${server.url}${requestPath}`);
expect(res.status).toBe(404);
expect(await res.text()).not.toContain("PRIVATE_RECORD");
const prepared = await fetch(`${server.url}.hyperframes/prepared-assets/clip.txt`);
expect(await prepared.text()).toBe("prepared");
});
});
4 changes: 2 additions & 2 deletions packages/cli/src/utils/staticProjectServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createServer, request as httpRequest, type Server, type ServerResponse
import { Duplex } from "node:stream";
import { createReadStream, existsSync, readFileSync, statSync } from "node:fs";
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
import { isPrivateFileUnder, isVcsOrSecretPath } from "@hyperframes/core/safe-path";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
import { isAbsolute, relative, resolve } from "node:path";
import {
PREVIEW_PROXY_BOX_PARAM,
Expand Down Expand Up @@ -332,7 +332,7 @@ export async function serveStaticProjectHtml(
const filePath = resolve(root, requestPath);
const rel = relative(root, filePath);
if (rel.startsWith("..") || isAbsolute(rel)) continue; // traversal guard; try next root
if (isPrivateFileUnder(root, filePath, isVcsOrSecretPath)) continue;
if (isPrivateFileUnder(root, filePath, isStudioPrivatePath)) continue;
if (existsSync(filePath)) {
if (proxyRequest) {
void serveProxyRequest(
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/compiler/htmlBundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe("bundleToSingleHtml", () => {
}
});

it("refuses the app's records only under the studio private-file policy", async () => {
it("refuses the app's records by default, and inlines them only when asked for git and env files alone", async () => {
const dir = makeTempProject({
"index.html": `<!doctype html><html><head>
<link rel="stylesheet" href=".hyperframes/handoff.css">
Expand All @@ -250,7 +250,10 @@ describe("bundleToSingleHtml", () => {
expect(studio).toContain('href=".hyperframes/handoff.css"');
expect(studio).toContain(".prepared { color: blue; }");
const plain = await bundleToSingleHtml(dir);
expect(plain).toContain(".app-record { color: red; }");
expect(plain).not.toContain(".app-record");
expect(plain).toContain(".prepared { color: blue; }");
const narrow = await bundleToSingleHtml(dir, { privateFiles: "vcs-and-secrets" });
expect(narrow).toContain(".app-record { color: red; }");
} finally {
rmSync(dir, { recursive: true, force: true });
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/compiler/htmlBundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ export interface BundleOptions {
onRead?: (filePath: string) => void;
/**
* Project files the build never reads; a refused file keeps its authored reference. Git metadata and env files
* always; `"studio"` also refuses the app's records under `.hyperframes/`, as Studio's file routes do.
* always; `"studio"`, the default, also refuses the app's records under `.hyperframes/`, as every file server does.
*/
privateFiles?: BundlePrivateFiles;
}
Expand Down Expand Up @@ -1069,7 +1069,7 @@ export function bundleToSingleHtml(projectDir: string, options?: BundleOptions):
};
const scope: BundleReadScope = {
projectDir,
privateFiles: options?.privateFiles ?? "vcs-and-secrets",
privateFiles: options?.privateFiles ?? "studio",
noteRead,
};
return bundleReads.run(scope, () => bundleProject(projectDir, options));
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/safePath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
folderGone,
isProjectRootMissing,
isSafePath,
isStudioPrivatePath,
mkdirWithinProject,
resolveWithinProject,
} from "./safePath.js";
Expand Down Expand Up @@ -263,3 +264,33 @@ describe("folderGone", () => {
},
);
});

describe("isStudioPrivatePath", () => {
it.each([
".hyperframes",
".hyperframes/app-history.jsonl",
".hyperframes/agent-handoff.json",
".HYPERFRAMES/comments.jsonl",
".hyperframes./heygen-jobs.jsonl",
".hyperframes /agent-handoff.json",
".hyperframes\\comments.jsonl",
".hyperframes:stream/comments.jsonl",
".git/config",
".env.local",
])("refuses %s", (path) => {
expect(isStudioPrivatePath(path)).toBe(true);
});

it.each([
".hyperframes/prepared-assets/gif/clip.gif",
".HYPERFRAMES/Prepared-Assets./heif/photo.png",
".hyperframes/requests/sketch.png",
".hyperframes/studio-motion.json",
".hyperframes/studio-manual-edits.json",
".hyperframes-pull.json",
"assets/.hyperframes/logo.png",
"assets/logo.png",
])("allows %s", (path) => {
expect(isStudioPrivatePath(path)).toBe(false);
});
});
10 changes: 5 additions & 5 deletions packages/core/src/safePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,18 @@ export function isVcsOrSecretPath(relPath: string): boolean {
);
}

// `.hyperframes/` holds the desktop app's and the CLI's own records (a hand-off, chat history, read marks): Studio
// reaches none of it, the folder itself included, but its own files and the app's request pictures.
// `.hyperframes/` holds the desktop app's and the CLI's own records (a hand-off, chat history, read marks): no file
// server serves any of it, the folder itself included, but Studio's own files and the assets compositions load.
const STUDIO_FILES = new Set([
".hyperframes/studio-motion.json",
".hyperframes/studio-manual-edits.json",
]);
const STUDIO_FOLDERS = [".hyperframes/prepared-assets", ".hyperframes/requests"];

/** Studio's private files, for a project-relative path with `/` separators: git, env, and the app's records. */
/** Files no composition loads, for a project-relative path in any spelling Windows opens: git, env, app records. */
export function isStudioPrivatePath(relPath: string): boolean {
const path = relPath.toLowerCase();
if (isVcsOrSecretPath(path)) return true;
if (isVcsOrSecretPath(relPath)) return true;
const path = projectPathSegments(relPath).join("/");
return (
(path === ".hyperframes" || path.startsWith(".hyperframes/")) &&
!STUDIO_FILES.has(path) &&
Expand Down
48 changes: 48 additions & 0 deletions packages/engine/src/services/fileServer.pathSafety.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,51 @@ describe("fileServer private project files", () => {
},
);
});

describe("fileServer tool records", () => {
it.each([
".hyperframes/app-history.jsonl",
".hyperframes/agent-handoff.json",
".hyperframes/comments.jsonl",
"%2ehyperframes/agent-handoff.json",
".hyperframes%2fcomments.jsonl",
"linked-history.json",
])(
"answers %s with 404 from either root and still serves prepared assets",
async (requestPath) => {
const projectDir = fs.mkdtempSync(path.join(tmpdir(), "hf-fileserver-records-"));
const compiledDir = fs.mkdtempSync(path.join(tmpdir(), "hf-fileserver-records-compiled-"));
for (const root of [projectDir, compiledDir]) {
fs.mkdirSync(path.join(root, ".hyperframes", "prepared-assets"), { recursive: true });
for (const name of ["app-history.jsonl", "agent-handoff.json", "comments.jsonl"]) {
fs.writeFileSync(path.join(root, ".hyperframes", name), "PRIVATE_RECORD");
}
}
fs.writeFileSync(
path.join(projectDir, ".hyperframes", "prepared-assets", "clip.txt"),
"prepared",
);
fs.symlinkSync(
path.join(projectDir, ".hyperframes", "app-history.jsonl"),
path.join(projectDir, "linked-history.json"),
);
const server = await createFileServer({
headScripts: [],
bodyScripts: [],
projectDir,
compiledDir,
});
try {
const response = await fetch(`${server.url}/${requestPath}`);
expect(response.status).toBe(404);
expect(await response.text()).not.toContain("PRIVATE_RECORD");
const prepared = await fetch(`${server.url}/.hyperframes/prepared-assets/clip.txt`);
expect(await prepared.text()).toBe("prepared");
} finally {
server.close();
fs.rmSync(projectDir, { recursive: true, force: true });
fs.rmSync(compiledDir, { recursive: true, force: true });
}
},
);
});
6 changes: 3 additions & 3 deletions packages/engine/src/services/fileServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { serve } from "@hono/node-server";
import { readFileSync, openSync, fstatSync, closeSync, statSync, constants } from "node:fs";
import { join, extname } from "node:path";
import { injectScriptsIntoHtml } from "@hyperframes/core/compiler";
import { isPrivateFileUnder, isVcsOrSecretPath } from "@hyperframes/core/safe-path";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution";

const MIME_TYPES: Record<string, string> = {
Expand Down Expand Up @@ -99,11 +99,11 @@ function resolveContainedPaths(
): { projectPath: string; compiledPath: string | null } | null {
const projectPath = join(projectDir, relativePath);
if (!isWithinProjectRoot(projectDir, projectPath)) return null;
if (isPrivateFileUnder(projectDir, projectPath, isVcsOrSecretPath)) return null;
if (isPrivateFileUnder(projectDir, projectPath, isStudioPrivatePath)) return null;
if (!compiledDir) return { projectPath, compiledPath: null };
const compiledPath = join(compiledDir, relativePath);
if (!isWithinProjectRoot(compiledDir, compiledPath)) return null;
if (isPrivateFileUnder(compiledDir, compiledPath, isVcsOrSecretPath)) return null;
if (isPrivateFileUnder(compiledDir, compiledPath, isStudioPrivatePath)) return null;
return { projectPath, compiledPath };
}

Expand Down
50 changes: 50 additions & 0 deletions packages/producer/src/services/fileServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,3 +1190,53 @@ describe("createFileServer private project files", () => {
},
);
});

describe("createFileServer tool records", () => {
it.each([
".hyperframes/app-history.jsonl",
".hyperframes/agent-handoff.json",
".hyperframes/comments.jsonl",
"%2ehyperframes/agent-handoff.json",
".hyperframes%2fcomments.jsonl",
"linked-history.json",
])(
"answers %s with 404 from either root and still serves prepared assets",
async (requestPath) => {
const projectDir = mkdtempSync(join(tmpdir(), "hf-fs-records-"));
const compiledDir = mkdtempSync(join(tmpdir(), "hf-fs-records-compiled-"));
try {
writeEmptyIndex(projectDir);
for (const root of [projectDir, compiledDir]) {
mkdirSync(join(root, ".hyperframes", "prepared-assets"), { recursive: true });
for (const name of ["app-history.jsonl", "agent-handoff.json", "comments.jsonl"]) {
writeFileSync(join(root, ".hyperframes", name), "PRIVATE_RECORD");
}
}
writeFileSync(join(projectDir, ".hyperframes", "prepared-assets", "clip.txt"), "prepared");
symlinkSync(
join(projectDir, ".hyperframes", "app-history.jsonl"),
join(projectDir, "linked-history.json"),
);
const server = await createFileServer({
projectDir,
compiledDir,
preHeadScripts: [],
headScripts: [],
bodyScripts: [],
});
try {
const response = await fetch(`${server.url}/${requestPath}`);
expect(response.status).toBe(404);
expect(await response.text()).not.toContain("PRIVATE_RECORD");
const prepared = await fetch(`${server.url}/.hyperframes/prepared-assets/clip.txt`);
expect(await prepared.text()).toBe("prepared");
} finally {
server.close();
}
} finally {
rmSync(projectDir, { recursive: true, force: true });
rmSync(compiledDir, { recursive: true, force: true });
}
},
);
});
4 changes: 2 additions & 2 deletions packages/producer/src/services/fileServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { Readable } from "node:stream";
import { join, extname, resolve, sep } from "node:path";
import { injectScriptsAtHeadStart, injectScriptsIntoHtml } from "@hyperframes/core/compiler";
import { fpsToNumber, type Fps } from "@hyperframes/core";
import { isPrivateFileUnder, isVcsOrSecretPath } from "@hyperframes/core/safe-path";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
import { openRangeEnd } from "@hyperframes/studio-server/media-range";
import { getVerifiedHyperframeRuntimeSource } from "./hyperframeRuntimeLoader.js";
Expand Down Expand Up @@ -837,7 +837,7 @@ export function createFileServer(options: FileServerOptions): Promise<FileServer
const candidate = join(root, relativePath);
const resolveSymlinks = !allowOutsideProjectAssets && root !== compiledDir;
if (!isPathInside(candidate, root, { resolveSymlinks })) continue;
if (isPrivateFileUnder(root, candidate, isVcsOrSecretPath)) continue;
if (isPrivateFileUnder(root, candidate, isStudioPrivatePath)) continue;
const checksLinks = allowOutsideProjectAssets && root !== compiledDir;
if (checksLinks && refusesOutsideLink(root, candidate, warnLeftOut)) continue;
file = openRegularFile(candidate);
Expand Down
24 changes: 23 additions & 1 deletion packages/producer/src/services/htmlCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,29 @@ describe("detectRenderModeHints", () => {
try {
await expect(
compileForRender(projectDir, join(projectDir, "index.html"), projectDir),
).rejects.toThrow(/compositions\/\.env\.html[\s\S]*git metadata or an env file/);
).rejects.toThrow(
/compositions\/\.env\.html[\s\S]*git metadata, an env file or a private tool record/,
);
} finally {
rmSync(projectDir, { recursive: true, force: true });
}
});

it("compileForRender refuses to inline the app's records under .hyperframes as a data-composition-src", async () => {
const projectDir = makeSubCompProject(
"hf-record-subcomp-",
[{ id: "intro", src: ".hyperframes/agent-handoff.html" }],
{},
);
try {
mkdirSync(join(projectDir, ".hyperframes"));
writeFileSync(
join(projectDir, ".hyperframes", "agent-handoff.html"),
validSubCompHtml("intro", "HANDOFF_SECRET_VALUE"),
);
await expect(
compileForRender(projectDir, join(projectDir, "index.html"), projectDir),
).rejects.toThrow(/\.hyperframes\/agent-handoff\.html[\s\S]*a private tool record/);
} finally {
rmSync(projectDir, { recursive: true, force: true });
}
Expand Down
11 changes: 7 additions & 4 deletions packages/producer/src/services/htmlCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import {
readTextFile,
resolveCompositionSrc,
} from "@hyperframes/parsers/asset-resolution";
import { isPrivateFileUnder, isVcsOrSecretPath } from "@hyperframes/core/safe-path";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
import { extractMediaMetadata, extractAudioMetadata } from "../utils/ffprobe.js";
import {
escapesAssetRoot,
Expand Down Expand Up @@ -270,9 +270,9 @@ function resolveCompositionFile(
return file !== null && COMPOSITION_FILE.test(file) ? file : null;
}

// Git metadata and env files are never inlined into a render, as the preview bundler refuses them.
// Git metadata, env files and the app's `.hyperframes/` records are never inlined into a render, as preview refuses them.
const isPrivateCompositionFile = (projectDir: string, filePath: string): boolean =>
isPrivateFileUnder(projectDir, filePath, isVcsOrSecretPath);
isPrivateFileUnder(projectDir, filePath, isStudioPrivatePath);

/**
* Recursively walk every `data-composition-src` reference reachable from
Expand Down Expand Up @@ -314,7 +314,10 @@ function assertSubCompositionsUsable(
continue;
}
if (isPrivateCompositionFile(projectDir, filePath)) {
problems.push({ srcPath, detail: "it is git metadata or an env file" });
problems.push({
srcPath,
detail: "it is git metadata, an env file or a private tool record",
});
continue;
}
if (!COMPOSITION_FILE.test(filePath)) {
Expand Down
Loading