GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add path traversal protection to File.read and File.list (#5985) · naraga/opencode@3c02d5d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/opencode
    • src/file
      • index.ts
    • test/file
      • path-traversal.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/opencode/src/file/index.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/file/index.ts
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from "path"
77
import fs from "fs"
88
import ignore from "ignore"
99
import { Log } from "../util/log"
10+
import { Filesystem } from "../util/filesystem"
1011
import { Instance } from "../project/instance"
1112
import { Ripgrep } from "./ripgrep"
1213
import fuzzysort from "fuzzysort"
@@ -235,6 +236,13 @@ export namespace File {
235236
using _ = log.time("read", { file })
236237
const project = Instance.project
237238
const full = path.join(Instance.directory, file)
239+
240+
// TODO: Filesystem.contains is lexical only - symlinks inside the project can escape.
241+
// TODO: On Windows, cross-drive paths bypass this check. Consider realpath canonicalization.
242+
if (!Filesystem.contains(Instance.directory, full)) {
243+
throw new Error(`Access denied: path escapes project directory`)
244+
}
245+
238246
const bunFile = Bun.file(full)
239247

240248
if (!(await bunFile.exists())) {
@@ -288,6 +296,13 @@ export namespace File {
288296
ignored = ig.ignores.bind(ig)
289297
}
290298
const resolved = dir ? path.join(Instance.directory, dir) : Instance.directory
299+
300+
// TODO: Filesystem.contains is lexical only - symlinks inside the project can escape.
301+
// TODO: On Windows, cross-drive paths bypass this check. Consider realpath canonicalization.
302+
if (!Filesystem.contains(Instance.directory, resolved)) {
303+
throw new Error(`Access denied: path escapes project directory`)
304+
}
305+
291306
const nodes: Node[] = []
292307
for (const entry of await fs.promises
293308
.readdir(resolved, {
Collapse file

‎packages/opencode/test/file/path-traversal.test.ts‎

Copy file name to clipboard
+115Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
})

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.