test(relay): align timeout admin regression

Signed-off-by: seiler <seiler@squareup.com>
Co-authored-by: Codex <noreply@openai.com>
(cherry picked from commit 31968772ebe1b30c2862966e46b75a9ad4b967b6)
Signed-off-by: Brad Seiler <seiler@squareup.com>
This commit is contained in:
seiler
2026-10-06 15:46:21 +00:00
committed by GitHub
parent d1add801cc
commit 7cb9312c8f
5 changed files with 54 additions and 54 deletions

View File

@@ -2776,10 +2776,10 @@ async fn ingest_event_inner(
// Community moderation commands (9040–9044) are direct, community-global
// mutations. They are never stored or fanned out as ordinary events; the
// handler writes the durable audit/enforcement rows after its own capability
// authorization. These commands are intentionally routed before the
// timeout/write-block gate below so a timed-out admin can lift a timeout.
// The handler independently checks the durable ban state before executing
// any command, which also covers NIP-98 and missed live disconnects.
// authorization. The write-restriction gate above permits only unban and
// untimeout commands during an active timeout. The handler independently
// checks durable ban state before executing any command, which also covers
// NIP-98 and missed live disconnects.
if buzz_core::kind::is_moderation_command_kind(kind_u32) {
super::moderation_commands::handle_moderation_command(tenant, state, &event).await?;
return Ok(IngestResult {
@@ -2981,9 +2981,8 @@ async fn ingest_event_inner(
}
// Handled directly — these mutate relay_members and do NOT get stored.
// The handler enforces the durable community ban itself: the write-path
// gate above exempts relay-admin kinds so timed-out admins keep their
// administrative capability, which leaves bans to the handler.
// The write-restriction gate above rejects banned and timed-out actors
// before this handler; the handler also checks durable community-ban state.
if is_relay_admin_kind(event.kind.as_u16() as u32) {
crate::handlers::relay_admin::handle_relay_admin_event(tenant, state, &event)
.await

View File

@@ -149,12 +149,12 @@ pub(super) enum RelayAdminError {
Internal(String),
}
/// Decide whether a durable restriction state admits a relay-admin command.
/// Apply the relay-admin handler's local ban check.
///
/// Ban only, deliberately: a timeout is a write-block on *content*, and
/// `ingest_event` exempts relay-admin kinds from its durable write-path gate
/// precisely so restricted-but-not-banned admins retain their administrative
/// capability. Mirrors `moderation_commands::ensure_actor_not_banned`.
/// `ingest_event` runs the shared write-restriction gate before relay-admin
/// dispatch, where active timeouts are rejected. This check independently
/// preserves the handler's durable-ban invariant; passing it does not mean a
/// timed-out command passed the earlier gate.
///
/// Split out as a pure function so the admission rule itself is unit-testable
/// without a live relay: the end-to-end HTTP test proves the transport, this
@@ -182,17 +182,14 @@ pub(super) async fn handle_relay_admin_event(
state: &Arc<AppState>,
event: &Event,
) -> Result<(), RelayAdminError> {
// A ban is an admission boundary, not only a WebSocket-auth check. HTTP
// NIP-98 requests and already-authenticated sockets reach this handler
// without passing through a fresh NIP-42 challenge, and `ingest_event`
// exempts relay-admin kinds from its durable write-path gate so a *timed
// out* admin can still administer the roster. That exemption is ban-blind,
// so the ban must be enforced here or not at all: a banned admin otherwise
// keeps mutating `relay_members` — the very table `moderation_authz`
// derives moderator capability from — until someone manually deletes the
// row. Mirrors `moderation_commands.rs`, which defends the same boundary
// for 9040–9044, and holds that file's stated invariant that a direct
// command handler rejects a banned actor on every transport.
// HTTP NIP-98 requests and already-authenticated sockets reach this handler
// without a fresh NIP-42 challenge. The shared write-restriction gate in
// `ingest_event` rejects bans and active timeouts before kind dispatch; this
// handler repeats the durable-ban check as defense in depth before any
// roster mutation. A banned admin must not change `relay_members` — the
// table `moderation_authz` derives moderator capability from. This mirrors
// `moderation_commands.rs`, which applies the same handler-level ban check
// for kinds 9040–9044.
//
// This gate wraps execution rather than opening it so no future early
// return inside the command body can precede it.
@@ -524,11 +521,10 @@ mod postgres_tests {
use super::*;
use nostr::{EventBuilder, Keys, Kind, Tag};
/// The vulnerability this file's ban gate closes: `ingest_event` exempts
/// relay-admin kinds 9030–9033 from its durable write-path restriction
/// gate, so a **banned** admin could add/remove relay members and change
/// the workspace icon over signed NIP-98 `POST /events`. Deleting the
/// admission check must fail here, in the default (non-ignored) suite.
/// The handler-level ban check protects relay-admin commands that reach it
/// after authenticated admission. The shared `ingest_event` restriction
/// gate rejects active timeouts before dispatch; this default (non-ignored)
/// unit test pins the handler's independent durable-ban check.
#[test]
fn banned_actor_is_not_admitted_to_a_relay_admin_command() {
let banned = buzz_db::moderation::RestrictionState {
@@ -545,11 +541,10 @@ mod postgres_tests {
);
}
/// The counter-invariant, and the reason the ingest exemption exists at
/// all: a timeout restricts *content* writes, not administrative
/// capability. Widening this gate to timeouts would silently change policy.
/// A timeout is not a ban at this handler-local check. The shared ingest
/// gate rejects timed-out relay-admin requests before this check runs.
#[test]
fn timed_out_actor_is_still_admitted() {
fn timed_out_actor_passes_local_ban_check() {
let timed_out = buzz_db::moderation::RestrictionState {
banned: false,
own_banned: false,
@@ -557,7 +552,7 @@ mod postgres_tests {
};
assert!(
admits_relay_admin_command(&timed_out).is_ok(),
"a timed-out admin must still administer the roster"
"the local ban check must not classify a timeout as a ban"
);
}

View File

@@ -16,5 +16,5 @@ media_fail_closed crates/buzz-relay/src/api/media.rs failed_restriction_lookup_i
git_fail_closed crates/buzz-relay/src/api/git/transport.rs ban_gate_fails_closed_with_503_when_the_store_is_unreachable postgres-ci git_restriction_lookup_fail_closed
ingest_owner_ban crates/buzz-relay/src/handlers/ingest.rs owner_ban_blocks_every_agent_write_until_unban postgres-ci agent_owner_ban_write_gate
moderation_owner_ban crates/buzz-relay/src/api/bridge.rs moderation_read_honors_fresh_and_stored_banned_agent_owners postgres-ci moderation_owner_ban_auth_time_and_stored_link
timeout_control crates/buzz-test-client/tests/regression_relay_admin_ban_gate.rs banned_admin_is_refused_but_timed_out_admin_still_administers relay-e2e timeout_is_not_ban
timeout_admin_denial crates/buzz-test-client/tests/regression_relay_admin_ban_gate.rs banned_and_timed_out_admins_are_refused_but_unrestricted_admin_can_administer relay-e2e timed_out_admin_relay_admin_denial
http_tenant_isolation crates/buzz-test-client/tests/community_ban_routes.rs ban_is_scoped_to_host_resolved_community relay-e2e http_ban_tenant_isolation
1 id file test lane property
16 git_fail_closed crates/buzz-relay/src/api/git/transport.rs ban_gate_fails_closed_with_503_when_the_store_is_unreachable postgres-ci git_restriction_lookup_fail_closed
17 ingest_owner_ban crates/buzz-relay/src/handlers/ingest.rs owner_ban_blocks_every_agent_write_until_unban postgres-ci agent_owner_ban_write_gate
18 moderation_owner_ban crates/buzz-relay/src/api/bridge.rs moderation_read_honors_fresh_and_stored_banned_agent_owners postgres-ci moderation_owner_ban_auth_time_and_stored_link
19 timeout_control timeout_admin_denial crates/buzz-test-client/tests/regression_relay_admin_ban_gate.rs banned_admin_is_refused_but_timed_out_admin_still_administers banned_and_timed_out_admins_are_refused_but_unrestricted_admin_can_administer relay-e2e timeout_is_not_ban timed_out_admin_relay_admin_denial
20 http_tenant_isolation crates/buzz-test-client/tests/community_ban_routes.rs ban_is_scoped_to_host_resolved_community relay-e2e http_ban_tenant_isolation

View File

@@ -1,17 +1,14 @@
//! Regression test for the NIP-43 relay-admin durable-ban bypass
//! (BUZZ-SEC-007 class, reported 2026-07-27).
//!
//! `ingest_event` exempts relay-admin kinds 9030-9033 from its durable
//! write-path restriction gate so a *timed out* admin keeps its administrative
//! capability. That exemption was ban-blind, so a **banned** admin could still
//! add/remove relay members and change the workspace icon via signed NIP-98
//! `POST /events`. The ban is now enforced inside
//! `relay_admin::handle_relay_admin_event`; this test pins both halves of that
//! contract — bans refused, timeouts still admitted.
//! `ingest_event` runs the durable write-restriction gate before relay-admin
//! kinds 9030-9033. Bans and active timeouts must reject those commands, while
//! an unrestricted admin can still mutate the roster and workspace icon via
//! signed NIP-98 `POST /events`.
//!
//! Requires a running relay and its Postgres. Ignored by default:
//! Requires a running relay and its Postgres. Export `DATABASE_URL` for a
//! disposable local database, then run (ignored by default):
//! REPRO_RELAY_HTTP=http://localhost:3999 REPRO_HOST=localhost:3999 \
//! DATABASE_URL=postgres://buzz:buzz_dev@localhost:5432/buzz_relay_admin_regression \
//! cargo test -p buzz-test-client --test regression_relay_admin_ban_gate \
//! -- --ignored --nocapture
@@ -123,14 +120,14 @@ async fn seed(p: &sqlx::Pool<sqlx::Postgres>, cid: Uuid, keys: &Keys, role: &str
/// Asserts the full contract rather than just "the exploit stopped":
/// - banned admin: 403 + exact `blocked:` prefix on 9030/9031/9033, and a
/// banned *owner* likewise on 9032 (owner-only kind), covering all four
/// exempt kinds,
/// relay-admin kinds,
/// - no roster, role, or icon mutation from any of those attempts,
/// - a *timed-out* admin still reaches relay-admin authorization (the ingest
/// exemption's whole purpose — the fix must not silently widen to timeouts),
/// - a *timed-out* admin gets 403 + the `restricted:` timeout prefix on 9031,
/// and the target member remains in the roster,
/// - an unrestricted admin's behaviour is unchanged, mutation included.
#[tokio::test]
#[ignore]
async fn banned_admin_is_refused_but_timed_out_admin_still_administers() {
async fn banned_and_timed_out_admins_are_refused_but_unrestricted_admin_can_administer() {
let p = pool().await;
let cid = community_id(&p).await;
@@ -290,7 +287,7 @@ async fn banned_admin_is_refused_but_timed_out_admin_still_administers() {
"9033: banned admin must not change the workspace icon, got {icon:?}"
);
// ── Timed-out admin: still administers (ingest exemption preserved). ──
// ── Timed-out admin: relay-admin and content writes are refused. ──
let (ts, tb) = post_event(
&timed_out_admin,
&signed(
@@ -302,13 +299,22 @@ async fn banned_admin_is_refused_but_timed_out_admin_still_administers() {
.await;
println!("[timed-out] 9031 remove -> {ts} {tb}");
assert_eq!(
ts, 200,
"timed-out admin must still administer the roster: {tb}"
ts, 403,
"timed-out admin must be refused relay-admin commands: {tb}"
);
let timeout_message: serde_json::Value = serde_json::from_str(&tb).unwrap_or_default();
let timeout_text = timeout_message
.get("error")
.and_then(|value| value.as_str())
.unwrap_or(&tb);
assert!(
timeout_text.starts_with("restricted: you are timed out"),
"timed-out admin denial must use the timeout restriction prefix: {tb}"
);
assert_eq!(
role_of(&victim2).await,
None,
"timed-out admin's removal must take effect"
role_of(&victim2).await.as_deref(),
Some("member"),
"timed-out admin must not remove a member"
);
// Control: the same timed-out admin is still write-blocked for content.

View File

@@ -75,7 +75,7 @@ REQUIRED_REGRESSIONS = {
"git_fail_closed",
"ingest_owner_ban",
"moderation_owner_ban",
"timeout_control",
"timeout_admin_denial",
"http_tenant_isolation",
}
RAW_STRING_PREFIX = re.compile(r"(?:b)?r(#+)?\"")