Summary
OpenCode versions before 0.31.0 do not properly validate the x-opencode-directory request header used by the /api/opencode proxy endpoint. A remote attacker may provide an arbitrary directory path, causing file operations to occur outside the intended project root when the endpoint is reachable.
Affected context
The vulnerability occurs in deployments that expose the /api/opencode proxy endpoint and use the x-opencode-directory header to select or resolve a working directory. Risk increases when the header is accepted from untrusted clients without canonicalization, boundary checks, or enforcement that the resolved path remains within the configured project root.
Exploit preconditions
- Attack surface: Network-accessible
/api/opencodeproxy endpoint - Required access: Ability to send requests to the endpoint; authentication requirements depend on deployment configuration
- Trigger point: Processing of the
x-opencode-directoryrequest header - Why exploitation is possible: Insufficient validation allows attacker-controlled paths to influence file-operation targets outside the permitted project directory
Why this happens
The underlying bug is insufficient validation of an externally controlled path before it is used in filesystem operations. This maps to CWE-73: External Control of File Name or Path, where request data influences a filename or directory without reliably enforcing an approved path boundary, canonical form, or allowed location.
Potential impact
- Unauthorized file operations outside the project root
- Exposure, modification, or deletion of files accessible to the service
- Tampering with project data or runtime configuration
- Possible compromise of dependent workflows if modified files are later trusted or executed
- Broader host impact if the service has excessive filesystem privileges
General mitigation guidance
Mitigation generally focuses on removing direct trust in client-supplied paths and enforcing a secure filesystem boundary.
Common defensive patterns include:
- Resolve paths against a fixed trusted root and verify the canonical result remains within that root
- Prefer server-side identifiers or allowlisted directory mappings over raw path headers
- Reject absolute paths, traversal components, ambiguous encodings, and symlink escapes where appropriate
- Apply consistent validation before every file operation rather than only at the request boundary
- Run the service with the least filesystem privilege necessary
- Use read-only mounts and isolated workspaces when write access is not required
- Avoid exposing the proxy endpoint beyond trusted network boundaries
How teams detect issues like this
Organizations can identify this class of issue through:
- Static analysis rules that trace request headers or parameters into filesystem APIs
- Code review checks for path concatenation without canonicalization and root-boundary verification
- Tests covering absolute paths, traversal sequences, symlinks, encoded separators, and platform-specific path formats
- Runtime monitoring for file access outside approved project directories
- Gateway policies that restrict or remove client-controlled directory headers
- Automated security review tools that flag externally controlled paths reaching file operations

