|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2026 Archgate |
| 3 | +import { describe, expect, test, beforeEach, afterEach } from "bun:test"; |
| 4 | +import { |
| 5 | + mkdtempSync, |
| 6 | + rmSync, |
| 7 | + mkdirSync, |
| 8 | + symlinkSync, |
| 9 | + writeFileSync, |
| 10 | +} from "node:fs"; |
| 11 | +import { tmpdir } from "node:os"; |
| 12 | +import { join, resolve } from "node:path"; |
| 13 | + |
| 14 | +import { |
| 15 | + isWithinRoot, |
| 16 | + resolveUserPath, |
| 17 | + safePath, |
| 18 | +} from "../../src/engine/safe-path"; |
| 19 | + |
| 20 | +describe("safe-path", () => { |
| 21 | + let tempDir: string; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + tempDir = resolve(mkdtempSync(join(tmpdir(), "archgate-safe-path-"))); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + rmSync(tempDir, { recursive: true, force: true }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("resolveUserPath", () => { |
| 32 | + test("resolves a relative path against the root", () => { |
| 33 | + expect(resolveUserPath(tempDir, "src/app.ts")).toBe( |
| 34 | + resolve(tempDir, "src", "app.ts") |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + test("keeps an absolute path as-is (resolved)", () => { |
| 39 | + const abs = join(tempDir, "config.yml"); |
| 40 | + expect(resolveUserPath(tempDir, abs)).toBe(resolve(abs)); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe("isWithinRoot", () => { |
| 45 | + test("accepts the root itself and paths under it", () => { |
| 46 | + expect(isWithinRoot(tempDir, tempDir)).toBe(true); |
| 47 | + expect(isWithinRoot(tempDir, join(tempDir, "a", "b"))).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + test("rejects siblings and parents", () => { |
| 51 | + expect(isWithinRoot(tempDir, resolve(tempDir, ".."))).toBe(false); |
| 52 | + expect(isWithinRoot(tempDir, `${tempDir}-sibling`)).toBe(false); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe("safePath", () => { |
| 57 | + test("returns the absolute path for an in-root file", () => { |
| 58 | + writeFileSync(join(tempDir, "ok.txt"), "x"); |
| 59 | + expect(safePath(tempDir, "ok.txt")).toBe(join(tempDir, "ok.txt")); |
| 60 | + }); |
| 61 | + |
| 62 | + test("accepts a deep chain of real directories", () => { |
| 63 | + mkdirSync(join(tempDir, "a", "b", "c"), { recursive: true }); |
| 64 | + writeFileSync(join(tempDir, "a", "b", "c", "deep.txt"), "x"); |
| 65 | + expect(() => safePath(tempDir, "a/b/c/deep.txt")).not.toThrow(); |
| 66 | + }); |
| 67 | + |
| 68 | + test("does not throw for a non-existent in-root path", () => { |
| 69 | + expect(() => safePath(tempDir, "missing/file.txt")).not.toThrow(); |
| 70 | + }); |
| 71 | + |
| 72 | + test("accepts the root itself", () => { |
| 73 | + expect(safePath(tempDir, ".")).toBe(tempDir); |
| 74 | + }); |
| 75 | + |
| 76 | + test("throws on traversal outside the root", () => { |
| 77 | + expect(() => safePath(tempDir, "../outside.txt")).toThrow( |
| 78 | + /escapes project root/u |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + test("throws when an ANCESTOR directory is a symlink", () => { |
| 83 | + // The leaf is an ordinary file — only the parent is a link, which a |
| 84 | + // leaf-only lstat cannot see. |
| 85 | + const outsideDir = mkdtempSync(join(tmpdir(), "archgate-outside-")); |
| 86 | + writeFileSync(join(outsideDir, "secret.txt"), "sensitive"); |
| 87 | + try { |
| 88 | + // "junction" is ignored on POSIX; on Windows it needs no admin rights |
| 89 | + // and lstat reports it as a symlink, so this runs on every platform. |
| 90 | + symlinkSync(outsideDir, join(tempDir, "linkdir"), "junction"); |
| 91 | + } catch { |
| 92 | + rmSync(outsideDir, { recursive: true, force: true }); |
| 93 | + return; |
| 94 | + } |
| 95 | + expect(() => safePath(tempDir, "linkdir/secret.txt")).toThrow( |
| 96 | + /traverses symbolic link "linkdir" — access denied/u |
| 97 | + ); |
| 98 | + rmSync(outsideDir, { recursive: true, force: true }); |
| 99 | + }); |
| 100 | + |
| 101 | + test("throws when the LEAF itself is a symlink", () => { |
| 102 | + const outsideDir = mkdtempSync(join(tmpdir(), "archgate-outside-")); |
| 103 | + writeFileSync(join(outsideDir, "secret.txt"), "sensitive"); |
| 104 | + try { |
| 105 | + symlinkSync(join(outsideDir, "secret.txt"), join(tempDir, "link.txt")); |
| 106 | + } catch { |
| 107 | + // File symlinks still need admin/developer mode on Windows — skip. |
| 108 | + rmSync(outsideDir, { recursive: true, force: true }); |
| 109 | + return; |
| 110 | + } |
| 111 | + expect(() => safePath(tempDir, "link.txt")).toThrow( |
| 112 | + /is a symbolic link — access denied/u |
| 113 | + ); |
| 114 | + rmSync(outsideDir, { recursive: true, force: true }); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments