Skip to content

Commit 9630c3e

Browse files
loganjLarry
andcommitted
fix(relay): read named channels of later types under the ordinary rules
The explicit-name lookup loaded only system channels, so a filter that named a channel of a type added later could read it only as a member through #h repair: an open channel's non-member read nothing, and #d on group-state kinds read nothing for anyone. Load every readable non-legacy channel (member, or open) instead, with its type. The same set fed the system opt-in, so a #P or #t:["system", <label>] sibling filter could also read a later-type channel that another filter named. Keep the type with each channel, and let the opt-in add system channels only. Workflow run reads and thread windows name their channel and use the same lookup. Add a production-path regression on a scratch database with a later enum value: member and open-non-member reads by #h and #d over WS REQ, WS COUNT, HTTP /query and /count, and #P and #t sibling filters on each. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: Logan Johnson <[email protected]>
1 parent 3855d7a commit 9630c3e

10 files changed

Lines changed: 563 additions & 258 deletions

File tree

‎NOSTR.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,11 @@ out unless they ask for it:
146146
filter returns only the legacy channel types (`stream`, `forum`, `dm`,
147147
`workflow`), so a channel type added later is also left out.
148148
- A filter that names it with `#h`, or with `#d` on kinds 39000–39003, reads
149-
it under the ordinary rules: the reader is a member, or it is open.
149+
it under the ordinary rules: the reader is a member, or it is open. A
150+
channel of a type added later is read the same way.
150151
- A filter on kinds 39000–39003 with `#t:["system"]` or any `#P` also
151152
returns the system channels the reader can read, then matches as usual.
153+
It does not return channels of a type added later.
152154
For example, `{kinds:[39000], #P:[<creator>], #t:[<label>]}` finds one
153155
creator's labeled channel, system or not. Values in one tag filter
154156
combine with OR, so `#t:["system", <label>]` does not mean "a system

