diff --git a/packages/studio-server/src/helpers/subComposition.test.ts b/packages/studio-server/src/helpers/subComposition.test.ts index e1fc3beb33..5866bd5c2d 100644 --- a/packages/studio-server/src/helpers/subComposition.test.ts +++ b/packages/studio-server/src/helpers/subComposition.test.ts @@ -1,5 +1,5 @@ // @vitest-environment node -import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it, onTestFinished } from "vitest"; @@ -20,6 +20,30 @@ function makeTempProject(files: Record): string { } describe("buildSubCompositionHtml", () => { + it("returns null for a composition path that resolves outside the project", () => { + const outside = makeTempProject({ "secret.txt": "outside-secret" }); + const dir = makeTempProject({ + "index.html": "", + }); + const escape = `../${outside.split("/").pop()}/secret.txt`; + expect(buildSubCompositionHtml(dir, escape, "/runtime.js")).toBeNull(); + expect(buildSubCompositionHtml(dir, join(outside, "secret.txt"), "/runtime.js")).toBeNull(); + }); + + it("leaves out an index.html head that a symlink points outside the project", () => { + const outside = makeTempProject({ + "index.html": + '', + }); + const dir = makeTempProject({ + "scenes/scene.html": '', + }); + symlinkSync(join(outside, "index.html"), join(dir, "index.html")); + const html = buildSubCompositionHtml(dir, "scenes/scene.html", "/runtime.js"); + expect(html).toContain('id="scene"'); + expect(html).not.toContain("outside-head"); + }); + it.each([ ["template", (body: string) => ``], ["document", (body: string) => `${body}`], diff --git a/packages/studio-server/src/helpers/subComposition.ts b/packages/studio-server/src/helpers/subComposition.ts index 48f54a19a7..5854617935 100644 --- a/packages/studio-server/src/helpers/subComposition.ts +++ b/packages/studio-server/src/helpers/subComposition.ts @@ -13,6 +13,7 @@ import { } from "@hyperframes/core/compiler"; import { findStartTags, isFullHtmlDocument } from "@hyperframes/core/compiler/html-document"; import { gsapCdnDist } from "@hyperframes/core/gsap-cdn"; +import { resolveWithinProject } from "@hyperframes/core/safe-path"; import { htmlHasGsapCore, insertGsapCoreBeforeHeadPlugin } from "./gsapCoreScript.js"; import { withRootPathsRelative } from "./rootPaths.js"; @@ -273,8 +274,8 @@ export function buildSubCompositionHtml( rawOverride?: string, gsapBaseUrl?: string, ): string | null { - const compFile = join(projectDir, compPath); - if (!existsSync(compFile)) return null; + const compFile = resolveWithinProject(projectDir, compPath); + if (!compFile || !existsSync(compFile)) return null; // rawOverride lets the preview route thread the hf-id-stamped content in // directly, so the build uses ids minted from the raw file, which is never @@ -332,10 +333,11 @@ export function buildSubCompositionHtml( rewrittenContent = tagRootCompositionFile(rewrittenContent, compPath); // Use the project's index.html to preserve all dependencies - const indexPath = join(projectDir, "index.html"); + const indexPath = resolveWithinProject(projectDir, "index.html"); let headContent = ""; - if (existsSync(indexPath)) headContent = rootHeadContent(readFileSync(indexPath, "utf-8")); + if (indexPath && existsSync(indexPath)) + headContent = rootHeadContent(readFileSync(indexPath, "utf-8")); // Inject for relative asset resolution (before other tags) if (baseHref && !hasBaseElement(headContent)) {