From c09b0b91830f26e34e2393d0c5ac8abc8b2da027 Mon Sep 17 00:00:00 2001 From: Vance Ingalls Date: Thu, 20 Aug 2026 23:37:11 -0700 Subject: [PATCH] fix(core): prevent symlink path traversal in htmlBundler safePath (F-005) (#1214) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(core): prevent symlink path traversal in htmlBundler safePath (F-005) safePath used resolve() for containment checking, which is lexical and does not follow symlinks. A symlink placed inside the project directory pointing at a file outside it would pass the startsWith(normalizedBase) check and expose arbitrary on-disk content to the bundle. Add isSymlinkWithinProject(), which calls realpathSync() on both the candidate and the project root and re-verifies containment after symlinks are resolved. safePath calls it after the lexical check; safeReadFile gains an optional projectDir parameter that triggers the same check when handling @import-resolved CSS paths (the @import code path bypasses safePath and reads the file directly, so the check is applied there instead). Both attack vectors are covered by new vitest tests that plant a symlink inside the project dir pointing at a file in a sibling tmpdir. Co-Authored-By: Claude Opus 4.8 (1M context) * fix(core): drop the redundant safeReadFile symlink guard, keep its regressions The branch's remaining production hunk called `isSymlinkWithinProject`, a helper that no longer exists: when four of the five original commits were cherry-picked to `main`, that helper was folded into `isSafePath`, which now canonicalizes both sides with `realpathSync`. So the leftover commit did not compile — `TS2304: Cannot find name 'isSymlinkWithinProject'` on Linux and Windows, with Test and regression failing downstream of the build. Swapping in `isSafePath(projectDir, filePath)` compiles, but measurably guards nothing: the sole caller that passes `projectDir` (`inlineCssFile`'s `@import` arm) runs `isSafePath(projectDir, resolved)` on the same path one line above, and both symlink regressions pass with the guard removed. A check no test can distinguish is weight, not defence, so the production delta goes and the tests stay — they now pin the `isSafePath` guard on both attack surfaces (`` through a symlink, and `@import` through one). --------- Co-authored-by: Claude Opus 4.8 (1M context) --- .../core/src/compiler/htmlBundler.test.ts | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/core/src/compiler/htmlBundler.test.ts b/packages/core/src/compiler/htmlBundler.test.ts index d28f7622fc..43c2b94caf 100644 --- a/packages/core/src/compiler/htmlBundler.test.ts +++ b/packages/core/src/compiler/htmlBundler.test.ts @@ -1,5 +1,5 @@ // @vitest-environment node -import { mkdtempSync, writeFileSync, mkdirSync, symlinkSync } from "node:fs"; +import { mkdtempSync, writeFileSync, mkdirSync, symlinkSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { parseHTML } from "linkedom"; @@ -53,6 +53,17 @@ function tryCreateSymlink(target: string, path: string, type: "dir" | "file"): b } } +function makeSymlinkProject( + projectFiles: Record, + secretCss: string, +): { dir: string; outsideDir: string } { + const outsideDir = mkdtempSync(join(tmpdir(), "hf-outside-")); + writeFileSync(join(outsideDir, "secret.css"), secretCss); + const dir = makeTempProject(projectFiles); + symlinkSync(join(outsideDir, "secret.css"), join(dir, "evil.css")); + return { dir, outsideDir }; +} + describe("bundleToSingleHtml", () => { it("bundles a direct composition entry with paths relative to its file", async () => { const dir = makeTempProject({ @@ -1389,6 +1400,41 @@ describe("bundleToSingleHtml", () => { warnSpy.mockRestore(); } }); + + describe("symlink path traversal (security: F-005)", () => { + it("does not inline CSS from a symlink pointing outside projectDir", async () => { + const { dir, outsideDir } = makeSymlinkProject( + { + "index.html": ` + +
`, + }, + ".outside-secret { color: red; }", + ); + try { + expect(await bundleToSingleHtml(dir)).not.toContain("outside-secret"); + } finally { + rmSync(outsideDir, { recursive: true, force: true }); + } + }); + + it("does not inline CSS via @import through a symlink pointing outside projectDir", async () => { + const { dir, outsideDir } = makeSymlinkProject( + { + "index.html": ` + +
`, + "main.css": "@import './evil.css';", + }, + ".import-secret { color: blue; }", + ); + try { + expect(await bundleToSingleHtml(dir)).not.toContain("import-secret"); + } finally { + rmSync(outsideDir, { recursive: true, force: true }); + } + }); + }); }); /**