-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-dev
More file actions
executable file
·332 lines (298 loc) · 10.1 KB
/
agent-dev
File metadata and controls
executable file
·332 lines (298 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env bash
# claude-code-isolated: Secure bubblewrap wrapper for running commands in isolation
#
# Provides isolated environment with:
# - ~/aihome mounted at your actual $HOME path (mostly writable)
# - Current repo mounted with overlayfs (changes isolated for review)
# - All paths preserved to avoid hardcoded path issues
# - No access to sensitive files (~/.ssh, ~/.aws, ~/.gnupg, etc.)
#
# Overlay security model:
# - Repository changes are written to ~/aihome/.overlay/<repo-full-path>/upper
# - Original repository remains untouched (read-only lower layer)
# - After session, review and merge changes with: ./merge-overlay
#
# Setup:
# 1. Create isolated home: mkdir -p ~/aihome
# 2. Login to Claude in the sandbox:
# cd /path/to/your/repo
# ./agent-dev bash
# claude # Follow login prompts
# exit
# 3. (Optional) Disable auto-updates in isolated home:
# mkdir -p ~/aihome/.claude
# echo '{"autoUpdates": false}' > ~/aihome/.claude/settings.local.json
#
# Usage:
# cd /path/to/your/repo
# ./agent-dev # Runs claude
# ./agent-dev bash # Inspect the environment
# ./agent-dev vim file # Run any command
#
# After session - review changes:
# ./merge-overlay /path/to/repo ~/aihome/.overlay/path/to/repo/upper
set -euo pipefail
# Configuration
AI_HOME="${AI_HOME:-$HOME/aihome}"
REPO_DIR="$(pwd)"
# Overlay directories for repository isolation (using full path to avoid conflicts)
OVERLAY_BASE="$AI_HOME/.overlay$(realpath "$REPO_DIR")"
OVERLAY_UPPER="$OVERLAY_BASE/upper"
OVERLAY_WORK="$OVERLAY_BASE/work"
# ===== Protected paths in repository (read-only if exist, defense-in-depth) =====
PROTECTED_REPO_FILES=(
".git/config"
".git/info/exclude" # Prevent sneaking ignore rules (not visible in git status)
".gitconfig"
".gitmodules"
".ripgreprc"
".project"
".classpath"
)
PROTECTED_REPO_DIRS=(
".git/hooks"
".vscode"
".idea"
".settings"
".claude/commands"
".claude/agents"
)
# ===== Protected paths in agent home (read-only if exist) =====
PROTECTED_AGENT_HOME_FILES=(
".gitconfig" # Safe config created during setup
".bashrc"
".bash_profile"
".zshrc"
".zprofile"
".profile"
".npmrc"
".gemrc"
".inputrc"
)
PROTECTED_AGENT_HOME_DIRS=(
".ssh" # If accidentally created, keep read-only
".bashrc.d" # Bash config scripts sourced by .bashrc
".zshrc.d" # Zsh config scripts (if used)
)
# ===== Whitelisted paths from real home (read-only mounts) =====
WHITELISTED_HOME_FILES=(
.tool-versions
)
WHITELISTED_HOME_DIRS=(
".asdf"
".npm"
".bundle"
".gem"
".cargo"
".rustup"
".local"
"bin"
"local"
)
# Validate environment
if [[ ! -d "$AI_HOME" ]]; then
echo "⚠️ AI home directory does not exist: $AI_HOME"
echo ""
echo "This directory will be used as Claude Code's isolated home."
echo "Create it with: mkdir -p $AI_HOME"
echo ""
exit 1
fi
if [[ ! -d .git ]]; then
echo "Error: Must run from a git repository root"
exit 1
fi
# Create overlay directories for repository isolation
mkdir -p "$OVERLAY_UPPER" "$OVERLAY_WORK"
# Update Claude before entering sandbox (via npm, not trusting claude update)
echo "🔄 Checking for Claude updates via npm..."
npm update -g @anthropics/claude-code 2>&1 | grep -E "(changed|up to date)" || true
echo ""
# Prepare Claude settings: create permissive default in temp file
CLAUDE_SETTINGS_TEMP=$(mktemp)
# https://code.claude.com/docs/en/settings
cat > "$CLAUDE_SETTINGS_TEMP" << 'EOF'
{
"permissions": {
"allow": [
"Bash",
"WebSearch",
"WebFetch",
"Read",
"Write",
"Edit",
"Glob",
"Grep",
"Task",
"NotebookEdit",
"Skill"
]
}
}
EOF
# Cleanup function: remove temp files
cleanup() {
rm -f "$CLAUDE_SETTINGS_TEMP"
}
# Register cleanup on exit (handles normal exit, errors, and interrupts)
trap cleanup EXIT
# Resolve DNS configuration (handles symlinks like /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf)
RESOLV_CONF_TARGET=$(readlink -f /etc/resolv.conf)
# Build bwrap arguments for protected repo paths
BWRAP_REPO_PROTECTIONS=()
# Protected files: ro-bind if exists (overlayfs will catch any creation attempts in upper)
for file in "${PROTECTED_REPO_FILES[@]}"; do
if [[ -f "$REPO_DIR/$file" ]]; then
BWRAP_REPO_PROTECTIONS+=(--ro-bind "$REPO_DIR/$file" "$REPO_DIR/$file")
fi
done
# Protected directories: ro-bind if exists (overlayfs will catch any creation attempts in upper)
for dir in "${PROTECTED_REPO_DIRS[@]}"; do
if [[ -d "$REPO_DIR/$dir" ]]; then
BWRAP_REPO_PROTECTIONS+=(--ro-bind "$REPO_DIR/$dir" "$REPO_DIR/$dir")
fi
done
# Build bwrap arguments for whitelisted real home paths
BWRAP_HOME_WHITELIST=()
# Whitelisted files from real home: ro-bind if exists
for file in "${WHITELISTED_HOME_FILES[@]}"; do
if [[ -f "$HOME/$file" ]]; then
BWRAP_HOME_WHITELIST+=(--ro-bind "$HOME/$file" "$HOME/$file")
fi
done
# Whitelisted directories from real home: ro-bind if exists
for dir in "${WHITELISTED_HOME_DIRS[@]}"; do
if [[ -d "$HOME/$dir" ]]; then
BWRAP_HOME_WHITELIST+=(--ro-bind "$HOME/$dir" "$HOME/$dir")
fi
done
# Build bwrap arguments for protected agent home paths
BWRAP_AGENT_HOME_PROTECTIONS=()
# Protected files in agent home: ro-bind if exists
for file in "${PROTECTED_AGENT_HOME_FILES[@]}"; do
if [[ -f "$AI_HOME/$file" ]]; then
BWRAP_AGENT_HOME_PROTECTIONS+=(--ro-bind "$AI_HOME/$file" "$HOME/$file")
fi
done
# Protected directories in agent home: ro-bind if exists
for dir in "${PROTECTED_AGENT_HOME_DIRS[@]}"; do
if [[ -d "$AI_HOME/$dir" ]]; then
BWRAP_AGENT_HOME_PROTECTIONS+=(--ro-bind "$AI_HOME/$dir" "$HOME/$dir")
fi
done
echo "🔒 Starting Claude Code in isolated environment..."
echo " Home: $HOME → $AI_HOME (isolated, mostly writable)"
echo " Repo: $REPO_DIR (overlayfs - changes go to overlay)"
echo ""
echo "📂 Overlay directories:"
echo " Lower: $REPO_DIR (original, read-only)"
echo " Upper: $OVERLAY_UPPER"
echo " Work: $OVERLAY_WORK"
echo ""
# Count protections by category
REPO_PROTECTED=$((${#PROTECTED_REPO_FILES[@]} + ${#PROTECTED_REPO_DIRS[@]}))
AGENT_HOME_PROTECTED=$((${#BWRAP_AGENT_HOME_PROTECTIONS[@]}))
WHITELIST_COUNT=$((${#BWRAP_HOME_WHITELIST[@]}))
echo "🛡️ Protection summary:"
echo " • Repo: $REPO_PROTECTED existing paths (read-only)"
echo " • Agent home: $AGENT_HOME_PROTECTED config files (read-only)"
echo " • Real home: $WHITELIST_COUNT dev tools (read-only)"
echo ""
echo "💡 All repo changes isolated in overlay (review with merge-overlay)"
echo "💡 After session: merge-overlay will run automatically"
echo "💡 To restore home: rm -rf $AI_HOME"
echo ""
exit_code=0
bwrap \
`# ===== System directories (read-only) =====` \
--ro-bind /usr /usr \
--ro-bind /etc /etc \
--ro-bind-try /opt /opt \
--ro-bind-try /nix /nix \
\
`# Symlinks for compatibility` \
--symlink /usr/lib /lib \
--symlink /usr/lib64 /lib64 \
--symlink /usr/bin /bin \
--symlink /usr/sbin /sbin \
\
`# ===== Pseudo-filesystems =====` \
--proc /proc \
--dev /dev \
--tmpfs /run \
--tmpfs /sys \
\
`# ===== DNS resolution =====` \
`# Mount the actual resolv.conf file at its real location (handles /etc/resolv.conf -> /run/systemd/... symlinks)` \
--ro-bind "$RESOLV_CONF_TARGET" "$RESOLV_CONF_TARGET" \
\
`# ===== Temporary directories (isolated, writable) =====` \
--tmpfs /tmp \
--tmpfs /var/tmp \
\
`# ===== AI Home mounted at real home path (WRITABLE) =====` \
`# This ensures hardcoded paths to $HOME work correctly` \
`# Agent can write anywhere here - easy to restore if corrupted` \
--bind "$AI_HOME" "$HOME" \
\
`# ===== Protected agent home paths (read-only overlays) =====` \
`# Config files created during setup - prevent tampering/persistence attacks` \
"${BWRAP_AGENT_HOME_PROTECTIONS[@]}" \
\
`# ===== Whitelisted real home paths (read-only overlays) =====` \
`# Dev tools and files from real home mounted read-only` \
"${BWRAP_HOME_WHITELIST[@]}" \
\
`# ===== Current repository with overlayfs (isolated writes) =====` \
`# Lower: original repo (read-only), Upper: changes go to overlay, Work: overlayfs internal` \
`# If under $HOME, this overlays the aihome mount at this specific location` \
--overlay-src "$REPO_DIR" \
--overlay "$OVERLAY_UPPER" "$OVERLAY_WORK" "$REPO_DIR" \
--chdir "$REPO_DIR" \
\
`# ===== Protected repo paths (read-only defense-in-depth) =====` \
`# Existing protected files/dirs are ro-bind (extra layer over overlayfs)` \
`# Non-existent paths can be created in overlay and rejected during merge` \
"${BWRAP_REPO_PROTECTIONS[@]}" \
\
`# ===== Claude settings override (permissive, non-persistent) =====` \
`# Writable temp file overlays repo settings - changes don't persist to host` \
--bind "$CLAUDE_SETTINGS_TEMP" "$REPO_DIR/.claude/settings.local.json" \
\
`# ===== Isolation and security =====` \
--unshare-all \
--share-net \
--die-with-parent \
--new-session \
\
`# ===== Environment variables =====` \
`# --setenv HOME "$HOME"` \
`# --setenv USER "$(whoami)"` \
`# --setenv LOGNAME "$(whoami)"` \
`# --setenv SHELL "/bin/bash"` \
`# --setenv TERM "${TERM:-xterm-256color}"` \
`# --setenv PATH "$HOME/.asdf/shims:$HOME/.asdf/bin:$HOME/bin:$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin"` \
`# --setenv ASDF_DIR "${HOME}/.asdf"` \
`# --setenv ASDF_DATA_DIR "${HOME}/.asdf"` \
\
`# Clean environment - remove potentially dangerous vars` \
--unsetenv DBUS_SESSION_BUS_ADDRESS \
--unsetenv XDG_RUNTIME_DIR \
--unsetenv SSH_AUTH_SOCK \
--unsetenv GPG_AGENT_INFO \
\
`# ===== Execute command (defaults to claude) =====` \
"${@:-claude}" || exit_code=$?
# After bwrap exits, run merge-overlay to review changes
echo ""
echo "============================================"
echo " Session ended - reviewing changes"
echo "============================================"
echo ""
# Check if there are any changes in overlay
if [[ -n "$(ls -A "$OVERLAY_UPPER" 2>/dev/null)" ]]; then
merge-overlay "$REPO_DIR" "$OVERLAY_UPPER"
else
echo "✓ No changes in overlay - nothing to merge"
fi
exit $exit_code