-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.sh
More file actions
executable file
·608 lines (533 loc) · 20.1 KB
/
cli.sh
File metadata and controls
executable file
·608 lines (533 loc) · 20.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/bin/bash
# Claude Code Sandbox CLI
#
# Usage:
# claude-sandbox run [options] Run the sandbox in current directory
# claude-sandbox start-chrome [options] Start Chrome with remote debugging
# claude-sandbox gpg-new Generate a new sandbox GPG key
# claude-sandbox gpg-export --file <f> Export sandbox GPG key to a file
# claude-sandbox gpg-import --file <f> Import a GPG key into the sandbox
# claude-sandbox gpg-revoke --file <f> Generate a revocation certificate
# claude-sandbox gpg-erase Erase the sandbox GPG key
# claude-sandbox volume-shell Open a shell in the sandbox volume
# claude-sandbox volume-backup --file <f> Backup the sandbox volume
# claude-sandbox volume-restore --file <f> Restore the sandbox volume from backup
# claude-sandbox volume-rm Remove the sandbox volume
# claude-sandbox settings Open sandbox settings.json in vi
# claude-sandbox help Show this help message
#
# For detailed help on each command:
# claude-sandbox run --help
# claude-sandbox start-chrome --help
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Ensure common Docker CLI locations are on PATH.
# IDE task runners (e.g. Zed, VS Code) may launch with a minimal environment
# that doesn't include the directories where Docker Desktop installs its CLI.
for p in /usr/local/bin /opt/homebrew/bin "$HOME/.docker/bin"; do
if [[ -d "$p" ]] && [[ ":$PATH:" != *":$p:"* ]]; then
export PATH="$p:$PATH"
fi
done
show_help() {
cat << 'EOF'
Claude Code Sandbox - Run Claude Code safely in Docker
Usage:
claude-sandbox <command> [options]
Commands:
run Run the sandbox in current directory (default)
start-chrome Start Chrome with remote debugging (run on host)
gpg-new Generate a new sandbox GPG key
gpg-export Export the sandbox GPG key to a file
gpg-import Import a GPG key into the sandbox
gpg-revoke Generate a revocation certificate
gpg-erase Erase the sandbox GPG key
volume-shell Open a shell in the sandbox volume
volume-backup Backup the sandbox volume to a file
volume-restore Restore the sandbox volume from a backup
volume-rm Remove the sandbox volume
settings Open sandbox settings.json in vi
help Show this help message
Examples:
claude-sandbox run
claude-sandbox run --yolo --with-chrome --port 3000
claude-sandbox start-chrome
claude-sandbox start-chrome --restart
claude-sandbox gpg-new
claude-sandbox gpg-export --file my-key.asc
claude-sandbox gpg-import --file my-key.asc
claude-sandbox gpg-revoke --file revoke.asc
claude-sandbox gpg-erase
claude-sandbox volume-shell
claude-sandbox volume-backup --file backup.tgz
claude-sandbox volume-restore --file backup.tgz
claude-sandbox volume-rm
claude-sandbox settings
For more information, see README.md
EOF
}
show_run_help() {
cat << 'EOF'
Run the Claude Code sandbox in the current directory
Usage:
claude-sandbox run [options] [-- claude-args]
Options:
--yolo Enable YOLO mode (no permission prompts)
--firewalled Restrict network to essential domains only
--with-chrome Start Chrome with remote debugging
--port <port> Expose a port for dev servers (can be repeated)
Examples:
claude-sandbox run
claude-sandbox run --yolo
claude-sandbox run --yolo --with-chrome --port 3000
claude-sandbox run --port 3000 --port 5173
claude-sandbox run --yolo -p "fix the tests"
EOF
}
ensure_docker_running() {
# Use "docker ps" as a lightweight check — it only needs the daemon to respond,
# unlike "docker info" which can fail on permission or timeout issues even when
# Docker is running.
if docker ps &>/dev/null; then
return 0
fi
echo "Docker is not running. Attempting to start Docker..."
case "$(uname -s)" in
Darwin)
open -a Docker 2>/dev/null || open -a "Docker Desktop" 2>/dev/null || {
echo "Error: Could not start Docker Desktop. Please start it manually."
exit 1
}
;;
Linux)
if command -v systemctl &>/dev/null; then
sudo systemctl start docker 2>/dev/null || {
echo "Error: Could not start Docker via systemctl. Please start it manually."
exit 1
}
elif command -v service &>/dev/null; then
sudo service docker start 2>/dev/null || {
echo "Error: Could not start Docker via service. Please start it manually."
exit 1
}
else
echo "Error: Could not determine how to start Docker. Please start it manually."
exit 1
fi
;;
*)
echo "Error: Unsupported platform. Please start Docker manually."
exit 1
;;
esac
# Wait for Docker to be ready
echo "Waiting for Docker to be ready..."
local retries=30
while ! docker ps &>/dev/null; do
retries=$((retries - 1))
if [[ $retries -le 0 ]]; then
echo "Error: Docker did not start in time. Please start it manually."
exit 1
fi
sleep 2
done
echo "Docker is ready."
}
show_start_chrome_help() {
cat << 'EOF'
Start Chrome with remote debugging for sandbox connection
Usage:
claude-sandbox start-chrome [options]
Options:
--port <port> Override debug port from config
--profile <name> Override Chrome profile from config
--restart, -r Kill running Chrome and restart with debugging
Examples:
claude-sandbox start-chrome
claude-sandbox start-chrome --restart
claude-sandbox start-chrome --port 9333 --profile "Profile 1"
Note: Chrome must not be running, or use --restart to auto-restart it.
EOF
}
# Parse command
command="${1:-}"
case "$command" in
run|"")
# Default command: run sandbox
shift 2>/dev/null || true
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
show_run_help
exit 0
fi
ensure_docker_running
exec "$SCRIPT_DIR/scripts/run_sandbox.sh" "$@"
;;
start-chrome)
shift
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
show_start_chrome_help
exit 0
fi
exec "$SCRIPT_DIR/scripts/start-chrome-debug.sh" "$@"
;;
gpg-new)
shift
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
echo "Generate a new sandbox GPG key"
echo ""
echo "Usage:"
echo " claude-sandbox gpg-new"
echo ""
echo "Reads GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL from config.sh."
echo "If a key already exists, prompts before overwriting."
exit 0
fi
# Source config for identity
if [ -f "$SCRIPT_DIR/config.sh" ]; then
source "$SCRIPT_DIR/config.sh"
fi
if [ -z "$GIT_AUTHOR_NAME" ] || [ -z "$GIT_AUTHOR_EMAIL" ]; then
echo "Error: GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL must be set in config.sh"
exit 1
fi
ensure_docker_running
# Check for existing key
existing=$(docker run --rm --entrypoint bash -v claude-sandbox:/data local/claude-sandbox \
-c 'gpg --homedir /data/.gnupg --no-permission-warning --list-keys --with-colons 2>/dev/null | grep "^uid" | head -1 | cut -d: -f10')
if [ -n "$existing" ]; then
echo "An existing GPG key was found: $existing"
echo "Generating a new key will erase the existing one."
echo ""
read -r -p "Continue? [y/N] " confirm
case "$confirm" in
y|Y) ;;
*)
echo "Aborted."
exit 0
;;
esac
docker run --rm --entrypoint bash -v claude-sandbox:/data local/claude-sandbox \
-c 'rm -rf /data/.gnupg/*'
fi
echo "Generating GPG signing key for $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>..."
docker run --rm --entrypoint bash -v claude-sandbox:/data \
-e "GIT_AUTHOR_NAME=$GIT_AUTHOR_NAME" \
-e "GIT_AUTHOR_EMAIL=$GIT_AUTHOR_EMAIL" \
local/claude-sandbox \
-c '
gpg --homedir /data/.gnupg --no-permission-warning --batch --gen-key <<GPGEOF
%no-protection
Key-Type: eddsa
Key-Curve: ed25519
Name-Real: $GIT_AUTHOR_NAME
Name-Email: $GIT_AUTHOR_EMAIL
Expire-Date: 0
%commit
GPGEOF
echo ""
echo "=== GPG Public Key (add to GitHub → Settings → SSH and GPG keys) ==="
gpg --homedir /data/.gnupg --no-permission-warning --armor --export "$GIT_AUTHOR_EMAIL"
echo "==================================================================="
chown -R 1000:1000 /data/.gnupg
'
;;
gpg-export)
shift
outfile=""
while [[ $# -gt 0 ]]; do
case "$1" in
--file) outfile="$2"; shift 2 ;;
--help|-h)
echo "Export the sandbox GPG key to a file"
echo ""
echo "Usage:"
echo " claude-sandbox gpg-export --file <path>"
echo ""
echo "Options:"
echo " --file <path> Output file (required)"
exit 0
;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
if [ -z "$outfile" ]; then
echo "Error: --file is required"
echo "Usage: claude-sandbox gpg-export --file <path>"
exit 1
fi
ensure_docker_running
docker run --rm --entrypoint bash -v claude-sandbox:/data local/claude-sandbox \
-c 'gpg --homedir /data/.gnupg --no-permission-warning --export-secret-keys --armor 2>/dev/null' > "$outfile"
if [ ! -s "$outfile" ]; then
rm -f "$outfile"
echo "Error: No GPG keys found in the sandbox volume."
exit 1
fi
echo "Exported GPG key to: $outfile"
echo "WARNING: This file contains your PRIVATE key. Do not commit or share it."
;;
gpg-import)
shift
infile=""
while [[ $# -gt 0 ]]; do
case "$1" in
--file) infile="$2"; shift 2 ;;
--help|-h)
echo "Import a GPG key into the sandbox"
echo ""
echo "Usage:"
echo " claude-sandbox gpg-import --file <key-file>"
echo ""
echo "Options:"
echo " --file <path> Key file to import (required)"
exit 0
;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
if [ -z "$infile" ]; then
echo "Error: --file is required"
echo "Usage: claude-sandbox gpg-import --file <key-file>"
exit 1
fi
if [ ! -f "$infile" ]; then
echo "Error: File not found: $infile"
exit 1
fi
ensure_docker_running
docker run --rm -i --entrypoint bash -v claude-sandbox:/data local/claude-sandbox \
-c '
gpg --homedir /data/.gnupg --no-permission-warning --import && \
fpr=$(gpg --homedir /data/.gnupg --no-permission-warning --list-keys --with-colons 2>/dev/null | grep "^fpr" | head -1 | cut -d: -f10) && \
if [ -n "$fpr" ]; then
echo "$fpr:6:" | gpg --homedir /data/.gnupg --no-permission-warning --import-ownertrust
fi && \
chown -R 1000:1000 /data/.gnupg
' < "$infile"
echo "GPG key imported into the sandbox."
;;
gpg-revoke)
shift
outfile=""
while [[ $# -gt 0 ]]; do
case "$1" in
--file) outfile="$2"; shift 2 ;;
--help|-h)
echo "Generate a revocation certificate for the sandbox GPG key"
echo ""
echo "Usage:"
echo " claude-sandbox gpg-revoke --file <path>"
echo ""
echo "Options:"
echo " --file <path> Output file (required)"
echo ""
echo "Upload the certificate to GitHub to invalidate the key."
echo "Commits signed before revocation remain verified."
exit 0
;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
if [ -z "$outfile" ]; then
echo "Error: --file is required"
echo "Usage: claude-sandbox gpg-revoke --file <path>"
exit 1
fi
ensure_docker_running
# Resolve to absolute path and mount the parent directory
outdir="$(cd "$(dirname "$outfile")" && pwd)"
outname="$(basename "$outfile")"
docker run --rm -it --entrypoint bash \
-v claude-sandbox:/data \
-v "$outdir:/output" \
-e "OUTNAME=$outname" \
local/claude-sandbox \
-c '
key_id=$(gpg --homedir /data/.gnupg --no-permission-warning --list-keys --keyid-format long 2>/dev/null | grep -oP "(?<=ed25519/)[A-F0-9]+" | head -1)
if [ -z "$key_id" ]; then
echo "Error: No GPG keys found in the sandbox volume." >&2
exit 1
fi
gpg --homedir /data/.gnupg --no-permission-warning --gen-revoke --output "/output/$OUTNAME" "$key_id"
'
if [ ! -s "$outfile" ]; then
rm -f "$outfile"
echo "Error: Failed to generate revocation certificate."
exit 1
fi
echo "Revocation certificate written to: $outfile"
echo "Upload this to GitHub to invalidate the key."
;;
gpg-erase)
shift
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
echo "Erase the sandbox GPG key"
echo ""
echo "Usage:"
echo " claude-sandbox gpg-erase"
echo ""
echo "This permanently deletes all GPG keys from the sandbox volume."
echo "A new key will be generated on the next launch if GPG_SIGNING is enabled."
exit 0
fi
read -r -p "This will permanently delete all GPG keys from the sandbox. Continue? [y/N] " confirm
case "$confirm" in
y|Y)
ensure_docker_running
docker run --rm --entrypoint bash -v claude-sandbox:/data local/claude-sandbox \
-c 'rm -rf /data/.gnupg/* && echo "GPG keys erased."'
;;
*)
echo "Aborted."
;;
esac
;;
volume-shell)
shift
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
echo "Open a shell in the sandbox volume"
echo ""
echo "Usage:"
echo " claude-sandbox volume-shell"
echo ""
echo "Opens an interactive shell in the sandbox Docker volume"
echo "for inspecting or modifying its contents."
exit 0
fi
ensure_docker_running
docker run --rm -it -v claude-sandbox:/data -w /data alpine sh
;;
volume-backup)
shift
outfile=""
while [[ $# -gt 0 ]]; do
case "$1" in
--file) outfile="$2"; shift 2 ;;
--help|-h)
echo "Backup the sandbox volume to a file"
echo ""
echo "Usage:"
echo " claude-sandbox volume-backup --file <path>"
echo ""
echo "Options:"
echo " --file <path> Output file (required, .tgz)"
exit 0
;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
if [ -z "$outfile" ]; then
echo "Error: --file is required"
echo "Usage: claude-sandbox volume-backup --file <path>"
exit 1
fi
ensure_docker_running
outdir="$(cd "$(dirname "$outfile")" && pwd)"
outname="$(basename "$outfile")"
docker run --rm -v claude-sandbox:/data -v "$outdir:/backup" alpine \
tar -czf "/backup/$outname" -C /data .
echo "Volume backed up to: $outfile"
;;
volume-restore)
shift
infile=""
while [[ $# -gt 0 ]]; do
case "$1" in
--file) infile="$2"; shift 2 ;;
--help|-h)
echo "Restore the sandbox volume from a backup"
echo ""
echo "Usage:"
echo " claude-sandbox volume-restore --file <path>"
echo ""
echo "Options:"
echo " --file <path> Backup file to restore (required, .tgz)"
echo ""
echo "WARNING: This replaces all current volume contents."
exit 0
;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
if [ -z "$infile" ]; then
echo "Error: --file is required"
echo "Usage: claude-sandbox volume-restore --file <path>"
exit 1
fi
if [ ! -f "$infile" ]; then
echo "Error: File not found: $infile"
exit 1
fi
read -r -p "This will replace all contents of the sandbox volume. Continue? [y/N] " confirm
case "$confirm" in
y|Y) ;;
*)
echo "Aborted."
exit 0
;;
esac
ensure_docker_running
indir="$(cd "$(dirname "$infile")" && pwd)"
inname="$(basename "$infile")"
docker run --rm -v claude-sandbox:/data -v "$indir:/backup" alpine \
sh -c "rm -rf /data/* /data/.[!.]* /data/..?* 2>/dev/null; tar -xzf /backup/$inname -C /data"
echo "Volume restored from: $infile"
;;
volume-rm)
shift
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
echo "Remove the sandbox volume"
echo ""
echo "Usage:"
echo " claude-sandbox volume-rm"
echo ""
echo "Permanently deletes the sandbox Docker volume and all its data"
echo "(credentials, settings, GPG keys, installed tools, etc.)."
echo "A fresh volume will be created on the next launch."
exit 0
fi
read -r -p "This will permanently delete all sandbox data (credentials, settings, GPG keys, etc.). Continue? [y/N] " confirm
case "$confirm" in
y|Y)
ensure_docker_running
docker volume rm claude-sandbox
echo "Volume 'claude-sandbox' removed."
;;
*)
echo "Aborted."
;;
esac
;;
settings)
shift
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
echo "Open the sandbox Claude Code settings.json in vi"
echo ""
echo "Usage:"
echo " claude-sandbox settings"
echo ""
echo "Edits ~/.claude/settings.json inside the sandbox Docker volume."
exit 0
fi
ensure_docker_running
docker run --rm -it --entrypoint vi -v claude-sandbox:/data local/claude-sandbox \
/data/.claude/settings.json
;;
help|--help|-h)
show_help
exit 0
;;
*)
# Unknown command - could be flags for open-workspace (backwards compat)
# Check if it looks like a flag
if [[ "$command" == -* ]]; then
ensure_docker_running
exec "$SCRIPT_DIR/scripts/run_sandbox.sh" "$@"
else
echo "Unknown command: $command"
echo ""
show_help
exit 1
fi
;;
esac