Skip to content

Commit 8837d7c

Browse files
vanceingallsclaude
andcommitted
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) <[email protected]>
1 parent a897806 commit 8837d7c

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎packages/core/src/compiler/htmlBundler.test.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @vitest-environment node
2-
import { mkdtempSync, writeFileSync, mkdirSync, symlinkSync } from "node:fs";
2+
import { mkdtempSync, writeFileSync, mkdirSync, symlinkSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { parseHTML } from "linkedom";
@@ -53,6 +53,17 @@ function tryCreateSymlink(target: string, path: string, type: "dir" | "file"): b
5353
}
5454
}
5555

56+
function makeSymlinkProject(
57+
projectFiles: Record<string, string>,
58+
secretCss: string,
59+
): { dir: string; outsideDir: string } {
60+
const outsideDir = mkdtempSync(join(tmpdir(), "hf-outside-"));
61+
writeFileSync(join(outsideDir, "secret.css"), secretCss);
62+
const dir = makeTempProject(projectFiles);
63+
symlinkSync(join(outsideDir, "secret.css"), join(dir, "evil.css"));
64+
return { dir, outsideDir };
65+
}
66+
5667
describe("bundleToSingleHtml", () => {
5768
it("bundles a direct composition entry with paths relative to its file", async () => {
5869
const dir = makeTempProject({
@@ -1389,6 +1400,41 @@ describe("bundleToSingleHtml", () => {
13891400
warnSpy.mockRestore();
13901401
}
13911402
});
1403+
1404+
describe("symlink path traversal (security: F-005)", () => {
1405+
it("does not inline CSS from a symlink pointing outside projectDir", async () => {
1406+
const { dir, outsideDir } = makeSymlinkProject(
1407+
{
1408+
"index.html": `<!doctype html><html><head>
1409+
<link rel="stylesheet" href="evil.css"></head>
1410+
<body><div data-composition-id="root" data-width="320" data-height="180"></div></body></html>`,
1411+
},
1412+
".outside-secret { color: red; }",
1413+
);
1414+
try {
1415+
expect(await bundleToSingleHtml(dir)).not.toContain("outside-secret");
1416+
} finally {
1417+
rmSync(outsideDir, { recursive: true, force: true });
1418+
}
1419+
});
1420+
1421+
it("does not inline CSS via @import through a symlink pointing outside projectDir", async () => {
1422+
const { dir, outsideDir } = makeSymlinkProject(
1423+
{
1424+
"index.html": `<!doctype html><html><head>
1425+
<link rel="stylesheet" href="main.css"></head>
1426+
<body><div data-composition-id="root" data-width="320" data-height="180"></div></body></html>`,
1427+
"main.css": "@import './evil.css';",
1428+
},
1429+
".import-secret { color: blue; }",
1430+
);
1431+
try {
1432+
expect(await bundleToSingleHtml(dir)).not.toContain("import-secret");
1433+
} finally {
1434+
rmSync(outsideDir, { recursive: true, force: true });
1435+
}
1436+
});
1437+
});
13921438
});
13931439

13941440
/**

‎packages/core/src/compiler/htmlBundler.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ function isRelativeUrl(url: string): boolean {
8484
return !isNonRelativeUrl(url) && !isAbsolute(url);
8585
}
8686

87-
function safeReadFile(filePath: string): string | null {
87+
function safeReadFile(filePath: string, projectDir?: string): string | null {
8888
if (!existsSync(filePath)) return null;
89+
if (projectDir != null && !isSymlinkWithinProject(filePath, projectDir)) return null;
8990
try {
9091
return readFileSync(filePath, "utf-8");
9192
} catch {
@@ -235,7 +236,7 @@ function inlineCssFile(
235236
// project root; isSafePath also blocks symlink escapes (content is inlined).
236237
if (!isSafePath(projectDir, resolved)) return full;
237238
if (visited.has(resolved)) return "";
238-
const content = safeReadFile(resolved);
239+
const content = safeReadFile(resolved, projectDir);
239240
if (content == null) return full;
240241
visited.add(resolved);
241242
const inlined = inlineCssFile(content, dirname(resolved), projectDir, visited);

0 commit comments

Comments
 (0)