mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): harden lambda destroy, snapshot media, upgrade and project file serving (#7509)
* fix(cli): lambda destroy runs SAM in a private folder and acts only on the selected stack - sam delete outside a checkout runs in a fresh private temp folder, removed afterwards, instead of the shared temp directory. - A stack state file must name the stack it is selected by. - destroy and deploy derive the video repository name from the stack name instead of trusting the recorded value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): snapshot keeps remote clips in a private folder and reads only allowed URLs - A video URL the download policy refuses (plain http, or a private, loopback or link-local host) is no longer handed to FFmpeg; the clip is skipped instead, as render does. - Remote clips are cached in a per-user, owner-only temp folder; a folder open to other users is never reused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): upgrade --project writes package.json through an exclusive temp file The rewrite now uses the shared atomic replace, which creates a fresh, unpredictable sibling with exclusive creation, so an existing package.json.tmp entry is never written through. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): keep env files, Git data and tool records out of served and uploaded projects - play and present answer 404 for .env files, .git and the tools' private .hyperframes records, as Studio's preview does for the records. - publish and cloud render never stage an env file or tool record, even when markup references it, and never pull in an env file from outside the project. - Cloud site uploads (Lambda and Cloud Run deploySite) leave env files out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): lambda commands accept a state file whose stack name differs only in case On a case-insensitive disk, deploy --stack-name=Demo and --stack-name=demo share one state file, so a name that matches apart from case is the same stack the user deployed and keeps working as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): snapshot's fallback media folder uses createStagingDir The repository check forbids bare mkdtemp beside a caller-supplied path, because it fails past 260 characters on Windows. Also trims a comment so the new helper file stays within its package's comment share. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): snapshot never hands FFmpeg a remote URL FFmpeg follows redirects and resolves hosts on its own, so a clip the downloader could not fetch must not reach it as a URL. Snapshot now leaves such a clip out and warns, as render does. A fallback media folder is removed when the process exits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): cover .envrc, .env-local and .env_prod, and test present's private-file 404 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cf90054613
commit
c96ff540c4
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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<DeployArgs> = {}): Promise<void> {
|
||||
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(),
|
||||
});
|
||||
|
||||
|
||||
@@ -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" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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<void> {
|
||||
// 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();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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`;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Hono>
|
||||
}
|
||||
|
||||
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"), "<html><head></head><body></body></html>");
|
||||
|
||||
@@ -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. `<dir>-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")) {
|
||||
|
||||
@@ -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) });
|
||||
});
|
||||
|
||||
@@ -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<string> =>
|
||||
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<string> => "/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<string> => 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);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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<string> {
|
||||
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<string | null> {
|
||||
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<string, Promise<boolean>>();
|
||||
const remoteCopies = new Map<string, Promise<string>>();
|
||||
const localCopy = (href: string): Promise<string> => {
|
||||
const copy = remoteCopies.get(href) ?? remoteVideoInput(href);
|
||||
const remoteCopies = new Map<string, Promise<string | null>>();
|
||||
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<string | null> => {
|
||||
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 */
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
@@ -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"),
|
||||
`<link href="${join(external, ".env")}"><link href="${sibling}/.env.production">` +
|
||||
`<audio src="${sibling}/song.mp3"></audio>`,
|
||||
);
|
||||
const keys = [...stagePublishFiles(project, Infinity).keys()];
|
||||
expect(keys.filter((key) => key.startsWith("_ext/"))).toEqual([
|
||||
expect.stringMatching(/song\.mp3$/),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1559,6 +1559,14 @@ describe("createPublishArchive referenced dot-paths", () => {
|
||||
const dir = projectWith(`<video src="${clip}"></video>`, { [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(
|
||||
`<link href=".env"><img src=".env.local"><a href=".hyperframes/history-turn.json">h</a>`,
|
||||
{ ".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", () => {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user