Skip to content

Commit 853e544

Browse files
loganjLarry
andcommitted
fix(relay): tighten channel label docs and tests from review
- NIP-CL and NOSTR.md: the cap is 8 distinct labels, and repeated ["t", ""] tags clear the set like one. This is what the relay does. - 0061 migration comment: labels are on kinds 39000-39002, not 39003. - members_cannot_change_labels: assert the join was accepted, so the refusal must come from the label rule. Add admins_can_change_labels. - Test that every ChannelType name is in RESERVED_CHANNEL_LABELS. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: Logan Johnson <[email protected]>
1 parent ac0bd78 commit 853e544

5 files changed

Lines changed: 96 additions & 18 deletions

File tree

‎NOSTR.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,13 @@ edit, so use the tag to find the older of two channels.
128128

129129
**Channel labels.** A label groups channels across channel types, for
130130
example `["t", "workspace"]`. Set labels with `["t", <label>]` tags on
131-
kind:9007. On kind:9002 the `t` tags replace the whole set, and a lone
132-
`["t", ""]` clears it; a kind:9002 without `t` tags leaves the labels as
133-
they are. Only an owner or admin may change labels. The relay refuses a
134-
label set with more than 8 labels, a label that is not 1-64 characters
135-
from `a-z`, `0-9`, `.`, `:` and `-`, or a label equal to a channel type
136-
name (`stream`, `forum`, `dm`, `workflow`, `system`).
131+
kind:9007. On kind:9002 the `t` tags replace the whole set, and
132+
`["t", ""]` with no other `t` tag clears it; a kind:9002 without `t` tags
133+
leaves the labels as they are. Only an owner or admin may change labels.
134+
The relay refuses a label set with more than 8 distinct labels, a label
135+
that is not 1-64 characters from `a-z`, `0-9`, `.`, `:` and `-`, or a
136+
label equal to a channel type name (`stream`, `forum`, `dm`, `workflow`,
137+
`system`).
137138

138139
Events are stored **channel-scoped** so access control applies — private channel member lists are
139140
only visible to members. Discover groups via historical REQ:

