-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbub.sh
More file actions
149 lines (119 loc) · 4.59 KB
/
Copy pathbub.sh
File metadata and controls
149 lines (119 loc) · 4.59 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
#!/usr/bin/env bash
set -euo pipefail
REPO="$(pwd)"
UID_NUM="$(id -u)"
GID_NUM="$(id -g)"
REAL_HOME="$(getent passwd "$UID_NUM" | cut -d: -f6)"
if [[ -z "$REAL_HOME" ]]; then
echo "Failed to determine real home directory for uid $UID_NUM" >&2
exit 1
fi
SANDBOX_HOME="$REPO/.bwrap-home"
mkdir -p "$SANDBOX_HOME"
mkdir -p "$SANDBOX_HOME/.codex"
mkdir -p "$SANDBOX_HOME/.claude"
# Share only Codex auth across repos. Keep sessions, logs, sqlite state, etc.
# project-local to avoid cross-instance conflicts.
LOCAL_AUTH="$SANDBOX_HOME/.codex/auth.json"
HOST_AUTH="$REAL_HOME/.codex/auth.json"
# Share only Claude auth across repos. Keep history, plugins, cache, etc.
# project-local to avoid cross-instance conflicts.
LOCAL_CLAUDE_AUTH="$SANDBOX_HOME/.claude/.credentials.json"
HOST_CLAUDE_AUTH="$REAL_HOME/.claude/.credentials.json"
LOCAL_CLAUDE_STATE="$SANDBOX_HOME/.claude.json"
HOST_CLAUDE_STATE="$REAL_HOME/.claude.json"
LOCAL_CLAUDE_MCP_AUTH_CACHE="$SANDBOX_HOME/.claude/mcp-needs-auth-cache.json"
HOST_CLAUDE_MCP_AUTH_CACHE="$REAL_HOME/.claude/mcp-needs-auth-cache.json"
mkdir -p "$REAL_HOME/.codex"
mkdir -p "$REAL_HOME/.claude"
# Seed the host auth file from an existing project-local sandbox auth if
# needed, so older repo-local setups can migrate forward automatically.
if [[ ! -s "$HOST_AUTH" && -e "$LOCAL_AUTH" ]]; then
cp -a "$LOCAL_AUTH" "$HOST_AUTH"
fi
# Ensure the bind target exists even before first login.
if [[ ! -e "$HOST_AUTH" ]]; then
: > "$HOST_AUTH"
fi
# Seed the host Claude auth file from an existing project-local sandbox auth if
# needed, so older repo-local setups can migrate forward automatically.
if [[ ! -s "$HOST_CLAUDE_AUTH" && -e "$LOCAL_CLAUDE_AUTH" ]]; then
install -m 600 "$LOCAL_CLAUDE_AUTH" "$HOST_CLAUDE_AUTH"
fi
# Ensure the bind target exists even before first login.
if [[ ! -e "$HOST_CLAUDE_AUTH" ]]; then
: > "$HOST_CLAUDE_AUTH"
fi
# Claude also keeps signed-in account metadata in ~/.claude.json.
if [[ ! -s "$HOST_CLAUDE_STATE" && -e "$LOCAL_CLAUDE_STATE" ]]; then
install -m 600 "$LOCAL_CLAUDE_STATE" "$HOST_CLAUDE_STATE"
fi
if [[ ! -e "$HOST_CLAUDE_STATE" ]]; then
: > "$HOST_CLAUDE_STATE"
fi
# Carry the MCP auth cache too so server auth prompts are shared.
if [[ ! -s "$HOST_CLAUDE_MCP_AUTH_CACHE" && -e "$LOCAL_CLAUDE_MCP_AUTH_CACHE" ]]; then
install -m 600 "$LOCAL_CLAUDE_MCP_AUTH_CACHE" "$HOST_CLAUDE_MCP_AUTH_CACHE"
fi
if [[ ! -e "$HOST_CLAUDE_MCP_AUTH_CACHE" ]]; then
: > "$HOST_CLAUDE_MCP_AUTH_CACHE"
fi
# Expose host ~/.local at its real absolute path so both relative symlinks and
# updater-created absolute symlinks keep working inside the sandbox.
HOSTHOME="$REAL_HOME"
args=(
# Lifecycle + namespaces
--die-with-parent
--unshare-ipc
--unshare-uts
--unshare-cgroup
# Optional hardening (recommended; should not break codex/node)
--unshare-user
--cap-drop ALL
# Core OS (read-only)
--ro-bind /usr /usr
--ro-bind /lib /lib
--ro-bind /lib64 /lib64
--ro-bind /bin /bin
--ro-bind /sbin /sbin
# /etc is needed for Fedora crypto-policies + CA trust + misc config (read-only)
--ro-bind /etc /etc
# Fedora /etc/resolv.conf commonly points into /run/systemd/resolve/...
--ro-bind /run/systemd/resolve /run/systemd/resolve
# Virtual filesystems
--proc /proc
--dev /dev
--tmpfs /tmp
# Identity (keep file ownership sane in /workspace)
--uid "$UID_NUM"
--gid "$GID_NUM"
# Workspace (writable)
--bind "$REPO" /workspace
--chdir /workspace
# Sandbox HOME (writable, project-local)
--bind "$SANDBOX_HOME" /home/sandbox
--setenv HOME /home/sandbox
# Share Codex auth only. Other ~/.codex state remains project-local.
--bind "$HOST_AUTH" /home/sandbox/.codex/auth.json
# Share Claude auth only. Other ~/.claude state remains project-local.
--bind "$HOST_CLAUDE_AUTH" /home/sandbox/.claude/.credentials.json
--bind "$HOST_CLAUDE_STATE" /home/sandbox/.claude.json
--bind "$HOST_CLAUDE_MCP_AUTH_CACHE" /home/sandbox/.claude/mcp-needs-auth-cache.json
# Host-installed CLI tools (read-only) at the real host path
--dir "$HOSTHOME"
--ro-bind "$REAL_HOME/.local" "$HOSTHOME/.local"
--setenv PATH "$HOSTHOME/.local/bin:/usr/local/bin:/usr/bin:/bin"
# --- Optional: make your real home readable (RO) for context ---
# --ro-bind "$HOME" "$HOME"
#
# --- Optional: if you RO-mount $HOME, mask common secret dirs so they look empty ---
# --tmpfs "$HOME/.ssh"
# --tmpfs "$HOME/.gnupg"
# --tmpfs "$HOME/.aws"
# --tmpfs "$HOME/.config/gcloud"
# --tmpfs "$HOME/.kube"
# --tmpfs "$HOME/.netrc"
# Make Python user-site packages resolve correctly
--setenv PYTHONUSERBASE "$HOSTHOME/.local"
)
exec bwrap "${args[@]}" bash -i