Skip to content

Commit 3aa9d9e

Browse files
committed
Add DevContainer configuration files
1 parent f46a23f commit 3aa9d9e

File tree

3 files changed

+321
-0
lines changed

3 files changed

+321
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
FROM node:20
2+
3+
ARG TZ
4+
ENV TZ="$TZ"
5+
6+
ARG CLAUDE_CODE_VERSION=latest
7+
8+
# Install basic development tools and iptables/ipset
9+
RUN apt-get update && apt-get install -y --no-install-recommends \
10+
less \
11+
git \
12+
procps \
13+
sudo \
14+
fzf \
15+
zsh \
16+
man-db \
17+
unzip \
18+
gnupg2 \
19+
gh \
20+
iptables \
21+
ipset \
22+
iproute2 \
23+
dnsutils \
24+
aggregate \
25+
jq \
26+
nano \
27+
vim \
28+
locales \
29+
# Ruby build dependencies
30+
build-essential \
31+
libssl-dev \
32+
libreadline-dev \
33+
zlib1g-dev \
34+
libyaml-dev \
35+
libffi-dev \
36+
libncurses-dev \
37+
aspell \
38+
aspell-en \
39+
aspell-fr \
40+
&& apt-get clean && rm -rf /var/lib/apt/lists/*
41+
42+
# Configure locale
43+
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \
44+
locale-gen en_US.UTF-8
45+
46+
ENV LANG=en_US.UTF-8
47+
ENV LC_ALL=en_US.UTF-8
48+
49+
# Ensure default node user has access to /usr/local/share
50+
RUN mkdir -p /usr/local/share/npm-global && \
51+
chown -R node:node /usr/local/share
52+
53+
ARG USERNAME=node
54+
55+
# Persist bash history.
56+
RUN SNIPPET="export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \
57+
&& mkdir /commandhistory \
58+
&& touch /commandhistory/.bash_history \
59+
&& chown -R $USERNAME /commandhistory
60+
61+
# Set `DEVCONTAINER` environment variable to help with orientation
62+
ENV DEVCONTAINER=true
63+
64+
# Create workspace and config directories and set permissions
65+
RUN mkdir -p /workspace /home/node/.claude && \
66+
chown -R node:node /workspace /home/node/.claude
67+
68+
WORKDIR /workspace
69+
70+
ARG GIT_DELTA_VERSION=0.18.2
71+
RUN ARCH=$(dpkg --print-architecture) && \
72+
wget "https://github.com/dandavison/delta/releases/download/${GIT_DELTA_VERSION}/git-delta_${GIT_DELTA_VERSION}_${ARCH}.deb" && \
73+
sudo dpkg -i "git-delta_${GIT_DELTA_VERSION}_${ARCH}.deb" && \
74+
rm "git-delta_${GIT_DELTA_VERSION}_${ARCH}.deb"
75+
76+
# Set up non-root user
77+
USER node
78+
79+
# Install rbenv and ruby-build
80+
ARG RUBY_VERSION=4.0.1
81+
RUN git clone https://github.com/rbenv/rbenv.git ~/.rbenv && \
82+
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build && \
83+
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc && \
84+
echo 'eval "$(rbenv init - zsh)"' >> ~/.zshrc && \
85+
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc && \
86+
echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
87+
88+
# Install Ruby
89+
ENV PATH="/home/node/.rbenv/bin:/home/node/.rbenv/shims:$PATH"
90+
RUN rbenv install ${RUBY_VERSION} && \
91+
rbenv global ${RUBY_VERSION} && \
92+
gem install bundler
93+
94+
# Install global packages
95+
ENV NPM_CONFIG_PREFIX=/usr/local/share/npm-global
96+
ENV PATH=$PATH:/usr/local/share/npm-global/bin
97+
98+
# Set the default shell to zsh rather than sh
99+
ENV SHELL=/bin/zsh
100+
101+
# Set the default editor and visual
102+
ENV EDITOR=nano
103+
ENV VISUAL=nano
104+
105+
# Default powerline10k theme
106+
ARG ZSH_IN_DOCKER_VERSION=1.2.0
107+
RUN sh -c "$(wget -O- https://github.com/deluan/zsh-in-docker/releases/download/v${ZSH_IN_DOCKER_VERSION}/zsh-in-docker.sh)" -- \
108+
-p git \
109+
-p fzf \
110+
-a "source /usr/share/doc/fzf/examples/key-bindings.zsh" \
111+
-a "source /usr/share/doc/fzf/examples/completion.zsh" \
112+
-a "export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \
113+
-x
114+
115+
# Install Claude
116+
RUN curl -fsSL https://claude.ai/install.sh | bash
117+
118+
119+
# Copy and set up firewall script
120+
COPY init-firewall.sh /usr/local/bin/
121+
USER root
122+
RUN chmod +x /usr/local/bin/init-firewall.sh && \
123+
echo "node ALL=(root) NOPASSWD: /usr/local/bin/init-firewall.sh" > /etc/sudoers.d/node-firewall && \
124+
chmod 0440 /etc/sudoers.d/node-firewall
125+
USER node

.devcontainer/devcontainer.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "Claude Code Sandbox",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"args": {
6+
"TZ": "${localEnv:TZ:America/Los_Angeles}",
7+
"CLAUDE_CODE_VERSION": "latest",
8+
"GIT_DELTA_VERSION": "0.18.2",
9+
"ZSH_IN_DOCKER_VERSION": "1.2.0"
10+
}
11+
},
12+
"runArgs": [
13+
"--cap-add=NET_ADMIN",
14+
"--cap-add=NET_RAW"
15+
],
16+
"customizations": {
17+
"vscode": {
18+
"extensions": [
19+
"anthropic.claude-code",
20+
"dbaeumer.vscode-eslint",
21+
"esbenp.prettier-vscode",
22+
"eamodio.gitlens"
23+
],
24+
"settings": {
25+
"editor.formatOnSave": true,
26+
"editor.defaultFormatter": "esbenp.prettier-vscode",
27+
"editor.codeActionsOnSave": {
28+
"source.fixAll.eslint": "explicit"
29+
},
30+
"terminal.integrated.defaultProfile.linux": "zsh",
31+
"terminal.integrated.profiles.linux": {
32+
"bash": {
33+
"path": "bash",
34+
"icon": "terminal-bash"
35+
},
36+
"zsh": {
37+
"path": "zsh"
38+
}
39+
}
40+
}
41+
}
42+
},
43+
"remoteUser": "node",
44+
"mounts": [
45+
"source=claude-code-bashhistory-${devcontainerId},target=/commandhistory,type=volume",
46+
"source=claude-code-config-${devcontainerId},target=/home/node/.claude,type=volume"
47+
],
48+
"containerEnv": {
49+
"NODE_OPTIONS": "--max-old-space-size=4096",
50+
"CLAUDE_CONFIG_DIR": "/home/node/.claude",
51+
"POWERLEVEL9K_DISABLE_GITSTATUS": "true"
52+
},
53+
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,consistency=delegated",
54+
"workspaceFolder": "/workspace",
55+
"postStartCommand": "sudo /usr/local/bin/init-firewall.sh",
56+
"waitFor": "postStartCommand"
57+
}

.devcontainer/init-firewall.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/bin/bash
2+
set -euo pipefail # Exit on error, undefined vars, and pipeline failures
3+
IFS=$'\n\t' # Stricter word splitting
4+
5+
# 1. Extract Docker DNS info BEFORE any flushing
6+
DOCKER_DNS_RULES=$(iptables-save -t nat | grep "127\.0\.0\.11" || true)
7+
8+
# Flush existing rules and delete existing ipsets
9+
iptables -F
10+
iptables -X
11+
iptables -t nat -F
12+
iptables -t nat -X
13+
iptables -t mangle -F
14+
iptables -t mangle -X
15+
ipset destroy allowed-domains 2>/dev/null || true
16+
17+
# 2. Selectively restore ONLY internal Docker DNS resolution
18+
if [ -n "$DOCKER_DNS_RULES" ]; then
19+
echo "Restoring Docker DNS rules..."
20+
iptables -t nat -N DOCKER_OUTPUT 2>/dev/null || true
21+
iptables -t nat -N DOCKER_POSTROUTING 2>/dev/null || true
22+
echo "$DOCKER_DNS_RULES" | xargs -L 1 iptables -t nat
23+
else
24+
echo "No Docker DNS rules to restore"
25+
fi
26+
27+
# First allow DNS and localhost before any restrictions
28+
# Allow outbound DNS
29+
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
30+
# Allow inbound DNS responses
31+
iptables -A INPUT -p udp --sport 53 -j ACCEPT
32+
# Allow outbound SSH
33+
iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT
34+
# Allow inbound SSH responses
35+
iptables -A INPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
36+
# Allow localhost
37+
iptables -A INPUT -i lo -j ACCEPT
38+
iptables -A OUTPUT -o lo -j ACCEPT
39+
40+
# Create ipset with CIDR support
41+
ipset create allowed-domains hash:net
42+
43+
# Fetch GitHub meta information and aggregate + add their IP ranges
44+
echo "Fetching GitHub IP ranges..."
45+
gh_ranges=$(curl -s https://api.github.com/meta)
46+
if [ -z "$gh_ranges" ]; then
47+
echo "ERROR: Failed to fetch GitHub IP ranges"
48+
exit 1
49+
fi
50+
51+
if ! echo "$gh_ranges" | jq -e '.web and .api and .git' >/dev/null; then
52+
echo "ERROR: GitHub API response missing required fields"
53+
exit 1
54+
fi
55+
56+
echo "Processing GitHub IPs..."
57+
while read -r cidr; do
58+
if [[ ! "$cidr" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}$ ]]; then
59+
echo "ERROR: Invalid CIDR range from GitHub meta: $cidr"
60+
exit 1
61+
fi
62+
echo "Adding GitHub range $cidr"
63+
ipset add allowed-domains "$cidr"
64+
done < <(echo "$gh_ranges" | jq -r '(.web + .api + .git)[]' | aggregate -q)
65+
66+
# Resolve and add other allowed domains
67+
for domain in \
68+
"storage.googleapis.com" \
69+
"registry.npmjs.org" \
70+
"api.anthropic.com" \
71+
"sentry.io" \
72+
"statsig.anthropic.com" \
73+
"statsig.com" \
74+
"marketplace.visualstudio.com" \
75+
"vscode.blob.core.windows.net" \
76+
"update.code.visualstudio.com" \
77+
"rubygems.org"; do
78+
echo "Resolving $domain..."
79+
ips=$(dig +noall +answer A "$domain" | awk '$4 == "A" {print $5}')
80+
if [ -z "$ips" ]; then
81+
echo "ERROR: Failed to resolve $domain"
82+
exit 1
83+
fi
84+
85+
while read -r ip; do
86+
if [[ ! "$ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
87+
echo "ERROR: Invalid IP from DNS for $domain: $ip"
88+
exit 1
89+
fi
90+
echo "Adding $ip for $domain"
91+
ipset add allowed-domains "$ip"
92+
done < <(echo "$ips")
93+
done
94+
95+
# Get host IP from default route
96+
HOST_IP=$(ip route | grep default | cut -d" " -f3)
97+
if [ -z "$HOST_IP" ]; then
98+
echo "ERROR: Failed to detect host IP"
99+
exit 1
100+
fi
101+
102+
HOST_NETWORK=$(echo "$HOST_IP" | sed "s/\.[0-9]*$/.0\/24/")
103+
echo "Host network detected as: $HOST_NETWORK"
104+
105+
# Set up remaining iptables rules
106+
iptables -A INPUT -s "$HOST_NETWORK" -j ACCEPT
107+
iptables -A OUTPUT -d "$HOST_NETWORK" -j ACCEPT
108+
109+
# Set default policies to DROP first
110+
iptables -P INPUT DROP
111+
iptables -P FORWARD DROP
112+
iptables -P OUTPUT DROP
113+
114+
# First allow established connections for already approved traffic
115+
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
116+
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
117+
118+
# Then allow only specific outbound traffic to allowed domains
119+
iptables -A OUTPUT -m set --match-set allowed-domains dst -j ACCEPT
120+
121+
# Explicitly REJECT all other outbound traffic for immediate feedback
122+
iptables -A OUTPUT -j REJECT --reject-with icmp-admin-prohibited
123+
124+
echo "Firewall configuration complete"
125+
echo "Verifying firewall rules..."
126+
if curl --connect-timeout 5 https://example.com >/dev/null 2>&1; then
127+
echo "ERROR: Firewall verification failed - was able to reach https://example.com"
128+
exit 1
129+
else
130+
echo "Firewall verification passed - unable to reach https://example.com as expected"
131+
fi
132+
133+
# Verify GitHub API access
134+
if ! curl --connect-timeout 5 https://api.github.com/zen >/dev/null 2>&1; then
135+
echo "ERROR: Firewall verification failed - unable to reach https://api.github.com"
136+
exit 1
137+
else
138+
echo "Firewall verification passed - able to reach https://api.github.com as expected"
139+
fi

0 commit comments

Comments
 (0)