Skip to content

Commit 312014d

Browse files
npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7wpfleger96
andcommitted
fix(relay): close NIP-98 HTTP bridge bypass in persona shared-gate
Apply the author-only-unless-shared read gate to the NIP-98 HTTP bridge (/query, /count, search) and centralize the per-event visibility decision into a single event_visible_to_reader helper callable from both WS and HTTP adapters. Bridge parity (CRITICAL): - /query catchall: replaced the two-step is_author_only_event + reader_authorized_for_event checks with event_visible_to_reader, which also covers is_unshared_persona_event. A foreign authenticated caller POSTing {kinds:[30175],authors:[victim]} or {ids:[unshared-id]} now receives nothing. - /count: added needs_persona_filtering to the fast-path guard (forces fallback when the filter can match kind:30175) and replaced the two-step checks in both fallback loops with event_visible_to_reader. Existence leak via HTTP COUNT is closed on both the channel-scoped and unconstrained paths. - FTS search helper: replaced is_author_only_event with event_visible_to_reader as defense-in-depth. Kind 30175 is not in the FTS allowlist today so the branch is not reachable, but the check prevents a future allowlist change from silently reopening the bypass (comment explains this at the site). Structural centralization (IMPORTANT): - Added event_visible_to_reader to handlers/req.rs, combining the three per-event access predicates (is_author_only_event, is_unshared_persona_event, reader_authorized_for_event) behind a single call. Both the WS and HTTP adapters now call this helper at their result-visibility sites. Test gaps closed (IMPORTANT): - Reworked test_persona_live_fanout_shared_gate with explicit monotonic created_at timestamps (t0 < t1 < t2) so NIP-33 head ordering is deterministic, not dependent on event-id tie-break. Added self-query assertions after each publish to verify the correct NIP-33 head is live, and added a live-fan-out assertion that foreign receives nothing when the shared head goes unshared. - Added shared=x and missing-value wire-level rejection cases to test_persona_ingest_shared_tag_validation (AC-5; previously only shared=false and duplicate were tested at the wire level). - Fixed test_persona_mixed_kind_filter_does_not_leak (AC-6): now publishes a kind:9 event from the same author and asserts it IS returned while the unshared persona is omitted. Previously the absence-only assertion would pass if the whole mixed filter were dropped. - Added two new HTTP bridge e2e tests: test_persona_http_query_cross_author_gate: verifies /query returns only shared persona to foreign, returns nothing for {ids:[unshared-id]}, and returns the shared event for {ids:[shared-id]}. test_persona_http_count_cross_author_gate: verifies /count returns 1 (shared only) to foreign and 3 (all) to author, on author-scoped and wildcard filters. NIP-AP.md (doc): - Replaced aspirational every relay read chokepoint wording with an enumerated list of gated surfaces including NIP-98 /query, /count, and FTS/search with their exact enforcement mechanism named. - Added Non-goal: side-band existence oracles note documenting the adjudicated decision not to gate reaction/report/deletion target resolution (leaks one bit of existence, never content; requires a known event id unshared personas never hand out through any gated path). Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 01c7fc8 commit 312014d

4 files changed

Lines changed: 501 additions & 28 deletions

File tree

