spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

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