[PR #5985] feat: add path traversal protection to File.read and File.list #11684

Closed
opened 2026-02-16 18:16:36 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/5985

State: closed
Merged: Yes


Summary

Adds path containment checks to File.read() and File.list() to prevent directory traversal attacks (e.g., ../../../etc/passwd).

Problem

The File module constructs paths via path.join(Instance.directory, file) without validating containment. An attacker-controlled path like ../../../etc/passwd resolves to a valid path outside the project directory.

Solution

Uses the existing Filesystem.contains() utility (already used in tool/read.ts, tool/write.ts, etc.) to validate that resolved paths remain within Instance.directory. Throws on violation.

Changes

  • packages/opencode/src/file/index.ts: Added containment checks to File.read() and File.list()
  • packages/opencode/test/file/path-traversal.test.ts: Added tests for traversal prevention

Known Limitations (documented via TODO)

  • Symlinks inside the project can still escape (lexical check only)
  • Windows cross-drive paths may bypass the check

These are pre-existing limitations in Filesystem.contains() affecting all current callers and warrant a separate PR.

Testing

bun test test/file/path-traversal.test.ts
# 4 pass, 0 fail
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/5985 **State:** closed **Merged:** Yes --- ## Summary Adds path containment checks to `File.read()` and `File.list()` to prevent directory traversal attacks (e.g., `../../../etc/passwd`). ## Problem The `File` module constructs paths via `path.join(Instance.directory, file)` without validating containment. An attacker-controlled path like `../../../etc/passwd` resolves to a valid path outside the project directory. ## Solution Uses the existing `Filesystem.contains()` utility (already used in `tool/read.ts`, `tool/write.ts`, etc.) to validate that resolved paths remain within `Instance.directory`. Throws on violation. ## Changes - `packages/opencode/src/file/index.ts`: Added containment checks to `File.read()` and `File.list()` - `packages/opencode/test/file/path-traversal.test.ts`: Added tests for traversal prevention ## Known Limitations (documented via TODO) - Symlinks inside the project can still escape (lexical check only) - Windows cross-drive paths may bypass the check These are pre-existing limitations in `Filesystem.contains()` affecting all current callers and warrant a separate PR. ## Testing ``` bun test test/file/path-traversal.test.ts # 4 pass, 0 fail ```
yindo added the pull-request label 2026-02-16 18:16:36 -05:00
yindo closed this issue 2026-02-16 18:16:37 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11684