‎crates/buzz-relay/src/api/bridge.rs‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,15 +1284,16 @@ async fn query_events_authed(
12841284
}
12851285
// Result-level read auth: never hand a viewer-private snapshot
12861286
// (kind:30622) to anyone but its owner, even via kindless `ids`.
1287-
if !buzz_core::filter::reader_authorized_for_event(
1287+
// Also enforces author-only kinds (30300/30350) and the persona
1288+
// shared-gate (kind:30175 without ["shared","true"]). Single call
1289+
// covers all three gated event classes.
1290+
if !crate::handlers::req::event_visible_to_reader(
12881291
&se.event,
1292+
&pubkey_bytes,
12891293
&authed_pubkey_hex,
12901294
) {
12911295
continue;
12921296
}
1293-
if crate::handlers::req::is_author_only_event(&se.event, &pubkey_bytes) {
1294-
continue;
1295-
}
12961297
if let Ok(v) = serde_json::to_value(&se.event) {
12971298
events.push(v);
12981299
}
@@ -1439,6 +1440,11 @@ async fn count_events_authed(
14391440
filter,
14401441
&authed_pubkey_hex,
14411442
);
1443+
// Force per-event fallback for filters that can match kind:30175 —
1444+
// the fast SQL count_events() path has no per-event gate and would
1445+
// over-count foreign unshared persona events (existence leak).
1446+
let needs_persona_filtering =
1447+
crate::handlers::req::filter_can_match_persona_shared_kinds(filter);
14421448

14431449
// If filter targets a specific channel, verify access.
14441450
if let Some(ch_id) = extract_channel_from_filter(filter) {
@@ -1462,6 +1468,7 @@ async fn count_events_authed(
14621468
if crate::handlers::req::filter_fully_pushable(filter)
14631469
&& (!needs_author_only_filtering || author_is_self)
14641470
&& !needs_result_gated_filtering
1471+
&& !needs_persona_filtering
14651472
{
14661473
match state.db.count_events(&query).await {
14671474
Ok(n) => total += n as u64,
@@ -1487,12 +1494,9 @@ async fn count_events_authed(
14871494
{
14881495
continue;
14891496
}
1490-
if crate::handlers::req::is_author_only_event(&se.event, &pubkey_bytes)
1491-
{
1492-
continue;
1493-
}
1494-
if !buzz_core::filter::reader_authorized_for_event(
1497+
if !crate::handlers::req::event_visible_to_reader(
14951498
&se.event,
1499+
&pubkey_bytes,
14961500
&authed_pubkey_hex,
14971501
) {
14981502
continue;
@@ -1526,6 +1530,7 @@ async fn count_events_authed(
15261530
if crate::handlers::req::filter_fully_pushable(filter)
15271531
&& (!needs_author_only_filtering || author_is_self)
15281532
&& !needs_result_gated_filtering
1533+
&& !needs_persona_filtering
15291534
{
15301535
query.limit = None;
15311536
match state.db.count_events(&query).await {
@@ -1551,12 +1556,9 @@ async fn count_events_authed(
15511556
{
15521557
continue;
15531558
}
1554-
if crate::handlers::req::is_author_only_event(&se.event, &pubkey_bytes)
1555-
{
1556-
continue;
1557-
}
1558-
if !buzz_core::filter::reader_authorized_for_event(
1559+
if !crate::handlers::req::event_visible_to_reader(
15591560
&se.event,
1561+
&pubkey_bytes,
15601562
&authed_pubkey_hex,
15611563
) {
15621564
continue;
@@ -1732,7 +1734,18 @@ async fn handle_bridge_search(
17321734
if !search_hit_accepted(filter, stored, accessible_channels, reader_pubkey_hex) {
17331735
continue;
17341736
}
1735-
if crate::handlers::req::is_author_only_event(&stored.event, pubkey_bytes) {
1737+
// Defense-in-depth: apply the full per-event visibility gate, which
1738+
// covers author-only kinds, the persona shared-gate (kind:30175), and
1739+
// result-gated kinds. Kind:30175 is not in the FTS positive allowlist
1740+
// today (migration 8 indexes only 0,9,40002,45001,45003), so this
1741+
// branch cannot currently return unshared persona content — but the
1742+
// check here ensures that a future FTS allowlist change cannot silently
1743+
// reopen the bypass.
1744+
if !crate::handlers::req::event_visible_to_reader(
1745+
&stored.event,
1746+
pubkey_bytes,
1747+
reader_pubkey_hex,
1748+
) {
17361749
continue;
17371750
}
17381751
// Dedup across filters.

‎crates/buzz-relay/src/handlers/req.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,40 @@ pub(crate) fn is_author_only_event(event: &nostr::Event, requester_pubkey_bytes:
12151215
AUTHOR_ONLY_KINDS.contains(&kind_u32) && event.pubkey.to_bytes() != requester_pubkey_bytes
12161216
}
12171217

1218+
/// Combined per-event result-visibility check for all gated event classes.
1219+
///
1220+
/// Returns `true` if the `event` should be delivered to / counted for the
1221+
/// reader identified by `requester_pubkey_bytes` + `requester_pubkey_hex`.
1222+
/// Returns `false` and the event must be silently omitted if any of the
1223+
/// following hold:
1224+
///
1225+
/// 1. **Author-only kinds** (`AUTHOR_ONLY_KINDS`, e.g. kind 30300/30350): only
1226+
/// the author may read their own events.
1227+
/// 2. **Persona shared-gate** (kind 30175 without `["shared","true"]`): the
1228+
/// event is only visible to the author unless explicitly opted into sharing.
1229+
/// 3. **Result-gated kinds** (kind 44200/30622 etc.): `reader_authorized_for_event`
1230+
/// carries the per-event ownership check.
1231+
///
1232+
/// Call this from every read surface — both WS (REQ/COUNT/fan-out) and HTTP
1233+
/// (NIP-98 `/query`, `/count`, FTS search) — instead of inlining the three
1234+
/// individual predicates at each site.
1235+
pub(crate) fn event_visible_to_reader(
1236+
event: &nostr::Event,
1237+
requester_pubkey_bytes: &[u8],
1238+
requester_pubkey_hex: &str,
1239+
) -> bool {
1240+
if is_author_only_event(event, requester_pubkey_bytes) {
1241+
return false;
1242+
}
1243+
if is_unshared_persona_event(event, requester_pubkey_bytes) {
1244+
return false;
1245+
}
1246+
if !buzz_core::filter::reader_authorized_for_event(event, requester_pubkey_hex) {
1247+
return false;
1248+
}
1249+
true
1250+
}
1251+
12181252
/// Pre-filter authorization for filters that exclusively target author-only kinds.
12191253
///
12201254
/// If a filter targets ONLY author-only kinds (e.g. `{kinds:[30300]}`), the

0 commit comments

Comments
 (0)