carlos / platform Public

edge: bound the body a replica set copies to replay

A replica route's transport read a client-claimed Content-Length (up to
64 KiB) whole, sized up front, with no time or per-client bound, before
trying a copy. The set's proxy now sits behind its body admission
(replicaBodyProxy): bufferBody with replay reads the body through
readBoundedBody and hands the copy on as GetBody; the transport only
replays that copy and never reads the client itself. The handoff path
hides GetBody from the lower transport. TestEveryBufferedBodyIsBounded
gains a replica-set row; the readBoundedBody coverage comment is true.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev d1458ff5c048025289501f9eac103157ec6a2f77 parent 9ec6b7c
5 files changed, +89 −35
  • internal/edge/edge.go +16 −5
  • internal/edge/hunt_H5-M5_test.go +28 −3
  • internal/edge/replica_handoff_test.go +5 −1
  • internal/edge/replica_reach_test.go +3 −0
  • internal/edge/replicaproxy.go +37 −26
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index d2f67442b..012562f86 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -1388,6 +1388,12 @@ func (e *Edge) Reload() error {
}
proxy = assetScrubbedProxy{p}
}
+ // A replica set may send a body to a second copy, so it takes the
+ // body whole first, through bufferBody's bounds, before the proxy
+ // (replicaBodyProxy): the transport never reads the client itself.
+ if arm == ArmReplica {
+ proxy = replicaBodyProxy{e: e, next: proxy}
+ }
if t.Version != "" && t.Kind != "static" && t.Kind != "status" && !genManaged {
proxy = versionHeader(t.Version, proxy)
}
@@ -4239,7 +4245,8 @@ func tunnelProxy(name string, hub *TunnelHub) *httputil.ReverseProxy {
// bodyTimeout bounds how long this edge waits for a request body it reads
// whole before handing the request on (readBoundedBody): a login POST it
-// judges or sends to another box, and the replay copy of an HA forward. The
+// judges or sends to another box, and the replay copy of an HA forward or
+// of a replica set. The
// read comes before any rate admission and the production listeners set
// only ReadHeaderTimeout, so without it a client that sends the headers and
// holds the body back would hold a handler (and whatever it holds in
@@ -4288,8 +4295,10 @@ func isLoginPost(r *http.Request) bool {
// arrive whole in time, or broke off, is answered unreadable (413 for a
// login, as every unreadable login body has been; 408 for the rest). Its
// callers are readLoginBody, which ServeHTTP judges a login by, and
-// bufferBody, which every hand-on that buffers goes through. Nothing else
-// in this package reads a client's body whole before sending it on.
+// bufferBody, which every hand-on that buffers goes through: a backhaul, a
+// path hop, both HA forwards and a replica set. Nothing else in this
+// package reads a client's body whole before sending it on: the replica
+// transport replays bufferBody's copy and never reads the client itself.
//
// The deadline is cleared once the body is in, so a request passed on is
// not cut off while the box answers. A writer that cannot set deadlines
@@ -4365,9 +4374,11 @@ func (e *Edge) readLoginBody(w http.ResponseWriter, r *http.Request) ([]byte, bo
// sends from. ok is false when it answered on w.
//
// Its callers are every hand-on that may buffer: a backhaul and a path hop
-// (backhaulRouteTo, replay false) and both region HA forwards
+// (backhaulRouteTo, replay false), both region HA forwards
// (forwardHAObserved: the front forward with replay on its first attempt,
-// the draining box's streaming release without).
+// the draining box's streaming release without) and a replica set
+// (replicaBodyProxy, replay on, its copy handed to the transport as
+// GetBody).
func (e *Edge) bufferBody(w http.ResponseWriter, r *http.Request, replay bool) (*wholeBody, bool) {
var b []byte
switch {
diff --git a/internal/edge/hunt_H5-M5_test.go b/internal/edge/hunt_H5-M5_test.go
index ff1bbe279..21b23c14c 100644
--- a/internal/edge/hunt_H5-M5_test.go
+++ b/internal/edge/hunt_H5-M5_test.go
@@ -216,6 +216,20 @@ func stallTLSPost(t *testing.T, addr, host string, pool *x509.CertPool, path, bo
return conn
}
+// stallPlainPost is stallTLSPost over plain HTTP, for a scene whose edge
+// serves on an httptest server.
+func stallPlainPost(t *testing.T, addr, host, path, body string) net.Conn {
+ t.Helper()
+ conn, err := net.Dial("tcp", addr)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { conn.Close() })
+ fmt.Fprintf(conn, "POST %s HTTP/1.1\r\nHost: %s\r\nContent-Type: application/octet-stream\r\nContent-Length: %d\r\n\r\n%s",
+ path, host, len(body), body[:5])
+ return conn
+}
+
// stallTLSLogin is stallTLSPost of a whole login form.
func stallTLSLogin(t *testing.T, addr, host string, pool *x509.CertPool) net.Conn {
t.Helper()
@@ -266,10 +280,12 @@ type bodyBoundCase struct {
// (readBoundedBody), before any hop is admitted. One witness per path that
// buffers: a backhaul, an in-region HA front forward (a login, and a
// non-login POST it copies whole to replay), the same forward for a path
-// route's target, and a draining box's streaming release to the holder.
+// route's target, a draining box's streaming release to the holder, and a
+// hub's replica set, which copies a body whole to try it on another copy.
// Before round 2 the HA forward's replay copy of a non-login body waited
-// for as long as the client liked, with no per-client count. Every scene is
-// the production wiring: real edges, real TLS, real signed hops.
+// for as long as the client liked, with no per-client count; before round 3
+// so did the replica set's. Every scene is the production wiring: real
+// edges, real TLS or a real hub and tunnel, real signed hops.
func TestEveryBufferedBodyIsBounded(t *testing.T) {
cases := []bodyBoundCase{
{"backhaul, a login", http.StatusRequestEntityTooLarge, func(t *testing.T) (*Edge, func(*testing.T) net.Conn) {
@@ -307,6 +323,15 @@ func TestEveryBufferedBodyIsBounded(t *testing.T) {
x.mv.mu.Unlock()
return x.a.edge, func(t *testing.T) net.Conn { return stallTLSLogin(t, x.a.tlsAddr, fwGatedHost, x.ca.pool) }
}},
+ {"replica set, a non-login POST", http.StatusRequestTimeout, func(t *testing.T) (*Edge, func(*testing.T) net.Conn) {
+ // An ungated set of two live copies on fleet pi, behind a real
+ // hub (TestReplicaBoxDrainRefusedAtTunnel's rig).
+ rig := newReplicaDrainRig(t, drainRigOpts{})
+ addr := strings.TrimPrefix(rig.url, "http://")
+ return rig.edge, func(t *testing.T) net.Conn {
+ return stallPlainPost(t, addr, "shop.example.com", "/upload", huntM5Upload)
+ }
+ }},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
diff --git a/internal/edge/replica_handoff_test.go b/internal/edge/replica_handoff_test.go
index fef692608..9b498a062 100644
--- a/internal/edge/replica_handoff_test.go
+++ b/internal/edge/replica_handoff_test.go
@@ -239,8 +239,12 @@ func replicaTransportOf(t *testing.T, e *Edge, host string) *replicaTransport {
if !ok {
t.Fatalf("%s has no upstream", host)
}
+ h := up.proxy
+ if b, ok := h.(replicaBodyProxy); ok {
+ h = b.next
+ }
var rp *httputil.ReverseProxy
- switch p := up.proxy.(type) {
+ switch p := h.(type) {
case assetScrubbedProxy:
rp = p.proxy
case *httputil.ReverseProxy:
diff --git a/internal/edge/replica_reach_test.go b/internal/edge/replica_reach_test.go
index 3ba335c03..423aab819 100644
--- a/internal/edge/replica_reach_test.go
+++ b/internal/edge/replica_reach_test.go
@@ -382,6 +382,9 @@ func TestReplicaRefusedPostServedBySurvivor(t *testing.T) {
fb := replicaFramerBox(t, hub, p, rt.Host, "box-00", c.mode)
body := strings.Repeat("x", 32<<10)
req := httptest.NewRequest("POST", "https://shop.example.com/", strings.NewReader(body))
+ // The copy the set's body admission (replicaBodyProxy) hands on:
+ // the transport replays it and never reads the client itself.
+ req.GetBody = func() (io.ReadCloser, error) { return io.NopCloser(strings.NewReader(body)), nil }
resp, err := replicaProxy(rt.Host, "pi", p, hub).Transport.RoundTrip(req)
if !c.repeated {
if err == nil || survivor.Load() != 0 {
diff --git a/internal/edge/replicaproxy.go b/internal/edge/replicaproxy.go
index 505d91212..6a3d50ef4 100644
--- a/internal/edge/replicaproxy.go
+++ b/internal/edge/replicaproxy.go
@@ -1,7 +1,6 @@
package edge
import (
- "bytes"
"context"
"errors"
"io"
@@ -93,10 +92,7 @@ func (t *replicaTransport) RoundTrip(req *http.Request) (*http.Response, error)
if len(live) == 0 && t.handoff != nil && !risen {
return t.handoffRoundTrip(req)
}
- body, getBody, err := replicaReplayableBody(req)
- if err != nil {
- return nil, err
- }
+ body, getBody := replicaReplayableBody(req)
owned := false
defer func() {
if !owned && body != nil {
@@ -277,31 +273,40 @@ func (t *replicaTransport) RoundTrip(req *http.Request) (*http.Response, error)
return nil, last
}
-// replicaReplayLimit is the largest request body, of known length, the
-// transport reads whole before its first attempt, so that a stream a box
+// replicaReplayableBody is the body the attempts send, and how to make a
+// fresh copy of it (nil when none can be made), so that a stream a box
// refuses after the hub has sent part of the body can still go to another
-// copy (R26). It is region HA §25's H2 bound for a replayable forward.
-const replicaReplayLimit = 64 << 10
+// copy (R26). The transport never reads a client's body itself: a body it
+// may replay arrives already whole, read under bufferBody's bounds by the
+// set's body admission (replicaBodyProxy), with a GetBody over that copy.
+// Any other body (larger than replayBodyLimit, region HA §25's H2 bound for
+// a replayable forward, or of unknown length) streams as before, and an
+// attempt that spent part of it is never repeated.
+func replicaReplayableBody(req *http.Request) (io.ReadCloser, func() (io.ReadCloser, error)) {
+ return replicaBody(req.Body), req.GetBody
+}
-// replicaReplayableBody is the body the attempts send, and how to make a
-// fresh copy of it (nil when none can be made). A body with a GetBody keeps
-// it; one of known length up to replicaReplayLimit is read into memory
-// first; any other (larger, or of unknown length) streams as before, and
-// an attempt that spent part of it is never repeated.
-func replicaReplayableBody(req *http.Request) (io.ReadCloser, func() (io.ReadCloser, error), error) {
- body := replicaBody(req.Body)
- if body == nil || req.GetBody != nil || req.ContentLength <= 0 || req.ContentLength > replicaReplayLimit {
- return body, req.GetBody, nil
+// replicaBodyProxy is a replica set's proxy behind its body admission
+// (hunt H5-M5, O4 round 3): bufferBody, with replay, reads a body the set
+// may send to a second copy (a login, or a known length up to
+// replayBodyLimit) whole before the proxy runs, bounded in size, time and
+// per client, and hands the copy on as the request's GetBody, which the
+// transport replays from (replicaReplayableBody). Any other body streams.
+// Reload installs it around every ArmReplica entry's proxy.
+type replicaBodyProxy struct {
+ e *Edge
+ next http.Handler
+}
+
+func (p replicaBodyProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ whole, ok := p.e.bufferBody(w, r, true)
+ if !ok {
+ return
}
- buf := make([]byte, req.ContentLength)
- _, err := io.ReadFull(body, buf)
- body.Close()
- if err != nil {
- return nil, nil, err
+ if whole != nil && len(whole.b) > 0 {
+ r.GetBody = func() (io.ReadCloser, error) { return whole.reader(), nil }
}
- getBody := func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(buf)), nil }
- first, _ := getBody()
- return replicaBody(first), getBody, nil
+ p.next.ServeHTTP(w, r)
}
// handoffRoundTrip serves a request through the forward row the set
@@ -319,6 +324,12 @@ func (t *replicaTransport) handoffRoundTrip(req *http.Request) (*http.Response,
o.replicaBox = h.box
o.replicaAttempts = 1
}
+ // The body admission's copy (replicaBodyProxy) is for the set's own
+ // attempts: never expose GetBody to a lower transport.
+ if req.GetBody != nil {
+ req = req.WithContext(req.Context())
+ req.GetBody = nil
+ }
return t.hub.transportFor(channel).RoundTrip(req)
}