#12174: fix(agents): add path containment check in apply_patch for non-sandboxed mode
agents
stale
Cluster:
Sandbox Path Management Fixes
## Fix Summary
Add a workspace root containment check in `resolvePatchPath()` for the non-sandboxed code path. When `sandboxRoot` is `undefined`, the function now verifies that the resolved path stays within `cwd` (the workspace root), rejecting `../` traversal and absolute paths with a clear error message.
## Issue Linkage
Fixes #12173
## Security Snapshot
- CVSS v3.1: 8.5 (High)
- CVSS v4.0: 8.4 (High)
## Implementation Details
### Files Changed
- `src/agents/apply-patch.test.ts` (+37/-0)
- `src/agents/apply-patch.ts` (+5/-0)
### Technical Analysis
- Added 4-line containment check in `resolvePatchPath()` (`src/agents/apply-patch.ts:231-234`)
- Mirrors the existing `resolveSandboxPath` pattern from `sandbox-paths.ts`
- Computes `path.relative(cwd, resolved)` and rejects if it starts with `..` or is absolute
## Validation Evidence
- Command: `pnpm build`
- Status: passed
## Risk and Compatibility
- **Blast radius:** Minimal — only affects the non-sandboxed `apply_patch` code path
- **Backward compatible:** Yes — only rejects paths that were already outside the intended workspace
- **No behavioral change** for legitimate use (relative paths within workspace)
Fixes #12173
## AI-Assisted Disclosure
🤖 Generated with [Claude Code](https://claude.ai/code)
This fix was generated with AI assistance (Claude Opus 4.6).
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR hardens `apply_patch` when running without a sandbox root by adding a workspace-root containment check in `resolvePatchPath()`. It rejects absolute paths and `../`-style traversal in the non-sandboxed code path and adds tests covering traversal rejection, absolute path rejection, and an allowed in-workspace relative path.
<h3>Confidence Score: 4/5</h3>
- Mostly safe to merge, but the new containment check can incorrectly reject some valid in-workspace paths.
- The change is small and well-targeted, but the use of `relative.startsWith("..")` will block legitimate filenames/directories beginning with `..` (e.g. `..foo/`), which is a functional regression for those cases and should be corrected before merging.
- src/agents/apply-patch.ts (workspace containment logic); src/agents/apply-patch.test.ts (add coverage for '..foo' case)
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#11820: fix(sandbox): remap container paths in sandboxed file tools
by steflsd · 2026-02-08
80.8%
#17402: fix:sandbox path issue
by luckylhb90 · 2026-02-15
80.1%
#16929: fix(security): block access to sensitive directories from within sa...
by CornBrother0x · 2026-02-15
80.0%
#16509: Fix sandbox path validation rejecting Docker bind mount paths
by Clawborn · 2026-02-14
79.4%
#16922: fix: remove incorrect sandbox file tool guidance
by carrotRakko · 2026-02-15
78.3%
#20991: fix(sandbox): fall back to gateway UID:GID when no user is configur...
by cluster2600 · 2026-02-19
77.5%
#4226: Fix/sandbox containerworkdir rw access
by ozgur-polat · 2026-01-29
77.5%
#8517: Browser: sandbox download/trace paths
by coygeek · 2026-02-04
77.3%
#17757: fix(agents): resolve relative workspace paths against state dir, no...
by Phineas1500 · 2026-02-16
77.1%
#3907: fix(sandbox): use absolute /bin/sh path + add allowedReadPaths config
by pvoo · 2026-01-29
77.0%