Skip to content

Commit 16839a0

Browse files
wpfleger96DuncanHayt
authored
feat(nip-fi): admin disconnect/deny API with in-memory deny-until-TTL map (S4) (#7977)
This adds the NIP-FI S4 admin command surface. An authorized issuer principal can deny an `(issuer, pubkey)` pair until a TTL expires. When that happens, the relay records the entry in a bounded in-memory deny map, closes matching live sockets on every pod, and refuses new admissions while the entry is live. Supersedes #7265 ## Breaking In `BUZZ_NIP_FI_MODE=enforce`, every entry in `BUZZ_NIP_FI_ISSUERS` now has to carry `maximum_command_age_seconds` (1–60) and a non-empty `authorized_principals` list, and startup fails if any issuer is missing either one. That means an S3-only enforce config that boots on `main` won't boot with this change. No deployment sets `BUZZ_NIP_FI_*` yet. The per-issuer field table and a working example are in the `nip_fi_config.rs` module docs. ## What changes - **`buzz-auth`**: - `nip_fi/command.rs` verifies signed admin commands. It checks issuer policy, command age, the authorized principal (exact, case-sensitive `sub` match), and a `jti` of at most 512 bytes. - `nip_fi/deny_map.rs` is a per-issuer, capacity-bounded deny-until-TTL map on top of `DashMap`. Expired entries and `jti` reservations are only swept when a write would otherwise hit capacity. The sweep is O(n) and holds the lock every admission check takes, so this keeps it off the admission path. Reads compare against `now`, so an expired entry that hasn't been swept yet never counts as live. - **Cross-pod command replay fence**: `nip_fi/command_replay.rs` adds a `CommandReplayGuard` trait, and `buzz-pubsub` implements it in Redis (`RedisCommandReplayGuard`), following the NIP-98 replay guard. Once a command is authenticated, the verifier takes an atomic `SET buzz:nip-fi:command:{sha256(len(iss)‖iss‖len(jti)‖jti)} 1 NX EX <ttl>` claim, with the TTL set to the command's remaining validity rounded up to whole seconds plus skew, clamped like NIP-98, so the claim outlives acceptance by a verifier whose clock trails by the full skew. Before the claim, the verifier reserves a pending `jti` and deny-entry slot in this pod's deny map under the issuer lock, so a full deny set returns `503 deny set full` without touching Redis and the same signed command can be retried. The claim is taken only after the signature and claims verify, so a forged token cannot burn a real `jti`. A `jti` already claimed on any pod returns `403`. A Redis error fails closed with `503 authorization unavailable`: no deny entry, no session close, no publish. A replayed claim, a Redis error, or a cancelled request drops the reservation. After a successful claim the reservation commits to the `jti` record and deny entry, which cannot fail on capacity. A claim can still remain with no deny entry if Redis applied `SET` but the reply was lost, or the pod stops between claim and commit; that fails closed, and a retry gets `403` until the TTL expires. - **`api/nip_fi.rs`**: the admin disconnect/deny endpoint. It verifies the command, inserts the deny entry, disconnects locally, and publishes over `buzz-pubsub` `conn_control` so every other pod runs the same disconnect. A receiving pod clamps `until` to its own ceiling instead of rejecting it. Otherwise a pod whose clock trails the origin would drop a command the origin already answered with 200, and the target's sockets on that pod would stay open. Malformed pubkeys, unknown issuers, and unrepresentable timestamps are still rejected. - **Admission and live enforcement**: - WS upgrade (`router.rs`) and HTTP (`nip_fi_http.rs`) refuse keys that have a live entry with `403 authorization_denied`. - Root NIP-42 auth and audio auth register the proven identity (pubkey + admitting issuer) first, and only then check the deny map. With that order, a concurrent disconnect either finds the socket in its close scan or the check finds the entry. `OK(true)` isn't sent if a concurrent disconnect already cancelled the session. - `ConnectionManager::disconnect_nip_fi` and `CommunityConnectionRegistry::disconnect_nip_fi` are scoped to the issuer. - **One wire form for `authorization_denied`** (FI-TRACE-DENIAL-ORACLE): every root and audio WebSocket `authorization_denied` denial now goes through one first-writer-wins transition on `CommunityConnectionControl`. That covers the S4 deny-set hit and admin disconnect, and also the existing S3 key-mismatch, lease-expiry, and membership refusals. The transition sets the `AuthorizationDenied` reason only if nothing else has set a reason, and only the winner enqueues the denial frame. So every one of these denials is the class's denial frame followed by `Close(1008, "authorization denied")`, and a concurrent registry scan can't queue a second one. Before this, S3 key mismatch and expiry closed with `Close(None)`, which made them distinguishable from a deny-set hit on the wire. `evidence_rejected` and `authorization_unavailable` keep the bare close. - **Config**: see Breaking. `deny_set_capacity` is optional and defaults to 50000. Setting any command field without a command age fails closed. - **Boot** (`main.rs`): installs the command verifier and deny map. It subscribes to cross-pod disconnects before the Redis subscriber starts, so disconnects relayed during boot are buffered instead of dropped. --------- Signed-off-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz> Co-authored-by: Hayt <211b96e6a2b7f45fd4047988976c7bbbeeda0c15f3ae7b32eec20834b5a55118@buzz.block.builderlab.xyz>
1 parent 04b040e commit 16839a0

33 files changed

Lines changed: 10348 additions & 211 deletions

‎.github/workflows/_ci-relay.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,14 @@ jobs:
511511
--archive-file target/ci/backend-integration-tests.tar.zst \
512512
-E 'package(buzz-relay) and (test(/^rejection::/) or test(/^admission::/))' \
513513
--run-ignored all
514+
- name: NIP-FI cross-pod command replay regressions
515+
env:
516+
BUZZ_TEST_REDIS_URL: redis://localhost:6379
517+
run: |
518+
cargo nextest run \
519+
--archive-file target/ci/backend-integration-tests.tar.zst \
520+
-E 'package(buzz-relay) and test(/^api::nip_fi::route_integration_tests::external_infra_redis::/)' \
521+
--run-ignored ignored-only
514522
- name: Workflow message provenance unit tests
515523
# The relay's workflow_sink suite is not selected by the infra-free
516524
# unit job. Its ignored database cases run in the isolated PostgreSQL

‎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.

‎Justfile‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ test-unit:
366366
if command -v cargo-nextest &>/dev/null; then
367367
cargo nextest run -p buzz-core -p buzz-auth --lib
368368
cargo nextest run -p buzz-audit --lib
369+
# S4 cross-pod NIP-FI disconnect payload tests (infra-free).
370+
cargo nextest run -p buzz-pubsub --lib -E 'test(/^conn_control::tests::nip_fi_disconnect_/)'
369371
# buzz-auth NIP-FI verifier doctests. The sealed-authority
370372
# `compile_fail` doctests prove the default-feature public API alone
371373
# cannot forge the issuer→JWKS authority; nextest does not run
@@ -480,7 +482,8 @@ test-unit:
480482
# abnormal-stream-close fanout, and never-ready-sink writer witnesses —
481483
# are all selected by audio::join::tests and audio::handler::tests.
482484
# DB-backed audio join tests use #[ignore] and run in the postgres lane.
483-
# NIP-FI (S3) relay witnesses: the wholly-new nip_fi_upgrade module,
485+
# NIP-FI (S3/S4) relay witnesses: the wholly-new nip_fi_upgrade and
486+
# api::nip_fi modules,
484487
# the auth metrics contract module, plus the exact NIP-FI tests added,
485488
# or whose assertions changed, in mixed modules (audio::room,
486489
# connection, handlers::*, state). nip_fi_config and router are
@@ -511,13 +514,26 @@ test-unit:
511514
+ test(=connection::tests::f3_root_pre_built_expired_gate_terminates_connection)
512515
+ test(=handlers::auth::tests::b2_pre_cancelled_connection_never_becomes_authenticated)
513516
+ test(=handlers::auth::tests::fi_ban_check_error_emits_terminal_authorization_unavailable)
517+
+ test(=handlers::auth::tests::fi_root_authorization_denied_rows_emit_identical_frames)
514518
+ test(=handlers::auth::tests::fi_invalid_nip42_proof_emits_terminal_evidence_rejected)
515519
+ test(=handlers::auth::tests::handle_auth_pairing_mismatch_runs_full_root_denial_path)
516520
+ test(=handlers::auth::tests::nip42_denial_class_separates_internal_failure_from_bad_evidence)
517521
+ test(=handlers::event::tests::p1b_agent_observer_event_barrier_expiry_blocks_fanout_and_ack)
518522
+ test(=handlers::req::tests::p1a_huddle_liveness_req_barrier_expiry_blocks_query_and_emission)
519523
+ test(=state::tests::f3_cancellation_during_check_terminates_socket_without_waiting_for_check)
520-
+ test(=state::tests::on_not_run_runs_once_on_each_deny_arm_and_never_on_admit)'
524+
+ test(=state::tests::on_not_run_runs_once_on_each_deny_arm_and_never_on_admit)
525+
+ test(=state::tests::conn_manager_disconnect_nip_fi_ignores_unproven_connection)
526+
+ test(=state::tests::conn_manager_disconnect_nip_fi_is_issuer_scoped)
527+
+ test(=state::tests::conn_manager_disconnect_nip_fi_sets_authorization_denied_reason)
528+
+ test(=state::tests::nip_fi_disconnect_audio_is_issuer_scoped)
529+
+ test(=state::tests::nip_fi_disconnect_closes_proven_audio_socket_and_sends_policy_close_reason)
530+
+ test(=state::tests::nip_fi_disconnect_closes_target_audio_only_and_preserves_collocated_peer)
531+
+ test(=state::tests::nip_fi_disconnect_does_not_close_different_pubkey_audio_socket)
532+
+ test(=state::tests::nip_fi_disconnect_does_not_close_unproven_audio_socket)
533+
+ test(=state::tests::community_disconnect_then_nip_fi_keeps_community_deleted_reason)
534+
+ test(=state::tests::disconnect_community_wins_reason_losing_nip_fi_does_not_enqueue_frame)
535+
+ test(=state::tests::manager_disconnect_sets_reason_enqueues_frame_then_cancels)
536+
+ test(/^api::nip_fi::/)'
521537
# ACP author-gate and queue tests protect the trust boundary between
522538
# relay events and agent prompts. They are infra-free; ignored lifecycle
523539
# tests remain excluded and run in their dedicated integration lanes.

‎crates/buzz-auth/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tokio = { workspace = true, features = ["test-util"] }
2020
buzz-core = { workspace = true }
2121
base64 = { workspace = true }
2222
chrono = { workspace = true }
23+
dashmap = { workspace = true }
2324
jsonwebtoken = { workspace = true }
2425
nostr = { workspace = true }
2526
futures-util = { workspace = true }

‎crates/buzz-auth/src/lib.rs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ pub use rate_limit::{
4646
pub use scope::{parse_scopes, Scope};
4747

4848
pub use nip_fi::{
49-
validate_nip_fi_config, AssertionKeySet, AssertionPolicyId, CanonicalCapabilities,
50-
ClientSubjectPosture, ConfidentialAssertion, DenialClass, FederatedAssertionVerifier,
51-
FederatedIdentity, FederatedIdentityDiscovery, FreshnessClass, HttpJwksFetcher,
52-
IssuerJwksConfig, IssuerKeySource, IssuerPolicy, IssuerPolicyError, IssuerRegistry,
53-
JwksFetchError, JwksFetcher, JwksSourceContract, NipFiMode, NipFiStartupError,
54-
ProductionJwksSource, RevalidationDependencies, SubjectClass, SubjectClassContract, TokenClass,
55-
TransportContractId, VerifiedAssertion, VerifierError, VerifyAssertion, CLIENT_ATTACHED_HEADER,
56-
NOSTR_PUBKEY_CLAIM, OAUTH_CLIENT_ID_CLAIM,
49+
command_replay_key, validate_nip_fi_config, AssertionKeySet, AssertionPolicyId,
50+
CanonicalCapabilities, ClientSubjectPosture, CommandError, CommandIssuerPolicy,
51+
CommandPolicyError, CommandReplayGuard, CommandResult, CommandVerifier, ConfidentialAssertion,
52+
CrossPodMergeResult, DenialClass, DenySetFull, FederatedAssertionVerifier, FederatedIdentity,
53+
FederatedIdentityDiscovery, FreshnessClass, HttpJwksFetcher, IssuerCapacity, IssuerJwksConfig,
54+
IssuerKeySource, IssuerPolicy, IssuerPolicyError, IssuerRegistry, JwksFetchError, JwksFetcher,
55+
JwksSourceContract, NipFiDenyMap, NipFiMode, NipFiStartupError, ProductionJwksSource,
56+
RevalidationDependencies, SubjectClass, SubjectClassContract, TokenClass, TransportContractId,
57+
VerifiedAssertion, VerifierError, VerifyAssertion, CLIENT_ATTACHED_HEADER, COMMAND_JWT_TYP,
58+
MAX_COMMAND_AGE_SECONDS, NOSTR_PUBKEY_CLAIM, OAUTH_CLIENT_ID_CLAIM,
5759
};
5860

5961
#[cfg(any(test, feature = "test-utils"))]
@@ -63,6 +65,8 @@ pub use nip98_replay::AlwaysFreshReplayGuard;
6365
#[cfg(any(test, feature = "test-utils"))]
6466
pub use nip_fi::jwks::ScriptedJwksFetcher;
6567
#[cfg(any(test, feature = "test-utils"))]
68+
pub use nip_fi::InMemoryCommandReplayGuard;
69+
#[cfg(any(test, feature = "test-utils"))]
6670
pub use nip_fi::StaticIssuerKeySource;
6771
#[cfg(any(test, feature = "test-utils"))]
6872
pub use nip_fi::ToggleJwksFetcher;

0 commit comments

Comments
 (0)