diff --git a/packages/cli/src/server/studioServer.previewAssets.test.ts b/packages/cli/src/server/studioServer.previewAssets.test.ts index 56d8bce85a..4c08b2b1d6 100644 --- a/packages/cli/src/server/studioServer.previewAssets.test.ts +++ b/packages/cli/src/server/studioServer.previewAssets.test.ts @@ -1,6 +1,7 @@ -import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import * as fs from "node:fs"; import * as path from "node:path"; +import { fileURLToPath } from "node:url"; import { createStudioServer, type StudioServer } from "./studioServer.js"; import { cleanupStudioServerRoot, makeStudioServerRoot } from "./studioServerTestFixture.js"; @@ -141,3 +142,50 @@ describe("bundled GSAP", () => { } }); }); + +describe("preview image prep and files outside the project", () => { + const heic = fileURLToPath( + new URL("../../../producer/tests/fixtures/heic/rotated-grid.heic", import.meta.url), + ); + const outsidePhoto = () => path.join(root, "Pictures", "IMG_0001.HEIC"); + const preparedHeif = () => path.join(projectDir, ".hyperframes", "prepared-assets", "heif"); + + beforeEach(() => { + fs.mkdirSync(path.dirname(outsidePhoto())); + fs.copyFileSync(heic, outsidePhoto()); + fs.writeFileSync( + path.join(projectDir, "index.html"), + `
+ +
`, + ); + }); + + async function previewPhotoSrc(): Promise { + const html = await (await server.app.request("/api/projects/film/preview")).text(); + return html.match(/]*id="photo"[^>]*src="([^"]+)"/)?.[1]; + } + + it("converts no outside photo for a received project, as its render would not", async () => { + fs.mkdirSync(path.join(projectDir, ".hyperframes"), { recursive: true }); + fs.writeFileSync( + path.join(projectDir, ".hyperframes", "received-files.json"), + JSON.stringify({ files: ["index.html"] }), + ); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + expect(await previewPhotoSrc()).not.toContain("prepared-assets"); + expect(await previewPhotoSrc()).not.toContain("prepared-assets"); + expect(fs.existsSync(preparedHeif())).toBe(false); + const refusals = warn.mock.calls.filter(([line]) => String(line).includes("someone else")); + expect(refusals).toHaveLength(1); + } finally { + warn.mockRestore(); + } + }); + + it("still converts an outside photo for the developer's own project, as its render does", async () => { + expect(await previewPhotoSrc()).toMatch(/^\.hyperframes\/prepared-assets\/heif\//); + expect(fs.readdirSync(preparedHeif())).toHaveLength(1); + }); +}); diff --git a/packages/cli/src/server/studioServer.previewNetworkShare.win32.test.ts b/packages/cli/src/server/studioServer.previewNetworkShare.win32.test.ts index f7f24ae274..a0a19a3bbf 100644 --- a/packages/cli/src/server/studioServer.previewNetworkShare.win32.test.ts +++ b/packages/cli/src/server/studioServer.previewNetworkShare.win32.test.ts @@ -35,6 +35,8 @@ vi.mock("node:fs", async () => { return { ...fs, ...overrides, default: { ...fs, ...overrides } }; }); +vi.mock("../utils/receivedRender.js", () => ({ outsideAssetsAllowed: () => true })); + const { createStudioServer } = await import("./studioServer.js"); const { cleanupStudioServerRoot, makeStudioServerRoot } = await import("./studioServerTestFixture.js"); diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts index 8bf1b26563..3c26949487 100644 --- a/packages/cli/src/server/studioServer.ts +++ b/packages/cli/src/server/studioServer.ts @@ -540,6 +540,13 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { // launching a browser shutdown() has no way to know about and close. let shuttingDown = false; + const previewWarnings = new Set(); + const warnPreviewOnce = (line: string) => { + if (previewWarnings.has(line)) return; + previewWarnings.add(line); + console.warn(`[studio] ${line}`); + }; + const adapter: PreviewApiAdapter = { history: () => (hostAppOwnsHistory ? null : projectHistory()), loadSharp: () => loadSharpInBackground("image thumbnails"), @@ -594,11 +601,13 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { const preparedDirs = [gifOutputDir, gifDownloadDir, heifOutputDir]; if (!preparedDirs.every((dir) => isLinkFreeWithinProject(project.dir, dir))) return injectDeterministicFontFaces(html); + const allowOutsideFiles = outsideAssetsAllowed(project.dir, undefined, warnPreviewOnce); const { html: htmlWithJpegs } = await prepareHeifImageInputs(html, { projectDir: project.dir, downloadDir: gifDownloadDir, outputDir: heifOutputDir, outputSrcPrefix: ".hyperframes/prepared-assets/heif", + allowOutsideFiles, onFailure: (error) => console.warn(`[studio] ${error.message}`), }); const prepared = await prepareAnimatedGifInputs(htmlWithJpegs, { @@ -606,6 +615,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { downloadDir: gifDownloadDir, outputDir: gifOutputDir, outputSrcPrefix: ".hyperframes/prepared-assets/gif", + allowOutsideFiles, cacheDir: gifOutputDir, sourceAssets: await downloadRemoteGifImageSources(html, gifDownloadDir, (url, destDir) => downloadToTemp(url, destDir, undefined, undefined, undefined, { diff --git a/packages/producer/src/services/animatedGifPrep.test.ts b/packages/producer/src/services/animatedGifPrep.test.ts index 9c9c6dc927..f43a363989 100644 --- a/packages/producer/src/services/animatedGifPrep.test.ts +++ b/packages/producer/src/services/animatedGifPrep.test.ts @@ -256,6 +256,36 @@ describe("prepareAnimatedGifInputs", () => { expect(calls[0]?.args).toContain("2"); }); + it("reads no GIF outside the project unless the caller allows outside files", async () => { + const root = makeProject(); + const projectDir = join(root, "project"); + const outside = join(root, "Downloads", "reaction.gif"); + mkdirSync(projectDir); + mkdirSync(dirname(outside)); + writeFileSync(outside, gif([...frame(10), ...frame(10)], 0)); + const html = ``; + const calls: AnimatedGifTranscodeRequest[] = []; + const options = { + projectDir, + downloadDir: join(projectDir, "downloads"), + transcode: async (request: AnimatedGifTranscodeRequest) => { + calls.push(request); + writeFileSync(request.outputPath, "webm"); + }, + }; + + for (const src of ["../Downloads/reaction.gif", outside]) { + expect(resolveLocalImageSourcePath(src, options)).toBeNull(); + } + const contained = await prepareAnimatedGifInputs(html, options); + expect(contained.html).toBe(html); + expect(calls).toHaveLength(0); + + const trusted = await prepareAnimatedGifInputs(html, { ...options, allowOutsideFiles: true }); + expect(trusted.preparedGifs).toHaveLength(2); + expect(calls).toHaveLength(1); + }); + it("uses source asset mappings for remote GIF URLs", async () => { const projectDir = makeProject(); const sourceUrl = "https://cdn.example.com/reaction.gif"; diff --git a/packages/producer/src/services/animatedGifPrep.ts b/packages/producer/src/services/animatedGifPrep.ts index feb671468c..092f8a2a9b 100644 --- a/packages/producer/src/services/animatedGifPrep.ts +++ b/packages/producer/src/services/animatedGifPrep.ts @@ -39,7 +39,7 @@ export interface PrepareAnimatedGifInputsOptions { sourceAssets?: Map; timeoutMs?: number; transcode?: (input: AnimatedGifTranscodeRequest) => Promise; - assetRoot?: string; + allowOutsideFiles?: boolean; } export interface AnimatedGifTranscodeRequest { @@ -89,7 +89,7 @@ export function resolveLocalImageSourcePath( src: string, options: Pick< PrepareAnimatedGifInputsOptions, - "projectDir" | "downloadDir" | "sourceAssets" | "assetRoot" + "projectDir" | "downloadDir" | "sourceAssets" | "allowOutsideFiles" >, ): string | null { const trimmed = src.trim(); @@ -110,10 +110,11 @@ export function resolveLocalImageSourcePath( resolve(options.downloadDir, normalizedBase), ]; + const assetRoot = options.allowOutsideFiles ? undefined : options.projectDir; const allowed = (candidate: string) => !namesAnotherNetworkRoot(options.projectDir, candidate) && !isPrivateUnderAssetRoots(candidate, [options.projectDir, options.downloadDir]) && - ((!escapesAssetRoot(candidate, options.assetRoot) && + ((!escapesAssetRoot(candidate, assetRoot) && outsideAssetRefusal(options.projectDir, candidate) !== "blocklisted") || !escapesAssetRoot(candidate, options.downloadDir)); return candidates.find((candidate) => allowed(candidate) && existsSync(candidate)) ?? null; diff --git a/packages/producer/src/services/heifImagePrep.test.ts b/packages/producer/src/services/heifImagePrep.test.ts index 16bec681c0..5f37e44d1a 100644 --- a/packages/producer/src/services/heifImagePrep.test.ts +++ b/packages/producer/src/services/heifImagePrep.test.ts @@ -241,6 +241,29 @@ describe("prepareHeifImageInputs", () => { expect(primaryGrid({ stream_groups: [grid(5)] })?.width).toBe(5); }); + it("reads no HEIC outside the project unless the caller allows outside files", async () => { + const d = project({}); + const outside = join(d.base, "Pictures", "IMG_0001.HEIC"); + spawnSync("mkdir", ["-p", join(outside, "..")]); + writeFileSync(outside, readFileSync(FIXTURE)); + const html = ``; + const { calls, transcode } = countingTranscode(); + + const contained = await prepareHeifImageInputs(html, { ...d, transcode }); + expect(contained.html).toBe(html); + expect(contained.preparedImages).toEqual([]); + expect(calls).toHaveLength(0); + expect(existsSync(d.outputDir)).toBe(false); + + const trusted = await prepareHeifImageInputs(html, { + ...d, + transcode, + allowOutsideFiles: true, + }); + expect(trusted.preparedImages).toHaveLength(2); + expect(trusted.html).not.toContain("IMG_0001.HEIC"); + }); + it("spawns nothing and writes nothing without a HEIC image", async () => { const png = Buffer.from( "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==", diff --git a/packages/producer/src/services/heifImagePrep.ts b/packages/producer/src/services/heifImagePrep.ts index 3819957ff0..0c8f675f03 100644 --- a/packages/producer/src/services/heifImagePrep.ts +++ b/packages/producer/src/services/heifImagePrep.ts @@ -81,7 +81,7 @@ export interface PrepareHeifImageInputsOptions { sourceAssets?: Map; timeoutMs?: number; transcode?: (input: HeifTranscodeRequest) => Promise; - assetRoot?: string; + allowOutsideFiles?: boolean; /** Leaves an image whose conversion failed as authored and reports it, instead of throwing. */ onFailure?: (error: HeifImageConversionError | HeifToolUnavailableError) => void; } diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 327dc5c704..c876e6e9bf 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -2375,7 +2375,7 @@ export async function compileForRender( cacheDir: options.animatedGifCacheDir, sourceAssets: remoteImageAssets, timeoutMs: options.ffmpegProcessTimeout, - assetRoot, + allowOutsideFiles: assetRoot === undefined, }); if (preparedHeifImages.length > 0) { defaultLogger.debug(`[Compiler] Converted ${preparedHeifImages.length} HEIC image(s) to JPEG`); @@ -2392,7 +2392,7 @@ export async function compileForRender( cacheDir: options.animatedGifCacheDir, sourceAssets: gifSourceAssets, timeoutMs: options.ffmpegProcessTimeout, - assetRoot, + allowOutsideFiles: assetRoot === undefined, }); if (preparedGifs.length > 0) { defaultLogger.debug(`[Compiler] Prepared ${preparedGifs.length} animated GIF input(s) as WebM`); diff --git a/packages/producer/src/services/imagePrep.networkShare.win32.test.ts b/packages/producer/src/services/imagePrep.networkShare.win32.test.ts index 777b0e8f3f..67a83fb74f 100644 --- a/packages/producer/src/services/imagePrep.networkShare.win32.test.ts +++ b/packages/producer/src/services/imagePrep.networkShare.win32.test.ts @@ -70,6 +70,7 @@ describe("image prep under Windows path rules", () => { await prepareHeifImageInputs(page([...SHARES, "local.gif"]), { projectDir: PROJECT, downloadDir: DOWNLOADS, + allowOutsideFiles: true, }); expect(touched.filter(onAShare)).toEqual([]); expect(touched).toContain("C:\\project\\local.gif"); @@ -79,6 +80,7 @@ describe("image prep under Windows path rules", () => { await prepareAnimatedGifInputs(page([...SHARES, "local.gif"]), { projectDir: PROJECT, downloadDir: DOWNLOADS, + allowOutsideFiles: true, }); expect(touched.filter(onAShare)).toEqual([]); expect(touched).toContain("C:\\project\\local.gif");