Skip to content

Commit 0042c8e

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
feat(relay): allow agent owners to edit/manage agent-owned content (#1403)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 06ef533 commit 0042c8e

15 files changed

Lines changed: 1472 additions & 56 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,8 @@ pub(crate) fn effective_message_author(event: &Event, relay_pubkey: &nostr::Publ
676676
event.pubkey.to_bytes().to_vec()
677677
}
678678

679-
/// Validate kind:40003 edit ownership — event.pubkey must match target's effective author.
679+
/// Validate kind:40003 edit ownership — event.pubkey must match target's effective author,
680+
/// or the actor must be the owning human of the agent that authored the target message.
680681
async fn validate_edit_ownership(
681682
community_id: CommunityId,
682683
event: &Event,
@@ -723,8 +724,36 @@ async fn validate_edit_ownership(
723724

724725
let author = effective_message_author(&target_event.event, &state.relay_keypair.public_key());
725726
let actor = event.pubkey.to_bytes().to_vec();
726-
if author != actor {
727-
return Err("must be event author to edit".to_string());
727+
if author == actor {
728+
// Author editing their own message: re-gate on membership/open visibility so that
729+
// a removed private-channel member cannot mutate old messages after access is revoked.
730+
if let Some(ch_id) = target_event.channel_id {
731+
let is_member = state
732+
.is_member_cached(community_id, ch_id, &actor)
733+
.await
734+
.map_err(|e| format!("db error checking membership: {e}"))?;
735+
if !is_member {
736+
let is_open = state
737+
.db
738+
.get_channel(community_id, ch_id)
739+
.await
740+
.map(|ch| ch.visibility == "open")
741+
.unwrap_or(false);
742+
if !is_open {
743+
return Err("restricted: not a channel member".to_string());
744+
}
745+
}
746+
}
747+
} else {
748+
// Allow the owning human to edit messages authored by their agent.
749+
let is_owner = state
750+
.db
751+
.is_agent_owner(community_id, &author, &actor)
752+
.await
753+
.map_err(|e| format!("db error checking agent ownership: {e}"))?;
754+
if !is_owner {
755+
return Err("must be event author to edit".to_string());
756+
}
728757
}
729758
Ok(())
730759
}
@@ -1379,8 +1408,17 @@ async fn ingest_event_inner(
13791408
if let Some(ch_id) = channel_id {
13801409
// kind:9021 (join) doesn't require prior membership.
13811410
// kind:9007 (create) — channel doesn't exist yet; creator becomes owner in step 16.
1382-
let skip_membership =
1383-
kind_u32 == KIND_NIP29_JOIN_REQUEST || kind_u32 == KIND_NIP29_CREATE_GROUP;
1411+
// kind:40003/9002/9005/9008 — per-kind validators are the authority; they
1412+
// individually enforce authorization and fail closed. Bypassing the generic
1413+
// member/open gate here lets the owning human act on private agent channels
1414+
// without being a member (OQ1 decision; see validate_edit_ownership /
1415+
// validate_admin_event for per-kind enforcement).
1416+
let skip_membership = kind_u32 == KIND_NIP29_JOIN_REQUEST
1417+
|| kind_u32 == KIND_NIP29_CREATE_GROUP
1418+
|| kind_u32 == KIND_STREAM_MESSAGE_EDIT
1419+
|| kind_u32 == KIND_NIP29_EDIT_METADATA
1420+
|| kind_u32 == KIND_NIP29_DELETE_EVENT
1421+
|| kind_u32 == KIND_NIP29_DELETE_GROUP;
13841422
if !skip_membership {
13851423
// Spec AuthCheck (line 794): emit the verdict at the actual
13861424
// call site. claimed_community comes from the event's h tag

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

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,30 @@ pub async fn validate_standard_deletion_event(
216216
Ok(())
217217
}
218218

219+
/// Returns `true` if `actor_bytes` is the NIP-OA owner of **any** active owner-role
220+
/// member in `members`. Used by kind:9002 and kind:9008 to authorize the owning
221+
/// human of a channel's agent-owner(s) even when the human is not a channel member.
222+
///
223+
/// Checking all active owners (not just the first) is correct under co-ownership:
224+
/// an agent may be promoted to owner later, or multiple agents may co-own a channel.
225+
async fn actor_owns_any_owner_agent(
226+
state: &Arc<AppState>,
227+
community_id: buzz_core::CommunityId,
228+
members: &[buzz_db::channel::MemberRecord],
229+
actor_bytes: &[u8],
230+
) -> anyhow::Result<bool> {
231+
for owner in members.iter().filter(|m| m.role == "owner") {
232+
if state
233+
.db
234+
.is_agent_owner(community_id, &owner.pubkey, actor_bytes)
235+
.await?
236+
{
237+
return Ok(true);
238+
}
239+
}
240+
Ok(false)
241+
}
242+
219243
/// Validate an admin kind event BEFORE storage.
220244
pub async fn validate_admin_event(
221245
tenant: &TenantContext,
@@ -459,9 +483,24 @@ pub async fn validate_admin_event(
459483
let actor_member = members.iter().find(|m| m.pubkey == actor_bytes);
460484
match actor_member {
461485
Some(m) if m.role == "owner" || m.role == "admin" => Ok(()),
462-
_ => Err(anyhow::anyhow!(
463-
"actor not authorized for name/about/archived/visibility/ttl changes"
464-
)),
486+
_ => {
487+
// Allow the owning human of any active owner-role agent in the
488+
// channel, even when the human is not a channel member —
489+
// diverges from kind:9001 intentionally.
490+
if actor_owns_any_owner_agent(
491+
state,
492+
tenant.community(),
493+
&members,
494+
&actor_bytes,
495+
)
496+
.await?
497+
{
498+
return Ok(());
499+
}
500+
Err(anyhow::anyhow!(
501+
"actor not authorized for name/about/archived/visibility/ttl changes"
502+
))
503+
}
465504
}
466505
} else {
467506
// topic/purpose: any member
@@ -516,26 +555,66 @@ pub async fn validate_admin_event(
516555
let author =
517556
effective_message_author(&target_event.event, &state.relay_keypair.public_key());
518557
if author == actor_bytes {
519-
return Ok(()); // Author can always delete their own messages
558+
// Author deleting their own message: re-gate on membership/open visibility so that
559+
// a removed private-channel member cannot mutate old messages after access is revoked.
560+
let is_member = state
561+
.is_member_cached(tenant.community(), channel_id, &actor_bytes)
562+
.await?;
563+
if is_member {
564+
return Ok(());
565+
}
566+
let is_open = state
567+
.db
568+
.get_channel(tenant.community(), channel_id)
569+
.await
570+
.map(|ch| ch.visibility == "open")
571+
.unwrap_or(false);
572+
if is_open {
573+
return Ok(());
574+
}
575+
// Not a member and channel is private — fall through to owner/admin/owner-of-agent check.
520576
}
521577

522-
// Not the author — must be owner/admin.
578+
// Not the author, or author who is no longer a member of a private channel —
579+
// must be owner/admin or the owning human of the message's agent-author.
523580
let members = state.db.get_members(tenant.community(), channel_id).await?;
524581
let actor_member = members.iter().find(|m| m.pubkey == actor_bytes);
525582
match actor_member {
526583
Some(m) if m.role == "owner" || m.role == "admin" => Ok(()),
527-
_ => Err(anyhow::anyhow!(
528-
"must be event author or channel owner/admin"
529-
)),
584+
_ => {
585+
// Allow the owning human of the agent that authored the target message,
586+
// even when the human is not a channel member.
587+
if state
588+
.db
589+
.is_agent_owner(tenant.community(), &author, &actor_bytes)
590+
.await?
591+
{
592+
Ok(())
593+
} else {
594+
Err(anyhow::anyhow!(
595+
"must be event author or channel owner/admin"
596+
))
597+
}
598+
}
530599
}
531600
}
532601
9008 => {
533-
// DELETE_GROUP: owner only
602+
// DELETE_GROUP: owner only, or the owning human of the channel's agent-owner.
534603
let members = state.db.get_members(tenant.community(), channel_id).await?;
535604
let actor_member = members.iter().find(|m| m.pubkey == actor_bytes);
536605
match actor_member {
537606
Some(m) if m.role == "owner" => Ok(()),
538-
_ => Err(anyhow::anyhow!("only owner can delete group")),
607+
_ => {
608+
// Allow the owning human of any active owner-role agent in the
609+
// channel, even when the human is not a channel member —
610+
// diverges from kind:9001 intentionally.
611+
if actor_owns_any_owner_agent(state, tenant.community(), &members, &actor_bytes)
612+
.await?
613+
{
614+
return Ok(());
615+
}
616+
Err(anyhow::anyhow!("only owner can delete group"))
617+
}
539618
}
540619
}
541620
9022 => {

‎crates/buzz-test-client/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ rand = { workspace = true }
3535
sha2 = { workspace = true }
3636
chrono = { workspace = true }
3737
s3 = { version = "0.37", package = "rust-s3", default-features = false, features = ["tokio-rustls-tls", "fail-on-err", "tags"] }
38+
buzz-sdk = { workspace = true }
3839

3940
[[bin]]
4041
name = "buzz-test-cli"

‎crates/buzz-test-client/src/lib.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ impl BuzzTestClient {
106106
Ok(())
107107
}
108108

109+
/// Performs NIP-42 authentication with a NIP-OA `auth` tag, establishing
110+
/// the agent→owner relationship in the relay's DB. Call this as the
111+
/// *agent* connection; the tag must be signed by `owner_keys`.
112+
pub async fn authenticate_with_nip_oa(
113+
&mut self,
114+
agent_keys: &Keys,
115+
auth_tag: &Tag,
116+
) -> Result<(), TestClientError> {
117+
self.inner.authenticate(agent_keys, Some(auth_tag)).await?;
118+
Ok(())
119+
}
120+
109121
/// Sends a signed event to the relay and waits for the OK response.
110122
pub async fn send_event(&mut self, event: Event) -> Result<OkResponse, TestClientError> {
111123
Ok(self.inner.send_event(event).await?)

0 commit comments

Comments
 (0)