Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/opencode/src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ export namespace File {
const full = path.join(Instance.directory, file)

// TODO: Filesystem.contains is lexical only - symlinks inside the project can escape.
// TODO: On Windows, cross-drive paths bypass this check. Consider realpath canonicalization.
if (!Instance.containsPath(full)) {
throw new Error(`Access denied: path escapes project directory`)
}
Expand Down Expand Up @@ -573,7 +572,6 @@ export namespace File {
const resolved = dir ? path.join(Instance.directory, dir) : Instance.directory

// TODO: Filesystem.contains is lexical only - symlinks inside the project can escape.
// TODO: On Windows, cross-drive paths bypass this check. Consider realpath canonicalization.
if (!Instance.containsPath(resolved)) {
throw new Error(`Access denied: path escapes project directory`)
}
Expand Down
8 changes: 8 additions & 0 deletions packages/opencode/src/util/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ export namespace Filesystem {
}

export function contains(parent: string, child: string) {
// On Windows, path.relative across different drive letters (e.g. D:\project vs C:\evil)
// returns the absolute child path instead of a ".." prefixed relative path,
// which causes a false positive (the check thinks the child is inside the parent).
if (process.platform === "win32") {
const parentRoot = parent.slice(0, 3).toUpperCase()
const childRoot = child.slice(0, 3).toUpperCase()
if (parentRoot !== childRoot) return false
}
return !relative(parent, child).startsWith("..")
}

Expand Down
22 changes: 22 additions & 0 deletions packages/opencode/test/file/path-traversal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ describe("Filesystem.contains", () => {
expect(Filesystem.contains("/project", "/project-other/file")).toBe(false)
expect(Filesystem.contains("/project", "/projectfile")).toBe(false)
})

if (process.platform === "win32") {
test("blocks cross-drive paths on Windows", () => {
expect(Filesystem.contains("D:\\project", "C:\\evil\\file.txt")).toBe(false)
expect(Filesystem.contains("D:\\project", "C:\\Windows\\System32\\config")).toBe(false)
expect(Filesystem.contains("C:\\Users\\user\\project", "D:\\other\\file")).toBe(false)
})

test("allows same-drive paths on Windows", () => {
expect(Filesystem.contains("D:\\project", "D:\\project\\src\\file.ts")).toBe(true)
expect(Filesystem.contains("C:\\Users\\project", "C:\\Users\\project\\file.ts")).toBe(true)
})

test("handles case-insensitive drive letters on Windows", () => {
expect(Filesystem.contains("d:\\project", "D:\\project\\file.ts")).toBe(true)
expect(Filesystem.contains("D:\\project", "d:\\project\\file.ts")).toBe(true)
})

test("blocks cross-drive with forward slashes on Windows", () => {
expect(Filesystem.contains("D:/project", "C:/evil/file.txt")).toBe(false)
})
}
})

/*
Expand Down
Loading