From 9e603e64037a3b4bffc64c4ff414aee7043338f5 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Wed, 11 Nov 2026 09:35:51 -0800 Subject: [PATCH 1/7] fix(core): keep git metadata and env files out of bundled HTML The bundler now checks every file it reads against the project's private paths (git metadata and env files, spelled directly or through a link) and leaves a refused reference as authored instead of inlining it. Bundle options gain a canRead predicate; Studio's preview passes its own private-file rule so the preview document matches what the file routes serve. Co-Authored-By: Claude Opus 5.5 --- .../core/src/compiler/htmlBundler.test.ts | 51 +++++++++++++++++++ packages/core/src/compiler/htmlBundler.ts | 35 +++++++++---- packages/core/src/safePath.ts | 31 +++++++++++ .../studio-server/src/helpers/safePath.ts | 24 ++------- .../studio-server/src/routes/preview.test.ts | 29 +++++++++++ packages/studio-server/src/routes/preview.ts | 1 + packages/studio-server/src/types.ts | 2 +- 7 files changed, 141 insertions(+), 32 deletions(-) diff --git a/packages/core/src/compiler/htmlBundler.test.ts b/packages/core/src/compiler/htmlBundler.test.ts index 00e1570ab19..fc9282c3f09 100644 --- a/packages/core/src/compiler/htmlBundler.test.ts +++ b/packages/core/src/compiler/htmlBundler.test.ts @@ -201,6 +201,57 @@ describe("bundleToSingleHtml", () => { expect(reads).not.toContain(join(dir, "notes.md")); }); + it("never inlines git metadata or env files, spelled directly or through a link", async () => { + const secret = "SECRET_TOKEN_VALUE"; + const dir = makeTempProject({ + "index.html": ` + + +
+ +
+
+
+ + + `, + "style.css": '@import ".env.local"; .a { color: red; }', + "scenes/intro.html": ``, + ".env": `TOKEN=${secret}`, + ".env.local": `TOKEN=${secret}`, + ".env.html": ``, + ".git/config": `[remote "origin"] url = https://${secret}@example.com/repo.git`, + }); + symlinkSync(join(dir, ".env"), join(dir, "linked.js")); + try { + const bundled = await bundleToSingleHtml(dir, { inlineAssets: true }); + expect(bundled).not.toContain(secret); + expect(bundled).not.toContain(Buffer.from(`TOKEN=${secret}`).toString("base64")); + expect(bundled).toContain(".a { color: red; }"); + expect(bundled).toContain("

intro

"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + it("leaves a file canRead refuses as an authored reference", async () => { + const dir = makeTempProject({ + "index.html": ` +
`, + "private.css": ".kept-out { color: red; }", + }); + try { + const bundled = await bundleToSingleHtml(dir, { + canRead: (file) => file !== join(dir, "private.css"), + }); + expect(bundled).not.toContain(".kept-out"); + expect(bundled).toContain('href="private.css"'); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + it.each([ [ "UTF-16LE", diff --git a/packages/core/src/compiler/htmlBundler.ts b/packages/core/src/compiler/htmlBundler.ts index 4f6e87b9621..ce90cbbbdd9 100644 --- a/packages/core/src/compiler/htmlBundler.ts +++ b/packages/core/src/compiler/htmlBundler.ts @@ -70,7 +70,7 @@ import { isLibraryCopy, refuseSwapsReachedByRootScripts, } from "./inlineSubCompositions"; -import { isSafePath, resolveWithinProject } from "../safePath.js"; +import { isSafePath, isVcsOrSecretFile, resolveWithinProject } from "../safePath.js"; import { ensureHfIds } from "@hyperframes/parsers/hf-ids"; import { readTextFile } from "@hyperframes/parsers/asset-resolution"; import { HF_COLOR_GRADING_ATTR } from "../colorGrading"; @@ -111,15 +111,27 @@ function isRelativeUrl(url: string): boolean { return !isNonRelativeUrl(url) && !isAbsolute(url); } -const bundleReads = new AsyncLocalStorage<(filePath: string) => void>(); +interface BundleReadScope { + projectDir: string; + canRead?: (filePath: string) => boolean; + noteRead: (filePath: string) => void; +} + +const bundleReads = new AsyncLocalStorage(); function noteRead(filePath: string): void { - bundleReads.getStore()?.(filePath); + bundleReads.getStore()?.noteRead(filePath); +} + +function readAllowed(filePath: string): boolean { + const scope = bundleReads.getStore(); + if (!scope) return true; + return !isVcsOrSecretFile(scope.projectDir, filePath) && (scope.canRead?.(filePath) ?? true); } function safeReadFile(filePath: string): string | null { noteRead(filePath); - if (!existsSync(filePath)) return null; + if (!readAllowed(filePath) || !existsSync(filePath)) return null; try { return readTextFile(filePath); } catch { @@ -290,7 +302,7 @@ function inlineCssFile( function safeReadFileBuffer(filePath: string): Buffer | null { noteRead(filePath); - if (!existsSync(filePath)) return null; + if (!readAllowed(filePath) || !existsSync(filePath)) return null; try { return readFileSync(filePath); } catch { @@ -912,6 +924,8 @@ export interface BundleOptions { staticGuard?: boolean; /** Called once per build for each project file the build reads or looks for. */ onRead?: (filePath: string) => void; + /** Project files the build may read; a refused file keeps its authored reference, not inlined. */ + canRead?: (filePath: string) => boolean; } /** @@ -1038,16 +1052,15 @@ function hoistCompositionScripts( } export function bundleToSingleHtml(projectDir: string, options?: BundleOptions): Promise { - const bundle = () => bundleProject(projectDir, options); const onRead = options?.onRead; - if (!onRead) return bundle(); const noted = new Set(); - const noteOnce = (filePath: string) => { + const noteRead = (filePath: string) => { if (noted.has(filePath)) return; noted.add(filePath); - onRead(filePath); + onRead?.(filePath); }; - return bundleReads.run(noteOnce, bundle); + const scope: BundleReadScope = { projectDir, canRead: options?.canRead, noteRead }; + return bundleReads.run(scope, () => bundleProject(projectDir, options)); } // A