Skip to content

Commit ca384d0

Browse files
kalvinnchaunpub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7
andauthored
fix: recover community access visibility (#2074)
Signed-off-by: Kalvin Chau <[email protected]> Co-authored-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@sprout-oss.stage.blox.sqprod.co>
1 parent 3afc518 commit ca384d0

13 files changed

Lines changed: 477 additions & 46 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ jobs:
690690
run: |
691691
cargo test -p buzz-test-client --test e2e_persona --test e2e_nostr_interop -- --ignored --nocapture
692692
cargo test -p buzz-test-client --test e2e_relay invite -- --ignored --nocapture
693+
cargo test -p buzz-test-client --test e2e_relay nip43_membership_snapshots_are_rejected -- --ignored --nocapture
693694
env:
694695
RELAY_URL: ws://localhost:3000
695696
GIT_CREDENTIAL_NOSTR_BIN: ${{ github.workspace }}/target/ci/git-credential-nostr

‎crates/buzz-core/src/kind.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,12 +677,13 @@ pub const fn is_command_kind(kind: u32) -> bool {
677677
)
678678
}
679679

680-
/// Returns `true` if `kind` is a relay-only sidecar kind.
680+
/// Returns `true` if `kind` may only be authored by the relay.
681681
/// Client submission of these kinds must be rejected.
682682
pub const fn is_relay_only_kind(kind: u32) -> bool {
683683
matches!(
684684
kind,
685-
KIND_CHANNEL_SUMMARY
685+
KIND_NIP43_MEMBERSHIP_LIST
686+
| KIND_CHANNEL_SUMMARY
686687
| KIND_PRESENCE_SNAPSHOT
687688
| KIND_DM_VISIBILITY
688689
| KIND_THREAD_SUMMARY
@@ -755,6 +756,12 @@ mod tests {
755756
}
756757
}
757758

759+
#[test]
760+
fn nip43_membership_snapshot_is_relay_only() {
761+
assert!(is_relay_only_kind(KIND_NIP43_MEMBERSHIP_LIST));
762+
assert!(!is_relay_only_kind(KIND_NIP43_LEAVE_REQUEST));
763+
}
764+
758765
#[test]
759766
fn parameterized_replaceable_range() {
760767
assert!(!is_parameterized_replaceable(29999));

‎crates/buzz-db/src/lib.rs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,6 +3232,53 @@ impl Db {
32323232
))
32333233
}
32343234

3235+
/// Returns whether the relay-authored NIP-43 snapshot is absent or differs
3236+
/// from the canonical membership rows for `community_id`.
3237+
///
3238+
/// Snapshot and canonical rows are compared directly rather than by
3239+
/// timestamp: relay membership events use whole-second Nostr timestamps,
3240+
/// and multiple mutations within one second must still be repaired.
3241+
pub async fn nip43_membership_snapshot_needs_reconciliation(
3242+
&self,
3243+
community_id: CommunityId,
3244+
relay_pubkey: &nostr::PublicKey,
3245+
) -> Result<bool> {
3246+
let snapshot = self
3247+
.query_events(&crate::event::EventQuery {
3248+
kinds: Some(vec![buzz_core::kind::KIND_NIP43_MEMBERSHIP_LIST as i32]),
3249+
pubkey: Some(relay_pubkey.to_bytes().to_vec()),
3250+
global_only: true,
3251+
limit: Some(1),
3252+
..crate::event::EventQuery::for_community(community_id)
3253+
})
3254+
.await?
3255+
.into_iter()
3256+
.next();
3257+
let members = self.list_relay_members(community_id).await?;
3258+
3259+
let Some(snapshot) = snapshot else {
3260+
return Ok(true);
3261+
};
3262+
let mut snapshot_members = snapshot
3263+
.event
3264+
.tags
3265+
.iter()
3266+
.filter_map(|tag| {
3267+
let parts = tag.as_slice();
3268+
(parts.first().map(String::as_str) == Some("member") && parts.len() >= 3)
3269+
.then(|| (parts[1].to_ascii_lowercase(), parts[2].clone()))
3270+
})
3271+
.collect::<Vec<_>>();
3272+
let mut canonical_members = members
3273+
.into_iter()
3274+
.map(|member| (member.pubkey.to_ascii_lowercase(), member.role))
3275+
.collect::<Vec<_>>();
3276+
snapshot_members.sort_unstable();
3277+
canonical_members.sort_unstable();
3278+
3279+
Ok(snapshot_members != canonical_members)
3280+
}
3281+
32353282
/// Atomically publish a NIP-43 membership snapshot under a single
32363283
/// transaction-scoped advisory lock.
32373284
///

