fix(agent): hairpin DNAT so a colocated edge proxy reaches sandboxed apps #135

Merged
gmackie merged 1 commit from fix/sandbox-hairpin-ingress into main 2026-07-23 19:06:50 +00:00
Owner

Problem

EnsureIngress (PR #91) only installs a PREROUTING DNAT matching -i <host-iface> — traffic arriving from outside the host. But Caddy runs on hetzner-master itself, and its reverse-proxy connections to the node's own public IP are locally originated: they traverse the nat OUTPUT chain, never PREROUTING. Result: the DNAT is bypassed and the connection lands on the host port, where nothing listens in sandbox mode — a sandboxed app can be perfectly healthy yet unreachable through its own node's edge.

Found live during the classback cutover: edge health checks kept returning 200 but were being served by the old workaround unit, not the sandbox. Stopping the workaround would have taken the edge dark.

Fix

Add the standard hairpin rule per ingress port (mirrors Docker's approach):

-t nat -A OUTPUT ! -d 127.0.0.0/8 -p tcp -m addrtype --dst-type LOCAL --dport <p> -j DNAT --to-destination <appIP>:<p>

--dst-type LOCAL covers any host-local address (public IP, Tailscale IP) without hardcoding one. Loopback is excluded because DNATing 127.0.0.1 traffic into the veth produces a martian source without route_localnet.

TDD: expectations extended first (red), then the rule (green). go test ./internal/sandbox/, go build ./..., go vet all clean.

Operator state

The equivalent rule is hand-applied on hetzner-master for classback:8095 (verified serving: edge requests log from the sandboxed unit; workaround stopped+disabled). EnsureIngress is check-before-add with the identical spec, so the next deploy/agent-roll will not duplicate it. This code takes fleet effect on the next agent release (0.1.36).

🤖 Generated with Claude Code

## Problem `EnsureIngress` (PR #91) only installs a PREROUTING DNAT matching `-i <host-iface>` — traffic arriving **from outside** the host. But Caddy runs on hetzner-master itself, and its reverse-proxy connections to the node's own public IP are **locally originated**: they traverse the nat OUTPUT chain, never PREROUTING. Result: the DNAT is bypassed and the connection lands on the host port, where nothing listens in sandbox mode — a sandboxed app can be perfectly healthy yet unreachable through its own node's edge. Found live during the classback cutover: edge health checks kept returning 200 but were being served by the old workaround unit, not the sandbox. Stopping the workaround would have taken the edge dark. ## Fix Add the standard hairpin rule per ingress port (mirrors Docker's approach): ``` -t nat -A OUTPUT ! -d 127.0.0.0/8 -p tcp -m addrtype --dst-type LOCAL --dport <p> -j DNAT --to-destination <appIP>:<p> ``` `--dst-type LOCAL` covers any host-local address (public IP, Tailscale IP) without hardcoding one. Loopback is excluded because DNATing 127.0.0.1 traffic into the veth produces a martian source without `route_localnet`. TDD: expectations extended first (red), then the rule (green). `go test ./internal/sandbox/`, `go build ./...`, `go vet` all clean. ## Operator state The equivalent rule is **hand-applied on hetzner-master** for classback:8095 (verified serving: edge requests log from the sandboxed unit; workaround stopped+disabled). `EnsureIngress` is check-before-add with the identical spec, so the next deploy/agent-roll will not duplicate it. This code takes fleet effect on the next agent release (0.1.36). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
fix(agent): hairpin DNAT so a colocated edge proxy reaches sandboxed apps
All checks were successful
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 6m1s
92f6ed27a7
EnsureIngress only installed a PREROUTING DNAT (-i <host-iface>), which
matches traffic arriving from outside the host. When the edge proxy runs
on the app's own node (Caddy on hetzner-master reverse-proxying to the
node's public IP), its traffic is locally originated and traverses the
nat OUTPUT chain instead — bypassing the DNAT and hitting the host port
where nothing listens. A sandboxed app could be healthy yet unreachable
through its own node's edge.

Add the standard hairpin rule (mirrors Docker's approach): nat OUTPUT
DNAT for any host-local destination address, excluding loopback (DNATing
127.0.0.1 traffic into the veth would produce a martian source without
route_localnet).

Found live during the classback cutover: with the workaround host unit
stopped, edge requests via the colocated Caddy would have gone dark. The
equivalent rule is hand-applied on hetzner-master for classback:8095
until an agent release carries this.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Author
Owner

🤖 Bob subscription review — APPROVE: Adds OUTPUT chain DNAT rule for colocated proxy hairpin case, excludes loopback correctly, updates tests consistently including index shifts and count expectations.

🤖 Bob subscription review — **APPROVE**: Adds OUTPUT chain DNAT rule for colocated proxy hairpin case, excludes loopback correctly, updates tests consistently including index shifts and count expectations.
gmackie deleted branch fix/sandbox-hairpin-ingress 2026-07-23 19:06:50 +00:00
Sign in to join this conversation.
No description provided.