Repository navigation
feat(plugins): sign manifest-declared relay kinds and read relay info #3725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57ad851
feat(plugins): declare relay event kinds in the manifest
LordMelkor ab1772d
feat(relay): sign and publish a declared kind for a plugin
LordMelkor 9a1e74e
feat(host): host.publish and host.relayInfo for plugins
LordMelkor 9424add
fix(relay): leave old-event acceptance to the relay; 429 is a refusal
LordMelkor ccc8f0a
docs(plugins): say where host.publish checks the scope
LordMelkor 83c2743
fix(plugins): no relay-kind grant may include a private-message or si…
LordMelkor f26a106
Plugin relay kinds: say what each kind is for at install
LordMelkor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| //! `host.relayKinds`: event kinds a plugin may sign as the viewer and publish. | ||
| //! The reserved list is the one place that says which kinds Buzz keeps for its | ||
| //! own signing or as private; admission and every publish both ask it. | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| /// Most kinds one plugin may declare. | ||
| pub const MAX_RELAY_KINDS: usize = 8; | ||
| /// Longest purpose, in UTF-8 bytes. | ||
| const MAX_PURPOSE_BYTES: usize = 120; | ||
|
|
||
| /// One kind a plugin may sign as the viewer, with the plugin's own words for | ||
| /// what those events do. The install screen shows the purpose beside the kind. | ||
| #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] | ||
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | ||
| pub struct RelayKind { | ||
| pub kind: u16, | ||
| pub purpose: String, | ||
| } | ||
|
|
||
| /// Kinds Buzz signs for its own purposes. The sources are | ||
| /// the native writer's set (`nativeWriteKinds`, `WORKFLOW_KINDS`), the | ||
| /// packaged-relay signer's allow-list (`validate_event` in `relay.rs`), the | ||
| /// purpose-bound signers (auth proofs, DM, reminders, read state, sidebar, | ||
| /// channel recipes, Huddles, observer archives, pairing, Mesh status) and the | ||
| /// kinds the credential provider and app agents sign. | ||
| const RESERVED: &[u16] = &[ | ||
| 0, 3, 5, 7, 9, 1064, 1617, 1618, 1619, 1621, 1984, 10000, 10001, 10002, 10003, 10030, 10100, | ||
| 13534, 13535, 20001, 20002, 20003, 22242, 24134, 24200, 24242, 24243, 27235, 28936, 30000, | ||
| 30003, 30023, 30030, 30078, 30174, 30175, 30176, 30177, 30178, 30179, 30300, 30315, 30617, | ||
| 30618, 30620, 30621, 39002, 40100, 41010, 41011, 41012, 42000, 44100, 44200, 45010, 45011, | ||
| 46020, 46030, 46031, | ||
| ]; | ||
| /// Inclusive ranges. 9000-9999 is every channel admin and command kind. | ||
| const RESERVED_RANGES: &[(u16, u16)] = &[ | ||
| (1630, 1633), | ||
| (9000, 9999), | ||
| (40002, 40008), | ||
| (43001, 43006), | ||
| (45001, 45003), | ||
| (48100, 48103), | ||
| ]; | ||
|
|
||
| /// Private messages and remote signing: NIP-04 kind 4, NIP-17 kinds 14 and 15 | ||
| /// and its inbox relay list 10050, NIP-59 seal 13 and gift wrap 1059, and | ||
| /// NIP-46 signer messages 24133. No plugin may send a private message as the | ||
| /// viewer, move where the viewer's private messages arrive, or speak for their | ||
| /// signer. | ||
| const PRIVATE: &[u16] = &[4, 13, 14, 15, 1059, 10050, 24133]; | ||
|
|
||
| /// Whether Buzz keeps `kind` for its own signing or as private, so no grant may | ||
| /// include it. | ||
| pub fn reserved_relay_kind(kind: u16) -> bool { | ||
| RESERVED.contains(&kind) | ||
| || PRIVATE.contains(&kind) | ||
| || RESERVED_RANGES | ||
| .iter() | ||
| .any(|(first, last)| (*first..=*last).contains(&kind)) | ||
| } | ||
|
|
||
| pub(crate) fn validate(kinds: &[RelayKind]) -> Result<(), String> { | ||
| if kinds.len() > MAX_RELAY_KINDS { | ||
| return Err(format!( | ||
| "At most {MAX_RELAY_KINDS} relay kinds may be declared" | ||
| )); | ||
| } | ||
| let mut seen = std::collections::HashSet::new(); | ||
| for RelayKind { kind, purpose } in kinds { | ||
| if *kind == 0 || reserved_relay_kind(*kind) || !seen.insert(kind) { | ||
| return Err("Invalid relay kind declaration".into()); | ||
| } | ||
| if purpose.trim().is_empty() | ||
| || purpose.len() > MAX_PURPOSE_BYTES | ||
| || purpose.chars().any(char::is_control) | ||
| { | ||
| return Err(format!( | ||
| "A relay kind's purpose must contain 1–{MAX_PURPOSE_BYTES} bytes of text without control characters" | ||
| )); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn declared(kinds: &[u16]) -> Vec<RelayKind> { | ||
| kinds | ||
| .iter() | ||
| .map(|&kind| RelayKind { | ||
| kind, | ||
| purpose: "Example".into(), | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// The integer literals of the first `[...]` after `marker` in `source`. | ||
| fn literals(source: &str, marker: &str) -> Vec<u16> { | ||
| let list = &source[source.find(marker).unwrap()..]; | ||
| let list = &list[list.find('[').unwrap() + 1..list.find(']').unwrap()]; | ||
| list.split(',') | ||
| .filter_map(|item| item.trim().parse().ok()) | ||
| .collect() | ||
| } | ||
|
|
||
| #[test] | ||
| fn every_kind_the_native_writer_signs_is_reserved() { | ||
| let native = include_str!("../../../src/features/relay/native.ts"); | ||
| let workflows = include_str!("../../../src/features/workflows/protocol.ts"); | ||
| let kinds: Vec<_> = literals(native, "nativeWriteKinds = [") | ||
| .into_iter() | ||
| .chain(literals(workflows, "WORKFLOW_KINDS = [")) | ||
| .collect(); | ||
| assert!(kinds.len() > 15, "the TypeScript lists were not found"); | ||
| for kind in kinds { | ||
| assert!(reserved_relay_kind(kind), "{kind} is not reserved"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn purpose_bound_kinds_are_reserved_and_example_kinds_are_not() { | ||
| for kind in [ | ||
| 0, 5, 7, 1984, 9000, 9007, 9999, 22242, 24242, 27235, 30078, 30300, 41010, 41012, | ||
| 45010, 48103, | ||
| ] { | ||
| assert!(reserved_relay_kind(kind), "{kind}"); | ||
| } | ||
| for kind in [1, 47000, 47001, 65535] { | ||
| assert!(!reserved_relay_kind(kind), "{kind}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_grant_may_include_a_private_message_or_signer_kind() { | ||
| for kind in [4, 13, 14, 15, 1059, 10050, 24133] { | ||
| assert!(validate(&declared(&[47000, kind])).is_err(), "{kind}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_declaration_has_at_most_eight_distinct_unreserved_kinds() { | ||
| assert!(validate(&[]).is_ok()); | ||
| assert!(validate(&declared(&[47000, 47001])).is_ok()); | ||
| assert!(validate(&declared(&[ | ||
| 47000, 47001, 47002, 47003, 47004, 47005, 47006, 47007 | ||
| ])) | ||
| .is_ok()); | ||
| assert!(validate(&declared(&[ | ||
| 47000, 47001, 47002, 47003, 47004, 47005, 47006, 47007, 47008 | ||
| ])) | ||
| .is_err()); | ||
| assert!(validate(&declared(&[47000, 47000])).is_err()); | ||
| assert!(validate(&declared(&[0])).is_err()); | ||
| assert!(validate(&declared(&[47000, 9000])).is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn each_kind_says_in_one_line_of_text_what_it_is_for() { | ||
| let with = |purpose: &str| { | ||
| validate(&[RelayKind { | ||
| kind: 47000, | ||
| purpose: purpose.into(), | ||
| }]) | ||
| }; | ||
| assert!(with("Create or edit a group").is_ok()); | ||
| assert!(with(&"a".repeat(MAX_PURPOSE_BYTES)).is_ok()); | ||
| let long = "a".repeat(MAX_PURPOSE_BYTES + 1); | ||
| for purpose in ["", " ", "Two\nlines", "A\ttab", long.as_str()] { | ||
| assert!(with(purpose).is_err(), "{purpose:?}"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 P2: this list is a denylist, so any kind not named here can be granted. Kinds 4, 13, 14, 1059, 10050 and 24133 are not reserved. The drift test only checks
nativeWriteKindsandWORKFLOW_KINDS. Please reserve these, and consider a test that fails when a new Buzz signing path uses a kind that is not listed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 I've kept the list to the kinds Buzz signs itself, as the docs state, so these aren't added. Buzz signs none of 4, 13, 14, 1059, 10050 or 24133. I searched
src,src-tauri/src,cratesandruntimefor the numbers, the nostrKind::names and the nip04 and nip59 helpers. The only one Buzz looks at is kind 4: the agent runtime treats a kind 4 that p-tags an agent as addressed to it, and starts nothing from it (src/features/agents/runner.ts:2265-2271).The list exists so that a plugin can't write a Buzz feature's own records. It isn't a policy on which standard kinds plugins may build on. Each kind still shows at install and needs the owner's approval, and the relay decides what it accepts.
The drift-test limit is real: the test compares only the two TypeScript lists. A new Buzz signing path that uses a new kind has to add that kind to this list by hand. A test that found every signing path would need an inventory of every signer call, so I've left it out of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 That makes sense: the list protects Buzz's own records, not a policy on standard kinds, and Buzz signs none of these. I'll leave it as a maintainer call. The one thing I'd still flag for them is that a grant for 4, 14 or 1059 lets a plugin send private messages as the owner, and the install row reads the same as for any other kind.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 Update: these are now reserved, in
83c2743725. You and a second review both flagged it, and the one plugin built on this API doesn't use them. The reserved list also holds 4, 13, 14, 15, 1059, 10050 and 24133 (private messages, the inbox relay list and NIP-46 signer messages). Install and every publish check it, and a test covers each kind. Other standard kinds stay grantable. The drift-test limit from my earlier reply still stands.