diff --git a/packages/cli/src/commands/compositionRoute.encoding.test.ts b/packages/cli/src/commands/compositionRoute.encoding.test.ts index 3c9eb5c081..3234bc04a0 100644 --- a/packages/cli/src/commands/compositionRoute.encoding.test.ts +++ b/packages/cli/src/commands/compositionRoute.encoding.test.ts @@ -50,4 +50,21 @@ describe.each(ROUTES)("%s /composition/*", (_route, register) => { expect(result.status).toBe(200); expect(result.text).toBe(`ASSET ${name}`); }); + + it("does not serve env files, Git data or the tools' own records", async () => { + dir = mkdtempSync(join(tmpdir(), "hf-composition-route-")); + mkdirSync(join(dir, ".git")); + mkdirSync(join(dir, ".hyperframes")); + for (const name of [".env", ".envrc", ".env-local", ".git/config", ".hyperframes/x.json"]) { + writeFileSync(join(dir, name), "KEY=secret"); + } + writeFileSync(join(dir, "logo.png"), "png"); + const app = new Hono(); + await register(app, { dir, name: "test-project", indexPath: join(dir, "index.html") }); + + for (const path of [".env", ".envrc", ".env-local", ".git/config", ".hyperframes/x.json"]) { + expect((await app.request(`/composition/${path}`)).status).toBe(404); + } + expect((await app.request("/composition/logo.png")).status).toBe(200); + }); }); diff --git a/packages/cli/src/commands/lambda/deploy.test.ts b/packages/cli/src/commands/lambda/deploy.test.ts index 00a053df38..35b0148060 100644 --- a/packages/cli/src/commands/lambda/deploy.test.ts +++ b/packages/cli/src/commands/lambda/deploy.test.ts @@ -20,6 +20,7 @@ vi.mock("./state.js", () => ({ })); vi.mock("./videoImage.js", () => ({ pushVideoImage: vi.fn(async () => ({ imageUri: "repo@sha256:a", repositoryName: "hf-video" })), + videoRepositoryName: (stackName: string) => `${stackName}-video`, })); const { runDeploy } = await import("./deploy.js"); diff --git a/packages/cli/src/commands/lambda/deploy.ts b/packages/cli/src/commands/lambda/deploy.ts index f3c3171cc6..a3ad9b8496 100644 --- a/packages/cli/src/commands/lambda/deploy.ts +++ b/packages/cli/src/commands/lambda/deploy.ts @@ -16,7 +16,7 @@ import { stagePrebuiltHandler } from "./prebuiltHandler.js"; import { fetchStackOutputs, locateSamTemplate, samDeploy } from "./sam.js"; import { repoRoot } from "./repoRoot.js"; import { DEFAULT_STACK_NAME, readStackOutputs, writeStackOutputs } from "./state.js"; -import { pushVideoImage, type VideoArch } from "./videoImage.js"; +import { pushVideoImage, videoRepositoryName, type VideoArch } from "./videoImage.js"; import { warmFunction } from "./warm.js"; export interface DeployArgs { @@ -95,7 +95,11 @@ export async function runDeploy(args: Partial = {}): Promise { reservedConcurrency: outputs.reservedConcurrency, videoFunctionName: outputs.videoFunctionName, // Back on x86 the repository outlives the video function, so destroy still needs its name. - videoRepository: video?.repositoryName ?? readStackOutputs(resolved.stackName)?.videoRepository, + videoRepository: + video?.repositoryName ?? + (readStackOutputs(resolved.stackName)?.videoRepository + ? videoRepositoryName(resolved.stackName) + : undefined), deployedAt: new Date().toISOString(), }); diff --git a/packages/cli/src/commands/lambda/destroy.test.ts b/packages/cli/src/commands/lambda/destroy.test.ts new file mode 100644 index 0000000000..75ac9f2c5f --- /dev/null +++ b/packages/cli/src/commands/lambda/destroy.test.ts @@ -0,0 +1,70 @@ +import { existsSync, mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { stateFilePath, writeStackOutputs } from "./state.js"; + +const samDirs = vi.hoisted(() => [] as string[]); +vi.mock("./repoRoot.js", () => ({ repoRoot: () => undefined })); +vi.mock("./sam.js", () => ({ + samDelete: vi.fn((opts: { samDir: string }) => { + samDirs.push(opts.samDir); + }), + deleteEcrRepository: vi.fn(), +})); + +const { runDestroy } = await import("./destroy.js"); +const { deleteEcrRepository, samDelete } = await import("./sam.js"); + +let cwd: string; +let previousCwd: string; +let quiet: { mockRestore(): void }; + +beforeEach(() => { + cwd = mkdtempSync(join(tmpdir(), "hf-lambda-destroy-")); + previousCwd = process.cwd(); + process.chdir(cwd); + samDirs.length = 0; + vi.mocked(samDelete).mockClear(); + vi.mocked(deleteEcrRepository).mockClear(); + quiet = vi.spyOn(console, "log").mockImplementation(() => {}); +}); + +afterEach(() => { + quiet.mockRestore(); + process.chdir(previousCwd); + rmSync(cwd, { recursive: true, force: true }); +}); + +const stack = { + stackName: "Demo", + region: "us-east-1", + bucketName: "b", + stateMachineArn: "s", + functionName: "f", + lambdaMemoryMb: 10240, + deployedAt: "2026-01-01T00:00:00Z", +}; + +describe("runDestroy", () => { + it("runs sam delete outside a checkout in a fresh folder it removes afterwards", async () => { + writeStackOutputs(stack, cwd); + + await runDestroy({ stackName: "Demo" }); + + expect(samDirs).toHaveLength(1); + expect(samDirs[0]).not.toBe(tmpdir()); + expect(existsSync(samDirs[0]!)).toBe(false); + expect(existsSync(stateFilePath("Demo", cwd))).toBe(false); + }); + + it("deletes the stack's own video repository, whatever name the state file records", async () => { + writeStackOutputs({ ...stack, videoRepository: "someone-elses-repo" }, cwd); + + await runDestroy({ stackName: "Demo" }); + + expect(deleteEcrRepository).toHaveBeenCalledWith( + expect.objectContaining({ repositoryName: "demo-video" }), + ); + }); +}); diff --git a/packages/cli/src/commands/lambda/destroy.ts b/packages/cli/src/commands/lambda/destroy.ts index fb4c74c32f..f2d893e28f 100644 --- a/packages/cli/src/commands/lambda/destroy.ts +++ b/packages/cli/src/commands/lambda/destroy.ts @@ -9,12 +9,14 @@ * rather than re-implementing the empty-and-delete dance here. */ +import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { c } from "../../ui/colors.js"; import { deleteStackOutputs, requireStack } from "./state.js"; import { deleteEcrRepository, samDelete } from "./sam.js"; import { repoRoot } from "./repoRoot.js"; +import { videoRepositoryName } from "./videoImage.js"; export interface DestroyArgs { stackName: string; @@ -29,19 +31,18 @@ export async function runDestroy(args: DestroyArgs): Promise { // Mirror deploy.ts's AWS_PROFILE env fallback — `AWS_PROFILE=prod // hyperframes lambda destroy` should hit the same account `deploy` // did, not the default credentials chain. - samDelete({ - samDir: root ? join(root, "examples", "aws-lambda") : tmpdir(), - stackName: stack.stackName, - region: stack.region, - awsProfile, - }); + const samDir = root + ? join(root, "examples", "aws-lambda") + : mkdtempSync(join(tmpdir(), "hf-sam-")); + try { + samDelete({ samDir, stackName: stack.stackName, region: stack.region, awsProfile }); + } finally { + if (!root) rmSync(samDir, { recursive: true, force: true }); + } if (stack.videoRepository) { - console.log(c.dim(`→ Deleting ECR repository ${stack.videoRepository}`)); - deleteEcrRepository({ - repositoryName: stack.videoRepository, - region: stack.region, - awsProfile, - }); + const repositoryName = videoRepositoryName(stack.stackName); + console.log(c.dim(`→ Deleting ECR repository ${repositoryName}`)); + deleteEcrRepository({ repositoryName, region: stack.region, awsProfile }); } deleteStackOutputs(args.stackName); console.log(); diff --git a/packages/cli/src/commands/lambda/state.test.ts b/packages/cli/src/commands/lambda/state.test.ts index 08792ac9fc..151e6e757a 100644 --- a/packages/cli/src/commands/lambda/state.test.ts +++ b/packages/cli/src/commands/lambda/state.test.ts @@ -1,11 +1,12 @@ -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { deleteStackOutputs, listStackNames, readStackOutputs, + requireStack, stateFilePath, type StackOutputs, writeStackOutputs, @@ -64,4 +65,20 @@ describe("lambda state file", () => { writeFileSync(path, "{ not json"); expect(readStackOutputs("bad", workdir)).toBeNull(); }); + + it("requireStack refuses a file that names a different stack", () => { + writeStackOutputs({ ...sample, stackName: "other" }, workdir); + const path = stateFilePath("other", workdir); + mkdirSync(dirname(stateFilePath("test", workdir)), { recursive: true }); + writeFileSync(stateFilePath("test", workdir), readFileSync(path)); + const quiet = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + expect(() => requireStack("test", workdir)).toThrow(); + expect(requireStack("other", workdir).stackName).toBe("other"); + writeFileSync(stateFilePath("OTHER", workdir), readFileSync(path)); + expect(requireStack("OTHER", workdir).stackName).toBe("other"); + } finally { + quiet.mockRestore(); + } + }); }); diff --git a/packages/cli/src/commands/lambda/state.ts b/packages/cli/src/commands/lambda/state.ts index b4b20be5bd..916d537ec6 100644 --- a/packages/cli/src/commands/lambda/state.ts +++ b/packages/cli/src/commands/lambda/state.ts @@ -85,6 +85,9 @@ export function listStackNames(cwd: string = process.cwd()): string[] { .map((f) => f.slice(STATE_FILE_PREFIX.length, -".json".length)); } +const sameFileOnCaseInsensitiveDisk = (a: string, b: string): boolean => + a.toLowerCase() === b.toLowerCase(); + /** * Read stack outputs or print a helpful error and exit. Shared between * `render`, `progress`, and `destroy` so the "did you run `deploy` first?" @@ -92,14 +95,19 @@ export function listStackNames(cwd: string = process.cwd()): string[] { */ export function requireStack(stackName: string, cwd: string = process.cwd()): StackOutputs { const stack = readStackOutputs(stackName, cwd); + const path = stateFilePath(stackName, cwd); + const deploy = `Run \`hyperframes lambda deploy${stackName === DEFAULT_STACK_NAME ? "" : ` --stack-name=${stackName}`}\``; if (!stack) { const known = listStackNames(cwd); - let hint = `Run \`hyperframes lambda deploy${stackName === DEFAULT_STACK_NAME ? "" : ` --stack-name=${stackName}`}\` first.`; - if (known.length) { - hint += ` Known stacks here: ${known.join(", ")}.`; - } + const hint = known.length ? ` Known stacks here: ${known.join(", ")}.` : ""; console.error( - `[hyperframes lambda] no stack state for "${stackName}" at ${stateFilePath(stackName, cwd)}. ${hint}`, + `[hyperframes lambda] no stack state for "${stackName}" at ${path}. ${deploy} first.${hint}`, + ); + failCommand(); + } + if (!sameFileOnCaseInsensitiveDisk(String(stack.stackName), stackName)) { + console.error( + `[hyperframes lambda] ${path} names stack "${stack.stackName}", not "${stackName}". ${deploy} to rewrite it.`, ); failCommand(); } diff --git a/packages/cli/src/commands/lambda/videoImage.ts b/packages/cli/src/commands/lambda/videoImage.ts index a337fda99f..def5ed18b3 100644 --- a/packages/cli/src/commands/lambda/videoImage.ts +++ b/packages/cli/src/commands/lambda/videoImage.ts @@ -10,7 +10,7 @@ import { defaultDeps, fetchPinned, type StageDeps } from "./prebuiltHandler.js"; export type VideoArch = "x86_64" | "arm64"; /** ECR repository names must be lowercase; stack names need not be. */ -function videoRepositoryName(stackName: string): string { +export function videoRepositoryName(stackName: string): string { return `${stackName.toLowerCase()}-video`; } diff --git a/packages/cli/src/commands/play.test.ts b/packages/cli/src/commands/play.test.ts index 62731d0103..0829bbc85b 100644 --- a/packages/cli/src/commands/play.test.ts +++ b/packages/cli/src/commands/play.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { Hono } from "hono"; @@ -153,6 +153,24 @@ async function buildApp(project: ProjectDir, autoProxy: boolean): Promise } describe("registerCompositionRoute", () => { + it("does not serve env files, Git data or the tools' own records", async () => { + const project = tmpProject(); + mkdirSync(join(project.dir, ".git")); + mkdirSync(join(project.dir, ".hyperframes")); + writeFileSync(join(project.dir, ".env"), "KEY=secret"); + writeFileSync(join(project.dir, ".env.local"), "KEY=secret"); + writeFileSync(join(project.dir, ".git", "config"), "[core]"); + writeFileSync(join(project.dir, ".hyperframes", "lambda-stack-x.json"), "{}"); + writeFileSync(join(project.dir, "logo.png"), "png"); + const app = await buildApp(project, false); + + for (const path of [".env", ".ENV", ".env.local", ".git/config"]) { + expect((await app.request(`/composition/${path}`)).status).toBe(404); + } + expect((await app.request("/composition/.hyperframes/lambda-stack-x.json")).status).toBe(404); + expect((await app.request("/composition/logo.png")).status).toBe(200); + }); + it("lets a browser revalidate the composition and its assets instead of refetching them", async () => { const project = tmpProject(); writeFileSync(join(project.dir, "index.html"), ""); diff --git a/packages/cli/src/commands/play.ts b/packages/cli/src/commands/play.ts index 19045347a5..37115d6204 100644 --- a/packages/cli/src/commands/play.ts +++ b/packages/cli/src/commands/play.ts @@ -42,6 +42,7 @@ import { revalidatedResponse, assetContentType, } from "../utils/compositionServer.js"; +import { isPrivateCompositionFile } from "../utils/privateProjectFile.js"; import { resolveProxy, ProxyCapacityError, @@ -230,7 +231,9 @@ export async function registerCompositionRoute( // an in-project symlink to an external target nor a sibling dir whose name // shares the project-dir prefix (e.g. `-evil`) can escape. if (!isSafePath(project.dir, filePath)) return ctx.text("Forbidden", 403); - if (!existsSync(filePath)) return ctx.text("Not found", 404); + if (isPrivateCompositionFile(project.dir, filePath) || !existsSync(filePath)) { + return ctx.text("Not found", 404); + } // HTML gets the runtime + codec-map injected; other assets pass through. if (filePath.endsWith(".html")) { diff --git a/packages/cli/src/commands/present.ts b/packages/cli/src/commands/present.ts index 853b945f1e..684ad4b921 100644 --- a/packages/cli/src/commands/present.ts +++ b/packages/cli/src/commands/present.ts @@ -29,6 +29,7 @@ import { listenOnFreePort, assetContentType, } from "../utils/compositionServer.js"; +import { isPrivateCompositionFile } from "../utils/privateProjectFile.js"; export default defineCommand({ meta: { @@ -318,7 +319,9 @@ export async function registerPresentCompositionRoute( // Security: canonicalizes symlinks + guards the trailing separator so neither // an in-project symlink nor a sibling dir sharing the prefix can escape. if (!isSafePath(project.dir, filePath)) return ctx.text("Forbidden", 403); - if (!existsSync(filePath)) return ctx.text("Not found", 404); + if (isPrivateCompositionFile(project.dir, filePath) || !existsSync(filePath)) { + return ctx.text("Not found", 404); + } if (filePath.endsWith(".html")) return ctx.html(readFileSync(filePath, "utf-8")); return ctx.body(readFileSync(filePath), 200, { "Content-Type": assetContentType(filePath) }); }); diff --git a/packages/cli/src/commands/snapshot.remoteVideo.test.ts b/packages/cli/src/commands/snapshot.remoteVideo.test.ts index 6b7f12b5cd..1a0702b18a 100644 --- a/packages/cli/src/commands/snapshot.remoteVideo.test.ts +++ b/packages/cli/src/commands/snapshot.remoteVideo.test.ts @@ -1,5 +1,8 @@ -import { describe, expect, it, vi } from "vitest"; -import { remoteVideoInput } from "./snapshot.js"; +import { chmodSync, mkdirSync, mkdtempSync, rmSync, statSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { privateMediaDir, remoteVideoInput } from "./snapshot.js"; describe("remoteVideoInput", () => { const url = "https://cdn.example.com/clip.mp4"; @@ -22,12 +25,64 @@ describe("remoteVideoInput", () => { expect(download.mock.calls[0]).toEqual([url, expect.any(String), 30_000]); }); - it("keeps the URL when the clip cannot be downloaded", async () => { + it("gives FFmpeg nothing when the clip cannot be downloaded, so FFmpeg never follows the host's redirects", async () => { const download = vi.fn( async (_url: string, _dir: string, _timeoutMs?: number): Promise => - Promise.reject(new Error("not a public https URL")), + Promise.reject(new Error("socket hang up")), ); - await expect(remoteVideoInput(url, download)).resolves.toBe(url); + await expect(remoteVideoInput(url, download)).resolves.toBeNull(); + }); + + it.each([ + "http://cdn.example.com/clip.mp4", + "https://127.0.0.1:8080/clip.mp4", + "https://169.254.169.254/latest", + "https://10.0.0.5/clip.mp4", + ])("gives FFmpeg nothing for %s, which the download policy refuses", async (refused) => { + const download = vi.fn(async (): Promise => "/cache/clip.mp4"); + + await expect(remoteVideoInput(refused, download)).resolves.toBeNull(); + expect(download).not.toHaveBeenCalled(); + }); + + it("gives FFmpeg nothing when the download is refused, as for a host resolving to a private address", async () => { + const refusal = Object.assign(new Error("Download URL is not permitted"), { + kind: "http_rejected", + }); + const download = vi.fn(async (): Promise => Promise.reject(refusal)); + + await expect(remoteVideoInput(url, download)).resolves.toBeNull(); }); }); + +describe("privateMediaDir", () => { + let parent: string | undefined; + afterEach(() => { + if (parent) rmSync(parent, { recursive: true, force: true }); + parent = undefined; + }); + + it("keeps clips in one folder per user that only that user can open", () => { + parent = mkdtempSync(join(tmpdir(), "hf-snapshot-media-")); + const dir = privateMediaDir(parent); + + expect(privateMediaDir(parent)).toBe(dir); + if (process.platform !== "win32") expect(statSync(dir).mode & 0o077).toBe(0); + }); + + it.skipIf(process.platform === "win32")( + "uses a fresh folder when the shared one is open to other users", + () => { + parent = mkdtempSync(join(tmpdir(), "hf-snapshot-media-")); + const shared = join(parent, `hyperframes-snapshot-media-${process.getuid?.()}`); + mkdirSync(shared); + chmodSync(shared, 0o777); + + const dir = privateMediaDir(parent); + + expect(dir).not.toBe(shared); + expect(statSync(dir).mode & 0o077).toBe(0); + }, + ); +}); diff --git a/packages/cli/src/commands/snapshot.ts b/packages/cli/src/commands/snapshot.ts index 548425500a..c493892a3e 100644 --- a/packages/cli/src/commands/snapshot.ts +++ b/packages/cli/src/commands/snapshot.ts @@ -1,5 +1,6 @@ import { DEFAULT_CONFIG, + assertPublicHttpsUrl, buildZeroDurationDiagnostic, downloadToTemp, evaluateHfDiagnostic, @@ -44,6 +45,7 @@ import { import { defineCommand } from "../utils/defineCommand.js"; import { existsSync, + lstatSync, mkdtempSync, readdirSync, readFileSync, @@ -53,6 +55,7 @@ import { writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; +import { createStagingDir } from "@hyperframes/core/atomic-file"; import { sumOf } from "../measure/media.js"; import { resolve, join, relative, isAbsolute, basename, posix } from "node:path"; import { @@ -420,14 +423,38 @@ const processStderr = (error: unknown) => ? error.stderr : ""; -const REMOTE_VIDEO_DIR = join(tmpdir(), "hyperframes-snapshot-media"); +/** `parent`'s folder for remote clips kept across runs, or a fresh one when that folder is not this user's alone. */ +export function privateMediaDir(parent: string): string { + const dir = join(parent, `hyperframes-snapshot-media-${process.getuid?.() ?? "u"}`); + mkdirSync(dir, { recursive: true, mode: 0o700 }); + const stat = lstatSync(dir); + const ours = + stat.isDirectory() && + (process.platform === "win32" || + (stat.uid === process.getuid?.() && (stat.mode & 0o077) === 0)); + if (ours) return dir; + const staging = createStagingDir(parent, "hyperframes-snapshot-media-"); + process.once("exit", () => rmSync(staging, { recursive: true, force: true })); + return staging; +} -/** A local copy of a remote clip, since an FFmpeg built without network protocols (the Desktop app's) reads no URL. */ -export async function remoteVideoInput(url: string, download = downloadToTemp): Promise { +let remoteVideoDir: string | undefined; + +/** A local copy of a remote clip, or null: FFmpeg never gets the URL, since it would follow redirects unchecked. */ +export async function remoteVideoInput( + url: string, + download = downloadToTemp, +): Promise { try { - return await download(url, REMOTE_VIDEO_DIR, FFMPEG_EXTRACT_TIMEOUT_MS); + assertPublicHttpsUrl(url); } catch { - return url; + return null; + } + try { + remoteVideoDir ??= privateMediaDir(tmpdir()); + return await download(url, remoteVideoDir, FFMPEG_EXTRACT_TIMEOUT_MS); + } catch { + return null; } } @@ -1179,9 +1206,14 @@ export async function captureSnapshots( ); } const alphaDecoderCache = new Map>(); - const remoteCopies = new Map>(); - const localCopy = (href: string): Promise => { - const copy = remoteCopies.get(href) ?? remoteVideoInput(href); + const remoteCopies = new Map>(); + const leaveOutUnreadable = (href: string) => (path: string | null) => { + if (!path) warnings.push(`Remote video from ${new URL(href).host} could not be downloaded`); + return path; + }; + const localCopy = (href: string): Promise => { + const copy = + remoteCopies.get(href) ?? remoteVideoInput(href).then(leaveOutUnreadable(href)); remoteCopies.set(href, copy); return copy; }; @@ -1299,7 +1331,7 @@ export async function captureSnapshots( active, VIDEO_FRAME_EXTRACT_CONCURRENCY, async (v) => { - // Prefer the project-local file; remote http(s) clips go to FFmpeg directly, + // Prefer the project-local file; remote http(s) clips go to FFmpeg as a downloaded copy, // since headless Chrome can't seek them and they would render blank. let ffmpegInput: string | null = null; let inputIsLocal = false; @@ -1312,7 +1344,7 @@ export async function captureSnapshots( const url = new URL(v.src); if (url.protocol === "http:" || url.protocol === "https:") { ffmpegInput = await localCopy(url.href); - inputIsLocal = ffmpegInput !== url.href; + inputIsLocal = true; } } catch { /* unresolvable src (e.g. blob:, data:) — skip */ diff --git a/packages/cli/src/commands/upgrade.project.test.ts b/packages/cli/src/commands/upgrade.project.test.ts index b29e107e0b..d55101f249 100644 --- a/packages/cli/src/commands/upgrade.project.test.ts +++ b/packages/cli/src/commands/upgrade.project.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { mkdtempSync, readFileSync, writeFileSync, rmSync } from "node:fs"; +import { mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -91,6 +91,24 @@ describe("upgradeProjectPins", () => { expect(readFileSync(publish, "utf8")).toContain("hyperframes@0.7.55"); }); + it.skipIf(process.platform === "win32")( + "leaves the target of a package.json.tmp link untouched", + async () => { + const d = project({ render: "npx --yes hyperframes@0.7.48 render" }); + const outside = mkdtempSync(join(tmpdir(), "hf-proj-outside-")); + dirs.push(outside); + const target = join(outside, "keep.txt"); + writeFileSync(target, "original"); + symlinkSync(target, join(d, "package.json.tmp")); + + await upgradeProjectPins(d, { json: false, check: false }); + + expect(readFileSync(target, "utf8")).toBe("original"); + const pkg = JSON.parse(readFileSync(join(d, "package.json"), "utf-8")); + expect(pkg.scripts.render).toBe("npx --yes hyperframes@0.7.55 render"); + }, + ); + it("--check reports without writing", async () => { const d = project({ render: "npx --yes hyperframes@0.7.48 render" }); const before = readFileSync(join(d, "package.json"), "utf-8"); diff --git a/packages/cli/src/commands/upgrade.ts b/packages/cli/src/commands/upgrade.ts index 1f9b02b123..31001a8805 100644 --- a/packages/cli/src/commands/upgrade.ts +++ b/packages/cli/src/commands/upgrade.ts @@ -3,7 +3,8 @@ import { defineCommand } from "../utils/defineCommand.js"; import type { Example } from "./_examples.js"; import * as clack from "../ui/prompts.js"; import { execFileSync } from "node:child_process"; -import { existsSync, readFileSync, writeFileSync, renameSync, readdirSync } from "node:fs"; +import { existsSync, readFileSync, writeFileSync, readdirSync } from "node:fs"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { join, resolve } from "node:path"; import { c } from "../ui/colors.js"; import { isUnattended } from "../utils/attendedTerminal.js"; @@ -286,9 +287,7 @@ export async function upgradeProjectPins( }); if (rewrite.changed && !opts.check) { raw.scripts = rewrite.scripts; - const tmp = `${pkgPath}.tmp`; - writeFileSync(tmp, `${JSON.stringify(raw, null, 2)}\n`, "utf-8"); - renameSync(tmp, pkgPath); + replaceFileAtomically(pkgPath, `${JSON.stringify(raw, null, 2)}\n`); } if (!opts.check) { for (const wrapper of wrapperRewrites) { diff --git a/packages/cli/src/utils/privateProjectFile.test.ts b/packages/cli/src/utils/privateProjectFile.test.ts new file mode 100644 index 0000000000..4e19cb1f36 --- /dev/null +++ b/packages/cli/src/utils/privateProjectFile.test.ts @@ -0,0 +1,55 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { isEnvOrGitPath, isPrivateCompositionFile } from "./privateProjectFile.js"; + +let dir: string | undefined; + +afterEach(() => { + if (dir) rmSync(dir, { recursive: true, force: true }); + dir = undefined; +}); + +describe("isEnvOrGitPath", () => { + it("names env files and Git folders, whatever the letter case", () => { + expect(isEnvOrGitPath("/home/me/.env")).toBe(true); + expect(isEnvOrGitPath("a\\.Env.Local")).toBe(true); + expect(isEnvOrGitPath("repo/.git/config")).toBe(true); + expect(isEnvOrGitPath(".envrc")).toBe(true); + expect(isEnvOrGitPath("sub/.env-local")).toBe(true); + expect(isEnvOrGitPath(".env_prod")).toBe(true); + expect(isEnvOrGitPath(".environment.json")).toBe(false); + expect(isEnvOrGitPath("assets/environment.png")).toBe(false); + expect(isEnvOrGitPath("dotfiles.git/logo.png")).toBe(false); + }); +}); + +describe("isPrivateCompositionFile", () => { + it("covers env files, Git data and tool records, not composition assets", () => { + dir = mkdtempSync(join(tmpdir(), "hf-private-file-")); + const at = (path: string) => isPrivateCompositionFile(dir!, join(dir!, path)); + + expect(at(".env")).toBe(true); + expect(at(".env.production")).toBe(true); + expect(at("sub/.env")).toBe(true); + expect(at(".git/HEAD")).toBe(true); + expect(at(".hyperframes/history-turn.json")).toBe(true); + expect(at(".hyperframes/prepared-assets/gif/a.webm")).toBe(false); + expect(at(".media/clip.mp4")).toBe(false); + expect(at("environment.png")).toBe(false); + expect(at("index.html")).toBe(false); + }); + + it.skipIf(process.platform === "win32")( + "follows a link inside the project to an env file", + () => { + dir = mkdtempSync(join(tmpdir(), "hf-private-file-")); + writeFileSync(join(dir, ".env"), "KEY=secret"); + mkdirSync(join(dir, "assets")); + symlinkSync(join(dir, ".env"), join(dir, "assets", "logo.png")); + + expect(isPrivateCompositionFile(dir, join(dir, "assets", "logo.png"))).toBe(true); + }, + ); +}); diff --git a/packages/cli/src/utils/privateProjectFile.ts b/packages/cli/src/utils/privateProjectFile.ts new file mode 100644 index 0000000000..2d94361507 --- /dev/null +++ b/packages/cli/src/utils/privateProjectFile.ts @@ -0,0 +1,18 @@ +import { relative } from "node:path"; +import { isPrivateProjectFile, realFilePath } from "@hyperframes/studio-server/safe-path"; + +const ENV_FILE = /^\.env([._-].*|rc)?$/; + +export function isEnvOrGitPath(path: string): boolean { + const segments = path.toLowerCase().split(/[\\/]/); + return segments.includes(".git") || ENV_FILE.test(segments.at(-1) ?? ""); +} + +/** Project files no composition loads, so none is served or uploaded for one: env files, Git data, tool records. */ +export function isPrivateCompositionFile(projectDir: string, filePath: string): boolean { + return ( + isEnvOrGitPath(relative(projectDir, filePath)) || + isEnvOrGitPath(relative(realFilePath(projectDir), realFilePath(filePath))) || + isPrivateProjectFile(projectDir, filePath) + ); +} diff --git a/packages/cli/src/utils/publishProject.absolutePath.test.ts b/packages/cli/src/utils/publishProject.absolutePath.test.ts index 04b286c032..54ecd335ae 100644 --- a/packages/cli/src/utils/publishProject.absolutePath.test.ts +++ b/packages/cli/src/utils/publishProject.absolutePath.test.ts @@ -1,6 +1,6 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { basename, join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { stagePublishFiles } from "./publishProject.js"; @@ -61,3 +61,23 @@ describe("a media file referenced by its filesystem path", () => { expect(index).toContain('src="/assets/none.png"'); }); }); + +describe("an env file outside the project", () => { + it("is not archived, whether named by its path or by a relative reference", () => { + const project = temp(); + const external = temp(); + writeFileSync(join(external, ".env"), "SECRET=1"); + writeFileSync(join(external, ".env.production"), "SECRET=1"); + writeFileSync(join(external, "song.mp3"), "audio"); + const sibling = `../${basename(external)}`; + writeFileSync( + join(project, "index.html"), + `` + + ``, + ); + const keys = [...stagePublishFiles(project, Infinity).keys()]; + expect(keys.filter((key) => key.startsWith("_ext/"))).toEqual([ + expect.stringMatching(/song\.mp3$/), + ]); + }); +}); diff --git a/packages/cli/src/utils/publishProject.test.ts b/packages/cli/src/utils/publishProject.test.ts index e729991128..d869305a57 100644 --- a/packages/cli/src/utils/publishProject.test.ts +++ b/packages/cli/src/utils/publishProject.test.ts @@ -1559,6 +1559,14 @@ describe("createPublishArchive referenced dot-paths", () => { const dir = projectWith(``, { [clip]: "v", ".env": "SECRET=1" }); expect(names(dir)).not.toContain(".env"); }); + + it("leaves out an env file or a tool record even when the markup names it", () => { + const dir = projectWith( + `h`, + { ".env": "SECRET=1", ".env.local": "SECRET=1", ".hyperframes/history-turn.json": "{}" }, + ); + expect(names(dir)).toEqual(["index.html"]); + }); }); describe("archive keys for names written in either Unicode form", () => { diff --git a/packages/cli/src/utils/publishProject.ts b/packages/cli/src/utils/publishProject.ts index 70f34c4ba7..f70d6e000d 100644 --- a/packages/cli/src/utils/publishProject.ts +++ b/packages/cli/src/utils/publishProject.ts @@ -7,6 +7,7 @@ import { openSync, readdirSync, readFileSync, + realpathSync, type Stats, statSync, } from "node:fs"; @@ -22,6 +23,7 @@ import { isAuthError } from "../auth/errors.js"; import { envCredentialVar, isTokenExpired, type ResolvedCredential } from "../auth/resolver.js"; import { refreshIfNeeded } from "../cloud/auth.js"; import { writeProjectLink } from "./projectLink.js"; +import { isEnvOrGitPath, isPrivateCompositionFile } from "./privateProjectFile.js"; const IGNORED_DIRS = new Set([".git", "node_modules", "dist", ".next", "coverage"]); const IGNORED_FILES = new Set([".DS_Store", "Thumbs.db"]); @@ -414,6 +416,11 @@ function addExternalAsset(ctx: ExternalAssetContext, absPath: string): string { return archivePath; } +function staysOut(ctx: ExternalAssetContext, rel: string, absPath: string): boolean { + if (!rel || ctx.fileContents.has(rel) || rel.split("/").some(shouldIgnoreSegment)) return true; + return isPrivateCompositionFile(ctx.absProjectDir, absPath); +} + /** Ships a referenced in-project file the ignore rules skipped, resolved from its folder or the root. */ function includeReferencedProjectFile( ctx: ExternalAssetContext, @@ -426,7 +433,7 @@ function includeReferencedProjectFile( const absPath = candidate.replace(/[?#].*$/, ""); if (!isPathInside(absPath, ctx.absProjectDir)) continue; const rel = archiveKey(relative(ctx.absProjectDir, absPath).replaceAll("\\", "/")); - if (!rel || ctx.fileContents.has(rel) || rel.split("/").some(shouldIgnoreSegment)) return; + if (staysOut(ctx, rel, absPath)) return; if (!existsSync(absPath)) continue; if (statSync(absPath).isFile()) { ctx.fileContents.set(rel, stageFile(absPath, ctx.onDiskMinBytes)); @@ -455,6 +462,7 @@ function archiveTargetOf( } try { if (!existsSync(absPath) || !statSync(absPath).isFile()) return undefined; + if (isEnvOrGitPath(absPath) || isEnvOrGitPath(realpathSync(absPath))) return undefined; } catch { return undefined; } diff --git a/packages/producer/src/services/distributed/projectHash.test.ts b/packages/producer/src/services/distributed/projectHash.test.ts index cfe3dc8c35..c93c41cf6a 100644 --- a/packages/producer/src/services/distributed/projectHash.test.ts +++ b/packages/producer/src/services/distributed/projectHash.test.ts @@ -51,6 +51,18 @@ describe("listSiteFiles / hashProjectDir", () => { expect(listSiteFiles(dir)).toEqual(["index.html"]); }); + it("leaves env files out at every depth", () => { + const dir = project(); + mkdirSync(join(dir, "sub")); + writeFileSync(join(dir, ".env"), "KEY=1"); + writeFileSync(join(dir, "sub", ".env.local"), "KEY=1"); + writeFileSync(join(dir, ".envrc"), "KEY=1"); + writeFileSync(join(dir, "sub", ".env-local"), "KEY=1"); + writeFileSync(join(dir, "sub", ".env_prod"), "KEY=1"); + writeFileSync(join(dir, "sub", "environment.css"), "a{}"); + expect(listSiteFiles(dir)).toEqual(["index.html", "sub/environment.css"]); + }); + 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 a34888ad8c..2dc140bf12 100644 --- a/packages/producer/src/services/distributed/projectHash.ts +++ b/packages/producer/src/services/distributed/projectHash.ts @@ -17,18 +17,23 @@ 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); /** * 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, and a dangling symlink throws. + * does, env files are left out at every depth, and a dangling symlink throws. */ export function listSiteFiles(projectDir: string): string[] { const files: string[] = []; const ancestors = new Set([realpathSync(projectDir)]); function walk(dir: string, rel: string): void { for (const name of readdirSync(dir).sort()) { - if (rel === "" && PLAN_PROJECT_DIR_SKIP_SEGMENTS.has(name)) continue; + if (leftOut(rel, name)) continue; const full = join(dir, name); const relPath = rel === "" ? name : `${rel}/${name}`; let stat;