#!/bin/sh # Close the host paths a sandbox container can use to reach ARTEX itself. # The recording proxy already refuses loopback, metadata, postgres and the # admin port. This script covers traffic that never enters the proxy: # - packets from the sandbox bridge to the host's published :8787 # - packets routed from the sandbox bridge into the compose internal network # (postgres, docker-socket-proxy, the ARTEX container) # - packets toward the cloud metadata address # # Run on the Docker host after the networks exist. Safe to repeat. # Pass --cleanup to remove ARTEX sandbox firewall and residual gateway rules # (used when switching back / rolling back from sandbox edition to standalone). set -eu sandbox_net="${ARTEX_SANDBOX_NETWORK:-artex-sandbox}" tun_dev="${ARTEX_GATEWAY_TUN:-artex-tun0}" tun_table="${ARTEX_GATEWAY_ROUTE_TABLE:-218}" tun_prio="${ARTEX_GATEWAY_RULE_PRIO:-218}" sandbox_subnet="${ARTEX_SANDBOX_SUBNET:-172.30.0.0/16}" if [ "${1:-}" = "--cleanup" ]; then # Best-effort removal of residual gateway chains, policy routing, and DOCKER-USER rules. sandbox_br="" if docker network inspect "$sandbox_net" >/dev/null 2>&1; then id=$(docker network inspect "$sandbox_net" --format '{{.Id}}') sandbox_br=$(printf 'br-%.12s\n' "$id") fi if command -v ip >/dev/null 2>&1; then if [ -n "$sandbox_br" ]; then ip rule del iif "$sandbox_br" lookup "$tun_table" priority "$tun_prio" 2>/dev/null || true fi ip rule del lookup "$tun_table" priority "$tun_prio" 2>/dev/null || true ip route flush table "$tun_table" 2>/dev/null || true ip link del "$tun_dev" 2>/dev/null || true fi if command -v iptables >/dev/null 2>&1; then iptables -t raw -D PREROUTING -i "$tun_dev" -d "$sandbox_subnet" -j ACCEPT 2>/dev/null || true iptables -t nat -D POSTROUTING -o "$tun_dev" -j ACCEPT 2>/dev/null || true if [ -n "$sandbox_br" ]; then iptables -t filter -D DOCKER-USER -i "$tun_dev" -o "$sandbox_br" -j ACCEPT 2>/dev/null || true iptables -t filter -D DOCKER-USER -i "$sandbox_br" -o "$tun_dev" -j ACCEPT 2>/dev/null || true iptables -t filter -D DOCKER-USER -i "$sandbox_br" ! -o "$sandbox_br" -j REJECT 2>/dev/null || true iptables -t filter -D DOCKER-USER -i "$sandbox_br" -d 169.254.169.254/32 -j REJECT 2>/dev/null || true iptables -t filter -D INPUT -i "$sandbox_br" -p tcp --dport 8787 -j REJECT 2>/dev/null || true iptables -t filter -D DOCKER-USER -i "$sandbox_br" -j ARTEX_GW_FWD 2>/dev/null || true iptables -t filter -D INPUT -i "$sandbox_br" -j ARTEX_GW_INPUT 2>/dev/null || true iptables -t nat -D PREROUTING -i "$sandbox_br" -j ARTEX_GW_NAT 2>/dev/null || true iptables -t mangle -D PREROUTING -i "$sandbox_br" -j ARTEX_GW_MANGLE 2>/dev/null || true fi for pair in "filter:ARTEX_GW_FWD" "filter:ARTEX_GW_INPUT" "nat:ARTEX_GW_NAT" "mangle:ARTEX_GW_MANGLE"; do tbl="${pair%%:*}" ch="${pair##*:}" iptables -t "$tbl" -F "$ch" 2>/dev/null || true iptables -t "$tbl" -X "$ch" 2>/dev/null || true done fi echo "sandbox firewall & gateway rules cleaned up" exit 0 fi bridge_for() { id=$(docker network inspect "$1" --format '{{.Id}}') printf 'br-%.12s\n' "$id" } ensure() { chain=$1 shift if iptables -C "$chain" "$@" 2>/dev/null; then return 0 fi iptables -I "$chain" "$@" } if ! docker network inspect "$sandbox_net" >/dev/null 2>&1; then echo "sandbox network $sandbox_net not found" >&2 exit 1 fi sandbox_br=$(bridge_for "$sandbox_net") ensure INPUT -i "$sandbox_br" -p tcp --dport 8787 -j REJECT # If the ARTEX control-plane container is directly attached to artex-sandbox # (SNAT-free source IP attribution, #218), allow ONLY sandbox -> ARTEX :8788 # (plus established return packets) while keeping enable_icc=false between # sandbox containers and explicitly rejecting :8787. artex_ct=$(docker ps --filter "network=$sandbox_net" --format '{{.Names}}' | awk '/artex/ && !/artex-sbx-/ {print; exit}') if [ -n "$artex_ct" ]; then artex_sbx_ip=$(docker inspect "$artex_ct" --format "{{with index .NetworkSettings.Networks \"$sandbox_net\"}}{{.IPAddress}}{{end}}") if [ -n "$artex_sbx_ip" ]; then ensure DOCKER-USER -i "$sandbox_br" -o "$sandbox_br" -d "$artex_sbx_ip/32" -p tcp --dport 8787 -j REJECT ensure DOCKER-USER -i "$sandbox_br" -o "$sandbox_br" -s "$artex_sbx_ip/32" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ensure DOCKER-USER -i "$sandbox_br" -o "$sandbox_br" -d "$artex_sbx_ip/32" -p tcp --dport 8788 -j ACCEPT fi fi # Compose prefixes the internal network with the project name. internal_net=$(docker network ls --format '{{.Name}}' | awk '/artex-internal$/ {print; exit}') if [ -n "$internal_net" ]; then internal_cidr=$(docker network inspect "$internal_net" --format '{{(index .IPAM.Config 0).Subnet}}') if [ -n "$internal_cidr" ]; then # Published :8788 is DNATed onto the ARTEX container, which lives in this # subnet. Let only that port through, then refuse postgres, the Docker # socket proxy, and the admin port. ensure DOCKER-USER -i "$sandbox_br" -d "$internal_cidr" -j REJECT ensure DOCKER-USER -i "$sandbox_br" -d "$internal_cidr" -p tcp --dport 8788 -j RETURN fi fi ensure DOCKER-USER -i "$sandbox_br" -d 169.254.169.254/32 -j REJECT # Optional transparent gateway mode (#218 Phase 3): allow traffic routed into # artex-tun0 (gVisor netstack) and reject any direct outbound NAT bypass. if [ "${ARTEX_SANDBOX_TRANSPARENT:-false}" = "true" ] || [ "${ARTEX_SANDBOX_TRANSPARENT:-0}" = "1" ]; then ensure DOCKER-USER -i "$sandbox_br" ! -o "$sandbox_br" -j REJECT ensure DOCKER-USER -i "$sandbox_br" -o artex-tun0 -j ACCEPT ensure DOCKER-USER -i artex-tun0 -o "$sandbox_br" -j ACCEPT fi echo "sandbox firewall applied on $sandbox_br"