Skip to content

Commit 724ab39

Browse files
jrusso1020claude
andauthored
fix(studio-server): buildSubCompositionHtml reads only compositions inside the project (heygen-com#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 <[email protected]> * fix(studio-server): keep subComposition.ts's comment share flat Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio-server): the sub-composition head reads only an index.html inside the project Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0ba8941 commit 724ab39

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎packages/studio-server/src/helpers/subComposition.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @vitest-environment node
2-
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
2+
import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { describe, expect, it, onTestFinished } from "vitest";
@@ -20,6 +20,30 @@ function makeTempProject(files: Record<string, string>): string {
2020
}
2121

2222
describe("buildSubCompositionHtml", () => {
23+
it("returns null for a composition path that resolves outside the project", () => {
24+
const outside = makeTempProject({ "secret.txt": "outside-secret" });
25+
const dir = makeTempProject({
26+
"index.html": "<!doctype html><html><head></head><body></body></html>",
27+
});
28+
const escape = `../${outside.split("/").pop()}/secret.txt`;
29+
expect(buildSubCompositionHtml(dir, escape, "/runtime.js")).toBeNull();
30+
expect(buildSubCompositionHtml(dir, join(outside, "secret.txt"), "/runtime.js")).toBeNull();
31+
});
32+
33+
it("leaves out an index.html head that a symlink points outside the project", () => {
34+
const outside = makeTempProject({
35+
"index.html":
36+
'<!doctype html><html><head><meta name="outside-head"></head><body></body></html>',
37+
});
38+
const dir = makeTempProject({
39+
"scenes/scene.html": '<template><div id="scene"></div></template>',
40+
});
41+
symlinkSync(join(outside, "index.html"), join(dir, "index.html"));
42+
const html = buildSubCompositionHtml(dir, "scenes/scene.html", "/runtime.js");
43+
expect(html).toContain('id="scene"');
44+
expect(html).not.toContain("outside-head");
45+
});
46+
2347
it.each([
2448
["template", (body: string) => `<template>${body}</template>`],
2549
["document", (body: string) => `<!doctype html><html><body>${body}</body></html>`],

‎packages/studio-server/src/helpers/subComposition.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "@hyperframes/core/compiler";
1414
import { findStartTags, isFullHtmlDocument } from "@hyperframes/core/compiler/html-document";
1515
import { gsapCdnDist } from "@hyperframes/core/gsap-cdn";
16+
import { resolveWithinProject } from "@hyperframes/core/safe-path";
1617
import { htmlHasGsapCore, insertGsapCoreBeforeHeadPlugin } from "./gsapCoreScript.js";
1718
import { withRootPathsRelative } from "./rootPaths.js";
1819

@@ -273,8 +274,8 @@ export function buildSubCompositionHtml(
273274
rawOverride?: string,
274275
gsapBaseUrl?: string,
275276
): string | null {
276-
const compFile = join(projectDir, compPath);
277-
if (!existsSync(compFile)) return null;
277+
const compFile = resolveWithinProject(projectDir, compPath);
278+
if (!compFile || !existsSync(compFile)) return null;
278279

279280
// rawOverride lets the preview route thread the hf-id-stamped content in
280281
// directly, so the build uses ids minted from the raw file, which is never
@@ -332,10 +333,11 @@ export function buildSubCompositionHtml(
332333
rewrittenContent = tagRootCompositionFile(rewrittenContent, compPath);
333334

334335
// Use the project's index.html <head> to preserve all dependencies
335-
const indexPath = join(projectDir, "index.html");
336+
const indexPath = resolveWithinProject(projectDir, "index.html");
336337
let headContent = "";
337338

338-
if (existsSync(indexPath)) headContent = rootHeadContent(readFileSync(indexPath, "utf-8"));
339+
if (indexPath && existsSync(indexPath))
340+
headContent = rootHeadContent(readFileSync(indexPath, "utf-8"));
339341

340342
// Inject <base> for relative asset resolution (before other tags)
341343
if (baseHref && !hasBaseElement(headContent)) {

0 commit comments

Comments
 (0)