From 724ab39b3fdba730a2215e93b5843b4479ef8f7b Mon Sep 17 00:00:00 2001 From: James Russo Date: Tue, 10 Nov 2026 08:30:30 +0000 Subject: [PATCH] fix(studio-server): buildSubCompositionHtml reads only compositions inside the project (#7482) * fix(studio-server): buildSubCompositionHtml reads only compositions inside the project The helper joined projectDir with compPath and read the result, trusting every caller to have confined the path first. Studio's own preview route does; an embedder that forgets would serve any readable file. The helper now resolves compPath with core's resolveWithinProject and returns null for a path outside the project, the same answer as a missing file. Co-Authored-By: Claude Opus 5.5 * fix(studio-server): keep subComposition.ts's comment share flat Co-Authored-By: Claude Opus 5.5 * fix(studio-server): the sub-composition head reads only an index.html inside the project Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../src/helpers/subComposition.test.ts | 26 ++++++++++++++++++- .../src/helpers/subComposition.ts | 10 ++++--- 2 files changed, 31 insertions(+), 5 deletions(-) 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)) {