mirror of
https://github.com/block/buzz.git
synced 2026-11-27 19:52:31 +00:00
fix(relay): persist NIP-FI revocations across restarts and bridge shadow to enforce (#9860)
## Summary Stack: this PR → #9917 NIP-FI revocations (deny entries from `POST /api/nip-fi/disconnect`) now survive relay restarts and lost pub/sub subscriptions, and an enforce relay can optionally carry over revocations accepted during the `shadow` period. ## Changes - **Durable deny store (Redis).** Per mode and issuer hash, a sorted set (score = `until` in ms, rounded up) plus a hash holding the exact signed `until`, merged by maximum. One Lua script runs all checks before any write: replay → expiry/capacity → write. Outcomes: claimed, replay (403), full (503, nothing written). Every write and prune also clears the target's field in the issuer's revoke-through cutoff hash, so a full revoke is never narrowed by a cutoff a newer relay version stored. A Redis error is an unknown outcome (503); the issuer sends a fresh command. - **Shared capacity.** `deny_set_capacity` is enforced in Redis, across pods. - **Fail-closed restore.** A pod starts closed to protected admission, refusing as authorization unavailable (HTTP 503 and the matching Nostr denial on root and audio AUTH; `shadow` records it as unavailable). Active deny entries still refuse with 403. Each key's verdict (allowed, denied, unavailable) comes from one read of the deny map, so a restore that starts mid-check is never reported as a revocation. A single consumer task restores by generation: wait for a confirmed subscription, load the stores, apply buffered messages, then reopen. A reconnect, a lag, or a received deny that does not fit the local capacity starts a new generation, so admission reopens only once the active union fits. A restore merges every stored entry first, then closes sessions only for targets it added or extended (also when it fails part-way; a capacity miss or a store error for one issuer or source doesn't end the pass, so every reachable target is still merged or closed and the first failure keeps admission closed), so restoring after a Redis blip doesn't walk every connection once per entry. While a restore keeps failing, the consumer still applies disconnects that arrive, so revoked sessions close while admission stays closed; the retry runs on a fixed one-second deadline that traffic can't push back. A stored deny record that doesn't parse fails the restore rather than being skipped; the issuer's other records are still applied and their sessions closed. Restore failures are counted in `buzz_nip_fi_deny_restore_failures_total{reason}`; a stored record that doesn't decode is counted as `reason="unreadable"`, apart from `store` (Redis unavailable). - **Bridge setting.** `BUZZ_NIP_FI_ACCEPT_SHADOW_DISCONNECTS` (default false, enforce only) makes an enforce pod also subscribe to the shadow channel and restore the shadow store. - **NIP-FI spec.** Restart behaviour now allows shared storage that is restored before protected connections or HTTP requests are admitted. Disconnects document the unknown-outcome reply: when a relay can't tell whether its shared-storage write applied, it returns `503` `authorization unavailable` and closes no sessions, and the caller retries with a freshly signed command (new `jti`). Added to the step 7 pseudocode, the disconnect response table and the `authorization_unavailable` rejection row. - **Runbook.** New "Revocation during a deploy" section in `docs/operator-nip-fi-bindings.md`: coverage (restarts, not lost Redis data), non-evicting Redis, disconnect responses, capacity, boot behaviour when Redis is unreachable, recovering from an unreadable deny record (`HDEL`), and the shadow → enforce rollout steps. ## Tests - Restart restores denies from the shared store; unavailable store keeps admission closed; consumer reopens only for a live, restored subscription. - Store error returns 503 and the command retries; lost reply returns 503 and needs a fresh command. - Capacity enforced across pods; over-capacity restore stays closed; a capacity miss during the restore handoff or after a successful restore keeps or closes admission. - A restoring deny map refuses as unavailable (503 / bare close), not revoked, at each admission point: WebSocket upgrade, protected HTTP, root AUTH and audio, and when the restore starts during the check. - Restore closes only added or extended targets, still closes them when it fails part-way (a capacity miss, a failing source before a healthy one, and the first-visited issuer failing before a healthy one, and an unreadable record reported as `unreadable`, not `store`), and live delivery of an unchanged entry still closes sessions. A broadcast lag after a restore restores again. During repeated restore failures, delivered disconnects still close their sessions, admission stays closed, and steady traffic doesn't delay the next attempt (run on a paused Tokio clock). - The buzz-pubsub deny-store key and rounding unit tests now run in `just test-unit` and `run-tests.sh`. - Subscribe channels and restore sources follow mode and bridge; config rejects the bridge outside enforce. - Redis-backed (ignored, run in CI's NIP-FI Redis step; each case uses its own store namespace): script checks before writing and keeps exact `until`; expired entries free their slot and a shorter command never lowers a score (`ZADD GT`); bridge restores shadow-period denies on enforce pods; a record with an unreadable key or `until` fails the load while every readable active record after it still loads, and expired entries are still filtered; restore applies a good record past a bad one for the same issuer, closing its sessions and leaving others open, fails as `unreadable` with admission unavailable, and reopens on retry once the record is removed (fixture hashes expire after 60s); an accept with an equal or shorter `until`, and a prune, clear the target's stored cutoff. --------- Signed-off-by: Will Pfleger <wpfleger@block.xyz> Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz> Co-authored-by: wpfleger <wpfleger@users.noreply.github.com>
This commit is contained in:
@@ -109,6 +109,11 @@ run_unit_tests() {
|
||||
cargo test -p buzz-pubsub --lib -- --exact --nocapture \
|
||||
tests::queue_full_retain_does_not_wait_and_schedules_reconciliation
|
||||
|
||||
# NIP-FI deny-store keys and `until` rounding (infra-free). Mirrors
|
||||
# `just test-unit`.
|
||||
run_test_step "buzz-pubsub NIP-FI deny store tests" \
|
||||
cargo test -p buzz-pubsub --lib nip_fi_command_replay::tests:: -- --nocapture
|
||||
|
||||
run_test_step "buzz-voice tests" \
|
||||
cargo test -p buzz-voice --lib -- --nocapture
|
||||
|
||||
|
||||
Reference in New Issue
Block a user