path-traversal.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { expect, describe } from "bun:test"
  2. import { Cause, Effect, Exit } from "effect"
  3. import path from "path"
  4. import fs from "fs/promises"
  5. import { Filesystem } from "@/util/filesystem"
  6. import { File } from "../../src/file"
  7. import { InstanceState } from "../../src/effect/instance-state"
  8. import { containsPath } from "../../src/project/instance-context"
  9. import { TestInstance } from "../fixture/fixture"
  10. import { testEffect } from "../lib/effect"
  11. const it = testEffect(File.defaultLayer)
  12. const read = (file: string) => File.Service.use((svc) => svc.read(file))
  13. const list = (dir?: string) => File.Service.use((svc) => svc.list(dir))
  14. const expectAccessDenied = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
  15. Effect.gen(function* () {
  16. const exit = yield* effect.pipe(Effect.exit)
  17. if (Exit.isSuccess(exit)) throw new Error("expected access denied")
  18. expect(Cause.squash(exit.cause)).toHaveProperty("message", "Access denied: path escapes project directory")
  19. })
  20. describe("Filesystem.contains", () => {
  21. it.effect("allows paths within project", () =>
  22. Effect.sync(() => {
  23. expect(Filesystem.contains("/project", "/project/src")).toBe(true)
  24. expect(Filesystem.contains("/project", "/project/src/file.ts")).toBe(true)
  25. expect(Filesystem.contains("/project", "/project")).toBe(true)
  26. }),
  27. )
  28. it.effect("blocks ../ traversal", () =>
  29. Effect.sync(() => {
  30. expect(Filesystem.contains("/project", "/project/../etc")).toBe(false)
  31. expect(Filesystem.contains("/project", "/project/src/../../etc")).toBe(false)
  32. expect(Filesystem.contains("/project", "/etc/passwd")).toBe(false)
  33. }),
  34. )
  35. it.effect("blocks absolute paths outside project", () =>
  36. Effect.sync(() => {
  37. expect(Filesystem.contains("/project", "/etc/passwd")).toBe(false)
  38. expect(Filesystem.contains("/project", "/tmp/file")).toBe(false)
  39. expect(Filesystem.contains("/home/user/project", "/home/user/other")).toBe(false)
  40. }),
  41. )
  42. it.effect("handles prefix collision edge cases", () =>
  43. Effect.sync(() => {
  44. expect(Filesystem.contains("/project", "/project-other/file")).toBe(false)
  45. expect(Filesystem.contains("/project", "/projectfile")).toBe(false)
  46. }),
  47. )
  48. })
  49. /*
  50. * Integration tests for read() and list() path traversal protection.
  51. *
  52. * These tests verify the HTTP API code path is protected. The HTTP endpoints
  53. * in server.ts (GET /file/content, GET /file) call read()/list()
  54. * directly - they do NOT go through ReadTool or the agent permission layer.
  55. *
  56. * This is a SEPARATE code path from ReadTool, which has its own checks.
  57. */
  58. describe("File.read path traversal protection", () => {
  59. it.instance("rejects ../ traversal attempting to read /etc/passwd", () =>
  60. Effect.gen(function* () {
  61. const test = yield* TestInstance
  62. yield* Effect.promise(() => Bun.write(path.join(test.directory, "allowed.txt"), "allowed content"))
  63. yield* expectAccessDenied(read("../../../etc/passwd"))
  64. }),
  65. )
  66. it.instance("rejects deeply nested traversal", () =>
  67. Effect.gen(function* () {
  68. yield* expectAccessDenied(read("src/nested/../../../../../../../etc/passwd"))
  69. }),
  70. )
  71. it.instance("allows valid paths within project", () =>
  72. Effect.gen(function* () {
  73. const test = yield* TestInstance
  74. yield* Effect.promise(() => Bun.write(path.join(test.directory, "valid.txt"), "valid content"))
  75. const result = yield* read("valid.txt")
  76. expect(result.content).toBe("valid content")
  77. }),
  78. )
  79. })
  80. describe("File.list path traversal protection", () => {
  81. it.instance("rejects ../ traversal attempting to list /etc", () =>
  82. Effect.gen(function* () {
  83. yield* expectAccessDenied(list("../../../etc"))
  84. }),
  85. )
  86. it.instance("allows valid subdirectory listing", () =>
  87. Effect.gen(function* () {
  88. const test = yield* TestInstance
  89. yield* Effect.promise(() => Bun.write(path.join(test.directory, "subdir", "file.txt"), "content"))
  90. const result = yield* list("subdir")
  91. expect(Array.isArray(result)).toBe(true)
  92. }),
  93. )
  94. })
  95. describe("containsPath", () => {
  96. it.instance("returns true for path inside directory", () =>
  97. Effect.gen(function* () {
  98. const test = yield* TestInstance
  99. const ctx = yield* InstanceState.context
  100. expect(containsPath(path.join(test.directory, "foo.txt"), ctx)).toBe(true)
  101. expect(containsPath(path.join(test.directory, "src", "file.ts"), ctx)).toBe(true)
  102. }),
  103. { git: true },
  104. )
  105. it.instance(
  106. "returns true for path inside worktree but outside directory (monorepo subdirectory scenario)",
  107. () =>
  108. Effect.gen(function* () {
  109. const test = yield* TestInstance
  110. const subdir = path.join(test.directory, "packages", "lib")
  111. yield* Effect.promise(() => fs.mkdir(subdir, { recursive: true }))
  112. const ctx = { ...(yield* InstanceState.context), directory: subdir }
  113. // .opencode at worktree root, but we're running from packages/lib
  114. expect(containsPath(path.join(test.directory, ".opencode", "state"), ctx)).toBe(true)
  115. // sibling package should also be accessible
  116. expect(containsPath(path.join(test.directory, "packages", "other", "file.ts"), ctx)).toBe(true)
  117. // worktree root itself
  118. expect(containsPath(test.directory, ctx)).toBe(true)
  119. }),
  120. { git: true },
  121. )
  122. it.instance("returns false for path outside both directory and worktree", () =>
  123. Effect.gen(function* () {
  124. const ctx = yield* InstanceState.context
  125. expect(containsPath("/etc/passwd", ctx)).toBe(false)
  126. expect(containsPath("/tmp/other-project", ctx)).toBe(false)
  127. }),
  128. { git: true },
  129. )
  130. it.instance("returns false for path with .. escaping worktree", () =>
  131. Effect.gen(function* () {
  132. const test = yield* TestInstance
  133. const ctx = yield* InstanceState.context
  134. expect(containsPath(path.join(test.directory, "..", "escape.txt"), ctx)).toBe(false)
  135. }),
  136. { git: true },
  137. )
  138. it.instance("handles directory === worktree (running from repo root)", () =>
  139. Effect.gen(function* () {
  140. const test = yield* TestInstance
  141. const ctx = yield* InstanceState.context
  142. expect(ctx.directory).toBe(ctx.worktree)
  143. expect(containsPath(path.join(test.directory, "file.txt"), ctx)).toBe(true)
  144. expect(containsPath("/etc/passwd", ctx)).toBe(false)
  145. }),
  146. { git: true },
  147. )
  148. it.instance("non-git project does not allow arbitrary paths via worktree='/'", () =>
  149. Effect.gen(function* () {
  150. const test = yield* TestInstance
  151. const ctx = yield* InstanceState.context
  152. // worktree is "/" for non-git projects, but containsPath should NOT allow all paths
  153. expect(containsPath(path.join(test.directory, "file.txt"), ctx)).toBe(true)
  154. expect(containsPath("/etc/passwd", ctx)).toBe(false)
  155. expect(containsPath("/tmp/other", ctx)).toBe(false)
  156. }),
  157. )
  158. })