Summary
A sandbox escape vulnerability in Claude Code's Git worktree handling allowed crafted repositories to create .git worktrees and traverse outside the intended sandbox, causing git directory confusion.
By combining symlink manipulation with git fsmonitor execution, an attacker could cause file overwrites in the user's home directory and achieve code execution outside seatbelt restrictions when a user ran Claude Code against a malicious repository.
Affected context
This vulnerability affects environments where Claude Code (versions 2.1.38 through 2.1.163) operates on cloned Git repositories within a supposed sandboxed worktree.
Risk emerges when users clone and open untrusted or attacker-controlled repositories that can influence worktree naming, symlink layout, and Git metadata, particularly on systems where Claude Code has access to the user's home directory and shell configuration files.
Exploit preconditions
- Attack surface: Claude Code's Git worktree management and associated filesystem operations.
- Required access: The attacker must convince a user to clone and run Claude Code against a malicious repository containing prompt injection and crafted Git metadata/symlinks.
- Trigger point: Execution of worktree-related operations, including creation of a
.git-named worktree and running git fsmonitor hooks within Claude Code's environment. - Why exploitation is possible: Inadequate validation of worktree names, path resolution, and symlink targets allowed navigation outside the sandbox and interaction with sensitive paths in the user's home directory.
Why this happens
The core issue stems from insufficient enforcement of directory boundaries and trust assumptions during Git worktree handling.
Path traversal and symlink attacks (CWE-22, CWE-59) were possible because Claude Code did not robustly normalize and constrain filesystem operations to a sandbox root, permitting .git worktrees and out-of-sandbox navigation.
Combined with the ability to trigger git fsmonitor execution and interact with shell configuration files, this also aligns with command execution risks (CWE-78), as untrusted repository content could indirectly influence commands executed on the host.
Potential impact
- Attacker-controlled overwrites of files in the user's home directory (e.g.,
.zshenvor similar shell init files). - Code execution on the host outside Claude Code's seatbelt sandbox when the user next launches a shell or affected program.
- Persistence via modified configuration or startup scripts.
- Escalation of impact from a confined coding assistant context to the broader user environment.
General mitigation guidance
Effective mitigation focuses on strengthening sandbox boundaries and treating repository content as untrusted input.
Typical patterns include:
- Strictly validating and constraining worktree names and locations to remain within a defined sandbox root.
- Resolving and normalizing paths before use, rejecting any that escape the sandbox or interact with special directories such as
.gitin unexpected ways. - Hardening symlink handling by either disallowing certain symlink patterns in worktrees or verifying resolved targets remain inside trusted directories.
- Isolating helper tooling (e.g., Git fsmonitor hooks) from user home directories and sensitive configuration, and running them with least privilege.
- Applying defense-in-depth, such as explicit allowlists for writable paths and denying interaction with shell initialization files.
How teams detect issues like this
Detection typically combines code-level and behavioral approaches:
- Static analysis or code scanning rules looking for unvalidated use of filesystem APIs, path concatenation without normalization, and operations that follow symlinks when interacting with user-controlled repositories.
- Code review checklists that flag any logic permitting worktree or
.gitdirectory manipulation without explicit sandbox boundary checks. - Runtime monitoring for unexpected writes from Claude Code or its helper processes to home-directory configuration files (e.g.,
.zshenv,.bashrc,.profile) and other sensitive locations. - Policy-based enforcement requiring all repository operations to be scoped to a dedicated workspace directory, with automated tests that assert paths never escape the sandbox.

