carlos / platform Public

edge: bound tcpProxy's connect to a remote upstream

tcpProxy, the unprobed addr-route proxy, dialled with upstreamDialer
(15b1ee6b's guarded net.Dialer{}) and nothing else: no Timeout, and the
local-hold retry that bounds a dial applies only to loopback. A
black-holed remote upstream held each request, its goroutine and the
client connection for the kernel's connect timeout (about two minutes
on Linux) before the 502.

tcpProxy now dials with tcpUpstreamDialer: the same upstreamguard
dialer (the link-local and IMDS refusal is unchanged) with Timeout and
KeepAlive of 30 s. Those are net/http's DefaultTransport dialer values
and what the activator's wake health check already uses on the same
route addrs (waitHealthyAddr, backend_unit.go), so anything that
connects today still connects; only a hang is cut short. multiProxy
and the prober keep upstreamDialer, since they bound each dial with a
context deadline (multiDialTimeout, the probe timeout).

Test: TestTCPUpstreamDialerIsBounded checks the dialer's Timeout and
KeepAlive and that it still refuses 169.254.169.254.

Audit 2026-11-16, activator-edge finding 10c.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev c6ec153ff64f3fb829f563725aeae0d8f0123555 parent 275369d
3 files changed, +49 −5
  • internal/edge/edge.go +4 −3
  • internal/edge/upstreamdial.go +23 −2
  • internal/edge/upstreamdial_test.go +22 −0

CI cancelled — superseded by a newer push — run details

diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 355314446..86e490c37 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -4009,12 +4009,13 @@ func unixProxy(socketPath string) *httputil.ReverseProxy {
// A loopback upstream is this box's own process, so a refused dial there is
// a restart in progress: the dial is retried for hold.dial (one deadline
// fixed per dial) instead of 502ing at once. hold.dial <= 0, or a remote
-// addr, dials once. Either way the dial is upstreamDialer's (hold.dialer is
-// nil in production), which refuses link-local and IMDS addresses.
+// addr, dials once. Either way the dial is tcpUpstreamDialer's (hold.dialer
+// is nil in production), which refuses link-local and IMDS addresses and
+// gives up on a connect after tcpUpstreamDialTimeout.
func tcpProxy(addr string, hold localHold) *httputil.ReverseProxy {
dial := hold.dialer
if dial == nil {
- dial = upstreamDialer().DialContext
+ dial = tcpUpstreamDialer().DialContext
}
tr := &http.Transport{
MaxIdleConns: 32,
diff --git a/internal/edge/upstreamdial.go b/internal/edge/upstreamdial.go
index 28857146e..dfd4d778a 100644
--- a/internal/edge/upstreamdial.go
+++ b/internal/edge/upstreamdial.go
@@ -2,6 +2,7 @@ package edge
import (
"net"
+ "time"
"github.com/carlosframework/platform/internal/upstreamguard"
)
@@ -9,8 +10,28 @@ import (
// upstreamDialer is the dialer for a TCP upstream when no test dialer is set
// (hold.dialer nil, as in production): the plain net.Dialer the transports
// used before, behind upstreamguard, which refuses link-local and IMDS
-// addresses after resolution and before connect. tcpProxy, multiProxy and
-// the prober use it; unix sockets, backhaul hops and tunnels do not.
+// addresses after resolution and before connect. multiProxy and the prober
+// use it (each bounds its dials with a context deadline); tcpProxy uses
+// tcpUpstreamDialer; unix sockets, backhaul hops and tunnels use neither.
func upstreamDialer() *net.Dialer {
return upstreamguard.Dialer(net.Dialer{})
}
+
+// tcpUpstreamDialTimeout and tcpUpstreamKeepAlive are tcpProxy's dial
+// settings. Its transport has no other bound on a connect, so without a
+// Timeout a black-holed remote upstream holds the request, its goroutine
+// and the client's connection for the kernel's connect timeout (about two
+// minutes on Linux) before the 502. The values are net/http's
+// DefaultTransport dialer, the same the activator's wake health check uses
+// on these route addrs (waitHealthyAddr): a real handshake finishes far
+// inside 30 s, so nothing that connects today stops connecting.
+const (
+ tcpUpstreamDialTimeout = 30 * time.Second
+ tcpUpstreamKeepAlive = 30 * time.Second
+)
+
+// tcpUpstreamDialer is upstreamDialer with tcpProxy's connect bound and
+// keep-alive: still behind upstreamguard, so the link-local refusal holds.
+func tcpUpstreamDialer() *net.Dialer {
+ return upstreamguard.Dialer(net.Dialer{Timeout: tcpUpstreamDialTimeout, KeepAlive: tcpUpstreamKeepAlive})
+}
diff --git a/internal/edge/upstreamdial_test.go b/internal/edge/upstreamdial_test.go
index 4c2d367b2..672bf1bd0 100644
--- a/internal/edge/upstreamdial_test.go
+++ b/internal/edge/upstreamdial_test.go
@@ -141,3 +141,25 @@ func TestUpstreamDialSitesRefuseLinkLocal(t *testing.T) {
conn.Close()
})
}
+
+// TestTCPUpstreamDialerIsBounded: tcpProxy's production dialer gives up on a
+// connect after tcpUpstreamDialTimeout instead of the kernel's two minutes,
+// keeps TCP keep-alives on, and is still behind upstreamguard.
+func TestTCPUpstreamDialerIsBounded(t *testing.T) {
+ d := tcpUpstreamDialer()
+ if d.Timeout != tcpUpstreamDialTimeout || d.KeepAlive != tcpUpstreamKeepAlive {
+ t.Fatalf("Timeout %v KeepAlive %v, want %v and %v", d.Timeout, d.KeepAlive, tcpUpstreamDialTimeout, tcpUpstreamKeepAlive)
+ }
+ if d.Timeout <= 0 || d.Timeout > 30*time.Second {
+ t.Fatalf("Timeout %v: want a bound no longer than net/http's 30 s default", d.Timeout)
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ conn, err := d.DialContext(ctx, "tcp", "169.254.169.254:80")
+ if conn != nil {
+ conn.Close()
+ }
+ if !errors.Is(err, upstreamguard.ErrLinkLocal) {
+ t.Fatalf("dial err = %v, want upstreamguard.ErrLinkLocal", err)
+ }
+}