‎crates/buzz-db/src/store/channel_members.rs‎

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,8 +1210,8 @@ pub async fn get_members_bulk(
12101210
/// Only legacy channel types are in either half
12111211
/// ([`buzz_core::channel::LEGACY_CHANNEL_TYPES`]): this is the implicit scope
12121212
/// of queries that do not name a channel, so system channels and any type
1213-
/// added later stay out. A query that names a system channel, or opts in to
1214-
/// them, adds them from [`get_readable_system_channel_ids`].
1213+
/// added later stay out. A query that names such a channel, or opts in to
1214+
/// system channels, adds them from [`get_readable_named_only_channels`].
12151215
pub async fn get_accessible_channel_ids(
12161216
pool: &PgPool,
12171217
community_id: CommunityId,
@@ -1253,24 +1253,30 @@ pub async fn get_accessible_channel_ids(
12531253
.collect()
12541254
}
12551255

1256-
/// Get the system channels a pubkey can read: those where it is an active
1257-
/// member, and every open system channel. Uncached, so a caller can trust it
1258-
/// for a channel the request names.
1259-
pub async fn get_readable_system_channel_ids(
1256+
/// The channels of non-legacy types (system channels, and any type added
1257+
/// later) that a pubkey can read: those where it is an active member, and
1258+
/// every open one. Each comes with its type. Uncached, so a caller can trust
1259+
/// it for a channel the request names.
1260+
///
1261+
/// These are the channels that [`get_accessible_channel_ids`] leaves out. A
1262+
/// read reaches one only by naming it, or, for system channels, by opting in
1263+
/// to them.
1264+
pub async fn get_readable_named_only_channels(
12601265
pool: &PgPool,
12611266
community_id: CommunityId,
12621267
pubkey: &[u8],
1263-
) -> Result<Vec<Uuid>> {
1268+
) -> Result<Vec<(Uuid, String)>> {
12641269
let mut connection = crate::observability::acquire_writer(
12651270
pool,
12661271
crate::observability::WriterOperation::Authorization,
12671272
)
12681273
.await?;
1269-
let rows = sqlx::query(
1274+
let sql = format!(
12701275
r#"
1271-
SELECT c.id AS channel_id
1276+
SELECT c.id AS channel_id, c.channel_type::text AS channel_type
12721277
FROM channels c
1273-
WHERE c.community_id = $1 AND c.deleted_at IS NULL AND c.channel_type = 'system'
1278+
WHERE c.community_id = $1 AND c.deleted_at IS NULL
1279+
AND NOT {legacy_types}
12741280
AND (
12751281
c.visibility = 'open'
12761282
OR EXISTS (
@@ -1280,17 +1286,17 @@ pub async fn get_readable_system_channel_ids(
12801286
)
12811287
)
12821288
"#,
1283-
)
1284-
.bind(community_id.as_uuid())
1285-
.bind(pubkey)
1286-
.fetch_all(&mut *connection)
1287-
.await?;
1289+
legacy_types = crate::store::channel::legacy_channel_type_sql("c.channel_type"),
1290+
);
1291+
// Built only from the constant type list.
1292+
let rows = sqlx::query(sqlx::AssertSqlSafe(sql))
1293+
.bind(community_id.as_uuid())
1294+
.bind(pubkey)
1295+
.fetch_all(&mut *connection)
1296+
.await?;
12881297

12891298
rows.into_iter()
1290-
.map(|r| {
1291-
let id: Uuid = r.try_get("channel_id")?;
1292-
Ok(id)
1293-
})
1299+
.map(|r| Ok((r.try_get("channel_id")?, r.try_get("channel_type")?)))
12941300
.collect()
12951301
}
12961302

@@ -1340,14 +1346,15 @@ pub async fn system_channel_ids_among(
13401346
.await?)
13411347
}
13421348

1343-
/// The IDs in `channel_ids` whose type is not a legacy type
1344-
/// ([`buzz_core::channel::LEGACY_CHANNEL_TYPES`]), deleted or not: system
1345-
/// channels and any type added later. A filter reads them only by naming them.
1346-
pub async fn non_legacy_channel_ids_among(
1349+
/// The channels in `channel_ids` whose type is not a legacy type
1350+
/// ([`buzz_core::channel::LEGACY_CHANNEL_TYPES`]), deleted or not, each with
1351+
/// its type: system channels and any type added later. A filter reads them
1352+
/// only by naming them.
1353+
pub async fn named_only_channels_among(
13471354
pool: &PgPool,
13481355
community_id: CommunityId,
13491356
channel_ids: &[Uuid],
1350-
) -> Result<Vec<Uuid>> {
1357+
) -> Result<Vec<(Uuid, String)>> {
13511358
if channel_ids.is_empty() {
13521359
return Ok(Vec::new());
13531360
}
@@ -1357,10 +1364,11 @@ pub async fn non_legacy_channel_ids_among(
13571364
)
13581365
.await?;
13591366
let sql = format!(
1360-
"SELECT id FROM channels WHERE community_id = $1 AND id = ANY($2) AND NOT {}",
1367+
"SELECT id, channel_type::text FROM channels \
1368+
WHERE community_id = $1 AND id = ANY($2) AND NOT {}",
13611369
crate::store::channel::legacy_channel_type_sql("channel_type")
13621370
);
1363-
Ok(sqlx::query_scalar(sqlx::AssertSqlSafe(sql))
1371+
Ok(sqlx::query_as(sqlx::AssertSqlSafe(sql))
13641372
.bind(community_id.as_uuid())
13651373
.bind(channel_ids)
13661374
.fetch_all(&mut *connection)
@@ -1999,14 +2007,15 @@ impl Db {
19992007
get_accessible_channel_ids(&self.pool, community_id, pubkey).await
20002008
}
20012009

2002-
/// Get the system channels a pubkey can read (member, or open).
2003-
#[datastore_span(name = "get_readable_system_channel_ids", system = "postgresql")]
2004-
pub async fn get_readable_system_channel_ids(
2010+
/// The channels of non-legacy types a pubkey can read (member, or open),
2011+
/// each with its type.
2012+
#[datastore_span(name = "get_readable_named_only_channels", system = "postgresql")]
2013+
pub async fn get_readable_named_only_channels(
20052014
&self,
20062015
community_id: CommunityId,
20072016
pubkey: &[u8],
2008-
) -> Result<Vec<Uuid>> {
2009-
get_readable_system_channel_ids(&self.pool, community_id, pubkey).await
2017+
) -> Result<Vec<(Uuid, String)>> {
2018+
get_readable_named_only_channels(&self.pool, community_id, pubkey).await
20102019
}
20112020

20122021
/// The type of channel `channel_id`, deleted or not, or `None` when no
@@ -2030,15 +2039,15 @@ impl Db {
20302039
system_channel_ids_among(&self.pool, community_id, channel_ids).await
20312040
}
20322041

2033-
/// The IDs in `channel_ids` whose type is not a legacy type, deleted or
2034-
/// not.
2035-
#[datastore_span(name = "non_legacy_channel_ids_among", system = "postgresql")]
2036-
pub async fn non_legacy_channel_ids_among(
2042+
/// The channels in `channel_ids` whose type is not a legacy type, deleted
2043+
/// or not, each with its type.
2044+
#[datastore_span(name = "named_only_channels_among", system = "postgresql")]
2045+
pub async fn named_only_channels_among(
20372046
&self,
20382047
community_id: CommunityId,
20392048
channel_ids: &[Uuid],
2040-
) -> Result<Vec<Uuid>> {
2041-
non_legacy_channel_ids_among(&self.pool, community_id, channel_ids).await
2049+
) -> Result<Vec<(Uuid, String)>> {
2050+
named_only_channels_among(&self.pool, community_id, channel_ids).await
20422051
}
20432052

20442053
/// Returns large active-channel rosters whose relay-authored snapshots differ.

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,11 @@ async fn query_events_authed(
15111511
.get_accessible_channel_ids_cached(tenant.community(), &pubkey_bytes)
15121512
.await
15131513
.map_err(|e| internal_error(&format!("channel access lookup: {e}")))?;
1514-
// System channels are outside `accessible_channels`; a filter that names
1515-
// one reads it in its own scope only (see `handlers::system_channels`).
1516-
let mut readable_system =
1517-
crate::handlers::system_channels::readable_system_channels_for_filters(
1514+
// Named-only channels (system, and types added later) are outside
1515+
// `accessible_channels`; a filter that names one reads it in its own
1516+
// scope only (see `handlers::system_channels`).
1517+
let mut named_only =
1518+
crate::handlers::system_channels::readable_named_only_channels_for_filters(
15181519
state,
15191520
tenant.community(),
15201521
&pubkey_bytes,
@@ -1523,14 +1524,14 @@ async fn query_events_authed(
15231524
None,
15241525
)
15251526
.await
1526-
.map_err(|e| internal_error(&format!("system channel access lookup: {e}")))?;
1527+
.map_err(|e| internal_error(&format!("named-only channel access lookup: {e}")))?;
15271528
repair_requested_channel_access(
15281529
state,
15291530
tenant,
15301531
&filters,
15311532
&pubkey_bytes,
15321533
&mut accessible_channels,
1533-
&mut readable_system,
1534+
&mut named_only,
15341535
)
15351536
.await?;
15361537

@@ -1546,7 +1547,7 @@ async fn query_events_authed(
15461547
&raw_filters,
15471548
&filters,
15481549
&accessible_channels,
1549-
&readable_system,
1550+
&named_only,
15501551
tenant,
15511552
&authed_pubkey_hex,
15521553
&pubkey_bytes,
@@ -1581,7 +1582,7 @@ async fn query_events_authed(
15811582
let scope = crate::handlers::system_channels::filter_channel_scope(
15821583
filter,
15831584
&accessible_channels,
1584-
&readable_system,
1585+
&named_only,
15851586
);
15861587
events.extend(thread_roots::query(state, tenant, filter, &scope).await?);
15871588
handled.insert(idx);
@@ -1602,7 +1603,7 @@ async fn query_events_authed(
16021603
&crate::handlers::system_channels::filter_channel_scope(
16031604
filter,
16041605
&accessible_channels,
1605-
&readable_system,
1606+
&named_only,
16061607
),
16071608
&pubkey_bytes,
16081609
&mut events,
@@ -1728,7 +1729,7 @@ async fn query_events_authed(
17281729
let accessible_channels = crate::handlers::system_channels::filter_channel_scope(
17291730
filter,
17301731
&accessible_channels,
1731-
&readable_system,
1732+
&named_only,
17321733
);
17331734

17341735
if let Some(ch_id) = extract_channel_from_filter(filter) {
@@ -1822,7 +1823,7 @@ async fn query_events_authed(
18221823
let accessible_channels = crate::handlers::system_channels::filter_channel_scope(
18231824
filter,
18241825
&accessible_channels,
1825-
&readable_system,
1826+
&named_only,
18261827
);
18271828

18281829
if let Some(ch_id) = extract_channel_from_filter(filter) {
@@ -1928,7 +1929,7 @@ async fn query_events_authed(
19281929
let accessible_channels = crate::handlers::system_channels::filter_channel_scope(
19291930
filter,
19301931
&accessible_channels,
1931-
&readable_system,
1932+
&named_only,
19321933
);
19331934
match filter_events {
19341935
Ok(stored_events) => {
@@ -1967,7 +1968,7 @@ pub(crate) async fn repair_requested_channel_access(
19671968
filters: &[nostr::Filter],
19681969
pubkey_bytes: &[u8],
19691970
accessible_channels: &mut Vec<uuid::Uuid>,
1970-
readable_system: &mut Vec<uuid::Uuid>,
1971+
named_only: &mut crate::handlers::system_channels::NamedOnlyChannels,
19711972
) -> Result<(), (StatusCode, Json<Value>)> {
19721973
let repaired_from = accessible_channels.len();
19731974
let requested: Vec<uuid::Uuid> = filters
@@ -1984,19 +1985,19 @@ pub(crate) async fn repair_requested_channel_access(
19841985
&requested,
19851986
None,
19861987
accessible_channels,
1987-
readable_system,
1988+
named_only.ids(),
19881989
)
19891990
.await
19901991
.map_err(|e| internal_error(&format!("channel membership confirmation: {e}")))?;
1991-
crate::handlers::system_channels::keep_repaired_non_legacy_channels_named(
1992+
crate::handlers::system_channels::keep_repaired_named_only_channels_named(
19921993
state,
19931994
tenant.community(),
19941995
accessible_channels,
19951996
repaired_from,
1996-
readable_system,
1997+
named_only,
19971998
)
19981999
.await
1999-
.map_err(|e| internal_error(&format!("system channel access lookup: {e}")))
2000+
.map_err(|e| internal_error(&format!("named-only channel access lookup: {e}")))
20002001
}
20012002

20022003
/// Count events via HTTP bridge (NIP-98 auth). Returns `{"count": N}`.
@@ -2154,10 +2155,11 @@ async fn count_events_authed(
21542155
.get_accessible_channel_ids_cached(tenant.community(), &pubkey_bytes)
21552156
.await
21562157
.map_err(|e| internal_error(&format!("channel access lookup: {e}")))?;
2157-
// System channels are outside `accessible_channels`; a filter that names
2158-
// one reads it in its own scope only (see `handlers::system_channels`).
2159-
let mut readable_system =
2160-
crate::handlers::system_channels::readable_system_channels_for_filters(
2158+
// Named-only channels (system, and types added later) are outside
2159+
// `accessible_channels`; a filter that names one reads it in its own
2160+
// scope only (see `handlers::system_channels`).
2161+
let mut named_only =
2162+
crate::handlers::system_channels::readable_named_only_channels_for_filters(
21612163
state,
21622164
tenant.community(),
21632165
&pubkey_bytes,
@@ -2166,14 +2168,14 @@ async fn count_events_authed(
21662168
None,
21672169
)
21682170
.await
2169-
.map_err(|e| internal_error(&format!("system channel access lookup: {e}")))?;
2171+
.map_err(|e| internal_error(&format!("named-only channel access lookup: {e}")))?;
21702172
repair_requested_channel_access(
21712173
state,
21722174
tenant,
21732175
&filters,
21742176
&pubkey_bytes,
21752177
&mut accessible_channels,
2176-
&mut readable_system,
2178+
&mut named_only,
21772179
)
21782180
.await?;
21792181

@@ -2182,7 +2184,7 @@ async fn count_events_authed(
21822184
let accessible_channels = crate::handlers::system_channels::filter_channel_scope(
21832185
filter,
21842186
&accessible_channels,
2185-
&readable_system,
2187+
&named_only,
21862188
);
21872189
let needs_author_only_filtering =
21882190
crate::handlers::req::filter_can_match_author_only_kinds(filter);
@@ -2415,7 +2417,7 @@ async fn handle_bridge_search(
24152417
raw_filters: &[Value],
24162418
filters: &[nostr::Filter],
24172419
implicit_channels: &[uuid::Uuid],
2418-
readable_system: &[uuid::Uuid],
2420+
named_only: &crate::handlers::system_channels::NamedOnlyChannels,
24192421
tenant: &buzz_core::tenant::TenantContext,
24202422
reader_pubkey_hex: &str,
24212423
pubkey_bytes: &[u8],
@@ -2435,12 +2437,12 @@ async fn handle_bridge_search(
24352437
if limit == 0 {
24362438
continue;
24372439
}
2438-
// Implicit channels plus the readable system channels this filter
2440+
// Implicit channels plus the readable named-only channels this filter
24392441
// names; never another filter's.
24402442
let accessible_channels = &*crate::handlers::system_channels::filter_channel_scope(
24412443
filter,
24422444
implicit_channels,
2443-
readable_system,
2445+
named_only,
24442446
);
24452447

24462448
// Scope by channel — push the #h tag (intersected with accessible

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ fn append(events: &mut Vec<Value>, budget: &mut Budget, event: &nostr::Event) ->
8585
}
8686

8787
/// The reader's implicit channel set and, when a request names a channel
88-
/// outside it, the system channels it can read. A thread window names its
89-
/// channel, so a readable system channel joins that request's scope only.
88+
/// outside it, the named-only channels it can read (system channels, and any
89+
/// type added later). A thread window names its channel, so a readable
90+
/// named-only channel joins that request's scope only.
9091
async fn access_sets(
9192
state: &AppState,
9293
tenant: &TenantContext,
@@ -105,9 +106,12 @@ async fn access_sets(
105106
{
106107
state
107108
.db
108-
.get_readable_system_channel_ids(tenant.community(), &reader.to_bytes())
109+
.get_readable_named_only_channels(tenant.community(), &reader.to_bytes())
109110
.await
110111
.map_err(|e| database_error(context, e))?
112+
.into_iter()
113+
.map(|(id, _)| id)
114+
.collect()
111115
} else {
112116
Vec::new()
113117
};

0 commit comments

Comments
 (0)