oficina / alta Public

adopt: probe keeps exact body, rejects oversized; tighten tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 05ba07f523f6692b2a4b9ef79f08623ca9bb1d22 parent c80ac77
3 files changed, +58 −11
  • adopt/delivery.go +6 −2
  • adopt/delivery_test.go +47 −7
  • adopt/types_test.go +5 −2
diff --git a/adopt/delivery.go b/adopt/delivery.go
index 294b5a9..bffd511 100644
--- a/adopt/delivery.go
+++ b/adopt/delivery.go
@@ -67,7 +67,7 @@ func Probe(ctx context.Context, h *http.Client, origin string) (Delivery, error)
case status >= 300 && status < 400:
d.Redirected = true
case status == http.StatusOK:
- d.Version = strings.TrimSpace(string(body))
+ d.Version = string(body)
d.Header = hdr.Get("X-Carlos-Version")
}
status, _, _, err = get(ctx, &c, base+"/healthz")
@@ -108,10 +108,14 @@ func get(ctx context.Context, c *http.Client, target string) (int, http.Header,
return 0, nil, nil, errors.New("adopt: probe unreachable")
}
defer resp.Body.Close()
- body, err := io.ReadAll(io.LimitReader(resp.Body, bodyLimit))
+ // One byte past the limit tells "exactly the limit" from "cut off".
+ body, err := io.ReadAll(io.LimitReader(resp.Body, bodyLimit+1))
if err != nil {
return 0, nil, nil, errors.New("adopt: probe answer unreadable")
}
+ if len(body) > bodyLimit {
+ return 0, nil, nil, errors.New("adopt: probe answer too large")
+ }
return resp.StatusCode, resp.Header, body, nil
}
diff --git a/adopt/delivery_test.go b/adopt/delivery_test.go
index 29e2e68..6b215c2 100644
--- a/adopt/delivery_test.go
+++ b/adopt/delivery_test.go
@@ -25,7 +25,7 @@ func (s *site) handler() http.Handler {
return
}
w.Header().Set("X-Carlos-Version", s.header)
- _, _ = w.Write([]byte(s.version + "\n"))
+ _, _ = w.Write([]byte(s.version))
})
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(s.health) })
mux.HandleFunc("/api/v1/operations", func(w http.ResponseWriter, r *http.Request) {
@@ -136,15 +136,55 @@ func TestProbeRefusesNonLoopbackHTTP(t *testing.T) {
}
func TestProbeErrorsCarryNoBody(t *testing.T) {
- s := &site{version: "secret-looking-body", header: "h", health: 200}
- srv := httptest.NewServer(s.handler())
- srv.Close()
+ // The answer promises more bytes than it sends, so the body read fails
+ // after recognisable content has arrived.
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Length", "1000")
+ _, _ = w.Write([]byte("LEAKY-BODY-CONTENT"))
+ }))
+ defer srv.Close()
_, err := Probe(context.Background(), srv.Client(), srv.URL)
if err == nil {
- t.Fatal("expected a transport error")
+ t.Fatal("expected a body read failure")
+ }
+ for _, leak := range []string{"LEAKY-BODY-CONTENT", srv.URL, strings.TrimPrefix(srv.URL, "http://")} {
+ if strings.Contains(err.Error(), leak) {
+ t.Fatalf("error leaks %q: %v", leak, err)
+ }
}
- if strings.Contains(err.Error(), "secret-looking-body") {
- t.Fatal("error leaks a body")
+}
+
+func TestProbeKeepsBodyExactly(t *testing.T) {
+ s := &site{version: "v1\n", header: "v1", health: 200}
+ a := probeOf(t, s)
+ s.version = "v1"
+ b := probeOf(t, s)
+ if a.Version == b.Version {
+ t.Fatal("a whitespace-only change was hidden")
+ }
+ before := Delivery{Version: a.Version, Header: "v1", Health: 200, Ops: "absent"}
+ now := Delivery{Version: b.Version, Header: "v1", Health: 200, Ops: "agent-401"}
+ if err := Delivered(before, now); err == nil {
+ t.Fatal("Delivered approved a whitespace-only change")
+ }
+}
+
+func TestProbeRejectsOversizedBodies(t *testing.T) {
+ prefix := strings.Repeat("a", bodyLimit)
+ for _, tail := range []string{"x", "y"} {
+ body := prefix + tail
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(body)) }))
+ _, err := Probe(context.Background(), srv.Client(), srv.URL)
+ srv.Close()
+ if err == nil {
+ t.Fatalf("oversized body (%s) accepted", tail)
+ }
+ }
+ // Exactly the limit is fine.
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(prefix)) }))
+ defer srv.Close()
+ if _, err := Probe(context.Background(), srv.Client(), srv.URL); err != nil {
+ t.Fatalf("a body of exactly the limit refused: %v", err)
}
}
diff --git a/adopt/types_test.go b/adopt/types_test.go
index ea23fc7..baa4c3f 100644
--- a/adopt/types_test.go
+++ b/adopt/types_test.go
@@ -110,7 +110,10 @@ func TestSameIdentityFlagsEachField(t *testing.T) {
}
func TestRequiredLiveChecksFixed(t *testing.T) {
- if len(RequiredLiveChecks) != 8 {
- t.Fatal("eight live checks")
+ want := []string{"non-member-refused", "member-allowed", "member-no-local-permission-refused",
+ "revoked-membership-refused", "local-access-kept", "owner-allowed",
+ "owner-revoked-refused", "owner-local-access-kept"}
+ if !reflect.DeepEqual(RequiredLiveChecks, want) {
+ t.Fatalf("live checks = %v", RequiredLiveChecks)
}
}