-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat: add path traversal protection to File.read and File.list #5985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add path traversal protection to File.read and File.list #5985
Conversation
|
/review |
|
lgtm |
|
I'd argue it would be good to use a whitelist / blacklist / have some sort of config setting configure this. Preventing malicious path traversals is important, but I find myself having genuine use cases for path traversal. E.g. cloning repos to study in /tmp or having opencode read / edit some config files (outside of current project) We could:
|
|
The agent tools already have a permission system for external paths and aren't affected here. This PR only hardens the UI file browser API, which should be scoped to the project directory. For external repos, you'd run opencode from that directory. |
|
The |
|
oh right i forgot |
|
i misread rhe description my b |
…aversal Adds integration tests that directly verify File.read() and File.list() reject path traversal attempts. Documents the distinction between HTTP API and agent tool code paths.

Summary
Adds path containment checks to
File.read()andFile.list()to prevent directory traversal attacks (e.g.,../../../etc/passwd).Problem
The
Filemodule constructs paths viapath.join(Instance.directory, file)without validating containment. An attacker-controlled path like../../../etc/passwdresolves to a valid path outside the project directory.Solution
Uses the existing
Filesystem.contains()utility (already used intool/read.ts,tool/write.ts, etc.) to validate that resolved paths remain withinInstance.directory. Throws on violation.Changes
packages/opencode/src/file/index.ts: Added containment checks toFile.read()andFile.list()packages/opencode/test/file/path-traversal.test.ts: Added tests for traversal preventionKnown Limitations (documented via TODO)
These are pre-existing limitations in
Filesystem.contains()affecting all current callers and warrant a separate PR.Testing