Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions NOSTR.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ PGPASSWORD=buzz_dev psql -h localhost -U buzz -d buzz -c \
| **User profiles (kind:0)** | ✅ | NIP-01 metadata; synced to users table (display_name, avatar, about, NIP-05). NIP-05 handles must canonicalize to this relay's domain — off-domain or invalid handles are silently cleared. If a NIP-05 handle collides with another user's (UNIQUE constraint), the handle is skipped but other profile fields (display_name, avatar, about) are still synced. |
| **Group creation (kind:9007)** | ✅ | NIP-29; include an `h` tag with the new channel id and a `name` tag, optional `visibility`, `channel_type`, label `t` and `unlisted` tags. The relay refuses a 9007 without `h`. It refuses a listed channel (open, not unlisted, stream or forum) whose name another listed channel already has, ignoring case and including archived ones: `invalid: an open channel named "<name>" already exists`. |
| **Add user (kind:9000)** | ✅ | Open: any user, subject to target's `channel_add_policy` (`owner_only`/`nobody` can block). Private: owner/admin only. Self-add bypasses agent policy but not private-channel auth. A guest cannot add anyone. A new member with no role tag gets the channel's `join_role`; a non-member adding themselves may ask for `join_role` or a lower role. One event may name up to 100 people: one `p` tag each, with an optional role as the `p` tag's third element (NIP-29), falling back to the event's `role` tag. Each person is checked and added on their own. When the event names several people, the OK message is `members: ` followed by JSON listing the non-empty `added`, `updated` (role changed), `already_member` and `rejected` (`[{"reason", "pubkeys"}]`) results. The event is refused only when it is malformed or nobody it names may be added; then one person keeps the single-add error, and several get `invalid: members: {…}`. The event and its membership changes are stored together, so an `error:` rejection stores nothing and the same event can be resent; a resent stored event returns `duplicate:` without results. Re-adding someone at their current role posts no second `member_joined` notice. |
| **Remove user (kind:9001)** | ✅ | Self-remove allowed (with last-owner guard). Removing others: owner/admin only. |
| **Remove user (kind:9001)** | ✅ | Self-remove allowed (with last-owner guard). Removing others: owner/admin, or an active member removing their own agent. Only a channel owner may remove another channel owner. |
| **Edit group metadata (kind:9002)** | ✅ | `name`/`about`/label `t`/`visibility`/`ttl`/`archived`/`join_role`/`unlisted` tags: owner/admin. `topic`/`purpose` tags: any member except a guest. `auto_join` tag: relay owner/admin (see [Default channels](#default-channels)). A relay operator may also send a 9002 that changes only `name` on a listed channel, archived ones included, whatever its channel role, so that duplicate names can be repaired; the channel stays archived. The relay refuses a `name`, `visibility` or `unlisted` change that leaves a listed channel with a name another listed channel already has: `invalid: an open channel named "<name>" already exists`. The relay refuses a private-to-open `visibility` change while the channel has, or once had, a member who is neither the sender nor an agent the sender owns (NIP-OA); removed members count, because their posts stay in its history: `invalid: a private channel with other members cannot be made open; create a new open channel instead`. Buzz applies 9002 as a partial update: an absent tag leaves that setting unchanged. |
| **Admin delete event (kind:9005)** | ✅ | Event author can always delete own. Otherwise owner/admin required. Target must be in same channel. |
| **Group deletion (kind:9008)** | ✅ | Owner only. |
Expand Down Expand Up @@ -121,7 +121,7 @@ What each role can do:
| Read, leave | Yes | Yes | Yes |
| Post, reply, edit, delete own, react, artifacts, canvas, forum, pins, typing, workflow messages, huddles | No | Yes | Yes |
| Topic, purpose (kind:9002) | No | Yes | Yes |
| Add people (kind:9000) | No | Yes, as member, guest or bot | Yes, any role |
| Add people (kind:9000) | No | Yes, as member, guest or bot | Yes; only owners can grant owner |
| Push to the channel's git repositories | No | Yes | Yes |
| Name, about, visibility, TTL, `join_role`, `unlisted` | No | No | Yes |

Expand All @@ -135,7 +135,11 @@ Other rules:
lower role. A higher role is refused.
- Any member who can write (member, bot, owner or admin) can make a guest a
member with kind:9000 (`role` `member`). This is the same for a person and
an agent. Only owners and admins can make any other role change.
an agent. Owners and admins can make other role changes, except that only
owners may grant ownership or change an owner's role. Admins cannot promote
themselves or anyone else to owner. Only owners may remove other owners;
owning an agent does not override its channel-owner role. The last-owner
guard still applies to owner demotion and departure.
- The human owner of an agent can edit (kind:40003) and delete (kind:9005)
that agent's messages, also as a guest or a non-member. This is existing
behavior. It lets a person correct or remove what their agent posted; it
Expand Down
89 changes: 81 additions & 8 deletions crates/buzz-core/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ pub enum MemberPutDenial {
NotAMember,
/// A non-elevated actor asked for owner or admin.
ElevatedRoleGrant,
/// Only an owner may grant ownership or change an owner's role.
OwnershipChange,
/// A guest, or a non-member where joining does not give `member`, tried
/// to add someone else. Adding people is a write.
GuestCannotAdd,
Expand All @@ -302,7 +304,8 @@ pub enum MemberPutDenial {
/// This is the one definition of the admission rules: a guest never adds
/// anyone; a non-member adds others only where joining gives `member`; a
/// joiner gets `join_role` or a lower role it asks for; only owners and admins
/// grant elevated roles; and an active role changes only as
/// grant admin, only owners grant ownership or change an owner's role;
/// and an active role changes only as
/// [`MemberRole::may_change_role`] allows. With no `role` tag, an active member
/// keeps their role and a new member gets `join_role`.
///
Expand Down Expand Up @@ -340,6 +343,15 @@ pub fn decide_member_put(put: MemberPut<'_>) -> Result<MemberRole, MemberPutDeni
}
}

if put.actor_role != Some(MemberRole::Owner)
&& put.requested.is_some_and(|requested| {
put.target_role != Some(requested.as_str())
&& (put.target_role == Some("owner") || requested == MemberRole::Owner)
})
{
return Err(MemberPutDenial::OwnershipChange);
}

let Some(current) = put.target_role else {
return Ok(put.requested.unwrap_or(put.join_role.member_role()));
};
Expand Down Expand Up @@ -482,8 +494,9 @@ impl MemberRole {
/// Whether an actor whose active role is `actor` may change another
/// active member's role from `current` to `requested`.
///
/// Owners and admins may make any change (the last-owner rule is checked
/// separately). Any other active member who may post may make a guest a
/// Owners may make any change (the last-owner rule is checked separately).
/// Admins may change roles other than owner, but cannot grant ownership.
/// Any other active member who may post may make a guest a
/// member: the same authority they have to add a new member. This holds
/// for agents too; a later restricted-agent level may limit it. Nobody
/// else may change a role; a guest cannot promote anyone, including
Expand All @@ -496,7 +509,8 @@ impl MemberRole {
let Some(actor) = actor else {
return false;
};
actor.is_elevated()
actor == Self::Owner
|| (actor == Self::Admin && current != Self::Owner && requested != Self::Owner)
|| (actor.can_post()
&& current == Self::Guest
&& matches!(requested, Self::Member | Self::Bot))
Expand Down Expand Up @@ -549,8 +563,9 @@ impl FromStr for MemberRole {
mod tests {
use super::{
canonical_channel_name, channel_name_error, channel_type_from_group_state_tags,
group_state_identity_tags, parse_channel_labels, ChannelJoinRole, ChannelLabelError,
ChannelType, MemberRole, LEGACY_CHANNEL_TYPES, MAX_CHANNEL_LABELS, RESERVED_CHANNEL_LABELS,
decide_member_put, group_state_identity_tags, parse_channel_labels, ChannelJoinRole,
ChannelLabelError, ChannelType, MemberPut, MemberPutDenial, MemberRole,
LEGACY_CHANNEL_TYPES, MAX_CHANNEL_LABELS, RESERVED_CHANNEL_LABELS,
};

#[test]
Expand Down Expand Up @@ -773,13 +788,17 @@ mod tests {
}

#[test]
fn only_elevated_roles_change_roles_except_promoting_a_guest() {
fn role_changes_respect_owner_precedence_and_guest_promotion() {
use MemberRole::*;
const ALL: [MemberRole; 5] = [Owner, Admin, Member, Guest, Bot];
for actor in [Owner, Admin] {
for current in ALL {
for requested in ALL {
assert!(MemberRole::may_change_role(Some(actor), current, requested));
assert_eq!(
MemberRole::may_change_role(Some(actor), current, requested),
actor == Owner || (current != Owner && requested != Owner),
"{actor}: {current} -> {requested}"
);
}
}
}
Expand All @@ -804,6 +823,60 @@ mod tests {
}
}

#[test]
fn admins_cannot_acquire_or_change_ownership_through_member_put() {
use MemberRole::*;
// New members, existing members (including self), and unknown stored
// roles must all use the same ownership boundary. Unchanged roles are
// still idempotent, and omitting the role still preserves it.
for target_role in [None, Some("member"), Some("admin"), Some("future_role")] {
for target_is_actor in [false, true] {
let put = MemberPut {
private: true,
join_role: ChannelJoinRole::Member,
actor_role: Some(Admin),
target_is_actor,
target_role,
requested: Some(Owner),
};
assert_eq!(
decide_member_put(put),
Err(MemberPutDenial::OwnershipChange)
);
assert_eq!(
decide_member_put(MemberPut {
actor_role: Some(Owner),
..put
}),
Ok(Owner)
);
}
}
for requested in [
Some(Admin),
Some(Member),
Some(Bot),
Some(Guest),
Some(Owner),
None,
] {
let result = decide_member_put(MemberPut {
private: true,
join_role: ChannelJoinRole::Member,
actor_role: Some(Admin),
target_is_actor: false,
target_role: Some("owner"),
requested,
});
let expected = if requested.is_none() || requested == Some(Owner) {
Ok(Owner)
} else {
Err(MemberPutDenial::OwnershipChange)
};
assert_eq!(result, expected, "requested {requested:?}");
}
}

#[test]
fn channel_names_trim_whitespace_and_drop_all_leading_hashes() {
assert_eq!(canonical_channel_name("channel"), "channel");
Expand Down
Loading
Loading