[BUG] mkdir fails in claude headless
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
● Claude Code 2.1.2 Sandbox Bug: Blocks File Operations in Subdirectories of Allowed Working Directory
Version: Claude Code 2.1.2
Platform: Linux (Fedora/RHEL based, kernel 6.12.0)
Description:
The sandbox incorrectly blocks all file operations (touch, mkdir, write) to subdirectories of the allowed working directory. The error message correctly identifies the allowed directory, but the path comparison fails to recognize that subdirectory paths are inside the allowed directory.
Reproduction Steps:
# 1. Create empty test directory
mkdir -p /tmp/claude-sandbox-test
# 2. Run Claude Code with --add-dir pointing to test directory
cd /tmp/claude-sandbox-test
claude -p "Run: touch test.txt" --add-dir /tmp/claude-sandbox-test --verbose --output-format stream-json
Expected Result: File /tmp/claude-sandbox-test/test.txt is created.
Actual Result:
Error: touch in '/tmp/claude-sandbox-test/test.txt' was blocked.
For security, Claude Code may only create or modify files in the
allowed working directories for this session: '/tmp/claude-sandbox-test'.
The path /tmp/claude-sandbox-test/test.txt IS inside /tmp/claude-sandbox-test, but the sandbox blocks it anyway.
Attempted Workarounds (None Work):
- --add-dir /path/to/project - still blocked
- settings.json with "sandbox": {"allowedDirectories": ["/path"]} - still blocked
- settings.json with "sandbox": {"enabled": false} - still blocked
- Using relative paths instead of absolute - still blocked
Only Working Workaround:
claude -p "prompt" --dangerously-skip-permissions
Impact: This completely breaks any automated/orchestrated use of Claude Code that needs to write files in project subdirectories. The sandbox path comparison logic appears to check if paths are equal rather than if one path is a prefix of another.
What Should Happen?
mkdir should work as it did previous.
Error Messages/Logs
Steps to Reproduce
See above.
Claude Model
Opus
Is this a regression?
Yes, this worked in a previous version
Last Working Version
_No response_
Claude Code Version
2.1.2
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
_No response_
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