| index 925d0b71e..a40ca3482 100644 |
| --- a/internal/edge/burst_notice_test.go |
| +++ b/internal/edge/burst_notice_test.go |
| @@ -2,16 +2,20 @@ package edge |
| |
| import ( |
| "bufio" |
| + "bytes" |
| "context" |
| "crypto/rand" |
| "crypto/tls" |
| + "encoding/base64" |
| "errors" |
| "fmt" |
| "io" |
| "net" |
| "net/http" |
| "net/http/httptest" |
| + "net/http/httputil" |
| "net/netip" |
| + "net/url" |
| "slices" |
| "strings" |
| "sync" |
| @@ -49,12 +53,32 @@ type noticeEdge struct { |
| // reached this box's 443. |
| mu sync.Mutex |
| at []time.Time |
| - // appNotice counts the requests for burstNoticePath this box's app saw. |
| + // wire is the body of each of those requests as it arrived. |
| + wire [][]byte |
| + // appNotice counts the requests for burstNoticePath this box's app |
| + // saw, and appBodies holds their bodies. |
| appNotice atomic.Int64 |
| + appBodies [][]byte |
| + // older makes the box's 443 behave as a build without the interceptor: |
| + // every request goes straight to the app, as that build's route lookup |
| + // would send it. |
| + older atomic.Bool |
| } |
| |
| func (x *noticeEdge) edge() *Edge { return x.be.live.Load().edge } |
| |
| +func (x *noticeEdge) wireBodies() [][]byte { |
| + x.mu.Lock() |
| + defer x.mu.Unlock() |
| + return slices.Clone(x.wire) |
| +} |
| + |
| +func (x *noticeEdge) appSaw() [][]byte { |
| + x.mu.Lock() |
| + defer x.mu.Unlock() |
| + return slices.Clone(x.appBodies) |
| +} |
| + |
| func (x *noticeEdge) arrivals() []time.Time { |
| x.mu.Lock() |
| defer x.mu.Unlock() |
| @@ -120,12 +144,17 @@ func (s *noticeScene) add(t *testing.T, box, ip, host, acct, app string, adverti |
| x := ¬iceEdge{file: ¬iceFile{box: box, ip: ip, host: host, advertise: advertise}, rec: &lockedRecorder{}} |
| backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| if r.URL.Path == burstNoticePath { |
| + b, _ := io.ReadAll(r.Body) |
| + x.mu.Lock() |
| + x.appBodies = append(x.appBodies, b) |
| + x.mu.Unlock() |
| x.appNotice.Add(1) |
| } |
| w.WriteHeader(http.StatusNotFound) |
| io.WriteString(w, "the app's own 404") |
| })) |
| t.Cleanup(backend.Close) |
| + toApp := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: backend.Listener.Addr().String()}) |
| be := &backhaulEdge{store: release.NewDirStore(s.bucket), reg: openReg(t), box: box} |
| if err := be.reg.Upsert(registry.Route{Host: host, Kind: "service", Addr: backend.Listener.Addr().String(), |
| Status: "ready", Account: acct, App: app}); err != nil { |
| @@ -143,6 +172,22 @@ func (s *noticeScene) add(t *testing.T, box, ip, host, acct, app string, adverti |
| x.mu.Lock() |
| x.at = append(x.at, s.clock.now()) |
| x.mu.Unlock() |
| + // The body as it is read, never read ahead: a stalled body must |
| + // stall the edge, not this tap. |
| + var wire bytes.Buffer |
| + r.Body = struct { |
| + io.Reader |
| + io.Closer |
| + }{io.TeeReader(r.Body, &wire), r.Body} |
| + defer func() { |
| + x.mu.Lock() |
| + x.wire = append(x.wire, wire.Bytes()) |
| + x.mu.Unlock() |
| + }() |
| + } |
| + if x.older.Load() { |
| + toApp.ServeHTTP(w, r) |
| + return |
| } |
| be.live.Load().edge.ServeHTTP(w, r) |
| })) |
| @@ -241,6 +286,17 @@ func (x *noticeEdge) post(auth, body string) *httptest.ResponseRecorder { |
| return w |
| } |
| |
| +// seal is a notice as a sender puts it on the wire for to: plain sealed |
| +// under key at ts, and the signature over the sealed body. |
| +func seal(t *testing.T, key []byte, to string, ts time.Time, plain string) (auth, body string) { |
| + t.Helper() |
| + b, err := sealBurstNotice(key, to, ts, []byte(plain)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return signBurstNotice(key, to, ts, b), string(b) |
| +} |
| + |
| func noticeBody(client string, until time.Time, from, ref string) string { |
| return fmt.Sprintf(`{"client":%q,"until":%d,"from":%q,"ref":%q}`, client, until.Unix(), from, ref) |
| } |
| @@ -328,14 +384,13 @@ func TestBurstNoticeRefusesForgery(t *testing.T) { |
| if !strings.Contains(client, "/") { |
| client += "/32" |
| } |
| - body := noticeBody(client, c.until, "annie", "x") |
| - if w := b.post(signBurstNotice(c.key, c.to, c.ts, []byte(body)), body); w.Code != http.StatusNotFound { |
| + if w := b.post(seal(t, c.key, c.to, c.ts, noticeBody(client, c.until, "annie", "x"))); w.Code != http.StatusNotFound { |
| t.Errorf("%s: answered %d, want 404", c.name, w.Code) |
| } |
| } |
| // A valid signature over a different body. |
| - body := noticeBody("203.0.113.9/32", now.Add(burstCooldown), "annie", "x") |
| - sig := signBurstNotice(s.key, "betty", now, []byte(noticeBody("203.0.113.10/32", now.Add(burstCooldown), "annie", "x"))) |
| + _, body := seal(t, s.key, "betty", now, noticeBody("203.0.113.9/32", now.Add(burstCooldown), "annie", "x")) |
| + sig, _ := seal(t, s.key, "betty", now, noticeBody("203.0.113.10/32", now.Add(burstCooldown), "annie", "x")) |
| if w := b.post(sig, body); w.Code != http.StatusNotFound { |
| t.Errorf("a signature over another body: answered %d, want 404", w.Code) |
| } |
| @@ -351,8 +406,7 @@ func TestBurstNoticeRefusesForgery(t *testing.T) { |
| |
| // The same shape, properly signed, applies: the refusals above were the |
| // faults and nothing else. |
| - good := noticeBody("203.0.113.11/32", now.Add(burstCooldown), "annie", "x") |
| - if w := b.post(signBurstNotice(s.key, "betty", now, []byte(good)), good); w.Code != http.StatusNoContent { |
| + if w := b.post(seal(t, s.key, "betty", now, noticeBody("203.0.113.11/32", now.Add(burstCooldown), "annie", "x"))); w.Code != http.StatusNoContent { |
| t.Fatalf("a valid notice: answered %d, want 204", w.Code) |
| } |
| if _, _, ok := b.edge().burst.cooling(noticePrefix(t, "203.0.113.11")); !ok { |
| @@ -380,8 +434,7 @@ func TestBurstNoticeNoKeyNoSendNoAccept(t *testing.T) { |
| key := make([]byte, 32) |
| rand.Read(key) |
| now := s.clock.now() |
| - body := noticeBody("203.0.113.8/32", now.Add(burstCooldown), "annie", "x") |
| - if w := b.post(signBurstNotice(key, "betty", now, []byte(body)), body); w.Code != http.StatusNotFound { |
| + if w := b.post(seal(t, key, "betty", now, noticeBody("203.0.113.8/32", now.Add(burstCooldown), "annie", "x"))); w.Code != http.StatusNotFound { |
| t.Fatalf("betty with no key answered %d, want 404", w.Code) |
| } |
| if _, _, ok := b.edge().burst.cooling(noticePrefix(t, "203.0.113.8")); ok { |
| @@ -435,8 +488,7 @@ func TestBurstNoticeCaps(t *testing.T) { |
| applied, refused := 0, 0 |
| for i := range 610 { |
| client := fmt.Sprintf("198.19.%d.%d", i/250, i%250+1) |
| - body := noticeBody(client+"/32", now.Add(burstCooldown), "annie", "x") |
| - switch w := b.post(signBurstNotice(s.key, "betty", now, []byte(body)), body); w.Code { |
| + switch w := b.post(seal(t, s.key, "betty", now, noticeBody(client+"/32", now.Add(burstCooldown), "annie", "x"))); w.Code { |
| case http.StatusNoContent: |
| applied++ |
| case http.StatusNotFound: |
| @@ -568,7 +620,7 @@ func TestBurstNoticeStalledBodyBounded(t *testing.T) { |
| b := s.add(t, "betty", "127.0.0.12", "blog.example.com", "bab", "blog", true) |
| s.publish(t) |
| now := s.clock.now() |
| - body := noticeBody("203.0.113.7/32", now.Add(burstCooldown), "annie", "x") |
| + auth, body := seal(t, s.key, "betty", now, noticeBody("203.0.113.7/32", now.Add(burstCooldown), "annie", "x")) |
| |
| conn, err := tls.Dial("tcp", net.JoinHostPort(b.file.ip, s.port), &tls.Config{ServerName: b.file.host, RootCAs: s.ca.pool, NextProtos: []string{"http/1.1"}}) |
| if err != nil { |
| @@ -576,8 +628,8 @@ func TestBurstNoticeStalledBodyBounded(t *testing.T) { |
| } |
| defer conn.Close() |
| start := time.Now() |
| - fmt.Fprintf(conn, "POST %s HTTP/1.1\r\nHost: %s\r\n%s: %s\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", |
| - burstNoticePath, b.file.host, burstNoticeAuthHeader, signBurstNotice(s.key, "betty", now, []byte(body)), len(body), body[:10]) |
| + fmt.Fprintf(conn, "POST %s HTTP/1.1\r\nHost: %s\r\n%s: %s\r\nContent-Type: application/octet-stream\r\nContent-Length: %d\r\n\r\n%s", |
| + burstNoticePath, b.file.host, burstNoticeAuthHeader, auth, len(body), body[:10]) |
| conn.SetReadDeadline(time.Now().Add(10 * time.Second)) |
| br := bufio.NewReader(conn) |
| resp, err := http.ReadResponse(br, nil) |
| @@ -605,8 +657,7 @@ func TestBurstNoticeStalledBodyBounded(t *testing.T) { |
| } |
| |
| // An oversized body is refused the same way. |
| - big := noticeBody("203.0.113.7/32", now.Add(burstCooldown), "annie", strings.Repeat("x", burstNoticeMaxBody)) |
| - if w := b.post(signBurstNotice(s.key, "betty", now, []byte(big)), big); w.Code != http.StatusNotFound { |
| + if w := b.post(seal(t, s.key, "betty", now, noticeBody("203.0.113.7/32", now.Add(burstCooldown), "annie", strings.Repeat("x", burstNoticeMaxBody)))); w.Code != http.StatusNotFound { |
| t.Fatalf("an oversized notice answered %d, want 404", w.Code) |
| } |
| if st := b.edge().notices.stats(); st.RefusedBody != 2 { |
| @@ -614,6 +665,117 @@ func TestBurstNoticeStalledBodyBounded(t *testing.T) { |
| } |
| } |
| |
| +// A notice on the wire is sealed: nothing in its body names the client, |
| +// either box or the trip, and it fits the receiver's cap. |
| +func TestBurstNoticeWireBodyIsOpaque(t *testing.T) { |
| + s := newNoticeScene(t, true) |
| + a := s.add(t, "annie", "127.0.0.11", "shop.example.com", "bdk", "shop", true) |
| + b := s.add(t, "betty", "127.0.0.12", "blog.example.com", "bab", "blog", true) |
| + s.publish(t) |
| + |
| + // An IPv6 client too: the longest client key a notice carries. |
| + for _, client := range []string{"203.0.113.7", "2001:db8:1:2::7"} { |
| + for i := range burstThreshold { |
| + r := httptest.NewRequest(http.MethodGet, "http://"+a.file.host+scanPath(i), nil) |
| + r.RemoteAddr = net.JoinHostPort(client, "40000") |
| + a.edge().ServeHTTP(httptest.NewRecorder(), r) |
| + } |
| + } |
| + waitFor(t, 5*time.Second, "both notices applied", func() bool { return b.edge().notices.stats().Accepted == 2 }) |
| + a.rec.mu.Lock() |
| + trips := slices.Clone(a.rec.trips) |
| + a.rec.mu.Unlock() |
| + if len(trips) != 2 { |
| + t.Fatalf("annie filed %d trips, want 2", len(trips)) |
| + } |
| + wire := b.wireBodies() |
| + if len(wire) != 2 { |
| + t.Fatalf("betty saw %d notice bodies, want 2", len(wire)) |
| + } |
| + for _, body := range wire { |
| + if len(body) > burstNoticeMaxBody { |
| + t.Fatalf("a notice body of %d bytes, over the %d cap", len(body), burstNoticeMaxBody) |
| + } |
| + for _, clear := range []string{"203.0.113", "2001:db8", "annie", "betty", "client", "until", trips[0].Ref, trips[1].Ref} { |
| + if bytes.Contains(body, []byte(clear)) { |
| + t.Fatalf("the notice body carries %q in clear: %s", clear, body) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// R3-1 whatever the rollout: a box that still advertises notices but runs |
| +// a build without the interceptor (rolled back, its file not yet |
| +// rewritten) hands its app the notice, and the app gets only ciphertext. |
| +func TestBurstNoticeOlderPeerSeesOnlyCiphertext(t *testing.T) { |
| + s := newNoticeScene(t, true) |
| + a := s.add(t, "annie", "127.0.0.11", "shop.example.com", "bdk", "shop", true) |
| + c := s.add(t, "carol", "127.0.0.13", "docs.example.com", "bab", "docs", true) |
| + c.older.Store(true) |
| + s.publish(t) |
| + |
| + const client = "203.0.113.7" |
| + s.trip(t, a, client) |
| + waitFor(t, 5*time.Second, "the notice to carol", func() bool { return transmitted(a) == 1 }) |
| + got := c.appSaw() |
| + if len(got) != 1 { |
| + t.Fatalf("carol's app saw %d notices, want the one the older build passes on", len(got)) |
| + } |
| + a.rec.mu.Lock() |
| + ref := a.rec.trips[0].Ref |
| + a.rec.mu.Unlock() |
| + for _, clear := range []string{client, "annie", "carol", "client", ref} { |
| + if bytes.Contains(got[0], []byte(clear)) { |
| + t.Fatalf("carol's app was handed %q in clear: %s", clear, got[0]) |
| + } |
| + } |
| + if _, err := base64.StdEncoding.Strict().DecodeString(string(got[0])); err != nil { |
| + t.Fatalf("carol's app was handed something other than the sealed body: %q", got[0]) |
| + } |
| +} |
| + |
| +// A notice whose signature checks but whose body does not open under the |
| +// mesh key, or opens only for another receiver or time, is refused. |
| +func TestBurstNoticeRefusesWhatDoesNotOpen(t *testing.T) { |
| + s := newNoticeScene(t, true) |
| + s.add(t, "annie", "127.0.0.11", "shop.example.com", "bdk", "shop", true) |
| + b := s.add(t, "betty", "127.0.0.12", "blog.example.com", "bab", "blog", true) |
| + s.publish(t) |
| + now := s.clock.now() |
| + other := make([]byte, len(s.key)) |
| + rand.Read(other) |
| + plain := func(i int) string { |
| + return noticeBody(fmt.Sprintf("203.0.113.%d/32", i), now.Add(burstCooldown), "annie", "x") |
| + } |
| + sealedWith := func(key []byte, to string, ts time.Time, p string) string { |
| + b, err := sealBurstNotice(key, to, ts, []byte(p)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return string(b) |
| + } |
| + cases := map[string]string{ |
| + "sealed under another key": sealedWith(other, "betty", now, plain(1)), |
| + "sealed for another box": sealedWith(s.key, "carol", now, plain(2)), |
| + "sealed at another time": sealedWith(s.key, "betty", now.Add(-time.Second), plain(3)), |
| + "not sealed at all": plain(4), |
| + "cut short": sealedWith(s.key, "betty", now, plain(5))[:20], |
| + } |
| + for name, body := range cases { |
| + if w := b.post(signBurstNotice(s.key, "betty", now, []byte(body)), body); w.Code != http.StatusNotFound { |
| + t.Errorf("%s: answered %d, want 404", name, w.Code) |
| + } |
| + } |
| + for i := 1; i <= 5; i++ { |
| + if _, _, ok := b.edge().burst.cooling(noticePrefix(t, fmt.Sprintf("203.0.113.%d", i))); ok { |
| + t.Errorf("203.0.113.%d is cooling after a notice that did not open", i) |
| + } |
| + } |
| + if st := b.edge().notices.stats(); st.RefusedSealed != 5 || st.RefusedAuth != 0 || st.Accepted != 0 { |
| + t.Fatalf("betty's counts %+v, want five refused as not opening", st) |
| + } |
| +} |
| + |
| // The path alone is not a notice: without the header it is the app's. |
| func TestBurstNoticePathWithoutAuthReachesApp(t *testing.T) { |
| s := newNoticeScene(t, true) |
| @@ -669,7 +831,7 @@ func TestBurstNoticeIsNotAHop(t *testing.T) { |
| b := s.add(t, "betty", "127.0.0.12", "blog.example.com", "bab", "blog", true) |
| s.publish(t) |
| now := s.clock.now() |
| - body := noticeBody("203.0.113.7/32", now.Add(burstCooldown), "annie", "x") |
| + sig, body := seal(t, s.key, "betty", now, noticeBody("203.0.113.7/32", now.Add(burstCooldown), "annie", "x")) |
| for name, sig := range map[string]string{ |
| "hop for the host": signHop(s.key, b.file.host, now), |
| "hop for the box": signHop(s.key, "betty", now), |
| @@ -687,7 +849,6 @@ func TestBurstNoticeIsNotAHop(t *testing.T) { |
| t.Fatalf("betty's counts %+v, want four auth refusals", st) |
| } |
| |
| - sig := signBurstNotice(s.key, "betty", now, []byte(body)) |
| for _, host := range []string{"betty", b.file.host} { |
| if verifyHopEitherKey(s.key, nil, host, sig, now) || verifyHopDoorEitherKey(s.key, nil, host, sig, now) { |
| t.Fatalf("a notice signature verifies as a hop or door mark for %s", host) |