Skip to content

Commit 5711c84

Browse files
Duncanwpfleger96
andcommitted
fix(auth): let a cross-pod deny fill its pending key's slot
merge_entry only exempted keys already in entries, so with a full shard a remote deny for a key this pod had reserved was dropped, and lost entirely if the local attempt then lost the shared claim. The publish-count test now drains every pod that could publish, not just the sentinel pod. Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 9a9def8 commit 5711c84

3 files changed

Lines changed: 126 additions & 19 deletions

File tree

‎crates/buzz-auth/src/nip_fi/command.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
//! Fail-closed: any failure inserts no deny entry, closes no session and
1919
//! publishes nothing. A failure after step 6's shared claim may leave that
2020
//! claim in place until its TTL (a guard error whose `SET` reply was lost, or a
21-
//! failed release); that is fail closed, since a retry is then denied.
21+
//! process stopped between claim and commit); that is fail closed, since a
22+
//! retry is then denied.
2223
2324
use chrono::{DateTime, Utc};
2425
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};

‎crates/buzz-auth/src/nip_fi/deny_map.rs‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub struct DenySetFull;
5656

5757
/// One issuer's worth of deny entries and jti deduplication state.
5858
///
59-
/// The shard mutex is acquired once per `AtomicReserveJtiAndDenyEntry` call
60-
/// so both mutations happen under the same lock (both-or-neither atomicity).
59+
/// Reserve and commit each take the shard mutex separately; the pending slot
60+
/// held between them keeps the jti and deny entry both-or-neither.
6161
struct IssuerShard {
6262
/// Active deny entries: hex-encoded pubkey → until.
6363
entries: HashMap<String, DateTime<Utc>>,
@@ -224,8 +224,10 @@ impl IssuerShard {
224224
until: DateTime<Utc>,
225225
now: DateTime<Utc>,
226226
) -> Result<(), CapacityExceeded> {
227-
// Overwriting an existing (active or expired) entry never grows the map.
227+
// Overwriting an existing (active or expired) entry never grows the
228+
// map, and a pending key already holds the slot this entry fills.
228229
if !self.entries.contains_key(pubkey_hex)
230+
&& !self.pending_keys.contains_key(pubkey_hex)
229231
&& self.full_after_eviction(now, |s| s.entry_slots() >= s.capacity)
230232
{
231233
return Err(CapacityExceeded);
@@ -954,6 +956,69 @@ mod tests {
954956
// unrelated-key is_denied assertions fail
955957
// (c) admit at exact equality (use <= instead of <) → equality assertion fails
956958

959+
#[test]
960+
fn remote_merge_of_pending_key_uses_its_reserved_slot() {
961+
// Another pod wins the shared claim for k while this pod's reservation
962+
// for k is pending; the propagated deny must land and survive the
963+
// local reservation being dropped, without exceeding capacity.
964+
let now = Utc::now();
965+
let until = now + Duration::seconds(300);
966+
let (k, other) = (key(), key());
967+
let m = NipFiDenyMap::new(
968+
1,
969+
vec![IssuerCapacity {
970+
issuer: iss().to_owned(),
971+
capacity: 1,
972+
}],
973+
);
974+
let pending = m.reserve(iss(), "jti-local", &k, now).expect("slot free");
975+
976+
assert_eq!(
977+
m.merge_cross_pod_deny(iss(), &k, until, now),
978+
CrossPodMergeResult::Merged
979+
);
980+
drop(pending);
981+
982+
assert!(m.is_denied(iss(), &k, now), "the remote deny must survive");
983+
assert_eq!(
984+
m.merge_cross_pod_deny(iss(), &other, until, now),
985+
CrossPodMergeResult::CapacityExceeded,
986+
"k still holds the only slot"
987+
);
988+
}
989+
990+
#[test]
991+
fn commit_after_remote_merge_of_pending_key_max_merges_into_one_entry() {
992+
let now = Utc::now();
993+
let (remote_until, local_until) =
994+
(now + Duration::seconds(600), now + Duration::seconds(300));
995+
let (k, other) = (key(), key());
996+
let m = NipFiDenyMap::new(
997+
1,
998+
vec![IssuerCapacity {
999+
issuer: iss().to_owned(),
1000+
capacity: 1,
1001+
}],
1002+
);
1003+
let pending = m.reserve(iss(), "jti-local", &k, now).expect("slot free");
1004+
assert_eq!(
1005+
m.merge_cross_pod_deny(iss(), &k, remote_until, now),
1006+
CrossPodMergeResult::Merged
1007+
);
1008+
pending.commit(local_until, local_until);
1009+
1010+
assert!(
1011+
m.is_denied(iss(), &k, local_until + Duration::seconds(1)),
1012+
"the later until wins"
1013+
);
1014+
assert!(!m.is_denied(iss(), &k, remote_until));
1015+
assert_eq!(
1016+
m.merge_cross_pod_deny(iss(), &other, remote_until, now),
1017+
CrossPodMergeResult::CapacityExceeded,
1018+
"still one entry"
1019+
);
1020+
}
1021+
9571022
#[test]
9581023
fn remote_merge_capacity_exceeded_preserves_active_entry_and_does_not_deny_missed_or_unrelated()
9591024
{

‎crates/buzz-relay/src/api/nip_fi.rs‎

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,19 +2667,17 @@ mod route_integration_tests {
26672667

26682668
/// Publications for the target so far.
26692669
///
2670-
/// Barrier: wait until every publish `via` has spawned has finished,
2671-
/// so each earlier `PUBLISH` is complete. Then send an accepted
2670+
/// Barrier: wait until every publish spawned by each pod in `drain`
2671+
/// (every pod that could have published) has finished, so each
2672+
/// earlier `PUBLISH` is complete. Then send an accepted
26722673
/// sentinel command and read up to its publication. It is published
26732674
/// strictly after the earlier ones and Redis delivers in publish
26742675
/// order, so the read drains them all; it is also a positive control
26752676
/// that the publisher and this collector both work.
2676-
async fn count(&mut self, via: &Arc<AppState>) -> usize {
2677-
let tasks = &via.nip_fi_publish_tasks;
2678-
tasks.close();
2679-
tokio::time::timeout(std::time::Duration::from_secs(10), tasks.wait())
2680-
.await
2681-
.expect("spawned publishes did not finish within 10s");
2682-
tasks.reopen();
2677+
async fn count(&mut self, drain: &[&Arc<AppState>], via: &Arc<AppState>) -> usize {
2678+
for state in drain {
2679+
drain_publishes(state).await;
2680+
}
26832681
let sentinel = nostr::Keys::generate().public_key();
26842682
let token = mint_token(&sentinel.to_hex(), 300, serde_json::json!({}));
26852683
let (status, _) = post_command(via, &token, &sentinel.to_hex()).await;
@@ -2700,6 +2698,16 @@ mod route_integration_tests {
27002698
}
27012699
}
27022700

2701+
/// Wait until every publish `state` has spawned so far has finished.
2702+
async fn drain_publishes(state: &AppState) {
2703+
let tasks = &state.nip_fi_publish_tasks;
2704+
tasks.close();
2705+
tokio::time::timeout(std::time::Duration::from_secs(10), tasks.wait())
2706+
.await
2707+
.expect("spawned publishes did not finish within 10s");
2708+
tasks.reopen();
2709+
}
2710+
27032711
async fn observe(
27042712
enabled: bool,
27052713
state: &AppState,
@@ -2713,12 +2721,17 @@ mod route_integration_tests {
27132721

27142722
async fn assert_publishes(
27152723
observer: &mut Option<PublishObserver>,
2724+
drain: &[&Arc<AppState>],
27162725
via: &Arc<AppState>,
27172726
expected: usize,
27182727
what: &str,
27192728
) {
27202729
if let Some(observer) = observer {
2721-
assert_eq!(observer.count(via).await, expected, "publications: {what}");
2730+
assert_eq!(
2731+
observer.count(drain, via).await,
2732+
expected,
2733+
"publications: {what}"
2734+
);
27222735
}
27232736
}
27242737

@@ -2736,7 +2749,14 @@ mod route_integration_tests {
27362749

27372750
let (status, _) = post_command(&pod_a, &token, &key.to_hex()).await;
27382751
assert_eq!(status, StatusCode::OK, "first use is accepted on pod A");
2739-
assert_publishes(&mut publishes, &pod_a, 1, "accepted command").await;
2752+
assert_publishes(
2753+
&mut publishes,
2754+
&[&pod_a, &pod_b],
2755+
&pod_a,
2756+
1,
2757+
"accepted command",
2758+
)
2759+
.await;
27402760

27412761
let (status, body) = post_command(&pod_b, &token, &key.to_hex()).await;
27422762
assert_eq!(status, StatusCode::FORBIDDEN, "replay on pod B is denied");
@@ -2746,7 +2766,14 @@ mod route_integration_tests {
27462766
"replay must not insert a deny entry on B"
27472767
);
27482768
on_b.assert_open("pod B after rejected replay");
2749-
assert_publishes(&mut publishes, &pod_b, 1, "rejected replay adds none").await;
2769+
assert_publishes(
2770+
&mut publishes,
2771+
&[&pod_a, &pod_b],
2772+
&pod_b,
2773+
1,
2774+
"rejected replay adds none",
2775+
)
2776+
.await;
27502777
}
27512778

27522779
#[tokio::test]
@@ -2808,7 +2835,7 @@ mod route_integration_tests {
28082835
assert_eq!(body.as_ref(), b"evidence rejected\n");
28092836
assert!(!is_denied(&pod_a, &key), "forgery inserts no deny entry");
28102837
on_a.assert_open("pod A after forgery");
2811-
assert_publishes(&mut publishes, &pod_a, 0, "forgery").await;
2838+
assert_publishes(&mut publishes, &[&pod_a, &pod_b], &pod_a, 0, "forgery").await;
28122839
if let Some(guard) = claims {
28132840
assert_eq!(
28142841
guard.claims.load(std::sync::atomic::Ordering::SeqCst),
@@ -2824,7 +2851,14 @@ mod route_integration_tests {
28242851
"the legitimate command's jti was not burned by the forgery"
28252852
);
28262853
assert!(is_denied(&pod_b, &key));
2827-
assert_publishes(&mut publishes, &pod_b, 1, "accepted command").await;
2854+
assert_publishes(
2855+
&mut publishes,
2856+
&[&pod_a, &pod_b],
2857+
&pod_b,
2858+
1,
2859+
"accepted command",
2860+
)
2861+
.await;
28282862
}
28292863

28302864
#[tokio::test]
@@ -2874,7 +2908,14 @@ mod route_integration_tests {
28742908
Arc::new(buzz_auth::InMemoryCommandReplayGuard::default()),
28752909
)
28762910
.await;
2877-
assert_publishes(&mut publishes, &healthy, 0, "guard error").await;
2911+
assert_publishes(
2912+
&mut publishes,
2913+
&[&state, &healthy],
2914+
&healthy,
2915+
0,
2916+
"guard error",
2917+
)
2918+
.await;
28782919
}
28792920

28802921
#[tokio::test]

0 commit comments

Comments
 (0)