# Apple Container transparent egress spike Issue: https://gitea.dideric.is/didericis/bot-bottle/issues/230#issuecomment-1994 ## Summary Transparent egress is mechanically possible on Apple Container 1.0.0, but it is not a free property of the platform and it is not a drop-in replacement for `HTTP_PROXY` yet. The spike proved two separate things: - Plain routing/NAT works if the sidecar has `CAP_NET_ADMIN`, IP forwarding, and masquerade rules, and if the agent default route is changed to the sidecar's host-only-network IP. - Transparent mitmproxy interception works if the sidecar redirects agent-facing TCP 80/443 traffic to `mitmdump --mode transparent`. Direct HTTP was logged by mitmproxy. Direct HTTPS reached mitmproxy; it failed with normal certificate verification until the client skipped verification, which is consistent with bot-bottle's existing requirement that agents trust the sidecar CA. - Running DNS on the sidecar and pointing the agent at the sidecar's host-only IP also works. This is cleaner than relying on forwarded UDP DNS to a public resolver and gives the backend a natural place to enforce or observe DNS policy. The hard blocker is agent routing. Apple Container 1.0.0 exposes no documented `--network` gateway option. An ordinary agent container cannot replace its default route: ```console $ container exec bb-spike-230t-agent sh -c \ 'ip route replace default via 192.168.128.2 dev eth0; ip route' default via 192.168.128.1 dev eth0 192.168.128.0/24 dev eth0 scope link src 192.168.128.3 ip: RTNETLINK answers: Operation not permitted ``` The successful route-through-sidecar tests used `--cap-add CAP_NET_ADMIN` on the agent so the route could be changed after start. That is not an acceptable final design by itself: it expands the agent's kernel-facing privilege and lets the agent mutate its own network namespace. A production design needs either a backend-owned init/shim that sets the route then drops privilege in a way the agent cannot regain, a platform-supported gateway option, or a different network attachment layer. ## Environment Tested on 2026-06-10: ```console $ sw_vers ProductName: macOS ProductVersion: 26.5.1 BuildVersion: 25F80 $ uname -m arm64 $ container --version container CLI version 1.0.0 (build: release, commit: ee848e3) ``` Apple Container system status: ```json { "apiServerAppName": "container-apiserver", "apiServerBuild": "release", "apiServerCommit": "ee848e3ebfd7c73b04dd419683be54fb450b8779", "apiServerVersion": "container-apiserver version 1.0.0 (build: release, commit: ee848e3)", "appRoot": "/Users/didericis/Library/Application Support/com.apple.container/", "installRoot": "/usr/local/", "status": "running" } ``` ## Baseline Networks: ```bash container network create bb-spike-230t-agent \ --internal \ --label bot-bottle.spike=transparent-egress container network create bb-spike-230t-egress \ --label bot-bottle.spike=transparent-egress ``` Sidecar, dual-homed with NAT first: ```bash container run --name bb-spike-230t-sidecar \ --label bot-bottle.spike=transparent-egress \ --network bb-spike-230t-egress \ --network bb-spike-230t-agent \ --dns 1.1.1.1 \ --detach docker.io/alpine:latest sleep 1800 ``` Agent, host-only network: ```bash container run --name bb-spike-230t-agent \ --label bot-bottle.spike=transparent-egress \ --network bb-spike-230t-agent \ --detach docker.io/alpine:latest sleep 1800 ``` Observed sidecar addresses: ```console eth0 192.168.66.2/24 # NAT egress network eth1 192.168.128.2/24 # host-only agent network default via 192.168.66.1 dev eth0 nameserver 1.1.1.1 ``` Observed agent baseline: ```console eth0 192.168.128.3/24 default via 192.168.128.1 dev eth0 nameserver 192.168.128.1 wget: bad address 'pypi.org' ``` That confirms the previous spike's baseline: sidecar can egress, agent cannot egress directly. ## Plain NAT Test Relaunch sidecar and agent with `CAP_NET_ADMIN`: ```bash container run --name bb-spike-230t-sidecar \ --label bot-bottle.spike=transparent-egress \ --network bb-spike-230t-egress \ --network bb-spike-230t-agent \ --dns 1.1.1.1 \ --cap-add CAP_NET_ADMIN \ --detach docker.io/alpine:latest sleep 1800 container run --name bb-spike-230t-agent \ --label bot-bottle.spike=transparent-egress \ --network bb-spike-230t-agent \ --cap-add CAP_NET_ADMIN \ --detach docker.io/alpine:latest sleep 1800 ``` Configure sidecar forwarding: ```bash container exec bb-spike-230t-sidecar sh -c ' apk add --no-cache iptables iproute2 sysctl -w net.ipv4.ip_forward=1 iptables -t nat -A POSTROUTING -s 192.168.128.0/24 -o eth0 -j MASQUERADE iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT iptables -A FORWARD -i eth0 -o eth1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ' ``` Point the agent at the sidecar: ```bash container exec bb-spike-230t-agent sh -c ' ip route replace default via 192.168.128.4 dev eth0 printf "nameserver 1.1.1.1\n" > /etc/resolv.conf ' ``` Normal direct PyPI fetch from the agent, with no proxy variables set: ```bash container exec bb-spike-230t-agent sh -c ' for v in HTTP_PROXY HTTPS_PROXY http_proxy https_proxy ALL_PROXY all_proxy; do if [ -n "$(printenv "$v")" ]; then echo "$v=SET"; fi done wget -T 10 -O- https://pypi.org/simple/pip/ | head -c 120 ' ``` Observed: ```console Connecting to pypi.org (151.101.0.223:443)
``` Sidecar NAT counters increased: ```console POSTROUTING MASQUERADE 3 packets / 168 bytes FORWARD eth1 -> eth0 22 packets / 2806 bytes FORWARD eth0 -> eth1 29 packets / 54781 bytes ``` Verdict: plain transparent routing through the sidecar works, but this is only NAT. It does not apply bot-bottle's existing route allowlist, authorization stripping/injection, or DLP logic. ## Transparent Mitmproxy Test The current sidecar launcher uses explicit proxy mode: ```sh MODE="--mode regular@9099" exec mitmdump $CONFDIR_FLAG $MODE $LISTEN_HOST_FLAG $TRUST_FLAG -s /app/egress_addon.py ``` So transparent egress needs a launcher mode change plus iptables redirects. Run a test mitmproxy container: ```bash container run --name bb-spike-230t-mitm \ --label bot-bottle.spike=transparent-egress \ --network bb-spike-230t-egress \ --network bb-spike-230t-agent \ --dns 1.1.1.1 \ --cap-add CAP_NET_ADMIN \ --detach mitmproxy/mitmproxy:11.1.3 \ sh -c 'apt-get update >/tmp/apt.log && apt-get install -y --no-install-recommends iptables iproute2 >>/tmp/apt.log && echo 1 > /proc/sys/net/ipv4/ip_forward && iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-port 8080 && iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 443 -j REDIRECT --to-port 8080 && mitmdump --mode transparent@8080 --set showhost=true --set ssl_insecure=true --set confdir=/tmp/mitm -v' ``` The container listened successfully: ```console Transparent Proxy listening at *:8080. ``` It had an agent-facing address of `192.168.128.7`. Point the agent at it and set DNS: ```bash container exec bb-spike-230t-agent sh -c ' ip route replace default via 192.168.128.7 dev eth0 printf "nameserver 1.1.1.1\n" > /etc/resolv.conf ' ``` DNS also needs NAT/forwarding because only TCP 80/443 is redirected: ```bash container exec bb-spike-230t-mitm sh -c ' iptables -t nat -A POSTROUTING -s 192.168.128.0/24 -o eth0 -j MASQUERADE iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT iptables -A FORWARD -i eth0 -o eth1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ' ``` An alternative, and likely better, DNS shape is to run a DNS forwarder on the sidecar's host-only IP and point the agent at it. This was tested with `dnsmasq`: ```bash container exec bb-spike-230t-mitm sh -c ' apt-get install -y --no-install-recommends dnsmasq cat >/tmp/dnsmasq.conf <