mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +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:
@@ -427,13 +427,18 @@ A disconnect call causes the relay to:
|
||||
MUST NOT be subsequently admitted. Connections
|
||||
registered under another issuer are not affected.
|
||||
|
||||
The deny set is held **in relay memory only** — no durable storage, no schema
|
||||
changes. A relay restart MAY forget active deny entries. If the issuer stops
|
||||
The relay holds the deny set in memory and MAY also keep it in shared storage
|
||||
that outlives a relay process, with each entry expiring at its `until`. A
|
||||
relay that keeps such storage MUST record an accepted command there before
|
||||
reporting success, and MUST NOT admit protected connections or protected HTTP requests after a restart
|
||||
until it has restored the active entries. A relay without such storage MAY
|
||||
forget active deny entries on restart. In that case, if the issuer stops
|
||||
issuing assertions and re-push completes before any expired-entry reconnection
|
||||
attempt, the residual exposure after a restart is bounded by
|
||||
`max(0, min(exp, iat + maximum_assertion_age) - now)`. If the issuer continues
|
||||
issuing or re-push does not complete in time, that formula does not apply and
|
||||
access may continue beyond it. [FI-TRACE-DENY-SET]
|
||||
access may continue beyond it. Neither mode covers loss of the shared storage
|
||||
itself. [FI-TRACE-DENY-SET]
|
||||
|
||||
The relay MUST bound the deny set size **per issuer**. Capacity exhaustion under
|
||||
one issuer MUST NOT cause rejection of another issuer's commands; the `503`
|
||||
@@ -565,12 +570,13 @@ VerifyCommandJwt(token, request_method, request_path, request_body_pubkey):
|
||||
target_pubkey=target_k, until=until
|
||||
) or DENY(authorization_denied) // replay: jti already reserved
|
||||
or REJECT(503) // capacity: deny set full; neither mutation applied
|
||||
or REJECT(authorization_unavailable) // shared storage outcome unknown: jti and deny entry may be recorded; no sessions closed
|
||||
|
||||
return CommandResult(target_pubkey=target_k, caller=(claims.iss, claims.sub))
|
||||
```
|
||||
|
||||
Any failure at any step is fail-closed: no side effects occur and the relay
|
||||
returns the appropriate error.
|
||||
Any failure at any step is fail-closed: no sessions are closed and the relay
|
||||
returns the appropriate error. Only an unknown shared-storage outcome, described below, can leave state behind.
|
||||
|
||||
### Request
|
||||
|
||||
@@ -588,7 +594,7 @@ performs all pure authorization checks and then, as its single atomic admission
|
||||
mutation (step 7), simultaneously reserves the `(iss, jti)` replay identity and
|
||||
inserts the deny entry — both or neither. A capacity failure at that step rejects
|
||||
`503`; neither the jti nor the deny entry is recorded, and the caller may safely
|
||||
retry the same signed command. On success, the relay MUST close every live connection
|
||||
retry the same signed command. A relay that keeps the deny set in shared storage and cannot learn whether its write applied MUST reject `503` `authorization_unavailable` and close no sessions. The `jti` and deny entry may already be recorded, so the caller retries with a freshly signed command (new `jti`), not the same one. On success, the relay MUST close every live connection
|
||||
registered under the command's issuer (`claims.iss`) whose proven key equals
|
||||
`CommandResult.target_pubkey` or whose recorded owner equals
|
||||
`CommandResult.target_pubkey`, whether or not admission has completed.
|
||||
@@ -612,6 +618,7 @@ under the merge rule.
|
||||
| Missing or invalid command JWT | `401` / `403` | Per the rejection table |
|
||||
| Malformed request body or `until` exceeds ceiling | `400` | `bad request\n` |
|
||||
| Deny set at capacity | `503` | `deny set full\n` |
|
||||
| Shared deny storage outcome unknown | `503` | `authorization unavailable\n` |
|
||||
|
||||
## HTTP ingress
|
||||
|
||||
@@ -863,7 +870,7 @@ is unavailable.
|
||||
| assertion or proof absent | `missing_evidence` | `auth-required: authentication required` | `401`; `WWW-Authenticate: Nostr`; `Content-Type: text/plain; charset=utf-8`; body `authentication required\n` |
|
||||
| unknown or community-unauthorized issuer; malformed, invalid, or expired evidence | `evidence_rejected` | `restricted: evidence rejected` | `403`; `Content-Type: text/plain; charset=utf-8`; body `evidence rejected\n` |
|
||||
| assertion–key mismatch; unauthorized issuer principal, signed-target–body mismatch, or replayed `jti` on a signed command; active deny-set entry for pubkey | `authorization_denied` | `restricted: authorization denied` | `403`; `Content-Type: text/plain; charset=utf-8`; body `authorization denied\n` |
|
||||
| required JWKS snapshot or community/Host resolution unavailable | `authorization_unavailable` | `restricted: authorization unavailable` | `503`; `Content-Type: text/plain; charset=utf-8`; body `authorization unavailable\n` |
|
||||
| required JWKS snapshot, community/Host resolution, or restored deny set unavailable | `authorization_unavailable` | `restricted: authorization unavailable` | `503`; `Content-Type: text/plain; charset=utf-8`; body `authorization unavailable\n` |
|
||||
|
||||
A denial decided on a WebSocket upgrade is the HTTP response in place of `101`.
|
||||
A denial decided on a protected HTTP request is the HTTP response.
|
||||
@@ -999,7 +1006,9 @@ longest still-live assertion it has issued to ensure no unexpired assertion
|
||||
slides through the expiry boundary; this is an operational recommendation,
|
||||
not a protocol invariant.
|
||||
|
||||
A relay restart clears the in-memory deny set. The issuer SHOULD re-push
|
||||
A relay that holds the deny set only in memory clears it on restart; a relay
|
||||
that keeps it in shared storage restores active entries before admitting
|
||||
protected connections. For memory-only relays, the issuer SHOULD re-push
|
||||
still-active entries on observed restart, but re-push is not required and
|
||||
carries no specified completion bound. If the issuer stops issuing
|
||||
assertions and re-push completes before any expired-entry reconnection
|
||||
|
||||
@@ -241,6 +241,97 @@ Ways out, in order:
|
||||
2. If that cannot land quickly, set `BUZZ_NIP_FI_MODE=off`. `off` loads no
|
||||
snapshot, so pods boot and admit as they did before NIP-FI.
|
||||
|
||||
## Revocation during a deploy
|
||||
|
||||
An accepted `POST /api/nip-fi/disconnect` is written to Redis before the relay
|
||||
answers 200, and each entry expires at its `until`. A pod that starts, or that
|
||||
loses its Redis subscription, restores every active entry before it admits a
|
||||
NIP-FI assertion again. Until then it refuses every assertion-bearing
|
||||
connection, so a key revoked just before a deploy can't come back in on a
|
||||
fresh pod.
|
||||
|
||||
What this covers, and what it does not:
|
||||
|
||||
- **Covered:** pod restarts, and a pod that loses its Redis subscription or
|
||||
falls behind on it.
|
||||
- **Not covered:** a pod that stays healthy still learns of a disconnect only
|
||||
through live pub/sub, which is best effort. If a publication never reaches
|
||||
it, that pod neither closes the target's open sessions nor refuses the
|
||||
target at its own admission check until its next restore.
|
||||
- **Not covered:** losing Redis data. Run Redis with a non-evicting
|
||||
`maxmemory-policy` of `noeviction`, with persistence if you need
|
||||
revocations to survive a Redis restart. Every deny key carries an expiry,
|
||||
so any `volatile-*` policy can evict it.
|
||||
- **Redis down at startup:** if Redis is completely unreachable at boot, the
|
||||
relay exits before it binds, and the process supervisor restarts it. If
|
||||
Redis is reachable but the restore fails, the pod runs and refuses
|
||||
assertion-bearing connections with `503` (authorization unavailable) until
|
||||
it has restored. Watch `buzz_nip_fi_deny_restore_failures_total{reason}`.
|
||||
- **Unreadable deny record:** `reason="unreadable"` means Redis answered but
|
||||
a stored deny record could not be decoded: its field is not a hex pubkey,
|
||||
or its value is not exactly `secs:nanos`. The pod still applies the
|
||||
issuer's other records and closes their sessions, but stays closed until the
|
||||
record is removed. The warning log names the record. Find it in the
|
||||
issuer's deny hash, `buzz:nip-fi:deny-until:<sha256(issuer) hex>`
|
||||
(`buzz:nip-fi:shadow-deny-until:…` in shadow mode), with `HGETALL`, then
|
||||
`HDEL` that field. The pod retries on its own. Removing the record drops
|
||||
that revocation, so send the disconnect again if it is still needed.
|
||||
- **Deny set over capacity on a pod:** if a revocation this pod receives does
|
||||
not fit its `deny_set_capacity`, the pod closes admission the same way and
|
||||
restores until its active entries fit.
|
||||
|
||||
### Disconnect responses
|
||||
|
||||
- `403` is a replay: this command's `jti` was already used.
|
||||
- `503 deny set full` means the issuer's shared deny set is full. Nothing was
|
||||
recorded, so the same command can be retried.
|
||||
- `503 authorization unavailable` after a Redis error means the outcome is
|
||||
unknown. If Redis applied the command before the reply was lost, the deny is
|
||||
already stored and its `jti` is used up, so a retry returns `403`. Send a
|
||||
freshly signed command to get a `200`.
|
||||
|
||||
### Capacity
|
||||
|
||||
`deny_set_capacity` applies to each issuer across all pods, not per pod. If a
|
||||
restore doesn't fit a pod's map, the pod stays closed and
|
||||
`buzz_nip_fi_deny_restore_failures_total{reason="capacity"}` increases. This
|
||||
can happen during the shadow → enforce switch below, when an enforce pod
|
||||
restores both stores. To recover, either:
|
||||
|
||||
- raise `deny_set_capacity` to at least the sum of the issuer's live entries
|
||||
and roll out; or
|
||||
- wait for entries to expire (at most one assertion lifetime). The pod retries
|
||||
the restore on its own.
|
||||
|
||||
Count live entries for an issuer with
|
||||
`ZCOUNT buzz:nip-fi:deny:<sha256(iss)> (<now_ms> +inf` and
|
||||
`ZCOUNT buzz:nip-fi:shadow-deny:<sha256(iss)> (<now_ms> +inf`, where
|
||||
`<sha256(iss)>` is the lowercase hex SHA-256 of the exact issuer URI.
|
||||
|
||||
### Switching shadow → enforce
|
||||
|
||||
Shadow pods write their accepts under `buzz:nip-fi:shadow-deny:*`, and enforce
|
||||
pods normally never read them. For the deploy that switches modes, the bridge
|
||||
setting `BUZZ_NIP_FI_ACCEPT_SHADOW_DISCONNECTS=true` makes enforce pods also
|
||||
subscribe to and restore shadow accepts. It gives disconnects accepted by
|
||||
shadow pods real effect on enforce pods, so keep the issuer configuration
|
||||
identical across the switch.
|
||||
|
||||
Let `L` be the largest `maximum_assertion_age_seconds + skew_seconds` across
|
||||
configured issuers.
|
||||
|
||||
1. Deploy this relay version while still in `shadow`.
|
||||
2. Wait until the last pod of the previous version has been gone for at least
|
||||
`L`. Older versions keep accepts only in memory, so this wait lets those
|
||||
expire before anything relies on the store.
|
||||
3. Deploy `BUZZ_NIP_FI_MODE=enforce` with
|
||||
`BUZZ_NIP_FI_ACCEPT_SHADOW_DISCONNECTS=true`.
|
||||
4. Keep the setting until both hold:
|
||||
- the last shadow pod has been gone for at least `L`;
|
||||
- `ZCOUNT buzz:nip-fi:shadow-deny:<sha256(iss)> (<now_ms> +inf` is `0` for
|
||||
every configured issuer.
|
||||
5. Deploy without the setting.
|
||||
|
||||
## Assertion policy ID change
|
||||
|
||||
This release changes every verified assertion's policy ID once, on purpose.
|
||||
|
||||
Reference in New Issue
Block a user