From 014326b40bf22bebb77a5cf08a50d5b0968c112e Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Mon, 16 Nov 2026 01:42:08 +0000 Subject: [PATCH] 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 --- packages/aws-lambda/src/sdk/deploySite.ts | 9 ++- .../aws-lambda/src/sdk/renderToLambda.test.ts | 27 ++++++- packages/cli/src/capture/localPage.test.ts | 23 ++++++ packages/cli/src/capture/localPage.ts | 4 +- packages/cli/src/commands/cloudrun.ts | 2 + .../cli/src/utils/privateProjectFile.test.ts | 2 +- packages/cli/src/utils/privateProjectFile.ts | 47 +---------- packages/cli/src/utils/publishProject.ts | 4 +- .../cli/src/utils/staticProjectServer.test.ts | 22 +++++ packages/core/src/safePath.test.ts | 17 ++++ packages/core/src/safePath.ts | 53 +++++++++++- .../services/fileServer.pathSafety.test.ts | 27 +++++++ packages/gcp-cloud-run/src/sdk/deploySite.ts | 5 +- packages/inspector/src/pageServer.ts | 14 ++++ packages/inspector/src/service.ts | 8 +- packages/inspector/tests/pageServer.test.ts | 59 +++++++++++++- packages/producer/src/distributed.ts | 7 +- .../services/distributed/projectHash.test.ts | 64 ++++++++++++++- .../src/services/distributed/projectHash.ts | 81 ++++++++++++------- .../producer/src/services/fileServer.test.ts | 31 +++++++ .../src/routes/files.pathSafety.test.ts | 20 +++++ .../studio-server/src/routes/preview.test.ts | 23 ++++++ 22 files changed, 456 insertions(+), 93 deletions(-) diff --git a/packages/aws-lambda/src/sdk/deploySite.ts b/packages/aws-lambda/src/sdk/deploySite.ts index a947ecd51b..812db12148 100644 --- a/packages/aws-lambda/src/sdk/deploySite.ts +++ b/packages/aws-lambda/src/sdk/deploySite.ts @@ -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 { 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); diff --git a/packages/aws-lambda/src/sdk/renderToLambda.test.ts b/packages/aws-lambda/src/sdk/renderToLambda.test.ts index 1a18b21181..34058738a0 100644 --- a/packages/aws-lambda/src/sdk/renderToLambda.test.ts +++ b/packages/aws-lambda/src/sdk/renderToLambda.test.ts @@ -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(); diff --git a/packages/cli/src/capture/localPage.test.ts b/packages/cli/src/capture/localPage.test.ts index e75970ec3d..74ffe2cbe4 100644 --- a/packages/cli/src/capture/localPage.test.ts +++ b/packages/cli/src/capture/localPage.test.ts @@ -24,6 +24,11 @@ beforeAll(() => { writeFileSync(join(site, "index.html"), "

home

"); writeFileSync(join(site, "pages", "about page.html"), "

about

"); 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"), "

outside

"); 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); }); diff --git a/packages/cli/src/capture/localPage.ts b/packages/cli/src/capture/localPage.ts index 22f3732f41..571c7b7009 100644 --- a/packages/cli/src/capture/localPage.ts +++ b/packages/cli/src/capture/localPage.ts @@ -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 { const sockets = new Set(); const server = createServer((request, response) => { diff --git a/packages/cli/src/commands/cloudrun.ts b/packages/cli/src/commands/cloudrun.ts index 404f45c436..0650d291fa 100644 --- a/packages/cli/src/commands/cloudrun.ts +++ b/packages/cli/src/commands/cloudrun.ts @@ -512,6 +512,7 @@ async function runSites(args: Record): Promise { 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): Promise { 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 }> = []; diff --git a/packages/cli/src/utils/privateProjectFile.test.ts b/packages/cli/src/utils/privateProjectFile.test.ts index a0c7785c2a..a1dc7f127d 100644 --- a/packages/cli/src/utils/privateProjectFile.test.ts +++ b/packages/cli/src/utils/privateProjectFile.test.ts @@ -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, diff --git a/packages/cli/src/utils/privateProjectFile.ts b/packages/cli/src/utils/privateProjectFile.ts index b99e2feab2..aacc8c8d74 100644 --- a/packages/cli/src/utils/privateProjectFile.ts +++ b/packages/cli/src/utils/privateProjectFile.ts @@ -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] === ".."; diff --git a/packages/cli/src/utils/publishProject.ts b/packages/cli/src/utils/publishProject.ts index 956e8b5b5d..e4119ecd7a 100644 --- a/packages/cli/src/utils/publishProject.ts +++ b/packages/cli/src/utils/publishProject.ts @@ -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; } diff --git a/packages/cli/src/utils/staticProjectServer.test.ts b/packages/cli/src/utils/staticProjectServer.test.ts index dd15a9e5dd..4e07faba34 100644 --- a/packages/cli/src/utils/staticProjectServer.test.ts +++ b/packages/cli/src/utils/staticProjectServer.test.ts @@ -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, ""); + + 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 = ''; const SANDBOX = "sandbox allow-scripts"; diff --git a/packages/core/src/safePath.test.ts b/packages/core/src/safePath.test.ts index 7439cb0ff0..8bb81016b0 100644 --- a/packages/core/src/safePath.test.ts +++ b/packages/core/src/safePath.test.ts @@ -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); }); diff --git a/packages/core/src/safePath.ts b/packages/core/src/safePath.ts index 728d05433d..64653ada55 100644 --- a/packages/core/src/safePath.ts +++ b/packages/core/src/safePath.ts @@ -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/")) && diff --git a/packages/engine/src/services/fileServer.pathSafety.test.ts b/packages/engine/src/services/fileServer.pathSafety.test.ts index a74f42766a..fab5cc0c30 100644 --- a/packages/engine/src/services/fileServer.pathSafety.test.ts +++ b/packages/engine/src/services/fileServer.pathSafety.test.ts @@ -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", diff --git a/packages/gcp-cloud-run/src/sdk/deploySite.ts b/packages/gcp-cloud-run/src/sdk/deploySite.ts index 75e0aab3f3..7bf63cca76 100644 --- a/packages/gcp-cloud-run/src/sdk/deploySite.ts +++ b/packages/gcp-cloud-run/src/sdk/deploySite.ts @@ -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 { 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 }); diff --git a/packages/inspector/src/pageServer.ts b/packages/inspector/src/pageServer.ts index bb0b5b736c..0455808fb1 100644 --- a/packages/inspector/src/pageServer.ts +++ b/packages/inspector/src/pageServer.ts @@ -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 { const closed = new Promise((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; + } +} diff --git a/packages/inspector/src/service.ts b/packages/inspector/src/service.ts index 4adedb1bce..0b90b41818 100644 --- a/packages/inspector/src/service.ts +++ b/packages/inspector/src/service.ts @@ -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; } diff --git a/packages/inspector/tests/pageServer.test.ts b/packages/inspector/tests/pageServer.test.ts index fcd8fa0f61..d166049d9e 100644 --- a/packages/inspector/tests/pageServer.test.ts +++ b/packages/inspector/tests/pageServer.test.ts @@ -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"), + ); + }); +}); diff --git a/packages/producer/src/distributed.ts b/packages/producer/src/distributed.ts index c1129ac3b8..2045a6ae0d 100644 --- a/packages/producer/src/distributed.ts +++ b/packages/producer/src/distributed.ts @@ -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, diff --git a/packages/producer/src/services/distributed/projectHash.test.ts b/packages/producer/src/services/distributed/projectHash.test.ts index c93c41cf6a..5b32123baa 100644 --- a/packages/producer/src/services/distributed/projectHash.test.ts +++ b/packages/producer/src/services/distributed/projectHash.test.ts @@ -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")); diff --git a/packages/producer/src/services/distributed/projectHash.ts b/packages/producer/src/services/distributed/projectHash.ts index 2dc140bf12..091b8b4807 100644 --- a/packages/producer/src/services/distributed/projectHash.ts +++ b/packages/producer/src/services/distributed/projectHash.ts @@ -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`. diff --git a/packages/producer/src/services/fileServer.test.ts b/packages/producer/src/services/fileServer.test.ts index 3db0126d76..747b972658 100644 --- a/packages/producer/src/services/fileServer.test.ts +++ b/packages/producer/src/services/fileServer.test.ts @@ -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) => { diff --git a/packages/studio-server/src/routes/files.pathSafety.test.ts b/packages/studio-server/src/routes/files.pathSafety.test.ts index e0aa597cd2..219bc9e9a6 100644 --- a/packages/studio-server/src/routes/files.pathSafety.test.ts +++ b/packages/studio-server/src/routes/files.pathSafety.test.ts @@ -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")); diff --git a/packages/studio-server/src/routes/preview.test.ts b/packages/studio-server/src/routes/preview.test.ts index 987e864112..f6092de5e9 100644 --- a/packages/studio-server/src/routes/preview.test.ts +++ b/packages/studio-server/src/routes/preview.test.ts @@ -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 });