diff --git a/packages/cli/src/utils/receivedRender.test.ts b/packages/cli/src/utils/receivedRender.test.ts index 437b1efc7b..708dd2dc2b 100644 --- a/packages/cli/src/utils/receivedRender.test.ts +++ b/packages/cli/src/utils/receivedRender.test.ts @@ -583,9 +583,11 @@ describe("outsideAssetsAllowed", () => { expect(line).toContain("scene.html is another name for a file that came from someone else"); }); + // With --include-outside-files, so a scene in a linked folder outside the project is read too. async function compiledPage(dir: string) { const compiled = await compileForRender(dir, join(dir, "index.html"), join(dir, ".dl"), { allowOutsideProjectAssets: true, + includeOutsideScripts: true, }); const rendered = [compiled.html, ...compiled.subCompositions.values()].join("\n"); return { rendered, outside: [...compiled.externalAssets.values()] }; diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index c876e6e9bf..05a8e434a7 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -100,6 +100,7 @@ import { resolveCompositionSrc, } from "@hyperframes/parsers/asset-resolution"; import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path"; +import { linksOutside } from "@hyperframes/studio-server/outside-file"; import { extractMediaMetadata, extractAudioMetadata } from "../utils/ffprobe.js"; import { assertCompositionFileSize } from "./compositionSizeLimit.js"; import { @@ -283,6 +284,18 @@ function resolveCompositionFile( return file !== null && COMPOSITION_FILE.test(file) ? file : null; } +const OUTSIDE_COMPOSITION_NEEDS_FLAG = + "it is outside the project and its scripts would run; pass --include-outside-files to hyperframes render to run it"; + +/** A trusted local render was not told to run markup whose real file is outside the project. */ +const runsOutsideMarkupUnasked = ( + projectDir: string, + filePath: string, + assetRoot: string | undefined, + outsideFiles: OutsideFilePolicy, +): boolean => + assetRoot === undefined && !outsideFiles.includeScripts && linksOutside(projectDir, filePath); + // Git metadata, env files and the app's `.hyperframes/` records are never inlined into a render, as preview refuses them. const isPrivateCompositionFile = (projectDir: string, filePath: string): boolean => isPrivateFileUnder(projectDir, filePath, isStudioPrivatePath); @@ -306,6 +319,7 @@ function assertSubCompositionsUsable( html: string, projectDir: string, assetRoot: string | undefined, + outsideFiles: OutsideFilePolicy, visited: Set = new Set(), ): void { const { document } = parseHTML(html); @@ -326,6 +340,10 @@ function assertSubCompositionsUsable( problems.push({ srcPath, detail: "it is outside the project in a private location" }); continue; } + if (runsOutsideMarkupUnasked(projectDir, filePath, assetRoot, outsideFiles)) { + problems.push({ srcPath, detail: OUTSIDE_COMPOSITION_NEEDS_FLAG }); + continue; + } if (isPrivateCompositionFile(projectDir, filePath)) { problems.push({ srcPath, @@ -376,7 +394,7 @@ function assertSubCompositionsUsable( const nestedVisited = new Set(visited); nestedVisited.add(filePath); try { - assertSubCompositionsUsable(fileHtml, projectDir, assetRoot, nestedVisited); + assertSubCompositionsUsable(fileHtml, projectDir, assetRoot, outsideFiles, nestedVisited); } catch (err) { if (err instanceof EmptyCompositionError) { problems.push(...err.problems); @@ -2218,9 +2236,16 @@ export async function compileForRender( options: CompileForRenderOptions = {}, ): Promise { const assetRoot = options.allowOutsideProjectAssets ? undefined : projectDir; + const outsideFiles: OutsideFilePolicy = { includeScripts: options.includeOutsideScripts }; if (isPathInside(htmlPath, projectDir) && escapesAssetRoot(htmlPath, assetRoot)) { throw new Error(`Entry file ${htmlPath} leads outside the project folder`); } + if ( + isPathInside(htmlPath, projectDir) && + runsOutsideMarkupUnasked(projectDir, htmlPath, assetRoot, outsideFiles) + ) { + throw new Error(`Entry file ${htmlPath} ${OUTSIDE_COMPOSITION_NEEDS_FLAG}`); + } if (!COMPOSITION_FILE.test(htmlPath)) { throw new Error(`Entry file ${htmlPath} is not an .html file, so it is refused`); } @@ -2237,7 +2262,7 @@ export async function compileForRender( // unconditional (not gated behind --strict like lint warnings) — a render // that silently drops a scene is strictly worse than one that refuses to // start. - assertSubCompositionsUsable(rawHtml, projectDir, assetRoot); + assertSubCompositionsUsable(rawHtml, projectDir, assetRoot, outsideFiles); const probes: SourceProbes = new Map(); const { html: compiledHtml, unresolvedCompositions } = await compileHtmlFile( diff --git a/packages/producer/src/services/outsideProjectAssets.test.ts b/packages/producer/src/services/outsideProjectAssets.test.ts index f23294e69b..6fc28256fd 100644 --- a/packages/producer/src/services/outsideProjectAssets.test.ts +++ b/packages/producer/src/services/outsideProjectAssets.test.ts @@ -394,11 +394,24 @@ describe.skipIf(!HAS_FFMPEG)("render servers keep markup inside the project", () await plan(projectDir, config, planDir, { allowOutsideProjectAssets: true }); expect(existsSync(join(planDir, "compiled", secretKey))).toBe(true); expect(existsSync(join(planDir, "compiled", "link.png"))).toBe(true); - - const linked = symlinkedComposition("scene"); - const { result } = await compileInProcess(linked.projectDir, true); - expect(result.compiled.html).toContain("OUTSIDE-SCENE"); }); + + it.each(["scene", "entry"] as const)( + "trusted local renders refuse a symlinked %s outside the project unless told to run it", + async (link) => { + const { root, projectDir } = symlinkedComposition(link); + const needsFlag = + /outside the project and its scripts would run; pass --include-outside-files/; + await expect(compileInProcess(projectDir, true)).rejects.toThrow(needsFlag); + const planDir = join(root, "plan"); + await expect( + plan(projectDir, config, planDir, { allowOutsideProjectAssets: true }), + ).rejects.toThrow(needsFlag); + + const { result } = await compileInProcess(projectDir, true, true); + expect(result.compiled.html).toContain("OUTSIDE-SCENE"); + }, + ); }); function outsideScriptsProject() {