fix: every composition file server refuses credential and tool-state files (#7889)

* wip: paused before review

* test: cover credential files on the preview routes and the deploy notice

* refactor: name the inspector's file lookup for what it serves

* fix(cli): a link to a private file outside the project keeps its own left-out reason

* fix: escape the upload notice and skip private files from old manifests
This commit is contained in:
Michael Wang
2026-11-16 01:42:08 +00:00
committed by GitHub
parent ed93a13125
commit 014326b40b
22 changed files with 456 additions and 93 deletions
+6 -3
View File
@@ -20,7 +20,8 @@ import {
buildAudioSidecar,
buildProbeFacts,
hashProjectDir,
listSiteFiles,
isPrivateSitePath,
listSiteFilesForUpload,
wantsAudioSidecar,
wantsProbeFacts,
} from "@hyperframes/producer/distributed";
@@ -116,7 +117,9 @@ export async function ensureSiteStored(
} catch (error) {
if ((error as Error).name !== "PLAN_ARTIFACT_MISSING") throw error;
const localPath = projectDir && join(projectDir, path);
if (!localPath || !(await hasContent(localPath, sha256))) return void missing.push(path);
const reupload =
localPath && !isPrivateSitePath(path) && (await hasContent(localPath, sha256));
if (!reupload) return void missing.push(path);
await putContentStoreFile(s3, localPath, bucket, sha256);
}
});
@@ -156,7 +159,7 @@ export async function deploySite(opts: DeploySiteOptions): Promise<SiteHandle> {
if (!statSync(opts.projectDir).isDirectory()) {
throw new Error(`[deploySite] projectDir is not a directory: ${opts.projectDir}`);
}
const files = listSiteFiles(opts.projectDir);
const files = listSiteFilesForUpload(opts.projectDir, opts.onNotice);
const siteId = opts.siteId ?? hashProjectDir(opts.projectDir, files);
const projectS3Uri = siteManifestUri(opts.bucketName, siteId);
@@ -4,7 +4,12 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import type { SFNClient } from "@aws-sdk/client-sfn";
import type { SerializableDistributedRenderConfig } from "../events.js";
import { SITE_MANIFEST_INVALID } from "../siteManifest.js";
import { createHash } from "node:crypto";
import {
parseSiteManifest,
SITE_MANIFEST_INVALID,
serializeSiteManifest,
} from "../siteManifest.js";
import { asS3Client, FakeS3 } from "./__fixtures__/fakeS3.js";
import { deploySite, type SiteHandle, siteHandleFromId } from "./deploySite.js";
import { renderToLambda } from "./renderToLambda.js";
@@ -272,6 +277,26 @@ describe("renderToLambda", () => {
expect(sfn.starts).toHaveLength(0);
});
it("never re-uploads a private file an older site manifest still lists", async () => {
const s3 = new FakeS3();
const sfn = new FakeSFN();
const site = await deploySite({ projectDir, bucketName: "test-bucket", s3: asS3Client(s3) });
const token = "//registry.npmjs.org/:_authToken=secret";
writeFileSync(join(projectDir, ".npmrc"), token);
const manifestId = `test-bucket/sites/${site.siteId}/files.json`;
const files = parseSiteManifest(s3.bodies.get(manifestId)!.toString());
const sha256 = createHash("sha256").update(token).digest("hex");
files.push({ path: ".npmrc", sha256, sizeBytes: token.length });
s3.bodies.set(manifestId, Buffer.from(serializeSiteManifest(files)));
s3.ops.length = 0;
await expect(renderWith(s3, sfn, site, projectDir)).rejects.toMatchObject({
name: "SITE_FILES_MISSING",
});
expect(s3.ops.filter((op) => op.kind === "put")).toHaveLength(0);
expect(sfn.starts).toHaveLength(0);
});
it("without a project dir, refuses with the missing-file count before starting", async () => {
const s3 = new FakeS3();
const sfn = new FakeSFN();
@@ -24,6 +24,11 @@ beforeAll(() => {
writeFileSync(join(site, "index.html"), "<h1>home</h1>");
writeFileSync(join(site, "pages", "about page.html"), "<h1>about</h1>");
writeFileSync(join(site, "logo.png"), "png-bytes");
for (const folder of [".git", ".hyperframes", ".aws"]) mkdirSync(join(site, folder));
for (const file of [".env", ".env.local", ".git/config", ".hyperframes/x.json", ".npmrc"]) {
writeFileSync(join(site, file), "SECRET=1");
}
writeFileSync(join(site, ".aws", "credentials"), "SECRET=1");
writeFileSync(join(base, "secret.txt"), "outside the root");
writeFileSync(join(base, "outside.html"), "<h1>outside</h1>");
if (process.platform !== "win32") {
@@ -98,6 +103,24 @@ describe("startLocalPageServer", () => {
expect(await response.text()).toBe("");
});
it.each([
"/.env",
"/.ENV",
"/.env.local",
"/.git/config",
"/%2egit/config",
"/.hyperframes/x.json",
"/.npmrc",
"/.aws/credentials",
])("keeps the private file %s from the page and the capture fetchers", async (path) => {
const response = await fetch(`${server.origin}${path}`);
expect(response.status).toBe(404);
expect(await response.text()).toBe("");
const local = await safeFetch(`${server.origin}${path}`);
expect(local?.status).toBe(404);
expect(await local?.text()).not.toContain("SECRET");
});
it.skipIf(process.platform === "win32")("refuses a symlink that leaves the root", async () => {
expect((await fetch(`${server.origin}/link.txt`)).status).toBe(404);
});
+3 -1
View File
@@ -14,6 +14,7 @@ import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep }
import { Readable } from "node:stream";
import { fileURLToPath, pathToFileURL } from "node:url";
import { listenOnBrowserSafePort } from "@hyperframes/engine";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
export interface LocalCaptureTarget {
/** Real path of the directory the page may read from. */
@@ -127,6 +128,7 @@ async function servedFile(root: string, pathname: string): Promise<string | null
if (decoded.includes("\0")) return null;
const candidate = resolve(root, `.${decoded}`);
if (!isInside(root, candidate)) return null;
if (isPrivateFileUnder(root, candidate, isStudioPrivatePath)) return null;
try {
// Symlinks are followed, so the containment check must run on the real path.
const real = await realpath(candidate);
@@ -146,7 +148,7 @@ function end(response: ServerResponse, status: number): void {
response.end();
}
/** Serves only regular files whose real path lies inside `target.root`, on a loopback port. */
/** Serves only regular, non-private files whose real path lies inside `target.root`, on a loopback port. */
export async function startLocalPageServer(target: LocalCaptureTarget): Promise<LocalPageServer> {
const sockets = new Set<Socket>();
const server = createServer((request, response) => {
+2
View File
@@ -512,6 +512,7 @@ async function runSites(args: Record<string, unknown>): Promise<void> {
projectDir: resolve(projectDir),
bucketName: state.bucketName,
siteId: args["site-id"] as string | undefined,
onNotice: (message) => console.error(message),
});
if (args.json) {
console.log(JSON.stringify(handle, null, 2));
@@ -693,6 +694,7 @@ async function runRenderBatch(args: Record<string, unknown>): Promise<void> {
projectDir: resolve(projectDir),
bucketName: state.bucketName,
siteId: args["site-id"] as string | undefined,
onNotice: (message) => console.error(message),
});
const results: Array<{ outputKey: string; executionName?: string; error?: string }> = [];
@@ -12,8 +12,8 @@ import {
import { spawn } from "node:child_process";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { isCredentialOrToolStatePath } from "@hyperframes/core/safe-path";
import {
isCredentialOrToolStatePath,
isEnvOrGitPath,
isPrivateCompositionFile,
receivedFiles,
+1 -46
View File
@@ -15,7 +15,7 @@ import {
} from "@hyperframes/studio-server/outside-file";
import { isPrivateProjectFile, realFilePath } from "@hyperframes/studio-server/safe-path";
import { mkdirWithinProject } from "@hyperframes/core";
import { projectPathSegments } from "@hyperframes/core/safe-path";
import { isCredentialOrToolStatePath } from "@hyperframes/core/safe-path";
import { publishRegistryFile, registryRoot, registryTargetPath } from "../registry/publication.js";
export { isEnvOrGitPath, isPrivateOutsideFile, linksOutside };
@@ -203,51 +203,6 @@ export function receivedRecordStatus(
return { path: join(nearest.root, RECEIVED_RECORD), listed: records.flatMap(listedUnder) };
}
const CREDENTIAL_FILES = new Set([
".npmrc",
".yarnrc",
".yarnrc.yml",
".pypirc",
"pip.conf",
"pip.ini",
".netrc",
"_netrc",
".git-credentials",
".gitconfig",
".mcp.json",
]);
const CREDENTIAL_FOLDERS = new Set([
".ssh",
".aws",
".gnupg",
".docker",
".kube",
".azure",
".claude",
".codex",
".cursor",
".agents",
".gemini",
".vscode",
".idea",
]);
const CREDENTIAL_CONFIG_FOLDERS = new Set(["gcloud", "gh"]);
const KEY_FILE = /^(?:id_(?:rsa|dsa|ecdsa|ed25519).*|.*\.(?:pem|key|p12|pfx))$/;
export function isCredentialOrToolStatePath(relPath: string): boolean {
const segments = projectPathSegments(relPath);
const name = segments.at(-1) ?? "";
return (
CREDENTIAL_FILES.has(name) ||
KEY_FILE.test(name) ||
segments.some(
(segment, at) =>
CREDENTIAL_FOLDERS.has(segment) ||
(segment === ".config" && CREDENTIAL_CONFIG_FOLDERS.has(segments[at + 1] ?? "")),
)
);
}
const leavesProject = (path: string): boolean =>
isAbsolute(path) || path.split(/[\\/]/)[0] === "..";
+2 -2
View File
@@ -467,8 +467,8 @@ function referencedFileLeftOut(
entryPath: string,
absPath: string,
): LeftOutReason | null {
if (isPrivateCompositionFile(ctx.absProjectDir, absPath)) return "private";
if (linksToPrivateOutsideFile(ctx.absProjectDir, absPath)) return "blocklisted";
if (isPrivateCompositionFile(ctx.absProjectDir, absPath)) return "private";
if (ctx.received(entryPath) && linksOutside(ctx.absProjectDir, absPath)) return "received";
if (!loadableReference(ctx.absProjectDir, absPath)) return "private";
return null;
@@ -875,8 +875,8 @@ function walkedFileLeftOut(
absPath: string,
received: (path: string) => boolean,
): WalkedLeftOutReason | null {
if (isPrivateCompositionFile(absProjectDir, absPath)) return "private";
if (linksToPrivateOutsideFile(absProjectDir, absPath)) return "blocklisted";
if (isPrivateCompositionFile(absProjectDir, absPath)) return "private";
if (received(filePath) && linksOutside(absProjectDir, absPath)) return "received";
return null;
}
@@ -729,6 +729,28 @@ describe("serveStaticProjectHtml private project files", () => {
);
});
describe("serveStaticProjectHtml credential files", () => {
it.each([".npmrc", ".mcp.json", ".claude/x.json", ".aws/credentials", "id_ed25519", "cert.pem"])(
"answers %s with 404",
async (requestPath) => {
dir = mkdtempSync(join(tmpdir(), "hf-static-credentials-"));
for (const folder of [".claude", ".aws"]) mkdirSync(join(dir, folder));
for (const file of [".npmrc", ".mcp.json", ".claude/x.json", ".aws/credentials"]) {
writeFileSync(join(dir, file), "SECRET=1");
}
writeFileSync(join(dir, "id_ed25519"), "SECRET=1");
writeFileSync(join(dir, "cert.pem"), "SECRET=1");
writeFileSync(join(dir, "asset.txt"), "ok");
server = await serveStaticProjectHtml(dir, "<html></html>");
const res = await fetch(`${server.url}${requestPath}`);
expect(res.status).toBe(404);
expect(await res.text()).not.toContain("SECRET");
expect((await fetch(`${server.url}asset.txt`)).status).toBe(200);
},
);
});
describe("serveStaticProjectHtml page another site loads", () => {
const MARK = '<meta name="hyperframes-preview-for-another-site">';
const SANDBOX = "sandbox allow-scripts";
+17
View File
@@ -277,6 +277,20 @@ describe("isStudioPrivatePath", () => {
".hyperframes:stream/comments.jsonl",
".git/config",
".env.local",
".npmrc",
".NPMRC",
".mcp.json",
".mcp.json::$DATA",
".claude/x.json",
".Claude./settings.local.json",
".aws/credentials",
"keys\\.ssh\\config",
"id_ed25519",
"deploy/id_rsa.pub",
"certs/cert.pem",
"certs/CERT.PEM.",
".config/gh/hosts.yml",
"../.aws/credentials",
])("refuses %s", (path) => {
expect(isStudioPrivatePath(path)).toBe(true);
});
@@ -290,6 +304,9 @@ describe("isStudioPrivatePath", () => {
".hyperframes-pull.json",
"assets/.hyperframes/logo.png",
"assets/logo.png",
"assets/keynote-cover.png",
".config/fonts/brand.woff2",
"fonts/aws-logo.svg",
])("allows %s", (path) => {
expect(isStudioPrivatePath(path)).toBe(false);
});
+51 -2
View File
@@ -134,9 +134,58 @@ const STUDIO_FILES = new Set([
]);
const STUDIO_FOLDERS = [".hyperframes/prepared-assets", ".hyperframes/requests"];
/** Files no composition loads, for a project-relative path in any spelling Windows opens: git, env, app records. */
// Credentials and coding-tool state that projects often sit next to: registry tokens, MCP and agent settings, cloud and
// SSH keys. No composition loads one, so no file server serves one and no upload ships one.
const CREDENTIAL_FILES = new Set([
".npmrc",
".yarnrc",
".yarnrc.yml",
".pypirc",
"pip.conf",
"pip.ini",
".netrc",
"_netrc",
".git-credentials",
".gitconfig",
".mcp.json",
]);
const CREDENTIAL_FOLDERS = new Set([
".ssh",
".aws",
".gnupg",
".docker",
".kube",
".azure",
".claude",
".codex",
".cursor",
".agents",
".gemini",
".vscode",
".idea",
]);
const CREDENTIAL_CONFIG_FOLDERS = new Set(["gcloud", "gh"]);
const KEY_FILE = /^(?:id_(?:rsa|dsa|ecdsa|ed25519).*|.*\.(?:pem|key|p12|pfx))$/;
/** Credential files and tool or agent state, for a project-relative path in any spelling Windows opens. */
export function isCredentialOrToolStatePath(relPath: string): boolean {
const segments = projectPathSegments(relPath);
const name = segments.at(-1) ?? "";
return (
CREDENTIAL_FILES.has(name) ||
KEY_FILE.test(name) ||
segments.some(
(segment, at) =>
CREDENTIAL_FOLDERS.has(segment) ||
(segment === ".config" && CREDENTIAL_CONFIG_FOLDERS.has(segments[at + 1] ?? "")),
)
);
}
/** Files no composition loads, for a project-relative path in any spelling Windows opens: git, env, credentials, tool
* state, app records. */
export function isStudioPrivatePath(relPath: string): boolean {
if (isVcsOrSecretPath(relPath)) return true;
if (isVcsOrSecretPath(relPath) || isCredentialOrToolStatePath(relPath)) return true;
const path = projectPathSegments(relPath).join("/");
return (
(path === ".hyperframes" || path.startsWith(".hyperframes/")) &&
@@ -86,6 +86,33 @@ describe("fileServer private project files", () => {
);
});
describe("fileServer credential files", () => {
const credentials = [
".npmrc",
".mcp.json",
".claude/x.json",
".aws/credentials",
"id_ed25519",
"cert.pem",
];
it.each(credentials.map((file) => `/${file}`))("answers %s with 404", async (requestPath) => {
const projectDir = fs.mkdtempSync(path.join(tmpdir(), "hf-fileserver-credentials-"));
for (const folder of [".claude", ".aws"]) fs.mkdirSync(path.join(projectDir, folder));
for (const file of credentials) fs.writeFileSync(path.join(projectDir, file), "SECRET=1");
fs.writeFileSync(path.join(projectDir, "asset.txt"), "ok");
const server = await createFileServer({ headScripts: [], bodyScripts: [], projectDir });
try {
const response = await fetch(`${server.url}${requestPath}`);
expect(response.status).toBe(404);
expect(await response.text()).not.toContain("SECRET");
expect((await fetch(`${server.url}/asset.txt`)).status).toBe(200);
} finally {
server.close();
fs.rmSync(projectDir, { recursive: true, force: true });
}
});
});
describe("fileServer tool records", () => {
it.each([
".hyperframes/app-history.jsonl",
+3 -2
View File
@@ -17,7 +17,7 @@ import { mkdtempSync, rmSync, statSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Storage } from "@google-cloud/storage";
import { hashProjectDir, listSiteFiles } from "@hyperframes/producer/distributed";
import { hashProjectDir, listSiteFilesForUpload } from "@hyperframes/producer/distributed";
import { formatGcsUri, tarDirectory, uploadFileToGcs } from "../gcsTransport.js";
/** Options for {@link deploySite}. */
@@ -32,6 +32,7 @@ export interface DeploySiteOptions {
* the hash of the project tree picks it.
*/
siteId?: string;
onNotice?: (message: string) => void;
/** Injection seam for tests. Production callers leave unset. */
storage?: Storage;
}
@@ -65,7 +66,7 @@ export async function deploySite(opts: DeploySiteOptions): Promise<SiteHandle> {
throw new Error(`[deploySite] projectDir is not a directory: ${opts.projectDir}`);
}
const files = listSiteFiles(opts.projectDir);
const files = listSiteFilesForUpload(opts.projectDir, opts.onNotice);
const siteId = opts.siteId ?? hashProjectDir(opts.projectDir, files);
const key = `sites/${siteId}/project.tar.gz`;
const projectGcsUri = formatGcsUri({ bucket: opts.bucketName, key });
+14
View File
@@ -1,4 +1,7 @@
import type { Server } from "node:http";
import { realpathSync } from "node:fs";
import { resolve, sep } from "node:path";
import { isPrivateFileUnder, isStudioPrivatePath } from "../../core/src/safePath";
export function closeServer(server: Server): Promise<void> {
const closed = new Promise<void>((done) => server.close(() => done()));
@@ -15,3 +18,14 @@ export function isLoopbackHost(host: string | undefined): boolean {
.toLowerCase();
return name === "127.0.0.1" || name === "localhost" || name === "::1";
}
export function servableProjectFile(project: string, urlPath: string): string | null {
const requested = resolve(project, `.${urlPath}`);
if (isPrivateFileUnder(project, requested, isStudioPrivatePath)) return null;
try {
const file = realpathSync(requested);
return file.startsWith(project + sep) ? file : null;
} catch {
return null;
}
}
+4 -4
View File
@@ -22,14 +22,14 @@ import {
realpathSync,
readdirSync,
} from "node:fs";
import { resolve, join, extname, sep } from "node:path";
import { join, extname } from "node:path";
import { getHyperframeRuntimeScript } from "../../core/src/generated/runtime-inline";
import puppeteer, { type Browser, type Page } from "puppeteer-core";
import {
bundleToSingleHtml,
type BundleScriptProvenance,
} from "../../core/src/compiler/htmlBundler";
import { closeServer, isLoopbackHost } from "./pageServer";
import { closeServer, isLoopbackHost, servableProjectFile } from "./pageServer";
import { Sources, fingerprint } from "./sources";
import type { InspectRequest, Snippet } from "./types";
import { hyperframesCacheDir } from "../../core/src/cacheDir";
@@ -140,8 +140,8 @@ export class Inspector {
res.end(getHyperframeRuntimeScript());
return;
}
const file = realpathSync(resolve(this.project, `.${path}`));
if (!file.startsWith(this.project + sep)) {
const file = servableProjectFile(this.project, path);
if (!file) {
res.writeHead(404).end();
return;
}
+57 -2
View File
@@ -1,7 +1,10 @@
import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { createServer } from "node:http";
import { connect, type AddressInfo, type Socket } from "node:net";
import { expect, it } from "vitest";
import { closeServer } from "../src/pageServer";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterAll, describe, expect, it } from "vitest";
import { closeServer, servableProjectFile } from "../src/pageServer";
it("closes while a client holds a connection it never sent a request on, as Chrome's preconnects do", async () => {
const server = createServer((_req, res) => res.end("ok"));
@@ -18,3 +21,55 @@ it("closes while a client holds a connection it never sent a request on, as Chro
preconnect.destroy();
expect(outcome).toBe("closed");
});
describe("servableProjectFile", () => {
const project = realpathSync(mkdtempSync(join(tmpdir(), "hf-inspector-files-")));
for (const folder of [".git", ".hyperframes", ".claude", "assets"])
mkdirSync(join(project, folder));
for (const file of [
".env",
".env.local",
".git/config",
".hyperframes/agent-handoff.json",
".npmrc",
".mcp.json",
".claude/settings.local.json",
"id_ed25519",
]) {
writeFileSync(join(project, file), "SECRET=1");
}
writeFileSync(join(project, "assets", "logo.png"), "png");
writeFileSync(join(project, ".hyperframes", "studio-motion.json"), "{}");
if (process.platform !== "win32")
symlinkSync(join(project, ".env"), join(project, "assets", "env.txt"));
afterAll(() => rmSync(project, { recursive: true, force: true }));
it.each([
"/.env",
"/.ENV",
"/.env.local",
"/.env.",
"/.git/config",
"/.hyperframes/agent-handoff.json",
"/.npmrc",
"/.mcp.json",
"/.claude/settings.local.json",
"/id_ed25519",
"/../outside.txt",
])("refuses %s", (path) => {
expect(servableProjectFile(project, path)).toBeNull();
});
it.skipIf(process.platform === "win32")("refuses a link to a private file", () => {
expect(servableProjectFile(project, "/assets/env.txt")).toBeNull();
});
it("serves project assets and Studio's own records", () => {
expect(servableProjectFile(project, "/assets/logo.png")).toBe(
join(project, "assets", "logo.png"),
);
expect(servableProjectFile(project, "/.hyperframes/studio-motion.json")).toBe(
join(project, ".hyperframes", "studio-motion.json"),
);
});
});
+6 -1
View File
@@ -134,7 +134,12 @@ export {
validateDistributedRenderConfig,
validateVariablesPayload,
} from "./services/distributed/renderConfigValidation.js";
export { hashProjectDir, listSiteFiles } from "./services/distributed/projectHash.js";
export {
hashProjectDir,
isPrivateSitePath,
listSiteFiles,
listSiteFilesForUpload,
} from "./services/distributed/projectHash.js";
export {
AUDIO_SIDECAR_MIN_BYTES,
buildAudioSidecar,
@@ -3,7 +3,7 @@ import { createHash } from "node:crypto";
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, truncateSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { hashProjectDir, listSiteFiles } from "./projectHash.js";
import { hashProjectDir, listSiteFiles, listSiteFilesForUpload } from "./projectHash.js";
const dirs: string[] = [];
function tempDir(): string {
@@ -63,6 +63,68 @@ describe("listSiteFiles / hashProjectDir", () => {
expect(listSiteFiles(dir)).toEqual(["index.html", "sub/environment.css"]);
});
it("leaves credentials and tool state out at every depth, and names each one", () => {
const dir = project();
for (const folder of [".claude", ".aws", "sub"]) mkdirSync(join(dir, folder));
writeFileSync(join(dir, ".npmrc"), "//registry.npmjs.org/:_authToken=x");
writeFileSync(join(dir, ".mcp.json"), "{}");
writeFileSync(join(dir, ".claude", "settings.local.json"), "{}");
writeFileSync(join(dir, ".aws", "credentials"), "[default]");
writeFileSync(join(dir, "sub", "id_ed25519"), "key");
writeFileSync(join(dir, "sub", "cert.PEM"), "key");
writeFileSync(join(dir, "sub", "logo.png"), "png");
const leftOut: string[] = [];
expect(listSiteFiles(dir, (path) => leftOut.push(path))).toEqual([
"index.html",
"sub/logo.png",
]);
expect(leftOut.sort()).toEqual([
".aws",
".claude",
".mcp.json",
".npmrc",
"sub/cert.PEM",
"sub/id_ed25519",
]);
});
it("names the private files an upload left out in one line, and says nothing when none was", () => {
const notices: string[] = [];
const clean = project();
expect(listSiteFilesForUpload(clean, (line) => notices.push(line))).toEqual(["index.html"]);
expect(notices).toEqual([]);
const dir = project();
mkdirSync(join(dir, ".ssh"));
for (const name of [".npmrc", ".mcp.json", "a.pem", "b.pem", "c.pem", "d.pem"]) {
writeFileSync(join(dir, name), "secret");
}
expect(listSiteFilesForUpload(dir, (line) => notices.push(line))).toEqual(["index.html"]);
expect(notices).toHaveLength(1);
expect(notices[0]).toContain(".mcp.json, .npmrc, .ssh, a.pem, b.pem and 2 more");
});
it("shows control characters in a left-out path as escapes, never raw", () => {
const dir = project();
const folder = "evil\u001b[2J\u009b";
mkdirSync(join(dir, folder));
writeFileSync(join(dir, folder, ".npmrc"), "secret");
const notices: string[] = [];
expect(listSiteFilesForUpload(dir, (line) => notices.push(line))).toEqual(["index.html"]);
expect(notices[0]).toContain("evil\\x1b[2J\\x9b/.npmrc");
expect(notices[0]).not.toContain("\u001b");
expect(notices[0]).not.toContain("\u009b");
});
it("leaves out a file whose link points at a credential", () => {
const dir = project();
const home = tempDir();
mkdirSync(join(dir, "assets"));
mkdirSync(join(home, ".ssh"));
writeFileSync(join(home, ".ssh", "config"), "Host x");
symlinkSync(join(home, ".ssh"), join(dir, "assets", "keys"));
expect(listSiteFiles(dir)).toEqual(["index.html"]);
});
it("does not loop on a folder that links back to an ancestor", () => {
const dir = project();
symlinkSync(dir, join(dir, "self"));
@@ -12,54 +12,81 @@
import { closeSync, openSync, readdirSync, readSync, realpathSync, statSync } from "node:fs";
import { createHash } from "node:crypto";
import { join } from "node:path";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
import { PLAN_PROJECT_DIR_SKIP_SEGMENTS } from "./plan.js";
// Bump when the hashed set changes so uploads cached under an older scheme are never reused.
const SITE_ID_SCHEME = "site-v2\0";
const READ_CHUNK_BYTES = 256 * 1024;
// Env files hold the author's keys; no composition loads one, so none is uploaded.
const ENV_FILE = /^\.env([._-].*|rc)?$/i;
const leftOut = (rel: string, name: string): boolean =>
(rel === "" && PLAN_PROJECT_DIR_SKIP_SEGMENTS.has(name)) || ENV_FILE.test(name);
/** Whether a site's `/`-separated project path is one no upload ships: env, Git, credentials, tool state, app records. */
export const isPrivateSitePath = (relPath: string): boolean => isStudioPrivatePath(relPath);
function statEntry(full: string, relPath: string) {
try {
return statSync(full);
} catch (err) {
throw new Error(`[listSiteFiles] cannot read ${relPath} (dangling symlink?)`, { cause: err });
}
}
/**
* The files a site upload ships, as `/`-separated paths under `projectDir`. Symlinks are followed
* (root included), top-level {@link PLAN_PROJECT_DIR_SKIP_SEGMENTS} are left out as the plan stage
* does, env files are left out at every depth, and a dangling symlink throws.
* does, and a dangling symlink throws. Private files (env, Git, credentials, tool and agent state,
* app records) are left out at every depth, by name or by where a link points; `onPrivate` hears
* each one left out, a folder once.
*/
export function listSiteFiles(projectDir: string): string[] {
export function listSiteFiles(projectDir: string, onPrivate?: (relPath: string) => void): string[] {
const files: string[] = [];
const ancestors = new Set([realpathSync(projectDir)]);
function visit(full: string, relPath: string): void {
if (isPrivateFileUnder(projectDir, full, isStudioPrivatePath)) return void onPrivate?.(relPath);
const stat = statEntry(full, relPath);
if (stat.isFile()) return void files.push(relPath);
if (!stat.isDirectory()) return;
const real = realpathSync(full);
// A folder linking back to an ancestor is skipped rather than walked forever.
if (ancestors.has(real)) return;
ancestors.add(real);
walk(full, relPath);
ancestors.delete(real);
}
function walk(dir: string, rel: string): void {
for (const name of readdirSync(dir).sort()) {
if (leftOut(rel, name)) continue;
const full = join(dir, name);
const relPath = rel === "" ? name : `${rel}/${name}`;
let stat;
try {
stat = statSync(full);
} catch (err) {
throw new Error(`[listSiteFiles] cannot read ${relPath} (dangling symlink?)`, {
cause: err,
});
}
if (stat.isFile()) {
files.push(relPath);
} else if (stat.isDirectory()) {
const real = realpathSync(full);
// A folder linking back to an ancestor is skipped rather than walked forever.
if (ancestors.has(real)) continue;
ancestors.add(real);
walk(full, relPath);
ancestors.delete(real);
}
if (rel === "" && PLAN_PROJECT_DIR_SKIP_SEGMENTS.has(name)) continue;
visit(join(dir, name), rel === "" ? name : `${rel}/${name}`);
}
}
walk(projectDir, "");
return files;
}
const printablePath = (path: string): string =>
[...path]
.map((ch) => {
const code = ch.charCodeAt(0);
const control = (code < 0x20 && code !== 0x09) || (code >= 0x7f && code <= 0x9f);
return control ? `\\x${code.toString(16).padStart(2, "0")}` : ch;
})
.join("");
/** {@link listSiteFiles} for an upload: one line to `onNotice` names the private files left out. */
export function listSiteFilesForUpload(
projectDir: string,
onNotice?: (message: string) => void,
): string[] {
const leftOut: string[] = [];
const files = listSiteFiles(projectDir, (path) => leftOut.push(path));
if (leftOut.length > 0) {
const more = leftOut.length > 5 ? ` and ${leftOut.length - 5} more` : "";
onNotice?.(
`[deploySite] left out private files no composition loads (env, Git, credentials, tool state): ${leftOut.slice(0, 5).map(printablePath).join(", ")}${more}`,
);
}
return files;
}
/**
* SHA-256 over a scheme tag plus each of {@link listSiteFiles}'s paths and
* bytes → 16-character hex prefix. The prefix is the `siteId`.
@@ -1192,6 +1192,37 @@ describe("createFileServer private project files", () => {
);
});
describe("createFileServer credential files", () => {
const credentials = [
".npmrc",
".mcp.json",
".claude/x.json",
".aws/credentials",
"id_ed25519",
"cert.pem",
];
it.each(credentials.map((file) => `/${file}`))(
"answers %s with 404 and keeps ordinary assets",
async (requestPath) => {
const projectDir = mkdtempSync(join(tmpdir(), "hf-fs-credentials-"));
try {
writeEmptyIndex(projectDir);
for (const folder of [".claude", ".aws"]) mkdirSync(join(projectDir, folder));
for (const file of credentials) writeFileSync(join(projectDir, file), "SECRET=1");
writeFileSync(join(projectDir, "asset.txt"), "ok");
await withFileServer(projectDir, async (server) => {
const response = await fetch(`${server.url}${requestPath}`);
expect(response.status).toBe(404);
expect(await response.text()).not.toContain("SECRET");
expect((await fetch(`${server.url}/asset.txt`)).status).toBe(200);
});
} finally {
rmSync(projectDir, { recursive: true, force: true });
}
},
);
});
function getWithHost(url: string, host: string): Promise<{ status: number; body: string }> {
return new Promise((resolveGet, rejectGet) => {
httpGet(url, { headers: { host } }, (res) => {
@@ -743,6 +743,26 @@ describe("the desktop app's private files", () => {
expect(await viaLink.text()).not.toContain("secret");
});
it("include credentials and tool state, which the file routes never serve", async () => {
const { app, project } = fixture();
const credentials = [
".npmrc",
".mcp.json",
".claude/x.json",
".aws/credentials",
"id_ed25519",
"cert.pem",
];
for (const folder of [".claude", ".aws"]) mkdirSync(join(project, folder));
for (const file of credentials) writeFileSync(join(project, file), "secret");
for (const path of credentials) {
const res = await app.request(`/projects/p/files/${path}`);
expect([403, 404]).toContain(res.status);
expect(await res.text()).not.toContain("secret");
}
expect((await app.request("/projects/p/files/inside.txt")).status).toBe(200);
});
it("can't be reached by renaming their folder", async () => {
const { app, project } = fixture();
mkdirSync(join(project, ".hyperframes"));
@@ -3142,6 +3142,29 @@ describe("the desktop app's private folder", () => {
expect(await res.text()).not.toContain("secret");
}
});
it("is joined by credentials and tool state, which neither preview route serves", async () => {
const projectDir = createProjectDir();
const credentials = [
".npmrc",
".mcp.json",
".claude/x.json",
".aws/credentials",
"id_ed25519",
"cert.pem",
];
for (const folder of [".claude", ".aws"])
mkdirSync(join(projectDir, folder), { recursive: true });
for (const file of credentials) writeFileSync(join(projectDir, file), "secret");
const app = new Hono();
registerPreviewRoutes(app, createAdapter(projectDir));
for (const route of ["preview", "preview/comp"]) {
for (const file of credentials) {
const res = await app.request(`http://localhost/projects/demo/${route}/${file}`);
expect(res.status).toBe(404);
expect(await res.text()).not.toContain("secret");
}
}
});
it("still serves the app's request pictures", async () => {
const projectDir = createProjectDir();
mkdirSync(join(projectDir, ".hyperframes", "requests", "1"), { recursive: true });