Skip to content

Commit 909cc1a

Browse files
committed
Merge remote-tracking branch 'origin/main' into push-gateway-mesh-delivery
* origin/main: feat(relay): enforce NIP-FI assertion+NIP-98 pairing on HTTP ingress (#7264) fix(ci): run the admin disabled-mode DB test in the PostgreSQL lane (#7900) 🤖 docs: add pre-PR checklist and review guidance to AGENTS.md (#7897) docs: specify durable data backfills (#7326) docs(vision): add /buzz/v1 read endpoints to the protocol contract (#7879) 🤖 fix(justfile): point just staging at the current staging relay (#7881) fix(relay-admin): make thread deletions atomic and fence expired action leases under row lock (#7853) feat(desktop): relay admin console for the /api/admin/v1 operator surface (#4768) fix(mobile): keep retired sections manager out of successor cache (#7873) Signed-off-by: Tom Brow <[email protected]>
2 parents 9ef501b + 6410e68 commit 909cc1a

100 files changed

Lines changed: 33272 additions & 1969 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AGENTS.md‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,48 @@ and runtime evidence answer different questions.
2525

2626
---
2727

28+
## Before opening a PR
29+
30+
Open work in progress as a draft PR. Mark it ready for review only when this
31+
checklist holds for the current change. Scale it to what changed:
32+
documentation-only changes need content, link, and diff checks plus human
33+
confirmation, not app runs.
34+
35+
1. **Agent review ran** under [Reviewing](#reviewing), and its recommended
36+
blockers were fixed or explicitly declined by the human author. Optional
37+
suggestions do not gate readiness.
38+
2. **An agent exercised the changed behavior.** Client changes: the affected flow
39+
in the app, using the native app or a device when the behavior needs it
40+
(browser or headless Playwright counts only for what it can exercise). Relay
41+
changes: a local relay, exercising the changed events or endpoints. CLI or
42+
tooling changes: the affected command or workflow. The human may skip this
43+
step, for example for a small change or while iterating.
44+
3. **A human then tested it themselves**: in the app, against the local relay
45+
(through the app or `curl`), or by running the changed command. Agent testing
46+
does not substitute. Agents give the human exact steps and what working looks
47+
like, then wait for explicit confirmation. Never mark this step done yourself.
48+
4. **Add `buzz-review-completed` to the PR description** once steps 1–3 hold. It
49+
attests the checklist, and automated reviewers may skip review because of it.
50+
If later edits change behavior, remove it and return the PR to draft until the
51+
affected steps are redone.
52+
53+
## Reviewing
54+
55+
- Before reviewing, read [VISION.md](VISION.md), the `VISION_*.md` docs for the affected surface, and the PR's stated goal and linked
56+
issue. Review the change against what it is trying to do.
57+
- Check the change against the [Review-Proven Rules](#review-proven-rules): the defects reviewers here find most often.
58+
- Judge minimalism, elegance, and correctness, aiming for 9/10 on each. A score
59+
below 9 names the concrete defect and the fix.
60+
- Recommend blocking only for concrete correctness, security, or agreed-contract
61+
defects with a realistic failure scenario: state the defect, how it fails, and
62+
the fix. Label everything else (nits, wording, speculative hardening,
63+
out-of-scope improvements) as optional.
64+
- Put all findings in the first review. Later reviews check prior blockers and
65+
defects the fixes introduced; reopen other areas only on new evidence of a
66+
material defect.
67+
- Agents post reviews as comments, never Request Changes. Humans decide which
68+
findings must be fixed.
69+
2870
## Ecosystem
2971

3072
Buzz spans five repos. This one (`block/buzz`) is the OSS source for the relay, desktop, mobile, and CLI. The others handle internal builds and deployment:
@@ -165,8 +207,8 @@ the same clusters and measured how often authors actually fix each class
165207
once flagged: test-seam binding and unbounded-resource findings were fixed
166208
**100%** of the time, swallowed-error findings **90%**, stale-state races
167209
**70%** — these are not style opinions, they are defects authors agree
168-
with on sight. Apply the rules **before writing code**; each cites the
169-
PRs where reviewers litigated it.
210+
with on sight. Authors apply them **before writing code**, and reviewers check
211+
against them; each cites the PRs where reviewers litigated it.
170212

171213
1. **Every caught failure must leave a durable retry record or propagate.**
172214
Never catch-log-and-return-success (opt-out revocation permanently

‎Cargo.lock‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Justfile‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -444,18 +444,11 @@ test-unit:
444444
# and a red one could ship green (exactly how a broken admin test slipped
445445
# past every gate once). Scoped to api::admin, not the whole buzz-relay
446446
# --lib, because api::media has non-ignored tests that require Postgres.
447-
# Two api::admin tests are excluded: both exercise a read-route DB
448-
# fallthrough and pass without a database only by waiting out the sqlx
449-
# acquire timeout (~30s each), so they do not belong in the infra-free
450-
# unit job. nip98_mode_unrostered_signer_does_not_consume_a_replay_slot
451-
# asserts a unique replay-guard invariant, so it is wired into the
452-
# Postgres-backed Backend Integration job (see ci.yml "Admin API
453-
# unrostered-signer replay invariant"). disabled_mode_allows_
454-
# unauthenticated_requests_on_the_admin_host has no unique invariant:
455-
# disabled-mode unauthenticated success is covered by
456-
# disabled_mode_regression_pin_unauthenticated_request_is_served on the
457-
# DB-free /probe route, and its Host/Origin gating is covered here by
458-
# disabled_mode_still_requires_the_correct_host / _a_matching_origin.
447+
# DB-backed api::admin tests are #[ignore]d and run in the PostgreSQL
448+
# lane; the non-ignored ones reject before touching the database. Any
449+
# new non-ignored test here must stay DB-free: without a database, a
450+
# DB fallthrough only "passes" by waiting out the ~30s sqlx acquire
451+
# timeout.
459452
# The second clause adds the relay's pure authorization-decision tests:
460453
# the NIP-29 channel membership grid (handlers::channel_authz), the
461454
# moderation capability grid (handlers::moderation_authz), and the pure
@@ -469,8 +462,17 @@ test-unit:
469462
# non-postgres_tests cases only "pass" without a database by waiting out
470463
# the ~30s sqlx acquire timeout, so they do not belong in the infra-free
471464
# unit job either.
472-
cargo nextest run -p buzz-relay --lib \
473-
-E '(test(/^api::admin::/) - test(=api::admin::tests::disabled_mode_allows_unauthenticated_requests_on_the_admin_host) - test(=api::admin::tests::nip98_mode_unrostered_signer_does_not_consume_a_replay_slot)) + test(/^handlers::channel_authz::/) + test(/^handlers::moderation_authz::/) + test(/^handlers::side_effects::tests::/) + test(/^storage_sweep::tests::/)'
465+
# The third clause adds the NIP-FI HTTP ingress and its router/config
466+
# neighbours: nip_fi_http, nip_fi_config, router, api::parse_query_tests,
467+
# and the Git transport off_mode_precedence_tests. All are infra-free
468+
# and finish in well under a second with no DATABASE_URL, so none wait
469+
# out the sqlx acquire timeout. `--bin buzz-relay` adds main.rs's
470+
# `tests::` module (JWKS refresh cadence) and `composition_tests::`
471+
# (JWKS source + refresh-loop composition), which `--lib` cannot reach
472+
# because they live in the binary target; the nested
473+
# `tests::postgres_tests::` stays in the PostgreSQL lane.
474+
cargo nextest run -p buzz-relay --lib --bin buzz-relay \
475+
-E 'test(/^api::admin::/) + test(/^handlers::channel_authz::/) + test(/^handlers::moderation_authz::/) + test(/^handlers::side_effects::tests::/) + test(/^storage_sweep::tests::/) + test(/^nip_fi_http::tests::/) + test(/^nip_fi_config::tests::/) + test(/^router::tests::/) + test(/^api::parse_query_tests::/) + test(/^api::git::transport::off_mode_precedence_tests::/) + (kind(bin) & (test(/^tests::/) + test(/^composition_tests::/)) - test(/^tests::postgres_tests::/))'
474476
# ACP author-gate and queue tests protect the trust boundary between
475477
# relay events and agent prompts. They are infra-free; ignored lifecycle
476478
# tests remain excluded and run in their dedicated integration lanes.
@@ -738,7 +740,7 @@ staging *ARGS: bootstrap _ensure-sidecar-stubs
738740
chmod +x "desktop/src-tauri/binaries/${bin}-${TARGET}"
739741
done
740742
cd {{desktop_dir}}
741-
export BUZZ_RELAY_URL="wss://sprout-oss.stage.blox.sqprod.co"
743+
export BUZZ_RELAY_URL="wss://buzz.test.blockstaging.build"
742744
source ../scripts/instance-env.sh
743745
# Ctrl+C kills the Tauri app before its in-process sweep finishes, leaking
744746
# agent workers. Reap this instance's agents on exit as a backstop.

‎VISION.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Buzz extends the standard Nostr event format with custom kind numbers for enterp
7474

7575
New message type? New kind integer. Zero breaking changes.
7676

77+
**The conversation is Nostr; Buzz adds accessories around it.** Messages, threads, reactions, and the rest of the conversation are signed Nostr events, and Nostr reads must cover all of that state, so a standards-leaning Buzz client stays possible. Around it, the relay serves accessories that don't fit that shape: Blossom media, git smart HTTP, workflow webhooks, derived unread counts, and namespaced `/buzz/v1/...` endpoints for backend-for-frontend reads (thread catch-up, sync) over the same authoritative store. We start with this rule and refine it as more cases show up.
78+
7779
---
7880

7981
## Architecture

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub use nip_fi::{
5252
IssuerJwksConfig, IssuerKeySource, IssuerPolicy, IssuerPolicyError, IssuerRegistry,
5353
JwksFetchError, JwksFetcher, JwksSourceContract, NipFiMode, NipFiStartupError,
5454
ProductionJwksSource, RevalidationDependencies, SubjectClass, SubjectClassContract, TokenClass,
55-
TransportContractId, VerifiedAssertion, VerifierError, CLIENT_ATTACHED_HEADER,
55+
TransportContractId, VerifiedAssertion, VerifierError, VerifyAssertion, CLIENT_ATTACHED_HEADER,
5656
NOSTR_PUBKEY_CLAIM, OAUTH_CLIENT_ID_CLAIM,
5757
};
5858

@@ -61,6 +61,10 @@ pub use access::MockAccessChecker;
6161
#[cfg(any(test, feature = "test-utils"))]
6262
pub use nip98_replay::AlwaysFreshReplayGuard;
6363
#[cfg(any(test, feature = "test-utils"))]
64+
pub use nip_fi::jwks::ScriptedJwksFetcher;
65+
#[cfg(any(test, feature = "test-utils"))]
66+
pub use nip_fi::StaticIssuerKeySource;
67+
#[cfg(any(test, feature = "test-utils"))]
6468
pub use rate_limit::AlwaysAllowRateLimiter;
6569

6670
/// How the connection was authenticated.

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

Lines changed: 147 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ pub fn verify_nip98_event(
149149
// sign without one. Rejecting duplicates closes the real attack: a valid-first
150150
// /contradictory-second pair would let `.find()` accept the first and silently
151151
// ignore the second, bypassing the body-hash check.
152+
//
153+
// Digest format contract: the content value must be exactly 64 lowercase
154+
// hex characters (a valid sha256 digest). A one-element `["payload"]` tag
155+
// with no content, or a malformed digest, must be rejected — the tag
156+
// *claims* body-hash binding but provides no valid digest. An absent tag
157+
// makes no claim; body-bearing routes that opt out of payload binding
158+
// (e.g. `/events`, `/query`, `/count`) fall here legitimately.
152159
{
153160
let count = event
154161
.tags
@@ -161,11 +168,40 @@ pub fn verify_nip98_event(
161168
)));
162169
}
163170
}
164-
// Keep a present-but-malformed tag distinct from an absent (optional) tag.
165-
if let (Some(payload_tag), Some(body_bytes)) = (event.tags.find(TagKind::Payload), body) {
166-
let payload_hex = payload_tag.content().ok_or_else(|| {
167-
AuthError::Nip98Invalid("payload tag is missing its SHA-256 hash".to_string())
168-
})?;
171+
// Validate the payload tag digest when the tag is present.
172+
//
173+
// A present tag *claims* body-hash binding. A missing hash value (one-element
174+
// tag or empty string content) is structurally invalid — the claim is made but
175+
// no digest is provided — and must be rejected. An absent tag makes no claim;
176+
// body-bearing routes that opt out of payload binding (e.g. `/events`, `/query`,
177+
// `/count`) fall here legitimately.
178+
//
179+
// When the tag is present, the content must be exactly 64 lowercase hex chars.
180+
let payload_tag = if let Some(tag) = event.tags.find(TagKind::Payload) {
181+
let hex_str = tag.content().unwrap_or("");
182+
if hex_str.is_empty() {
183+
return Err(AuthError::Nip98Invalid(
184+
"payload tag is missing its SHA-256 hash".to_string(),
185+
));
186+
}
187+
// Must be exactly 64 lowercase hex chars (valid sha256 digest).
188+
// Use a static diagnostic — attacker-controlled tag content must not
189+
// appear in error messages (length, format code, or unicode boundary
190+
// slicing could panic or leak attacker data).
191+
if hex_str.len() != 64
192+
|| !hex_str.chars().all(|c| c.is_ascii_hexdigit())
193+
|| hex_str.chars().any(|c| c.is_ascii_uppercase())
194+
{
195+
return Err(AuthError::Nip98Invalid(
196+
"payload tag digest must be exactly 64 lowercase hex chars (sha256)".to_string(),
197+
));
198+
}
199+
Some(hex_str)
200+
} else {
201+
None
202+
};
203+
204+
if let (Some(payload_hex), Some(body_bytes)) = (payload_tag, body) {
169205
let computed: [u8; 32] = Sha256::digest(body_bytes).into();
170206
let computed_hex = hex::encode(computed);
171207
if computed_hex != payload_hex {
@@ -479,6 +515,112 @@ mod tests {
479515
);
480516
}
481517

518+
// ── F1 regression: payload tag present with no or empty digest ─────────
519+
//
520+
// The old code did `.and_then(|t| t.content())` which returned `None` for a
521+
// one-element `["payload"]` tag — silently skipping the body-hash check.
522+
// A client could sign an event with `["payload"]` (no digest), present any
523+
// body, and the verifier would not check the body against the tag.
524+
//
525+
// Fix: a present tag with no content (or empty string) is rejected as
526+
// structurally invalid — the claim is made but no digest is provided.
527+
// An absent tag makes no claim and is accepted. A present tag with content
528+
// must be exactly 64 lowercase hex chars; invalid format rejects the event.
529+
//
530+
// Mutation evidence: removing the format check makes `unwrap_err()` panic.
531+
532+
#[test]
533+
fn payload_tag_malformed_digest_rejected() {
534+
// A payload tag present with a value that is NOT 64 lowercase hex chars
535+
// must be rejected — it is a structurally invalid event.
536+
use nostr::Tag;
537+
let keys = Keys::generate();
538+
let body = b"any body";
539+
540+
// Too short.
541+
let json = make_nip98_event_raw_tags(
542+
&keys,
543+
vec![
544+
Tag::parse(["u", TEST_URL]).unwrap(),
545+
Tag::parse(["method", TEST_METHOD]).unwrap(),
546+
Tag::parse(["payload", "deadbeef"]).unwrap(), // 8 chars, not 64
547+
],
548+
);
549+
let result = verify_nip98_event(&json, TEST_URL, TEST_METHOD, Some(body));
550+
assert!(
551+
matches!(result, Err(AuthError::Nip98Invalid(_))),
552+
"payload tag with short digest must be rejected: {result:?}"
553+
);
554+
555+
// Uppercase hex (structurally invalid per NIP-98 lowercase-hex contract).
556+
let keys2 = Keys::generate();
557+
let hash: [u8; 32] = Sha256::digest(body).into();
558+
let upper_hex = hex::encode(hash).to_uppercase();
559+
let json2 = make_nip98_event_raw_tags(
560+
&keys2,
561+
vec![
562+
Tag::parse(["u", TEST_URL]).unwrap(),
563+
Tag::parse(["method", TEST_METHOD]).unwrap(),
564+
Tag::parse(["payload", &upper_hex]).unwrap(),
565+
],
566+
);
567+
let result2 = verify_nip98_event(&json2, TEST_URL, TEST_METHOD, Some(body));
568+
assert!(
569+
matches!(result2, Err(AuthError::Nip98Invalid(_))),
570+
"payload tag with uppercase hex must be rejected: {result2:?}"
571+
);
572+
573+
// Non-hex content.
574+
let keys3 = Keys::generate();
575+
let non_hex = "z".repeat(64);
576+
let json3 = make_nip98_event_raw_tags(
577+
&keys3,
578+
vec![
579+
Tag::parse(["u", TEST_URL]).unwrap(),
580+
Tag::parse(["method", TEST_METHOD]).unwrap(),
581+
Tag::parse(["payload", &non_hex]).unwrap(),
582+
],
583+
);
584+
let result3 = verify_nip98_event(&json3, TEST_URL, TEST_METHOD, Some(body));
585+
assert!(
586+
matches!(result3, Err(AuthError::Nip98Invalid(_))),
587+
"payload tag with non-hex content must be rejected: {result3:?}"
588+
);
589+
}
590+
591+
#[test]
592+
fn payload_tag_multibyte_boundary_does_not_panic() {
593+
// Regression for R2 (Thufir round-1): the old code did
594+
// `&hex_str[..hex_str.len().min(80)]`
595+
// on attacker-controlled tag content. 79 ASCII 'a' chars followed by
596+
// 'é' (U+00E9, 2 UTF-8 bytes) produces a 81-byte str; truncating at
597+
// byte 80 splits the multibyte character and panics with a byte-index
598+
// boundary panic.
599+
//
600+
// Mutation evidence: restoring the old byte-slice panic reproduces the
601+
// panic here rather than returning an ordinary `AuthError`.
602+
use nostr::Tag;
603+
let keys = Keys::generate();
604+
// 79 lowercase 'a' chars + 'é' (2 UTF-8 bytes) = 81 bytes, not 64 chars.
605+
let malformed = format!("{}{}", "a".repeat(79), "é");
606+
assert_eq!(malformed.len(), 81, "precondition: 81 UTF-8 bytes");
607+
assert_eq!(malformed.chars().count(), 80, "precondition: 80 chars");
608+
let json = make_nip98_event_raw_tags(
609+
&keys,
610+
vec![
611+
Tag::parse(["u", TEST_URL]).unwrap(),
612+
Tag::parse(["method", TEST_METHOD]).unwrap(),
613+
Tag::parse(["payload", &malformed]).unwrap(),
614+
],
615+
);
616+
// Must return an ordinary AuthError, not panic.
617+
let result = verify_nip98_event(&json, TEST_URL, TEST_METHOD, None);
618+
assert!(
619+
matches!(result, Err(AuthError::Nip98Invalid(_))),
620+
"multibyte-boundary malformed payload tag must return AuthError, not panic: {result:?}"
621+
);
622+
}
623+
482624
#[test]
483625
fn loopback_aliases_are_distinct_hosts() {
484626
// Under multi-tenant, the `u`-tag host is the row-zero community

‎crates/buzz-auth/src/nip_fi/assertion.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,31 @@ impl VerifiedAssertion {
199199
pub const fn revalidation_dependencies(&self) -> &RevalidationDependencies {
200200
&self.revalidation_dependencies
201201
}
202+
203+
/// Test-only constructor: mint a minimal `VerifiedAssertion` for a given
204+
/// `asserted_key`. All other fields are set to safe, arbitrary defaults.
205+
///
206+
/// Used in unit tests that need to supply a `VerifiedAssertion` with a
207+
/// specific `asserted_key` without performing a real JWKS verification.
208+
#[cfg(any(test, feature = "test-utils"))]
209+
pub fn new_for_test(asserted_key: nostr::PublicKey) -> Self {
210+
use chrono::Duration;
211+
Self::seal(
212+
"https://test.issuer.example".to_owned(),
213+
"test-subject".to_owned(),
214+
Some(asserted_key),
215+
CanonicalCapabilities::from_pairs(vec![]),
216+
vec![Utc::now() + Duration::seconds(3600)],
217+
AssertionPolicyId::zero(),
218+
TransportContractId::zero(),
219+
RevalidationDependencies::new(
220+
"test-kid".to_owned(),
221+
1,
222+
Utc::now() + Duration::seconds(3600),
223+
"test.header.sig".to_owned(),
224+
),
225+
)
226+
}
202227
}
203228

204229
impl fmt::Debug for VerifiedAssertion {

‎crates/buzz-auth/src/nip_fi/config.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ impl AssertionPolicyId {
104104
pub const fn as_bytes(&self) -> &[u8; 32] {
105105
&self.0
106106
}
107+
108+
/// All-zeros sentinel for use in tests only.
109+
#[cfg(any(test, feature = "test-utils"))]
110+
pub fn zero() -> Self {
111+
Self([0u8; 32])
112+
}
107113
}
108114

109115
impl fmt::Debug for AssertionPolicyId {
@@ -144,6 +150,12 @@ impl TransportContractId {
144150
pub const fn as_bytes(&self) -> &[u8; 32] {
145151
&self.0
146152
}
153+
154+
/// All-zeros sentinel for use in tests only.
155+
#[cfg(any(test, feature = "test-utils"))]
156+
pub fn zero() -> Self {
157+
Self([0u8; 32])
158+
}
147159
}
148160

149161
impl fmt::Debug for TransportContractId {

0 commit comments

Comments
 (0)