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 <noreply@anthropic.com>

* fix(studio-server): keep subComposition.ts's comment share flat

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(studio-server): the sub-composition head reads only an index.html inside the project

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:
James Russo
2026-11-10 08:30:30 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 0ba8941235
commit 724ab39b3f
2 changed files with 31 additions and 5 deletions
@@ -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, string>): 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": "<!doctype html><html><head></head><body></body></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":
'<!doctype html><html><head><meta name="outside-head"></head><body></body></html>',
});
const dir = makeTempProject({
"scenes/scene.html": '<template><div id="scene"></div></template>',
});
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) => `<template>${body}</template>`],
["document", (body: string) => `<!doctype html><html><body>${body}</body></html>`],
@@ -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 <head> 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 <base> for relative asset resolution (before other tags)
if (baseHref && !hasBaseElement(headContent)) {