|
1 | 1 | // @vitest-environment node |
2 | | -import { mkdtempSync, writeFileSync, mkdirSync } from "node:fs"; |
| 2 | +import { mkdtempSync, writeFileSync, mkdirSync, symlinkSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
5 | 5 | import { parseHTML } from "linkedom"; |
@@ -50,6 +50,47 @@ describe("bundleToSingleHtml", () => { |
50 | 50 | expect(bundled).toContain('document.getElementById("scene")'); |
51 | 51 | }); |
52 | 52 |
|
| 53 | + it("inlines an in-project sub-composition script but not one reached through a symlink escaping the project root", async () => { |
| 54 | + // Security: a shared/cloned project may carry a symlink pointing outside the |
| 55 | + // root (e.g. ext -> /etc). The bundler reads+inlines local assets, so it must |
| 56 | + // refuse to follow such a symlink and leak external file contents. |
| 57 | + const dir = makeTempProject({ |
| 58 | + "index.html": `<!doctype html> |
| 59 | +<html><head> |
| 60 | + <script src="https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"></script> |
| 61 | +</head><body> |
| 62 | + <div id="root" data-composition-id="main" data-width="1920" data-height="1080"> |
| 63 | + <div id="scene-host" |
| 64 | + data-composition-id="scene" |
| 65 | + data-composition-src="compositions/scene.html" |
| 66 | + data-start="0" data-duration="5"></div> |
| 67 | + </div> |
| 68 | + <script>window.__timelines={}; const tl=gsap.timeline({paused:true}); window.__timelines["main"]=tl;</script> |
| 69 | +</body></html>`, |
| 70 | + "compositions/scene.html": `<template id="scene-template"> |
| 71 | + <div data-composition-id="scene" data-width="1920" data-height="1080"> |
| 72 | + <script src="assets/local.js"></script> |
| 73 | + <script src="ext/secret.js"></script> |
| 74 | + <script> |
| 75 | + window.__timelines = window.__timelines || {}; |
| 76 | + window.__timelines["scene"] = gsap.timeline({ paused: true }); |
| 77 | + </script> |
| 78 | + </div> |
| 79 | +</template>`, |
| 80 | + "assets/local.js": `window.__HF_LOCAL__ = "LOCAL_MARKER_INLINED";`, |
| 81 | + }); |
| 82 | + const external = mkdtempSync(join(tmpdir(), "hf-bundler-external-")); |
| 83 | + writeFileSync(join(external, "secret.js"), `window.__HF_SECRET__ = "SECRET_MARKER_LEAKED";`); |
| 84 | + symlinkSync(external, join(dir, "ext"), "dir"); |
| 85 | + |
| 86 | + const bundled = await bundleToSingleHtml(dir); |
| 87 | + |
| 88 | + // Positive control: the in-project sub-comp script IS inlined, so the bundler |
| 89 | + // would have inlined the symlinked one too had isSafePath not rejected it. |
| 90 | + expect(bundled).toContain("LOCAL_MARKER_INLINED"); |
| 91 | + expect(bundled).not.toContain("SECRET_MARKER_LEAKED"); |
| 92 | + }); |
| 93 | + |
53 | 94 | it("produces a self-contained runtime script when no HYPERFRAME_RUNTIME_URL is set", async () => { |
54 | 95 | // Regression guard: hf#XXX. The bundler used to emit |
55 | 96 | // <script ... src=""></script> when no runtime URL was configured. An |
|
0 commit comments