|
| 1 | +/** |
| 2 | + * Cross-platform containment + external-asset-key tests. |
| 3 | + * |
| 4 | + * Regression coverage for GH #321 — on Windows, every external asset was |
| 5 | + * wrongly rejected as "unsafe path" because the containment check used |
| 6 | + * `startsWith(parent + "/")` and the safe key carried a drive-letter |
| 7 | + * colon that made the downstream `path.join` absolute. |
| 8 | + * |
| 9 | + * We exercise both OS layouts by posing the hypothetical paths the |
| 10 | + * respective platforms would generate — the logic itself is expressed |
| 11 | + * using `path.relative()` so it works regardless of the runtime OS. |
| 12 | + */ |
| 13 | + |
| 14 | +import { describe, expect, it } from "vitest"; |
| 15 | +import { resolve } from "node:path"; |
| 16 | + |
| 17 | +import { isPathInside, toExternalAssetKey } from "./paths.js"; |
| 18 | + |
| 19 | +describe("isPathInside", () => { |
| 20 | + it("returns true when child is directly inside parent", () => { |
| 21 | + expect(isPathInside(resolve("/foo/bar/baz.wav"), resolve("/foo/bar"))).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it("returns true when child is deeply nested inside parent", () => { |
| 25 | + expect(isPathInside(resolve("/foo/bar/a/b/c/d.wav"), resolve("/foo/bar"))).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it("returns true when child equals parent (a dir contains itself)", () => { |
| 29 | + expect(isPathInside(resolve("/foo/bar"), resolve("/foo/bar"))).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it("returns false when child is a sibling whose name starts with parent", () => { |
| 33 | + // Regression: the old `startsWith(parent + "/")` accidentally worked for |
| 34 | + // this case, but a naive rewrite without the trailing separator would |
| 35 | + // admit `/foo/bar-sibling` as a child of `/foo/bar`. Verify we don't. |
| 36 | + expect(isPathInside(resolve("/foo/bar-sibling/x"), resolve("/foo/bar"))).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns false when child is outside parent", () => { |
| 40 | + expect(isPathInside(resolve("/tmp/evil/file.wav"), resolve("/foo/bar"))).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it("returns false when child resolves above parent via ..", () => { |
| 44 | + expect(isPathInside(resolve("/foo/bar/../../etc/passwd"), resolve("/foo/bar"))).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it("normalises trailing slashes on parent", () => { |
| 48 | + expect(isPathInside(resolve("/foo/bar/baz"), resolve("/foo/bar/"))).toBe(true); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("toExternalAssetKey", () => { |
| 53 | + it("prefixes with hf-ext/ and keeps a Unix absolute path", () => { |
| 54 | + expect(toExternalAssetKey("/Users/miguel/assets/segment.wav")).toBe( |
| 55 | + "hf-ext/Users/miguel/assets/segment.wav", |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it("converts Windows drive-letter paths to a colonless, slash-delimited key", () => { |
| 60 | + // GH #321: `D:\coder\reactGin\hyperframes\reading\assets\segment_001.wav` |
| 61 | + // used to become `hf-ext/D:\coder\...`, which makes the downstream |
| 62 | + // `path.join(compileDir, key)` absolute on Windows (drive letter wins). |
| 63 | + expect( |
| 64 | + toExternalAssetKey("D:\\coder\\reactGin\\hyperframes\\reading\\assets\\segment_001.wav"), |
| 65 | + ).toBe("hf-ext/D/coder/reactGin/hyperframes/reading/assets/segment_001.wav"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("handles Windows paths with forward slashes (mixed separators)", () => { |
| 69 | + expect(toExternalAssetKey("C:/Users/Alice/Downloads/clip.mp4")).toBe( |
| 70 | + "hf-ext/C/Users/Alice/Downloads/clip.mp4", |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it("lowercases / uppercases drive letters faithfully (we don't munge)", () => { |
| 75 | + expect(toExternalAssetKey("e:\\data\\a.wav")).toBe("hf-ext/e/data/a.wav"); |
| 76 | + expect(toExternalAssetKey("Z:\\data\\a.wav")).toBe("hf-ext/Z/data/a.wav"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("is idempotent for already-sanitised input", () => { |
| 80 | + // If someone double-wraps, the result is still a safe relative key. |
| 81 | + // The drive-letter detector only fires on the first char class. |
| 82 | + const once = toExternalAssetKey("/foo/bar.mp3"); |
| 83 | + const twice = toExternalAssetKey(once); |
| 84 | + expect(twice.startsWith("hf-ext/")).toBe(true); |
| 85 | + expect(twice).not.toContain(":"); |
| 86 | + expect(twice).not.toContain("\\"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("keeps UNIX paths stable after an extra leading slash", () => { |
| 90 | + expect(toExternalAssetKey("//foo/bar.mp3")).toBe("hf-ext/foo/bar.mp3"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("produces a key that path.join(compileDir, key) keeps inside compileDir", () => { |
| 94 | + // The real failure mode from #321: on Windows, join(compileDir, key) with |
| 95 | + // a key containing a drive letter silently escaped compileDir. Our key |
| 96 | + // must be a pure relative path — no `:`, no leading separator — so |
| 97 | + // `isPathInside(join(compileDir, key), compileDir)` is always true. |
| 98 | + const key = toExternalAssetKey("D:\\evil\\x.wav"); |
| 99 | + // Key cannot start with a separator or drive letter. |
| 100 | + expect(key.startsWith("/")).toBe(false); |
| 101 | + expect(/^[A-Za-z]:/.test(key)).toBe(false); |
| 102 | + }); |
| 103 | +}); |
0 commit comments