fix(core): prevent symlink path traversal in htmlBundler safePath (F-005) (#1214)

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

* 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 (`<link href>` through a symlink, and `@import` through one).

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vance Ingalls
2026-08-20 23:37:11 -07:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 36c7dffe5c
commit c09b0b9183
+47 -1
View File
@@ -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<string, string>,
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": `<!doctype html><html><head>
<link rel="stylesheet" href="evil.css"></head>
<body><div data-composition-id="root" data-width="320" data-height="180"></div></body></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": `<!doctype html><html><head>
<link rel="stylesheet" href="main.css"></head>
<body><div data-composition-id="root" data-width="320" data-height="180"></div></body></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 });
}
});
});
});
/**