Files
buzz/crates/buzz-core/src
Taylor Ho 833c41863a fix(relay): enforce channel owner precedence (#9592)
**Category:** fix
**User Impact:** Channel admins can manage the conversation while owners
retain control of ownership and owner membership.

**Problem:** Channel admins can currently grant themselves ownership,
change another owner's role, or remove an owner when another owner
remains. A racing promotion can stop a removal after admission while its
command is still stored and reported successful.

**Solution:** Reserve ownership grants, owner role changes, and owner
removal for channel owners, enforced during validation and under the
database membership lock. Persist each removal and its signed command
together so a locked refusal rejects the receipt without storing or
broadcasting the refused command; this applies equally to people and
agents, with no new role, migration or protocol field.

<details>
<summary>File changes</summary>

**crates/buzz-core/src/channel.rs**
Constrain the shared role policy and member admission decision. Keep
same-role additions idempotent and preserve ordinary guest-promotion
rules; extend the existing role matrix and cover ownership grants
through admission.

**crates/buzz-db/src/store/channel_members.rs**
Reject non-owner removal of an owner under the existing membership lock,
including removal of an agent the actor owns. Reuse those locked checks
when storing a removal command and its membership change atomically.
Retain database coverage for escalation denials, ordinary owner/admin
powers and concurrency; move the target-promotion race to the ingest
journey below.

**crates/buzz-relay/src/handlers/channel_authz.rs**
Make removal authorization aware of the target's role and expose an
ownership-specific role-change refusal. Extend the existing
authorization tables.

**crates/buzz-relay/src/handlers/side_effects.rs**
Pass the removal target into admission authorization. Persist removals
atomically, then run cache invalidation, eviction and best-effort
notices only after commit; duplicates run no second removal or notices.

**crates/buzz-relay/src/handlers/ingest.rs**
Route kind 9001 through atomic persistence and return locked membership
refusals as client rejection before dispatch.

**crates/buzz-relay/src/handlers/remove_user_postgres_tests.rs**
Exercise the real ingest race with a membership-lock barrier: target
promotion causes rejection, leaves the Owner intact and stores/publishes
no command. Then verify legitimate removal and duplicate replay after
rejoin. This replaces the DB-only target-promotion test rather than
duplicating it.

**crates/buzz-relay/src/handlers/mod.rs**
Register the new PostgreSQL ingest journey for automatic discovery.

**NOSTR.md**
Document owner precedence, including the agent-owner exception's
boundary.

</details>

### Try it

1. Run the updated relay and create a disposable channel with two
owners, one admin, and one ordinary member. Two owners ensure the
last-owner guard cannot mask the new permission check.
2. As the admin, attempt to promote yourself or another member to owner.
Both requests should be refused.
3. As the admin, attempt to demote or remove either owner. Both requests
should be refused and their memberships should remain unchanged.
4. Confirm the admin can still rename the channel and manage ordinary
members/admins.
5. As an owner, confirm ownership delegation and co-owner
demotion/removal still work. The sole remaining owner must still be
unable to demote or remove themselves.
6. Run the PostgreSQL lane's membership regressions and
`removal_receipt_tracks_the_locked_membership_write`. The controlled
race must return rejection with the target still Owner and no
stored/published removal; the same command must succeed once authorized,
and its later replay must preserve a rejoined member.

### Validation and remaining checks

- At `e7a8fe405`, all **2,048 infrastructure-free tests** across the
complete `buzz-db`/`buzz-relay` package inventory pass with nextest
process isolation, plus all **five package doctests**. The monolithic
Cargo run stalled in shared-process configuration tests; the same
package inventory passes with the repository's preferred isolated
runner, without exclusions or assertion changes.
- The new ingest journey fails on the original implementation with
`accepted:true` after the locked refusal, and passes with atomic
removal. Its positive controls verify actual publication for a
legitimate removal and no repeated removal/publication after rejoin. The
existing demoted-actor lock test retains direct-call concurrency
coverage.
- At the same head, all **1,425 PostgreSQL cases** across both packages
pass through the per-test isolation wrapper (PostgreSQL 17.12, UTC;
hosted CI uses 16). A rebuilt isolated native relay passes **24 signed
WebSocket operations** plus persisted-state and subscriber checks,
including the real promotion/removal race, later legitimate retry and
replay after rejoin. Temporary services are stopped; Git/media behavior
was not exercised. All mandatory push hooks pass, including the broad
Rust suite (709.20s) and native/desktop checks (302.32s).
- Test churn is **420 lines** (+399/-21); non-test churn is **196
lines** (186 source, 10 maintained docs), versus 279/69 at the preceding
head. The new 207-line ingest journey and two-line registration replace
the 68-line DB-only target-promotion race; no other authorization or
concurrency coverage is removed.
- The implementation tree passed 298 core library tests, 1,979
database/relay library tests, and 76 binary/integration-target tests:
2,353 total. Infrastructure-dependent ignored tests were excluded. These
runs preceded publication; the publication commit has the identical
source tree, with only its commit subject amended.
- Disabling the ownership-grant guard made the admission regression fail
with `Ok(Owner)`; restoring the guard returned all 298 core tests to
green.
- At follow-up head `6d4622d3e`, all 769 PostgreSQL tests in the
`buzz-db` package profile passed using the repository's per-test
database isolation wrapper (PostgreSQL 17.12, UTC; 245 cases outside the
selected profile skipped). This includes both new regressions and the
existing owner-management journey retained after removing two repeated
positive checks.
- An isolated native relay passed 18 signed WebSocket operations plus
persisted-state checks: admin escalation, co-owner demotion/removal,
retained admin management, owner delegation/demotion/removal, and
last-owner protection. This membership workflow used local PostgreSQL
and Redis; Git/media behavior was not exercised.
- PostgreSQL test discovery, formatting, and hosted CI passed at
`6d4622d3e`. Maintainer approval and human acceptance remain pending.

This relay permission change does not require a client update.

Related history: #3017 established bidirectional elevated-role checks
and last-owner protection; #8143 introduced the current shared role
predicate. This PR adds the Owner-over-Admin distinction to those rules.

---------

Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
Co-authored-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@feedback.communities.buzz.xyz>
2026-11-16 17:13:28 -08:00
..
…
…
…
…