|
| 1 | +import { test, expect, describe } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import { Filesystem } from "../../src/util/filesystem" |
| 4 | +import { File } from "../../src/file" |
| 5 | +import { Instance } from "../../src/project/instance" |
| 6 | +import { tmpdir } from "../fixture/fixture" |
| 7 | + |
| 8 | +describe("Filesystem.contains", () => { |
| 9 | + test("allows paths within project", () => { |
| 10 | + expect(Filesystem.contains("/project", "/project/src")).toBe(true) |
| 11 | + expect(Filesystem.contains("/project", "/project/src/file.ts")).toBe(true) |
| 12 | + expect(Filesystem.contains("/project", "/project")).toBe(true) |
| 13 | + }) |
| 14 | + |
| 15 | + test("blocks ../ traversal", () => { |
| 16 | + expect(Filesystem.contains("/project", "/project/../etc")).toBe(false) |
| 17 | + expect(Filesystem.contains("/project", "/project/src/../../etc")).toBe(false) |
| 18 | + expect(Filesystem.contains("/project", "/etc/passwd")).toBe(false) |
| 19 | + }) |
| 20 | + |
| 21 | + test("blocks absolute paths outside project", () => { |
| 22 | + expect(Filesystem.contains("/project", "/etc/passwd")).toBe(false) |
| 23 | + expect(Filesystem.contains("/project", "/tmp/file")).toBe(false) |
| 24 | + expect(Filesystem.contains("/home/user/project", "/home/user/other")).toBe(false) |
| 25 | + }) |
| 26 | + |
| 27 | + test("handles prefix collision edge cases", () => { |
| 28 | + expect(Filesystem.contains("/project", "/project-other/file")).toBe(false) |
| 29 | + expect(Filesystem.contains("/project", "/projectfile")).toBe(false) |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +/* |
| 34 | + * Integration tests for File.read() and File.list() path traversal protection. |
| 35 | + * |
| 36 | + * These tests verify the HTTP API code path is protected. The HTTP endpoints |
| 37 | + * in server.ts (GET /file/content, GET /file) call File.read()/File.list() |
| 38 | + * directly - they do NOT go through ReadTool or the agent permission layer. |
| 39 | + * |
| 40 | + * This is a SEPARATE code path from ReadTool, which has its own checks. |
| 41 | + */ |
| 42 | +describe("File.read path traversal protection", () => { |
| 43 | + test("rejects ../ traversal attempting to read /etc/passwd", async () => { |
| 44 | + await using tmp = await tmpdir({ |
| 45 | + init: async (dir) => { |
| 46 | + await Bun.write(path.join(dir, "allowed.txt"), "allowed content") |
| 47 | + }, |
| 48 | + }) |
| 49 | + |
| 50 | + await Instance.provide({ |
| 51 | + directory: tmp.path, |
| 52 | + fn: async () => { |
| 53 | + await expect(File.read("../../../etc/passwd")).rejects.toThrow("Access denied: path escapes project directory") |
| 54 | + }, |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + test("rejects deeply nested traversal", async () => { |
| 59 | + await using tmp = await tmpdir() |
| 60 | + |
| 61 | + await Instance.provide({ |
| 62 | + directory: tmp.path, |
| 63 | + fn: async () => { |
| 64 | + await expect(File.read("src/nested/../../../../../../../etc/passwd")).rejects.toThrow( |
| 65 | + "Access denied: path escapes project directory", |
| 66 | + ) |
| 67 | + }, |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + test("allows valid paths within project", async () => { |
| 72 | + await using tmp = await tmpdir({ |
| 73 | + init: async (dir) => { |
| 74 | + await Bun.write(path.join(dir, "valid.txt"), "valid content") |
| 75 | + }, |
| 76 | + }) |
| 77 | + |
| 78 | + await Instance.provide({ |
| 79 | + directory: tmp.path, |
| 80 | + fn: async () => { |
| 81 | + const result = await File.read("valid.txt") |
| 82 | + expect(result.content).toBe("valid content") |
| 83 | + }, |
| 84 | + }) |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +describe("File.list path traversal protection", () => { |
| 89 | + test("rejects ../ traversal attempting to list /etc", async () => { |
| 90 | + await using tmp = await tmpdir() |
| 91 | + |
| 92 | + await Instance.provide({ |
| 93 | + directory: tmp.path, |
| 94 | + fn: async () => { |
| 95 | + await expect(File.list("../../../etc")).rejects.toThrow("Access denied: path escapes project directory") |
| 96 | + }, |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + test("allows valid subdirectory listing", async () => { |
| 101 | + await using tmp = await tmpdir({ |
| 102 | + init: async (dir) => { |
| 103 | + await Bun.write(path.join(dir, "subdir", "file.txt"), "content") |
| 104 | + }, |
| 105 | + }) |
| 106 | + |
| 107 | + await Instance.provide({ |
| 108 | + directory: tmp.path, |
| 109 | + fn: async () => { |
| 110 | + const result = await File.list("subdir") |
| 111 | + expect(Array.isArray(result)).toBe(true) |
| 112 | + }, |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |