Skip to content

Commit c09b0b9

Browse files
vanceingallsclaude
andauthored
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) <[email protected]> * 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) <[email protected]>
1 parent 36c7dff commit c09b0b9

1 file changed

Lines changed: 47 additions & 1 deletion

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
/**

0 commit comments

Comments
 (0)