‎crates/buzz-core/src/channel.rs‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ pub enum ChannelLabelError {
4848
/// Validate the values of a channel's `["t", <label>]` tags and return the
4949
/// label set to store, in first-seen order without duplicates.
5050
///
51-
/// A single `""` means "no labels" and returns an empty set; kind:9002 uses
52-
/// it to clear the labels, the same convention as `["ttl", ""]`.
51+
/// `""` alone (one or more times) means "no labels" and returns an empty
52+
/// set; kind:9002 uses it to clear the labels, the same convention as
53+
/// `["ttl", ""]`. Repeated labels are stored once and count once toward
54+
/// [`MAX_CHANNEL_LABELS`].
5355
pub fn parse_channel_labels<'a>(
5456
values: impl IntoIterator<Item = &'a str>,
5557
) -> Result<Vec<String>, ChannelLabelError> {
@@ -297,9 +299,36 @@ impl FromStr for MemberRole {
297299
mod tests {
298300
use super::{
299301
canonical_channel_name, channel_type_from_group_state_tags, group_state_identity_tags,
300-
parse_channel_labels, ChannelLabelError, MAX_CHANNEL_LABELS,
302+
parse_channel_labels, ChannelLabelError, ChannelType, MAX_CHANNEL_LABELS,
303+
RESERVED_CHANNEL_LABELS,
301304
};
302305

306+
/// Every channel type name is reserved, so a label cannot pose as a type.
307+
/// The `match` has no wildcard: a new `ChannelType` variant does not
308+
/// compile until it is added to `ALL` here, and then this test fails
309+
/// until its name is in `RESERVED_CHANNEL_LABELS`.
310+
#[test]
311+
fn every_channel_type_name_is_a_reserved_label() {
312+
const ALL: [ChannelType; 4] = [
313+
ChannelType::Stream,
314+
ChannelType::Forum,
315+
ChannelType::Dm,
316+
ChannelType::Workflow,
317+
];
318+
for channel_type in ALL {
319+
match channel_type {
320+
ChannelType::Stream
321+
| ChannelType::Forum
322+
| ChannelType::Dm
323+
| ChannelType::Workflow => {}
324+
}
325+
assert!(
326+
RESERVED_CHANNEL_LABELS.contains(&channel_type.as_str()),
327+
"{channel_type:?} is not a reserved label"
328+
);
329+
}
330+
}
331+
303332
#[test]
304333
fn group_state_identity_tags_put_the_type_first_then_labels_then_the_creator() {
305334
let creator = [0xab; 32];
@@ -363,11 +392,22 @@ mod tests {
363392
.map(|labels| labels.len()),
364393
Ok(MAX_CHANNEL_LABELS)
365394
);
395+
// Nine tags with one repeat are 8 distinct labels.
396+
let mut repeated: Vec<&str> = names[..MAX_CHANNEL_LABELS]
397+
.iter()
398+
.map(String::as_str)
399+
.collect();
400+
repeated.push(repeated[0]);
401+
assert_eq!(
402+
parse_channel_labels(repeated).map(|labels| labels.len()),
403+
Ok(MAX_CHANNEL_LABELS)
404+
);
366405
}
367406

368407
#[test]
369-
fn a_lone_empty_label_clears_and_cannot_mix_with_labels() {
408+
fn empty_labels_clear_and_cannot_mix_with_labels() {
370409
assert_eq!(parse_channel_labels([""]), Ok(Vec::new()));
410+
assert_eq!(parse_channel_labels(["", ""]), Ok(Vec::new()));
371411
assert_eq!(
372412
parse_channel_labels(["", "workspace"]),
373413
Err(ChannelLabelError::ClearWithOthers)

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,10 @@ async fn invalid_labels_are_refused() {
425425
}
426426

427427
/// Only an owner or admin may change labels; a plain member may not.
428-
/// Mutation: drop `t` from the privileged tag list → RED.
428+
/// The join must be accepted, so the refusal comes from the label rule and
429+
/// not from "not a member".
430+
/// Mutation: drop `t` from the privileged tag list → RED (the member's
431+
/// edit is accepted).
429432
#[tokio::test]
430433
#[ignore = "requires Postgres"]
431434
async fn members_cannot_change_labels() {
@@ -438,9 +441,10 @@ async fn members_cannot_change_labels() {
438441
.tags([Tag::parse(["h", &channel_id.to_string()]).unwrap()])
439442
.sign_with_keys(&member)
440443
.unwrap();
441-
ingest_event(&state, &tenant, join, http_auth(&member))
444+
let joined = ingest_event(&state, &tenant, join, http_auth(&member))
442445
.await
443446
.expect("member joins");
447+
assert!(joined.accepted, "join refused: {}", joined.message);
444448

445449
assert!(
446450
edit_channel(&state, &tenant, &member, channel_id, &[&["t", "mine"]])
@@ -453,3 +457,35 @@ async fn members_cannot_change_labels() {
453457
vec!["stream", "keep"]
454458
);
455459
}
460+
461+
/// An admin who is not the owner may change labels.
462+
/// Mutation: allow only the owner role to set labels → RED.
463+
#[tokio::test]
464+
#[ignore = "requires Postgres"]
465+
async fn admins_can_change_labels() {
466+
let state = state().await;
467+
let tenant = community(&state, "group-state-label-admin").await;
468+
let owner = Keys::generate();
469+
let admin = Keys::generate();
470+
let channel_id = create_channel(&state, &tenant, &owner, &[&["t", "keep"]]).await;
471+
let grant = EventBuilder::new(Kind::Custom(9000), "")
472+
.tags([
473+
Tag::parse(["h", &channel_id.to_string()]).unwrap(),
474+
Tag::parse(["p", &admin.public_key().to_hex()]).unwrap(),
475+
Tag::parse(["role", "admin"]).unwrap(),
476+
])
477+
.sign_with_keys(&owner)
478+
.unwrap();
479+
let granted = ingest_event(&state, &tenant, grant, http_auth(&owner))
480+
.await
481+
.expect("owner adds admin");
482+
assert!(granted.accepted, "admin grant refused: {}", granted.message);
483+
484+
edit_channel(&state, &tenant, &admin, channel_id, &[&["t", "theirs"]])
485+
.await
486+
.expect("an admin may set labels");
487+
assert_eq!(
488+
t_values(&state, &tenant, channel_id, 39000).await,
489+
vec!["stream", "theirs"]
490+
);
491+
}

‎docs/nips/NIP-CL.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ mean that a key did not create the channel.
8888
A label groups channels across channel types, for example `workspace` or
8989
`agent-config`. Labels have no meaning to the relay.
9090

91-
**Set labels.** A kind:9007 (create group) MAY carry up to 8 `["t", <label>]`
92-
tags. On kind:9002 (edit metadata):
91+
**Set labels.** A kind:9007 (create group) MAY carry up to 8 distinct
92+
labels as `["t", <label>]` tags. On kind:9002 (edit metadata):
9393

9494
- `t` tags replace the whole label set;
95-
- one `["t", ""]` tag and no other `t` tag clears the set;
95+
- `["t", ""]` tags and no other `t` tag clear the set;
9696
- no `t` tag leaves the labels as they are.
9797

9898
Only an owner or admin of the channel MAY change its labels.
@@ -101,8 +101,9 @@ Only an owner or admin of the channel MAY change its labels.
101101
label MUST NOT be `stream`, `forum`, `dm`, `workflow` or `system`. These are
102102
channel type names, so this rule stops a label from making a channel look like
103103
another type. The relay MUST reject the whole event if it has more than 8
104-
labels, an invalid label, a reserved name, or `["t", ""]` together with other
105-
labels. The relay stores repeated labels once.
104+
distinct labels, an invalid label, a reserved name, or `["t", ""]` together
105+
with other labels. The relay stores repeated labels once, so a repeat does
106+
not count toward the 8, and repeated `["t", ""]` tags clear the set like one.
106107

107108
## Finding channels
108109

‎migrations/0061_channel_labels.sql‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- Channel labels: creator- or admin-chosen tags that group channels across
22
-- channel types. The relay publishes each label as a ["t", <label>] tag on
3-
-- the channel's group-state events (kinds 39000-39003), after the channel
3+
-- the channel's group-state events (kinds 39000-39002), after the channel
44
-- type tag. kind:9007 sets them and kind:9002 replaces them; the relay
55
-- validates their shape (1-64 of a-z 0-9 . : -, not a channel type name).
66
--

0 commit comments

Comments
 (0)