|
| 1 | +import { describe, it, expect, afterEach } from "vitest"; |
| 2 | +import { mkdtempSync, mkdirSync, writeFileSync, symlinkSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { isSafePath } from "./safePath.js"; |
| 6 | + |
| 7 | +describe("isSafePath", () => { |
| 8 | + const tmpDirs: string[] = []; |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + for (const d of tmpDirs) rmSync(d, { recursive: true, force: true }); |
| 12 | + tmpDirs.length = 0; |
| 13 | + }); |
| 14 | + |
| 15 | + function tmpDir(prefix: string): string { |
| 16 | + const dir = mkdtempSync(join(tmpdir(), prefix)); |
| 17 | + tmpDirs.push(dir); |
| 18 | + return dir; |
| 19 | + } |
| 20 | + |
| 21 | + // Mirror the repo convention (preview.test.ts): non-symlink-privileged Windows |
| 22 | + // runners can't create symlinks — skip those cases rather than crash the suite. |
| 23 | + function tryCreateSymlink(target: string, path: string, type: "dir" | "file"): boolean { |
| 24 | + try { |
| 25 | + symlinkSync(target, path, type); |
| 26 | + return true; |
| 27 | + } catch { |
| 28 | + return false; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + it("allows the base directory itself", () => { |
| 33 | + const base = tmpDir("safepath-base-"); |
| 34 | + expect(isSafePath(base, base)).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it("allows an existing nested path inside base", () => { |
| 38 | + const base = tmpDir("safepath-base-"); |
| 39 | + const file = join(base, "assets", "logo.png"); |
| 40 | + mkdirSync(join(base, "assets")); |
| 41 | + writeFileSync(file, "x"); |
| 42 | + expect(isSafePath(base, file)).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it("allows a not-yet-existing write target inside base", () => { |
| 46 | + const base = tmpDir("safepath-base-"); |
| 47 | + // Neither the dir nor the file exist yet — the create/write case. |
| 48 | + expect(isSafePath(base, join(base, "new", "deep", "file.txt"))).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it("rejects a `..` traversal that escapes base", () => { |
| 52 | + const base = tmpDir("safepath-base-"); |
| 53 | + expect(isSafePath(base, join(base, "..", "..", "etc", "passwd"))).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it("rejects an existing file reached through a symlink that points outside base", () => { |
| 57 | + const base = tmpDir("safepath-base-"); |
| 58 | + const external = tmpDir("safepath-external-"); |
| 59 | + const secret = join(external, "secret.txt"); |
| 60 | + writeFileSync(secret, "top secret"); |
| 61 | + // project/link -> external/ (the classic in-project symlink escape) |
| 62 | + if (!tryCreateSymlink(external, join(base, "link"), "dir")) return; |
| 63 | + expect(isSafePath(base, join(base, "link", "secret.txt"))).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it("rejects a not-yet-existing write target whose parent is a symlink to outside base", () => { |
| 67 | + const base = tmpDir("safepath-base-"); |
| 68 | + const external = tmpDir("safepath-external-"); |
| 69 | + // base/link -> external; writing base/link/evil.txt would land in external. |
| 70 | + if (!tryCreateSymlink(external, join(base, "link"), "dir")) return; |
| 71 | + expect(isSafePath(base, join(base, "link", "evil.txt"))).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it("rejects a file symlink inside base that targets a file outside base", () => { |
| 75 | + const base = tmpDir("safepath-base-"); |
| 76 | + const external = tmpDir("safepath-external-"); |
| 77 | + const secret = join(external, "secret.txt"); |
| 78 | + writeFileSync(secret, "top secret"); |
| 79 | + if (!tryCreateSymlink(secret, join(base, "passwd"), "file")) return; |
| 80 | + expect(isSafePath(base, join(base, "passwd"))).toBe(false); |
| 81 | + }); |
| 82 | + |
| 83 | + it("allows a symlink inside base that points to another location inside base", () => { |
| 84 | + const base = tmpDir("safepath-base-"); |
| 85 | + const realDir = join(base, "real"); |
| 86 | + mkdirSync(realDir); |
| 87 | + writeFileSync(join(realDir, "in.txt"), "x"); |
| 88 | + if (!tryCreateSymlink(realDir, join(base, "alias"), "dir")) return; |
| 89 | + expect(isSafePath(base, join(base, "alias", "in.txt"))).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it("canonicalizes base too: a symlinked base path still admits in-base targets", () => { |
| 93 | + // Guards against one-sided realpath: when base is reached via a symlink |
| 94 | + // (as on macOS where tmpdir lives under /var -> /private/var), an in-base |
| 95 | + // target must still be accepted. |
| 96 | + const realBase = tmpDir("safepath-realbase-"); |
| 97 | + const linkParent = tmpDir("safepath-linkparent-"); |
| 98 | + const baseLink = join(linkParent, "baseLink"); |
| 99 | + if (!tryCreateSymlink(realBase, baseLink, "dir")) return; |
| 100 | + writeFileSync(join(realBase, "file.txt"), "x"); |
| 101 | + expect(isSafePath(baseLink, join(baseLink, "file.txt"))).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it("fails closed when the base directory does not exist", () => { |
| 105 | + const base = join(tmpdir(), "safepath-does-not-exist-zzz", "nope"); |
| 106 | + expect(isSafePath(base, join(base, "file.txt"))).toBe(false); |
| 107 | + }); |
| 108 | +}); |
0 commit comments