Skip to content

Commit d22d587

Browse files
wesbillmanPrincess Donut
authored andcommitted
Enforce managed-agent revocation for direct members
Verify signed NIP-OA owner evidence independently from the closed-relay delegation feature flag. Preserve and materialize that evidence for directly enrolled agents so AUTH binds subsequent REQ and EVENT revalidation to the durable PMA tombstone. Co-authored-by: Princess Donut <68157ebd23b3897c1991015c3038658ea916200c67d3a54620b0754d1b92f6e0@buzz.block.builderlab.xyz> Signed-off-by: Wes <[email protected]>
1 parent 2fcf4b9 commit d22d587

3 files changed

Lines changed: 66 additions & 34 deletions

File tree

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,20 @@ pub mod relay_members {
4747
pub enum MembershipDecision {
4848
/// Relay membership enforcement is disabled.
4949
OpenRelay,
50-
/// Caller is directly present in `relay_members`.
51-
Member,
50+
/// Caller is directly present in `relay_members`. A verified owner is
51+
/// retained for revocation rechecks and durable owner materialization;
52+
/// it did not grant this membership.
53+
Member(Option<nostr::PublicKey>),
5254
/// Caller is admitted through a NIP-OA owner that is a relay member.
5355
ViaOwner(nostr::PublicKey),
5456
/// Caller is not admitted.
5557
Denied,
5658
}
5759

5860
fn verified_nip_oa_owner(
59-
allow_nip_oa_auth: bool,
6061
pubkey_bytes: &[u8],
6162
auth_tag_header: Option<&str>,
6263
) -> Option<nostr::PublicKey> {
63-
if !allow_nip_oa_auth {
64-
return None;
65-
}
6664
let tag_json = auth_tag_header?;
6765
let agent_pubkey = nostr::PublicKey::from_slice(pubkey_bytes).ok()?;
6866
match buzz_sdk::nip_oa::verify_auth_tag(tag_json, &agent_pubkey) {
@@ -91,11 +89,10 @@ pub mod relay_members {
9189
auth_tag_header: Option<&str>,
9290
) -> Result<MembershipDecision, String> {
9391
let pubkey_hex = hex::encode(pubkey_bytes);
94-
let verified_owner = verified_nip_oa_owner(
95-
!state.config.require_relay_membership || state.config.allow_nip_oa_auth,
96-
pubkey_bytes,
97-
auth_tag_header,
98-
);
92+
// Verify owner evidence independently of whether NIP-OA may grant
93+
// closed-relay membership. A direct member can still be a managed
94+
// agent, and its proven owner is required to enforce a PMA tombstone.
95+
let verified_owner = verified_nip_oa_owner(pubkey_bytes, auth_tag_header);
9996
let revoked = state
10097
.db
10198
.managed_agent_participation_is_revoked(
@@ -121,7 +118,7 @@ pub mod relay_members {
121118
.await
122119
.map_err(|e| format!("relay membership check failed: {e}"))?;
123120
if is_member {
124-
return Ok(MembershipDecision::Member);
121+
return Ok(MembershipDecision::Member(verified_owner));
125122
}
126123

127124
if state.config.allow_nip_oa_auth {
@@ -148,23 +145,24 @@ pub mod relay_members {
148145

149146
/// Enforce relay membership for a pubkey, with NIP-OA agent delegation fallback.
150147
///
151-
/// Returns `Ok(Some(owner_pubkey))` when the agent is not a direct member but
152-
/// its NIP-OA owner *is* — access is granted via delegation.
148+
/// Returns `Ok(Some(owner_pubkey))` whenever the caller supplies verified
149+
/// NIP-OA owner evidence, including when the caller is itself a direct
150+
/// member. The feature flag controls only whether that evidence can grant
151+
/// admission to a non-member on a closed relay.
153152
///
154-
/// On open relays (`require_relay_membership = false`), returns a verified
155-
/// NIP-OA owner unconditionally for durable backfill. The feature flag only
156-
/// controls whether delegation can grant access on a closed relay.
153+
/// On open relays (`require_relay_membership = false`), owner evidence is
154+
/// likewise returned unconditionally for durable backfill.
157155
///
158-
/// Returns `Ok(None)` when the caller is a direct member (closed relay) or when
159-
/// no NIP-OA tag is present/applicable (open relay without auth tag).
156+
/// Returns `Ok(None)` only when no valid NIP-OA tag is present.
160157
pub async fn enforce_relay_membership(
161158
state: &AppState,
162159
community: CommunityId,
163160
pubkey_bytes: &[u8],
164161
auth_tag_header: Option<&str>,
165162
) -> Result<Option<nostr::PublicKey>, (StatusCode, Json<serde_json::Value>)> {
166163
match check_relay_membership(state, community, pubkey_bytes, auth_tag_header).await {
167-
Ok(MembershipDecision::OpenRelay) | Ok(MembershipDecision::Member) => Ok(None),
164+
Ok(MembershipDecision::OpenRelay) => Ok(None),
165+
Ok(MembershipDecision::Member(owner)) => Ok(owner),
168166
Ok(MembershipDecision::ViaOwner(owner)) => Ok(Some(owner)),
169167
Ok(MembershipDecision::Denied) => Err((
170168
StatusCode::FORBIDDEN,
@@ -274,20 +272,16 @@ pub mod relay_members {
274272
use nostr::Keys;
275273

276274
#[test]
277-
fn closed_relay_disabled_nip_oa_does_not_supply_policy_owner_evidence() {
275+
fn owner_evidence_is_verified_independently_of_membership_grant_policy() {
278276
let owner = Keys::generate();
279277
let agent = Keys::generate();
280278
let tag = compute_auth_tag(&owner, &agent.public_key(), "").unwrap();
281279
assert_eq!(
282-
verified_nip_oa_owner(false, agent.public_key().as_bytes(), Some(&tag)),
283-
None
284-
);
285-
assert_eq!(
286-
verified_nip_oa_owner(true, agent.public_key().as_bytes(), Some(&tag)),
280+
verified_nip_oa_owner(agent.public_key().as_bytes(), Some(&tag)),
287281
Some(owner.public_key())
288282
);
289283
assert_eq!(
290-
verified_nip_oa_owner(true, agent.public_key().as_bytes(), Some("invalid")),
284+
verified_nip_oa_owner(agent.public_key().as_bytes(), Some("invalid")),
291285
None
292286
);
293287
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,20 @@ fn request_rejection_message(sub_id: Option<&str>, reason: &str) -> String {
591591
}
592592
}
593593

594+
fn requires_participation_revalidation(msg: &ClientMessage) -> bool {
595+
matches!(
596+
msg,
597+
ClientMessage::Req { .. } | ClientMessage::Count { .. } | ClientMessage::Event(_)
598+
)
599+
}
600+
594601
async fn enforce_ws_admission(
595602
msg: &ClientMessage,
596603
conn: &ConnectionState,
597604
state: &AppState,
598605
) -> bool {
599606
let is_event = matches!(msg, ClientMessage::Event(_));
600-
if !is_event && !matches!(msg, ClientMessage::Req { .. } | ClientMessage::Count { .. }) {
607+
if !requires_participation_revalidation(msg) {
601608
return true;
602609
}
603610

@@ -808,6 +815,35 @@ mod tests {
808815
.collect()
809816
}
810817

818+
#[test]
819+
fn managed_agent_revocation_revalidates_req_and_all_event_storage_classes() {
820+
use nostr::{EventBuilder, Filter, Keys, Kind};
821+
822+
let signed = |kind| {
823+
EventBuilder::new(kind, "")
824+
.sign_with_keys(&Keys::generate())
825+
.expect("sign event")
826+
};
827+
let cases = [
828+
ClientMessage::Req {
829+
sub_id: "history".into(),
830+
filters: vec![Filter::new()],
831+
},
832+
ClientMessage::Event(signed(Kind::TextNote)),
833+
ClientMessage::Event(signed(Kind::Custom(20_001))),
834+
];
835+
836+
for case in &cases {
837+
assert!(
838+
requires_participation_revalidation(case),
839+
"REQ, persistent EVENT, and ephemeral EVENT must all revalidate durable revocation"
840+
);
841+
}
842+
assert!(!requires_participation_revalidation(&ClientMessage::Close(
843+
"history".into()
844+
)));
845+
}
846+
811847
#[test]
812848
fn req_rejections_are_subscription_scoped() {
813849
let reason = "rate-limited: too many concurrent requests";

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
//!
33
//! Relay membership enforcement uses the shared
44
//! [`crate::api::relay_members::enforce_relay_membership`] helper. NIP-OA
5-
//! owner evidence is returned by the shared gate on open relays unconditionally
6-
//! and on closed relays when delegation auth is enabled, then durably
7-
//! materialized for later transport-neutral revocation checks.
5+
//! owner evidence is returned by the shared gate whenever it verifies, while
6+
//! the feature flag controls only whether that evidence may grant closed-relay
7+
//! membership. The relationship is then durably materialized for later
8+
//! transport-neutral revocation checks.
89
//!
910
//! For WebSocket auth, the NIP-OA `auth` tag is extracted from the signed AUTH
1011
//! event itself (the tag is integrity-protected by the event signature).
@@ -237,9 +238,10 @@ pub async fn handle_auth(event: nostr::Event, conn: Arc<ConnectionState>, state:
237238
}
238239
};
239240

240-
// The shared gate returns a verified owner only when NIP-OA is
241-
// enabled, including on open relays. Preserve that feature gate for
242-
// ownership materialization.
241+
// A verified owner is returned for both direct members and
242+
// delegated admissions. The feature flag controls only whether the
243+
// latter can grant access; owner evidence remains revocation
244+
// authority in either case.
243245
// Stash NIP-OA owner on the auth context only after the shared
244246
// backfill confirms the first-write-wins relationship.
245247
if let Some(owner) = nip_oa_owner {

0 commit comments

Comments
 (0)