Prekeys: authenticate bundle packets, fix consume-republish, deflake CI

Fixes the prekey-bundle PR review + CI failure:

- CI root cause: the receive queue (mesh.message) is concurrent, so a
  gossiped prekey bundle can be processed before the announce that binds
  its owner's signing key. The old handler dropped such bundles outright,
  so under CI parallel load the bundle was permanently lost and the
  cache/gossip tests flaked (verifiedBundleEntersGossipStore,
  prekeySealedMailTravelsViaCourierAndOpens). Bundles that arrive before
  their binding are now retained per-owner (bounded) and re-attempted when
  the verified announce lands, atomically to avoid a check-then-act race.

- Authenticate the OUTER prekey-bundle packet (Codex P2 / review MEDIUM):
  require senderID == PeerID(bundle.noiseStaticPublicKey) and verify the
  packet's Ed25519 signature (covers senderID + timestamp) against the
  owner's bound signing key, in addition to the inner bundle signature.
  Stops replay under a fresh timestamp / fake senderID.

- Key the gossip prekey-bundle store/dedup by the bundle's authenticated
  identity (noiseStaticPublicKey), not the unauthenticated packet
  senderID, so one valid bundle sprayed under many fabricated sender IDs
  can't multiply entries and exhaust the 200-owner cap.

- Bump published-bundle generatedAt strictly on consume (Codex P1):
  consuming a prekey shrinks the published bundle, so it now republishes
  with a strictly newer generatedAt and re-gossips, so peers replace the
  cached copy and stop assigning the consumed ID before its 48h grace.

- Guard the panic/clear detached Application Support tree-deletes behind
  TestEnvironment.isRunningTests: the SPM test process shares that tree,
  so the wipe could land mid-test and flake file-dependent tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
jack
2026-07-06 21:27:41 +02:00
co-authored by Claude Fable 5
parent ee148a018b
commit b481a70458
9 changed files with 309 additions and 30 deletions
+50 -1
View File
@@ -156,6 +156,15 @@ struct LocalPrekeyStoreTests {
LocalPrekeyStore(keychain: keychain, now: { clock.now })
}
private func bundle(noiseKey: Data, prekeys: [PrekeyBundle.Prekey], generatedAt: UInt64) -> PrekeyBundle {
PrekeyBundle(
noiseStaticPublicKey: noiseKey,
prekeys: prekeys,
generatedAt: generatedAt,
signature: Data(count: PrekeyBundle.signatureLength)
)
}
@Test func mintsFullBatchOnFirstUse() {
let store = makeStore(clock: Clock())
let (prekeys, generatedAt) = store.currentBundlePrekeys()
@@ -190,6 +199,42 @@ struct LocalPrekeyStoreTests {
#expect(survivorIDs.isSubset(of: Set(replenished.map(\.id))))
}
@Test func consumingAPrekeyRepublishesANewerBundlePeersAccept() {
// Codex P1: consuming a prekey (even above the replenish threshold)
// must republish a strictly newer bundle so a peer that cached the old
// one replaces it and stops assigning the consumed ID before its 48h
// grace lapses.
let clock = Clock()
let store = makeStore(clock: clock)
let noiseKey = Data(repeating: 0xC0, count: 32)
// Owner publishes; a peer caches it and would assign the first prekey.
let (initial, firstGeneratedAt) = store.currentBundlePrekeys()
let peerCache = PrekeyBundleStore(persistsToDisk: false)
#expect(peerCache.ingest(bundle(noiseKey: noiseKey, prekeys: initial, generatedAt: firstGeneratedAt)))
let consumedID = initial[0].id
#expect(peerCache.assignPrekey(messageID: "m1", recipientNoiseKey: noiseKey)?.id == consumedID)
// The owner opens mail sealed to that prekey: it's retired and the
// republished bundle is strictly newer and no longer offers the ID.
#expect(store.markConsumed(consumedID))
let (afterConsume, secondGeneratedAt) = store.currentBundlePrekeys()
#expect(secondGeneratedAt > firstGeneratedAt)
#expect(!afterConsume.contains { $0.id == consumedID })
// The peer accepts the replacement (a same-generatedAt copy would be
// rejected) and stops assigning the consumed ID for new mail.
#expect(peerCache.ingest(bundle(noiseKey: noiseKey, prekeys: afterConsume, generatedAt: secondGeneratedAt)))
#expect(peerCache.assignPrekey(messageID: "m2", recipientNoiseKey: noiseKey)?.id != consumedID)
// 48h grace: the owner can still open a redelivery of the in-flight
// ciphertext sealed to the consumed ID until the window lapses.
clock.now = clock.now.addingTimeInterval(LocalPrekeyStore.Policy.consumedGraceSeconds - 60)
#expect(store.privateKey(for: consumedID) != nil)
clock.now = clock.now.addingTimeInterval(120)
#expect(store.privateKey(for: consumedID) == nil)
}
@Test func consumedPrivateSurvivesGraceWindowThenDies() {
let clock = Clock()
let store = makeStore(clock: clock)
@@ -217,7 +262,11 @@ struct LocalPrekeyStoreTests {
let second = LocalPrekeyStore(keychain: keychain, now: { clock.now })
let (reloaded, reloadedGeneratedAt) = second.currentBundlePrekeys()
#expect(reloadedGeneratedAt == generatedAt)
// Consuming a prekey shrinks the published bundle, so its generation
// stamp advances strictly (even without the clock moving) peers must
// see a newer bundle to replace the one that still offered the
// consumed ID.
#expect(reloadedGeneratedAt > generatedAt)
#expect(Set(reloaded.map(\.id)) == Set(prekeys.dropFirst().map(\.id)))
// The consumed key is still openable within grace after a relaunch.
#expect(second.privateKey(for: prekeys[0].id) != nil)