|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package envelope_test |
| 4 | + |
| 5 | +// Regression tests for the outside-replay-window stuck-state bug |
| 6 | +// observed in v1.10.0-rc3 against list-agents on 2026-05-11: |
| 7 | +// |
| 8 | +// list-agents' MaxRecvNonce for our node stayed at 8518 while our |
| 9 | +// outbound counter sat at ~400 after a fresh key exchange. Every frame |
| 10 | +// we sent was rejected as ErrOutsideWindow. The current code path at |
| 11 | +// tunnel.go ErrOutsideWindow case logs and returns — no recovery. |
| 12 | +// Tunnel half-dead until a daemon restart on either side. |
| 13 | +// |
| 14 | +// The fix tracks consecutive outside-window rejections on the Crypto |
| 15 | +// (mirroring DecryptFailCount) and surfaces a ShouldDropOnOutsideWindow |
| 16 | +// gate that the L7 handler uses to drop + rekey. These tests exercise |
| 17 | +// the keyexchange/envelope half of the fix; the tunnel.go wiring is |
| 18 | +// covered by integration tests on the daemon. |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/TeoSlayer/pilotprotocol/pkg/daemon/envelope" |
| 26 | + "github.com/TeoSlayer/pilotprotocol/pkg/daemon/keyexchange" |
| 27 | +) |
| 28 | + |
| 29 | +// TestOutsideWindowCountIncrementsAndResets pins the bookkeeping: each |
| 30 | +// ErrOutsideWindow bumps OutsideWindowCount; a successful decrypt |
| 31 | +// resets it to 0. This is the input ShouldDropOnOutsideWindow consumes. |
| 32 | +func TestOutsideWindowCountIncrementsAndResets(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + s := newPeerSetup(t) |
| 35 | + |
| 36 | + // Stash a frame at counter=1. |
| 37 | + stale, err := envelope.EncryptFrame(s.localStore, s.peerID, []byte("stale")) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("encrypt stale: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + // Advance peer MaxRecvNonce past ReplayWindowSize+1 by encrypting and |
| 43 | + // delivering fresh frames so stale (counter=1) lands outside the window. |
| 44 | + advance := keyexchange.ReplayWindowSize + 8 |
| 45 | + for i := 0; i < advance; i++ { |
| 46 | + f, err := envelope.EncryptFrame(s.localStore, s.peerID, []byte("advance")) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("encrypt advance #%d: %v", i, err) |
| 49 | + } |
| 50 | + if r := envelope.DecryptFrame(s.peerStore, f[4:]); r.Err != nil { |
| 51 | + t.Fatalf("advance decrypt #%d: %v", i, r.Err) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + c := s.peerStore.Get(s.localID) |
| 56 | + c.ReplayMu.Lock() |
| 57 | + pre := c.OutsideWindowCount |
| 58 | + c.ReplayMu.Unlock() |
| 59 | + if pre != 0 { |
| 60 | + t.Fatalf("OutsideWindowCount=%d before any outside-window event; want 0", pre) |
| 61 | + } |
| 62 | + |
| 63 | + // Now deliver the stale frame. |
| 64 | + if r := envelope.DecryptFrame(s.peerStore, stale[4:]); !errors.Is(r.Err, envelope.ErrOutsideWindow) { |
| 65 | + t.Fatalf("stale decrypt err=%v; want ErrOutsideWindow", r.Err) |
| 66 | + } |
| 67 | + c.ReplayMu.Lock() |
| 68 | + post := c.OutsideWindowCount |
| 69 | + c.ReplayMu.Unlock() |
| 70 | + if post != 1 { |
| 71 | + t.Fatalf("OutsideWindowCount=%d after one outside-window event; want 1", post) |
| 72 | + } |
| 73 | + |
| 74 | + // Successful decrypt resets to 0. |
| 75 | + f, _ := envelope.EncryptFrame(s.localStore, s.peerID, []byte("fresh")) |
| 76 | + if r := envelope.DecryptFrame(s.peerStore, f[4:]); r.Err != nil { |
| 77 | + t.Fatalf("fresh decrypt: %v", r.Err) |
| 78 | + } |
| 79 | + c.ReplayMu.Lock() |
| 80 | + post2 := c.OutsideWindowCount |
| 81 | + c.ReplayMu.Unlock() |
| 82 | + if post2 != 0 { |
| 83 | + t.Fatalf("OutsideWindowCount=%d after successful decrypt; want 0", post2) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// TestShouldDropOnOutsideWindowGatesOnThresholdAndGrace pins the |
| 88 | +// recovery trigger: the gate must require BOTH the count threshold |
| 89 | +// AND the grace age to elapse. This is what the L7 handler uses to |
| 90 | +// decide whether to drop crypto and request a rekey. |
| 91 | +func TestShouldDropOnOutsideWindowGatesOnThresholdAndGrace(t *testing.T) { |
| 92 | + t.Parallel() |
| 93 | + s := newPeerSetup(t) |
| 94 | + |
| 95 | + c := s.peerStore.Get(s.localID) |
| 96 | + if c == nil { |
| 97 | + t.Fatalf("no Crypto for local on peer side") |
| 98 | + } |
| 99 | + |
| 100 | + // Below threshold: never drop, regardless of age. |
| 101 | + c.ReplayMu.Lock() |
| 102 | + c.OutsideWindowCount = keyexchange.OutsideWindowDropThreshold - 1 |
| 103 | + c.ReplayMu.Unlock() |
| 104 | + // Force age past grace so the only failing check is the threshold. |
| 105 | + backdateCreatedAt(c, time.Now().Add(-2*keyexchange.OutsideWindowDropGrace)) |
| 106 | + if s.peerStore.ShouldDropOnOutsideWindow(s.localID, c) { |
| 107 | + t.Fatalf("dropped at count<threshold") |
| 108 | + } |
| 109 | + |
| 110 | + // At threshold but within grace: don't drop yet. |
| 111 | + c.ReplayMu.Lock() |
| 112 | + c.OutsideWindowCount = keyexchange.OutsideWindowDropThreshold |
| 113 | + c.ReplayMu.Unlock() |
| 114 | + backdateCreatedAt(c, time.Now()) // fresh |
| 115 | + if s.peerStore.ShouldDropOnOutsideWindow(s.localID, c) { |
| 116 | + t.Fatalf("dropped within grace period") |
| 117 | + } |
| 118 | + |
| 119 | + // Threshold met AND grace elapsed: drop. |
| 120 | + backdateCreatedAt(c, time.Now().Add(-2*keyexchange.OutsideWindowDropGrace)) |
| 121 | + if !s.peerStore.ShouldDropOnOutsideWindow(s.localID, c) { |
| 122 | + t.Fatalf("did not drop at threshold + past grace") |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// TestOutsideWindowDropEndToEnd is the smoking-gun reproduction of the |
| 127 | +// list-agents bug: simulate 30+ consecutive outside-window rejections |
| 128 | +// over the grace period, then assert the gate signals drop. Prior to |
| 129 | +// the fix the count never moved (no recovery path); now it climbs and |
| 130 | +// the gate trips, letting the L7 handler tear down the diverged Crypto. |
| 131 | +func TestOutsideWindowDropEndToEnd(t *testing.T) { |
| 132 | + t.Parallel() |
| 133 | + s := newPeerSetup(t) |
| 134 | + |
| 135 | + // Capture N+1 stale frames before advancing peer's MaxRecvNonce so |
| 136 | + // that each one lands outside the window when delivered later. |
| 137 | + const stales = keyexchange.OutsideWindowDropThreshold + 1 |
| 138 | + staleFrames := make([][]byte, stales) |
| 139 | + for i := 0; i < stales; i++ { |
| 140 | + f, err := envelope.EncryptFrame(s.localStore, s.peerID, []byte("stale")) |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("encrypt stale #%d: %v", i, err) |
| 143 | + } |
| 144 | + staleFrames[i] = f |
| 145 | + } |
| 146 | + |
| 147 | + // Advance peer's window past all stale counters. |
| 148 | + advance := keyexchange.ReplayWindowSize + stales + 8 |
| 149 | + for i := 0; i < advance; i++ { |
| 150 | + f, err := envelope.EncryptFrame(s.localStore, s.peerID, []byte("advance")) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("encrypt advance #%d: %v", i, err) |
| 153 | + } |
| 154 | + if r := envelope.DecryptFrame(s.peerStore, f[4:]); r.Err != nil { |
| 155 | + t.Fatalf("advance decrypt #%d: %v", i, r.Err) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Backdate Crypto so the grace gate is satisfied at the moment we |
| 160 | + // hit the count threshold below. |
| 161 | + c := s.peerStore.Get(s.localID) |
| 162 | + backdateCreatedAt(c, time.Now().Add(-2*keyexchange.OutsideWindowDropGrace)) |
| 163 | + |
| 164 | + // Deliver the stale frames. Each one should produce ErrOutsideWindow |
| 165 | + // and bump the counter. ShouldDropOnOutsideWindow should flip true |
| 166 | + // once threshold is reached. |
| 167 | + for i, f := range staleFrames { |
| 168 | + r := envelope.DecryptFrame(s.peerStore, f[4:]) |
| 169 | + if !errors.Is(r.Err, envelope.ErrOutsideWindow) { |
| 170 | + t.Fatalf("stale #%d err=%v; want ErrOutsideWindow", i, r.Err) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if !s.peerStore.ShouldDropOnOutsideWindow(s.localID, c) { |
| 175 | + c.ReplayMu.Lock() |
| 176 | + count := c.OutsideWindowCount |
| 177 | + c.ReplayMu.Unlock() |
| 178 | + t.Fatalf("BUG-2: ShouldDropOnOutsideWindow=false after %d "+ |
| 179 | + "consecutive ErrOutsideWindow (count=%d, threshold=%d)", |
| 180 | + stales, count, keyexchange.OutsideWindowDropThreshold) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// backdateCreatedAt rewrites the CreatedAt timestamp on a Crypto so |
| 185 | +// age-gated tests don't have to sleep through the real grace period. |
| 186 | +func backdateCreatedAt(c *keyexchange.Crypto, when time.Time) { |
| 187 | + c.CreatedAt = when |
| 188 | +} |
0 commit comments