|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | +IFS=$'\n\t' |
| 4 | + |
| 5 | +allowed_domains_file="/etc/codex/allowed_domains.txt" |
| 6 | +include_github_meta_ranges="${CODEX_INCLUDE_GITHUB_META_RANGES:-1}" |
| 7 | + |
| 8 | +if [ -f "$allowed_domains_file" ]; then |
| 9 | + mapfile -t allowed_domains < <(sed '/^\s*#/d;/^\s*$/d' "$allowed_domains_file") |
| 10 | +else |
| 11 | + allowed_domains=("api.openai.com") |
| 12 | +fi |
| 13 | + |
| 14 | +if [ "${#allowed_domains[@]}" -eq 0 ]; then |
| 15 | + echo "ERROR: No allowed domains configured" |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +add_ipv4_cidr_to_allowlist() { |
| 20 | + local source="$1" |
| 21 | + local cidr="$2" |
| 22 | + |
| 23 | + if [[ ! "$cidr" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}/[0-9]{1,2}$ ]]; then |
| 24 | + echo "ERROR: Invalid ${source} CIDR range: $cidr" |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | + |
| 28 | + ipset add allowed-domains "$cidr" -exist |
| 29 | +} |
| 30 | + |
| 31 | +configure_ipv6_default_deny() { |
| 32 | + if ! command -v ip6tables >/dev/null 2>&1; then |
| 33 | + echo "ERROR: ip6tables is required to enforce IPv6 default-deny policy" |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + |
| 37 | + ip6tables -F |
| 38 | + ip6tables -X |
| 39 | + ip6tables -t mangle -F |
| 40 | + ip6tables -t mangle -X |
| 41 | + ip6tables -t nat -F 2>/dev/null || true |
| 42 | + ip6tables -t nat -X 2>/dev/null || true |
| 43 | + |
| 44 | + ip6tables -A INPUT -i lo -j ACCEPT |
| 45 | + ip6tables -A OUTPUT -o lo -j ACCEPT |
| 46 | + ip6tables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
| 47 | + ip6tables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
| 48 | + |
| 49 | + ip6tables -P INPUT DROP |
| 50 | + ip6tables -P FORWARD DROP |
| 51 | + ip6tables -P OUTPUT DROP |
| 52 | + |
| 53 | + echo "IPv6 firewall policy configured (default-deny)" |
| 54 | +} |
| 55 | + |
| 56 | +# Preserve docker-managed DNS NAT rules before clearing tables. |
| 57 | +docker_dns_rules="$(iptables-save -t nat | grep "127\\.0\\.0\\.11" || true)" |
| 58 | + |
| 59 | +iptables -F |
| 60 | +iptables -X |
| 61 | +iptables -t nat -F |
| 62 | +iptables -t nat -X |
| 63 | +iptables -t mangle -F |
| 64 | +iptables -t mangle -X |
| 65 | +ipset destroy allowed-domains 2>/dev/null || true |
| 66 | + |
| 67 | +if [ -n "$docker_dns_rules" ]; then |
| 68 | + echo "Restoring Docker DNS NAT rules" |
| 69 | + iptables -t nat -N DOCKER_OUTPUT 2>/dev/null || true |
| 70 | + iptables -t nat -N DOCKER_POSTROUTING 2>/dev/null || true |
| 71 | + while IFS= read -r rule; do |
| 72 | + [ -z "$rule" ] && continue |
| 73 | + iptables -t nat $rule |
| 74 | + done <<< "$docker_dns_rules" |
| 75 | +fi |
| 76 | + |
| 77 | +# Allow DNS resolution and localhost communication. |
| 78 | +iptables -A OUTPUT -p udp --dport 53 -j ACCEPT |
| 79 | +iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT |
| 80 | +iptables -A INPUT -p udp --sport 53 -j ACCEPT |
| 81 | +iptables -A INPUT -p tcp --sport 53 -j ACCEPT |
| 82 | +iptables -A INPUT -i lo -j ACCEPT |
| 83 | +iptables -A OUTPUT -o lo -j ACCEPT |
| 84 | + |
| 85 | +ipset create allowed-domains hash:net |
| 86 | + |
| 87 | +for domain in "${allowed_domains[@]}"; do |
| 88 | + echo "Resolving $domain" |
| 89 | + ips="$(dig +short A "$domain" | sed '/^\s*$/d')" |
| 90 | + if [ -z "$ips" ]; then |
| 91 | + echo "ERROR: Failed to resolve $domain" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + while IFS= read -r ip; do |
| 96 | + if [[ ! "$ip" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]]; then |
| 97 | + echo "ERROR: Invalid IPv4 address from DNS for $domain: $ip" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + ipset add allowed-domains "$ip" -exist |
| 101 | + done <<< "$ips" |
| 102 | +done |
| 103 | + |
| 104 | +if [ "$include_github_meta_ranges" = "1" ]; then |
| 105 | + echo "Fetching GitHub meta ranges" |
| 106 | + github_meta="$(curl -fsSL --connect-timeout 10 https://api.github.com/meta)" |
| 107 | + |
| 108 | + if ! echo "$github_meta" | jq -e '.web and .api and .git' >/dev/null; then |
| 109 | + echo "ERROR: GitHub meta response missing expected fields" |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | + |
| 113 | + while IFS= read -r cidr; do |
| 114 | + [ -z "$cidr" ] && continue |
| 115 | + if [[ "$cidr" == *:* ]]; then |
| 116 | + # Current policy enforces IPv4-only ipset entries. |
| 117 | + continue |
| 118 | + fi |
| 119 | + add_ipv4_cidr_to_allowlist "GitHub" "$cidr" |
| 120 | + done < <(echo "$github_meta" | jq -r '((.web // []) + (.api // []) + (.git // []))[]' | sort -u) |
| 121 | +fi |
| 122 | + |
| 123 | +host_ip="$(ip route | awk '/default/ {print $3; exit}')" |
| 124 | +if [ -z "$host_ip" ]; then |
| 125 | + echo "ERROR: Failed to detect host IP" |
| 126 | + exit 1 |
| 127 | +fi |
| 128 | + |
| 129 | +host_network="$(echo "$host_ip" | sed 's/\.[0-9]*$/.0\/24/')" |
| 130 | +iptables -A INPUT -s "$host_network" -j ACCEPT |
| 131 | +iptables -A OUTPUT -d "$host_network" -j ACCEPT |
| 132 | + |
| 133 | +iptables -P INPUT DROP |
| 134 | +iptables -P FORWARD DROP |
| 135 | +iptables -P OUTPUT DROP |
| 136 | + |
| 137 | +iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
| 138 | +iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
| 139 | +iptables -A OUTPUT -m set --match-set allowed-domains dst -j ACCEPT |
| 140 | + |
| 141 | +# Reject rather than silently drop to make policy failures obvious. |
| 142 | +iptables -A INPUT -j REJECT --reject-with icmp-admin-prohibited |
| 143 | +iptables -A OUTPUT -j REJECT --reject-with icmp-admin-prohibited |
| 144 | +iptables -A FORWARD -j REJECT --reject-with icmp-admin-prohibited |
| 145 | + |
| 146 | +configure_ipv6_default_deny |
| 147 | + |
| 148 | +echo "Firewall configuration complete" |
| 149 | + |
| 150 | +if curl --connect-timeout 5 https://example.com >/dev/null 2>&1; then |
| 151 | + echo "ERROR: Firewall verification failed - was able to reach https://example.com" |
| 152 | + exit 1 |
| 153 | +fi |
| 154 | + |
| 155 | +if ! curl --connect-timeout 5 https://api.openai.com >/dev/null 2>&1; then |
| 156 | + echo "ERROR: Firewall verification failed - unable to reach https://api.openai.com" |
| 157 | + exit 1 |
| 158 | +fi |
| 159 | + |
| 160 | +if [ "$include_github_meta_ranges" = "1" ] && ! curl --connect-timeout 5 https://api.github.com/zen >/dev/null 2>&1; then |
| 161 | + echo "ERROR: Firewall verification failed - unable to reach https://api.github.com" |
| 162 | + exit 1 |
| 163 | +fi |
| 164 | + |
| 165 | +if curl --connect-timeout 5 -6 https://example.com >/dev/null 2>&1; then |
| 166 | + echo "ERROR: Firewall verification failed - was able to reach https://example.com over IPv6" |
| 167 | + exit 1 |
| 168 | +fi |
| 169 | + |
| 170 | +echo "Firewall verification passed" |
0 commit comments