| index a40ca3482..2f578afae 100644 |
| --- a/internal/edge/burst_notice_test.go |
| +++ b/internal/edge/burst_notice_test.go |
| @@ -7,6 +7,7 @@ import ( |
| "crypto/rand" |
| "crypto/tls" |
| "encoding/base64" |
| + "encoding/hex" |
| "errors" |
| "fmt" |
| "io" |
| @@ -621,34 +622,7 @@ func TestBurstNoticeStalledBodyBounded(t *testing.T) { |
| s.publish(t) |
| now := s.clock.now() |
| 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 { |
| - t.Fatal(err) |
| - } |
| - 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/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) |
| - if err != nil { |
| - t.Fatalf("no answer to a stalled body: %v", err) |
| - } |
| - io.Copy(io.Discard, resp.Body) |
| - resp.Body.Close() |
| - took := time.Since(start) |
| - if resp.StatusCode != http.StatusNotFound { |
| - t.Fatalf("a stalled body answered %d, want 404", resp.StatusCode) |
| - } |
| - if took < burstNoticeReadTimeout-200*time.Millisecond || took > burstNoticeReadTimeout+time.Second { |
| - t.Fatalf("answered after %v, want about %v", took, burstNoticeReadTimeout) |
| - } |
| - conn.SetReadDeadline(time.Now().Add(time.Second)) |
| - if _, err := br.ReadByte(); !errors.Is(err, io.EOF) { |
| - t.Fatalf("connection still open after the refusal: %v", err) |
| - } |
| + s.stall(t, b, auth, body) |
| if _, _, ok := b.edge().burst.cooling(noticePrefix(t, "203.0.113.7")); ok { |
| t.Fatal("a stalled notice applied") |
| } |
| @@ -776,6 +750,164 @@ func TestBurstNoticeRefusesWhatDoesNotOpen(t *testing.T) { |
| } |
| } |
| |
| +// The same on a box without the mesh key (a fleet box, or an edge whose key |
| +// has not loaded): its refusal still waits no longer than the body deadline. |
| +func TestBurstNoticeStalledBodyBoundedWithoutAKey(t *testing.T) { |
| + s := newNoticeScene(t, false) |
| + b := s.add(t, "betty", "127.0.0.12", "blog.example.com", "bab", "blog", true) |
| + s.publish(t) |
| + key := make([]byte, 32) |
| + rand.Read(key) |
| + now := s.clock.now() |
| + auth, body := seal(t, key, "betty", now, noticeBody("203.0.113.7/32", now.Add(burstCooldown), "annie", strings.Repeat("x", 200))) |
| + s.stall(t, b, auth, body) |
| + if st := b.edge().notices.stats(); st.RefusedBody != 1 || st.Accepted != 0 { |
| + t.Fatalf("betty's counts %+v, want one body refusal", st) |
| + } |
| +} |
| + |
| +// stall sends x a notice over a real connection, declaring the whole body |
| +// and sending ten bytes of it, and checks the answer is a 404 at the body |
| +// deadline and the connection is closed after it. |
| +func (s *noticeScene) stall(t *testing.T, x *noticeEdge, auth, body string) { |
| + t.Helper() |
| + conn, err := tls.Dial("tcp", net.JoinHostPort(x.file.ip, s.port), &tls.Config{ServerName: x.file.host, RootCAs: s.ca.pool, NextProtos: []string{"http/1.1"}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + 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/octet-stream\r\nContent-Length: %d\r\n\r\n%s", |
| + burstNoticePath, x.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) |
| + if err != nil { |
| + t.Fatalf("no answer to a stalled body: %v", err) |
| + } |
| + io.Copy(io.Discard, resp.Body) |
| + resp.Body.Close() |
| + took := time.Since(start) |
| + if resp.StatusCode != http.StatusNotFound { |
| + t.Fatalf("a stalled body answered %d, want 404", resp.StatusCode) |
| + } |
| + if took < burstNoticeReadTimeout-200*time.Millisecond || took > burstNoticeReadTimeout+time.Second { |
| + t.Fatalf("answered after %v, want about %v", took, burstNoticeReadTimeout) |
| + } |
| + conn.SetReadDeadline(time.Now().Add(time.Second)) |
| + if _, err := br.ReadByte(); !errors.Is(err, io.EOF) { |
| + t.Fatalf("connection still open after the refusal: %v", err) |
| + } |
| +} |
| + |
| +// A delivered notice delivered again, byte for byte, is a replay: refused |
| +// and counted, taking no acceptance slot, leaving the cool-down as it was |
| +// and sending nothing on. |
| +func TestBurstNoticeReplayRefused(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() |
| + until := now.Add(burstCooldown) |
| + auth, body := seal(t, s.key, "betty", now, noticeBody("203.0.113.7/32", until, "annie", "x")) |
| + if w := b.post(auth, body); w.Code != http.StatusNoContent { |
| + t.Fatalf("first delivery answered %d, want 204", w.Code) |
| + } |
| + s.clock.advance(10 * time.Second) |
| + ts, sig, _ := strings.Cut(auth, ":") |
| + for name, a := range map[string]string{"the same bytes": auth, "its hex in capitals": ts + ":" + strings.ToUpper(sig)} { |
| + if w := b.post(a, body); w.Code != http.StatusNotFound { |
| + t.Errorf("%s again: answered %d, want 404", name, w.Code) |
| + } |
| + } |
| + got, _, ok := b.edge().burst.cooling(noticePrefix(t, "203.0.113.7")) |
| + if !ok || !got.Equal(until) { |
| + t.Fatalf("after the replays the client cools until %v (%v), want %v unchanged", got, ok, until) |
| + } |
| + if st := b.edge().notices.stats(); st.Accepted != 1 || st.RefusedReplay != 2 || st.RefusedCap != 0 { |
| + t.Fatalf("betty's counts %+v, want one accepted and two replays", st) |
| + } |
| + if n := transmitted(b); n != 0 || b.edge().notices.peerCount() != 0 { |
| + t.Fatalf("betty sent %d notices on after a replay", n) |
| + } |
| + // The window holds one: the replays took no slot. |
| + if b.edge().notices.accept.count() != 1 { |
| + t.Fatalf("the acceptance window holds %d, want 1", b.edge().notices.accept.count()) |
| + } |
| +} |
| + |
| +// The seen set stays within its bound and forgets after twice hopSkew. |
| +func TestBurstNoticeSeenBounded(t *testing.T) { |
| + c := burstSeen{n: burstNoticeAcceptPerMinute, keys: make(map[string]int64)} |
| + now := time.Date(2026, 10, 31, 12, 0, 0, 0, time.UTC) |
| + for i := range burstNoticeAcceptPerMinute + 1 { |
| + if !c.add(fmt.Sprint(i), now) { |
| + t.Fatalf("key %d refused as seen", i) |
| + } |
| + } |
| + if len(c.keys) != burstNoticeAcceptPerMinute || len(c.order) != burstNoticeAcceptPerMinute { |
| + t.Fatalf("holds %d keys, %d in order, want %d", len(c.keys), len(c.order), burstNoticeAcceptPerMinute) |
| + } |
| + if c.add("600", now) { |
| + t.Fatal("the newest key was not remembered") |
| + } |
| + if !c.add("0", now) { |
| + t.Fatal("the oldest key was not the one evicted at the bound") |
| + } |
| + if c.add("5", now.Add(2*hopSkew-time.Nanosecond)) { |
| + t.Fatal("a key was forgotten inside twice hopSkew") |
| + } |
| + if !c.add("5", now.Add(2*hopSkew)) { |
| + t.Fatal("a key was remembered past twice hopSkew") |
| + } |
| + if len(c.keys) > burstNoticeAcceptPerMinute || len(c.order) > burstNoticeAcceptPerMinute { |
| + t.Fatalf("holds %d keys after expiry", len(c.keys)) |
| + } |
| +} |
| + |
| +// A rotation: with .next mirrored, the receiver takes notices sealed and |
| +// signed under either key, and a trip's notice (now under .next, as |
| +// signingKey prefers) still applies. |
| +func TestBurstNoticeRotation(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) |
| + next := make([]byte, meshKeyBytes) |
| + rand.Read(next) |
| + putJSON(t, s.seed, meshKeyNextKey, hex.EncodeToString(next)) |
| + s.publish(t) |
| + cur, gotNext := b.be.homes.meshKeys() |
| + if len(cur) == 0 || !bytes.Equal(gotNext, next) { |
| + t.Fatal("test bug: betty does not mirror both keys") |
| + } |
| + now := s.clock.now() |
| + other := make([]byte, meshKeyBytes) |
| + rand.Read(other) |
| + for i, c := range []struct { |
| + name string |
| + key []byte |
| + want int |
| + }{{"current", cur, http.StatusNoContent}, {"next", next, http.StatusNoContent}, {"unknown", other, http.StatusNotFound}} { |
| + client := fmt.Sprintf("203.0.113.%d", i+1) |
| + if w := b.post(seal(t, c.key, "betty", now, noticeBody(client+"/32", now.Add(burstCooldown), "annie", "x"))); w.Code != c.want { |
| + t.Errorf("sealed and signed under the %s key: answered %d, want %d", c.name, w.Code, c.want) |
| + } |
| + } |
| + s.trip(t, a, "203.0.113.9") |
| + waitFor(t, 5*time.Second, "betty applies a notice sent under .next", func() bool { |
| + _, _, ok := b.edge().burst.cooling(noticePrefix(t, "203.0.113.9")) |
| + return ok |
| + }) |
| + wire := b.wireBodies() |
| + if len(wire) != 1 { |
| + t.Fatalf("betty saw %d notices over the wire, want 1", len(wire)) |
| + } |
| + if _, ok := openBurstNotice(nil, next, "betty", fmt.Sprint(now.Unix()), wire[0]); !ok { |
| + t.Fatal("the trip's notice was not sealed under .next") |
| + } |
| +} |
| + |
| // The path alone is not a notice: without the header it is the app's. |
| func TestBurstNoticePathWithoutAuthReachesApp(t *testing.T) { |
| s := newNoticeScene(t, true) |
| @@ -901,3 +1033,10 @@ func (n *burstNotices) peerCount() int { |
| defer n.mu.Unlock() |
| return len(n.peers) |
| } |
| + |
| +// count is how many admissions the window holds. |
| +func (c *burstWindowCap) count() int { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + return len(c.at) |
| +} |