‎crates/buzz-relay/src/connection.rs‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ async fn handle_text_message(text: String, conn: Arc<ConnectionState>, state: Ar
541541
let permit = match state.handler_semaphore.clone().try_acquire_owned() {
542542
Ok(p) => p,
543543
Err(_) => {
544-
conn.send(RelayMessage::notice(
544+
conn.send(request_rejection_message(
545+
Some(&sub_id),
545546
"rate-limited: too many concurrent requests",
546547
));
547548
return;
@@ -583,6 +584,13 @@ async fn handle_text_message(text: String, conn: Arc<ConnectionState>, state: Ar
583584
}
584585
}
585586

587+
fn request_rejection_message(sub_id: Option<&str>, reason: &str) -> String {
588+
match sub_id {
589+
Some(sub_id) => RelayMessage::closed(sub_id, reason),
590+
None => RelayMessage::notice(reason),
591+
}
592+
}
593+
586594
async fn enforce_ws_admission(
587595
msg: &ClientMessage,
588596
conn: &ConnectionState,
@@ -613,7 +621,11 @@ async fn enforce_ws_admission(
613621
ws_limit,
614622
)
615623
.await;
616-
if !send_admission_result(conn, ws_result) {
624+
let sub_id = match msg {
625+
ClientMessage::Req { sub_id, .. } => Some(sub_id.as_str()),
626+
_ => None,
627+
};
628+
if !send_admission_result(conn, ws_result, sub_id) {
617629
return false;
618630
}
619631

@@ -632,7 +644,7 @@ async fn enforce_ws_admission(
632644
message_limit,
633645
)
634646
.await;
635-
if !send_admission_result(conn, message_result) {
647+
if !send_admission_result(conn, message_result, None) {
636648
return false;
637649
}
638650
}
@@ -643,19 +655,22 @@ async fn enforce_ws_admission(
643655
fn send_admission_result(
644656
conn: &ConnectionState,
645657
result: Result<(), crate::admission::AdmissionError>,
658+
sub_id: Option<&str>,
646659
) -> bool {
647660
match result {
648661
Ok(()) => true,
649662
Err(crate::admission::AdmissionError::Exceeded { reset_in_secs }) => {
650663
metrics::counter!("buzz_admission_rejections_total", "transport" => "websocket", "reason" => "quota").increment(1);
651-
conn.send(RelayMessage::notice(&format!(
652-
"rate-limited: quota exceeded; retry in {reset_in_secs}s"
653-
)));
664+
conn.send(request_rejection_message(
665+
sub_id,
666+
&format!("rate-limited: quota exceeded; retry in {reset_in_secs}s"),
667+
));
654668
false
655669
}
656670
Err(crate::admission::AdmissionError::Unavailable) => {
657671
metrics::counter!("buzz_admission_rejections_total", "transport" => "websocket", "reason" => "unavailable").increment(1);
658-
conn.send(RelayMessage::notice(
672+
conn.send(request_rejection_message(
673+
sub_id,
659674
"rate-limited: shared admission unavailable",
660675
));
661676
false
@@ -757,6 +772,19 @@ mod tests {
757772
.collect()
758773
}
759774

775+
#[test]
776+
fn req_rejections_are_subscription_scoped() {
777+
let reason = "rate-limited: too many concurrent requests";
778+
let closed: serde_json::Value =
779+
serde_json::from_str(&request_rejection_message(Some("history-123"), reason))
780+
.expect("parse CLOSED");
781+
assert_eq!(closed, serde_json::json!(["CLOSED", "history-123", reason]));
782+
783+
let notice: serde_json::Value =
784+
serde_json::from_str(&request_rejection_message(None, reason)).expect("parse NOTICE");
785+
assert_eq!(notice, serde_json::json!(["NOTICE", reason]));
786+
}
787+
760788
#[tokio::test]
761789
async fn send_loop_batches_queued_data_frames_into_one_flush() {
762790
let (data_tx, data_rx) = mpsc::channel(MAX_WS_SEND_BATCH);

‎crates/buzz-relay/src/handlers/req.rs‎

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,21 @@ pub async fn handle_req(
8686
}
8787
};
8888

89-
let mut accessible_channels = match state
90-
.get_accessible_channel_ids_cached(conn.tenant.community(), &pubkey_bytes)
91-
.await
92-
{
93-
Ok(ids) => ids,
94-
Err(e) => {
95-
warn!(conn_id = %conn_id, "Failed to get accessible channels: {e}");
96-
conn.send(RelayMessage::closed(&sub_id, "error: database error"));
97-
return;
89+
let mut accessible_channels = if filters_are_nip43_membership_only(&filters) {
90+
metrics::counter!("buzz_req_global_access_resolution_skips_total", "kind" => "13534")
91+
.increment(1);
92+
Vec::new()
93+
} else {
94+
match state
95+
.get_accessible_channel_ids_cached(conn.tenant.community(), &pubkey_bytes)
96+
.await
97+
{
98+
Ok(ids) => ids,
99+
Err(e) => {
100+
warn!(conn_id = %conn_id, "Failed to get accessible channels: {e}");
101+
conn.send(RelayMessage::closed(&sub_id, "error: database error"));
102+
return;
103+
}
98104
}
99105
};
100106
if let Some(allowed) = token_channel_ids.as_deref() {
@@ -816,6 +822,21 @@ pub fn filter_fully_pushable(filter: &Filter) -> bool {
816822
true
817823
}
818824

825+
/// Return whether every filter exclusively targets the globally stored NIP-43
826+
/// membership snapshot. Such requests cannot return channel-scoped rows, so
827+
/// resolving the caller's complete accessible-channel set is wasted I/O.
828+
fn filters_are_nip43_membership_only(filters: &[Filter]) -> bool {
829+
!filters.is_empty()
830+
&& filters.iter().all(|filter| {
831+
filter.kinds.as_ref().is_some_and(|kinds| {
832+
!kinds.is_empty()
833+
&& kinds.iter().all(|kind| {
834+
kind.as_u16() as u32 == buzz_core::kind::KIND_NIP43_MEMBERSHIP_LIST
835+
})
836+
})
837+
})
838+
}
839+
819840
/// Extract a channel UUID from a single filter's `#h` tag.
820841
fn extract_channel_id_from_filter(filter: &Filter) -> Option<uuid::Uuid> {
821842
for (tag_key, tag_values) in filter.generic_tags.iter() {
@@ -1323,6 +1344,20 @@ mod tests {
13231344
));
13241345
}
13251346

1347+
#[test]
1348+
fn nip43_only_filters_skip_channel_access_resolution() {
1349+
let membership = Filter::new().kind(nostr::Kind::Custom(13_534));
1350+
assert!(filters_are_nip43_membership_only(&[
1351+
membership.clone(),
1352+
membership,
1353+
]));
1354+
assert!(!filters_are_nip43_membership_only(&[]));
1355+
assert!(!filters_are_nip43_membership_only(&[Filter::new()]));
1356+
assert!(!filters_are_nip43_membership_only(&[
1357+
Filter::new().kinds([nostr::Kind::Custom(13_534), nostr::Kind::TextNote]),
1358+
]));
1359+
}
1360+
13261361
#[test]
13271362
fn test_extract_channel_id_single_channel() {
13281363
let channel_id = uuid::Uuid::new_v4();

‎crates/buzz-relay/src/handlers/side_effects.rs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,53 @@ async fn emit_initial_ref_state(
27502750
Ok(())
27512751
}
27522752

2753+
/// Reconcile every community's event-backed NIP-43 membership view.
2754+
///
2755+
/// `relay_members` is canonical. A snapshot is rebuilt only when it is absent
2756+
/// or its member/role set differs from the canonical rows. This makes the sweep
2757+
/// safe to run at startup and periodically without producing an event stream
2758+
/// when nothing changed. A failure in one community is logged and counted but
2759+
/// does not prevent the remaining communities from being repaired.
2760+
pub async fn reconcile_nip43_membership_snapshots(state: &Arc<AppState>) -> anyhow::Result<usize> {
2761+
let communities = state.db.usage_community_hosts().await?;
2762+
let mut reconciled = 0usize;
2763+
2764+
for community in communities {
2765+
let community_id = buzz_core::CommunityId::from_uuid(community.id);
2766+
let host = community.host;
2767+
let result = async {
2768+
if !state
2769+
.db
2770+
.nip43_membership_snapshot_needs_reconciliation(
2771+
community_id,
2772+
&state.relay_keypair.public_key(),
2773+
)
2774+
.await?
2775+
{
2776+
return Ok::<bool, anyhow::Error>(false);
2777+
}
2778+
2779+
let tenant = TenantContext::resolved(community_id, host.clone());
2780+
publish_nip43_membership_list(&tenant, state).await?;
2781+
Ok::<bool, anyhow::Error>(true)
2782+
}
2783+
.await;
2784+
2785+
match result {
2786+
Ok(true) => reconciled += 1,
2787+
Ok(false) => {}
2788+
Err(error) => {
2789+
metrics::counter!("buzz_nip43_membership_reconciliation_failures_total")
2790+
.increment(1);
2791+
warn!(%community_id, %host, %error, "NIP-43 membership reconciliation failed");
2792+
}
2793+
}
2794+
}
2795+
2796+
metrics::counter!("buzz_nip43_membership_reconciliations_total").increment(reconciled as u64);
2797+
Ok(reconciled)
2798+
}
2799+
27532800
/// Publish a kind:13534 relay membership list event (NIP-43).
27542801
///
27552802
/// Queries all current relay members and emits a relay-signed, NIP-70-protected
@@ -2763,6 +2810,24 @@ async fn emit_initial_ref_state(
27632810
pub async fn publish_nip43_membership_list(
27642811
tenant: &TenantContext,
27652812
state: &Arc<AppState>,
2813+
) -> anyhow::Result<()> {
2814+
let started_at = std::time::Instant::now();
2815+
metrics::counter!("buzz_nip43_membership_publications_total", "result" => "attempted")
2816+
.increment(1);
2817+
let result = publish_nip43_membership_list_inner(tenant, state).await;
2818+
metrics::histogram!("buzz_nip43_membership_publication_seconds")
2819+
.record(started_at.elapsed().as_secs_f64());
2820+
metrics::counter!(
2821+
"buzz_nip43_membership_publications_total",
2822+
"result" => if result.is_ok() { "succeeded" } else { "failed" }
2823+
)
2824+
.increment(1);
2825+
result
2826+
}
2827+
2828+
async fn publish_nip43_membership_list_inner(
2829+
tenant: &TenantContext,
2830+
state: &Arc<AppState>,
27662831
) -> anyhow::Result<()> {
27672832
let relay_pubkey_hex = state.relay_keypair.public_key().to_hex();
27682833
let community = tenant.community();

0 commit comments

Comments
 (0)