Skip to content

Commit 13c011c

Browse files
fsola-sqcodex
andcommitted
fix(relay): protect verified profiles from deletion
Co-authored-by: Codex <[email protected]> Signed-off-by: Franco Sola <[email protected]>
1 parent 3dcf1dc commit 13c011c

2 files changed

Lines changed: 104 additions & 1 deletion

File tree

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7146,6 +7146,102 @@ mod postgres_tests {
71467146
assert_eq!(stored.channel_id, Some(channels[0]));
71477147
}
71487148

7149+
#[tokio::test]
7150+
#[ignore = "requires Postgres"]
7151+
async fn subject_cannot_delete_relay_verified_profile() {
7152+
use buzz_core::kind::KIND_VERIFIED_PROFILE;
7153+
use buzz_db::verified_profiles::ProfileAdmission;
7154+
use nostr::Tag;
7155+
let f = ScopedTargetFixture::new("verified-profile-deletion").await;
7156+
let subject = f.author.public_key().to_hex();
7157+
let now = Utc::now();
7158+
let profile = f
7159+
.state
7160+
.db
7161+
.publish_verified_profile(
7162+
ProfileAdmission {
7163+
community: f.tenant.community(),
7164+
issuer: "https://issuer.test",
7165+
pubkey: &subject,
7166+
issued_at: now,
7167+
admitted_at: now,
7168+
display_name: "Alice",
7169+
username: "alice",
7170+
full_name: Some("Alice Example"),
7171+
label: "Example",
7172+
refresh_seconds: 3600,
7173+
membership_required: false,
7174+
},
7175+
&f.state.relay_keypair,
7176+
)
7177+
.await
7178+
.unwrap()
7179+
.expect("publish relay-owned profile");
7180+
let auth = || IngestAuth::Http {
7181+
pubkey: f.author.public_key(),
7182+
scopes: vec![Scope::MessagesWrite],
7183+
auth_method: HttpAuthMethod::Nip98,
7184+
};
7185+
let deletion = |tag: Tag| {
7186+
EventBuilder::new(Kind::Custom(KIND_DELETION as u16), "")
7187+
.tags([tag])
7188+
.sign_with_keys(&f.author)
7189+
.unwrap()
7190+
};
7191+
let coordinate = |author: String| {
7192+
Tag::parse([
7193+
"a".to_owned(),
7194+
format!("{KIND_VERIFIED_PROFILE}:{author}:{subject}"),
7195+
])
7196+
.unwrap()
7197+
};
7198+
for tag in [
7199+
Tag::parse(["e", &profile.id.to_hex()]).unwrap(),
7200+
coordinate(profile.pubkey.to_hex()),
7201+
// Even a subject-authored coordinate cannot request deletion of this kind.
7202+
coordinate(subject.clone()),
7203+
] {
7204+
let event = deletion(tag);
7205+
let id = event.id;
7206+
let result = f.submit(event, auth()).await;
7207+
assert!(
7208+
matches!(&result, Err(IngestError::Rejected(message))
7209+
if message == "invalid: deletion target not found or not deletable by you"),
7210+
"profile deletion must be denied, got {:?}",
7211+
result.err()
7212+
);
7213+
assert!(f
7214+
.state
7215+
.db
7216+
.get_event_by_id(f.tenant.community(), id.as_bytes())
7217+
.await
7218+
.unwrap()
7219+
.is_none());
7220+
let stored = f
7221+
.state
7222+
.db
7223+
.get_event_by_id(f.tenant.community(), profile.id.as_bytes())
7224+
.await
7225+
.unwrap()
7226+
.expect("profile remains readable");
7227+
assert_eq!(stored.event, profile);
7228+
}
7229+
7230+
f.submit(
7231+
deletion(Tag::parse(["e", &f.global.id.to_hex()]).unwrap()),
7232+
auth(),
7233+
)
7234+
.await
7235+
.expect("ordinary author-owned note remains deletable");
7236+
assert!(f
7237+
.state
7238+
.db
7239+
.get_event_by_id(f.tenant.community(), f.global.id.as_bytes())
7240+
.await
7241+
.unwrap()
7242+
.is_none());
7243+
}
7244+
71497245
/// A community with `author`'s live message in an open channel, an
71507246
/// author global note, and a second open channel for out-of-scope tokens.
71517247
struct ScopedTargetFixture {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use buzz_core::kind::{
1111
KIND_DM_VISIBILITY, KIND_GIT_REPO_ANNOUNCEMENT, KIND_IA_ARCHIVED, KIND_IA_ARCHIVED_LIST,
1212
KIND_IA_UNARCHIVED, KIND_MEMBER_ADDED_NOTIFICATION, KIND_MEMBER_REMOVED_NOTIFICATION,
1313
KIND_NIP29_GROUP_ADMINS, KIND_NIP29_GROUP_MEMBERS, KIND_NIP29_GROUP_METADATA,
14-
KIND_NIP43_MEMBERSHIP_LIST, KIND_REACTION, KIND_THREAD_SUMMARY,
14+
KIND_NIP43_MEMBERSHIP_LIST, KIND_REACTION, KIND_THREAD_SUMMARY, KIND_VERIFIED_PROFILE,
1515
};
1616
use buzz_core::StoredEvent;
1717
use buzz_db::channel::{MemberRecord, MemberRole};
@@ -398,6 +398,10 @@ pub(crate) async fn authorized_standard_deletion_target(
398398
else {
399399
return Ok(None);
400400
};
401+
// The profile's p tag names its subject, not an author with deletion rights.
402+
if event_kind_u32(&target.event) == KIND_VERIFIED_PROFILE {
403+
return Ok(None);
404+
}
401405
let author = effective_message_author(&target.event, &state.relay_keypair.public_key());
402406
let authorized = author == actor
403407
|| state
@@ -471,6 +475,9 @@ pub async fn validate_standard_deletion_event(
471475
return Err(anyhow::anyhow!("invalid a-tag format"));
472476
}
473477
if let Ok(target_kind) = parts[0].parse() {
478+
if target_kind == KIND_VERIFIED_PROFILE {
479+
anyhow::bail!(DELETION_TARGET_DENIED);
480+
}
474481
check_deletion_privacy_k_tag(event, target_kind)?;
475482
}
476483
let target_pubkey_bytes =

0 commit comments

Comments
 (0)