diff --git a/bindings/c/bakashrot.h b/bindings/c/bakashrot.h index 3d79cf3..e2524c1 100644 --- a/bindings/c/bakashrot.h +++ b/bindings/c/bakashrot.h @@ -227,13 +227,14 @@ ptrdiff_t BakaShrot_GetMessageNodeW(uint16_t *buffer, ptrdiff_t size); /** * Lists the topics with messages that this client may push to or read - * (every one if it may query): how many, -1 on failure. Then - * `BakaShrot_GetNamesList()`. + * (every one if its roles have `list_topics` or `query`): how many, -1 on + * failure. Then `BakaShrot_GetNamesList()`. */ int32_t BakaShrot_ListTopics(void); /** - * Lists every node with messages: how many, -1 on failure. Then + * Lists the nodes with messages in the topics this client may read (every + * one if its roles have `list_nodes` or `query`): how many, -1 on failure. Then * `BakaShrot_GetNamesList()`. */ int32_t BakaShrot_ListNodes(void); diff --git a/bindings/python/bakashrot.py b/bindings/python/bakashrot.py index 42e3b9b..be5d57e 100644 --- a/bindings/python/bakashrot.py +++ b/bindings/python/bakashrot.py @@ -542,11 +542,11 @@ class Client: time.sleep(interval) def list_topics(self) -> NamesList: - """The topics with messages that the client may push to or read (every one if it may query).""" + """The topics with messages that the client may push to or read (every one with list_topics or query).""" return self._list(self._dll.ListTopics, "List topics") def list_nodes(self) -> NamesList: - """Every node with messages.""" + """The nodes with messages in the topics the client may read (every one with list_nodes or query).""" return self._list(self._dll.ListNodes, "List nodes") def _list(self, function, what: str) -> NamesList: diff --git a/crates/bakashrot-capi/src/lib.rs b/crates/bakashrot-capi/src/lib.rs index 400cd6a..88055c4 100644 --- a/crates/bakashrot-capi/src/lib.rs +++ b/crates/bakashrot-capi/src/lib.rs @@ -669,14 +669,15 @@ fn list(kind: NameKind) -> i32 { } /// Lists the topics with messages that this client may push to or read -/// (every one if it may query): how many, -1 on failure. Then -/// `BakaShrot_GetNamesList()`. +/// (every one if its roles have `list_topics` or `query`): how many, -1 on +/// failure. Then `BakaShrot_GetNamesList()`. #[unsafe(no_mangle)] pub extern "C" fn BakaShrot_ListTopics() -> i32 { list(NameKind::Topic) } -/// Lists every node with messages: how many, -1 on failure. Then +/// Lists the nodes with messages in the topics this client may read (every +/// one if its roles have `list_nodes` or `query`): how many, -1 on failure. Then /// `BakaShrot_GetNamesList()`. #[unsafe(no_mangle)] pub extern "C" fn BakaShrot_ListNodes() -> i32 { diff --git a/crates/bakashrot-client/src/lib.rs b/crates/bakashrot-client/src/lib.rs index 3e48f58..f570947 100644 --- a/crates/bakashrot-client/src/lib.rs +++ b/crates/bakashrot-client/src/lib.rs @@ -413,9 +413,10 @@ impl Client { } } - /// The topics with messages that this client may push to or read - /// (every one if it may query), or every node with messages. - /// `truncated` if some didn't fit. + /// The topics with messages that this client may push to or read, or + /// the nodes with messages in the topics it may read; every one if its + /// roles have `list_topics` / `list_nodes`, or `query`. `truncated` if + /// some didn't fit. pub fn list_names(&mut self, kind: NameKind) -> Result { match self.request(ListNames { kind }.into())? { Packet::NamesList(names) => Ok(names), diff --git a/crates/bakashrot-config/example.toml b/crates/bakashrot-config/example.toml index 52eb46e..c22f07a 100644 --- a/crates/bakashrot-config/example.toml +++ b/crates/bakashrot-config/example.toml @@ -56,6 +56,10 @@ push_topics = ["sensors.*"] # always as its own node (its name) [roles.dashboard] read_topics = ["*"] query = true # read-only SQL, reads every topic +# Listing answers the topics it may push to or read, and the nodes of the +# topics it may read. These answer every one (query does too): +# list_topics = true +# list_nodes = true # Peers are roles too: what's replicated with a server whose certificate # has this role, whichever end dials. diff --git a/crates/bakashrot-config/src/roles.rs b/crates/bakashrot-config/src/roles.rs index e1a67c0..7ae812f 100644 --- a/crates/bakashrot-config/src/roles.rs +++ b/crates/bakashrot-config/src/roles.rs @@ -61,6 +61,14 @@ pub struct Role { /// Whether it may run read-only SQL, which reads every topic. #[serde(default)] pub query: bool, + /// Whether listing topics answers every topic with messages, not only + /// those it may push to or read. + #[serde(default)] + pub list_topics: bool, + /// Whether listing nodes answers every node with messages, not only + /// those of the topics it may read. + #[serde(default)] + pub list_nodes: bool, /// Replication: the topics sent to it. #[serde(default)] pub send_topics: NameList, @@ -109,6 +117,17 @@ impl Permissions { self.roles.iter().any(|role| role.query) } + /// Whether listing topics answers every one: `list_topics`, or + /// `query` (which reads them all anyway). + pub fn lists_every_topic(&self) -> bool { + self.roles.iter().any(|role| role.list_topics || role.query) + } + + /// Whether listing nodes answers every one: `list_nodes`, or `query`. + pub fn lists_every_node(&self) -> bool { + self.roles.iter().any(|role| role.list_nodes || role.query) + } + /// Whether `node`'s messages in `topic` are sent to it. pub fn sends(&self, topic: &str, node: &str) -> bool { self.roles diff --git a/crates/bakashrot-config/tests/config.rs b/crates/bakashrot-config/tests/config.rs index e24ce12..5b48ee8 100644 --- a/crates/bakashrot-config/tests/config.rs +++ b/crates/bakashrot-config/tests/config.rs @@ -164,7 +164,19 @@ accept_nodes = ["garage"] assert!(both.can_read("Logs")); assert!(!both.can_read("sensors.garage")); assert!(!both.can_query()); - assert!(config.permissions(&["admin"]).unwrap().can_query()); + assert!(!both.lists_every_topic() && !both.lists_every_node()); + let admin = config.permissions(&["admin"]).unwrap(); + assert!(admin.can_query()); + // A query reads them all anyway. + assert!(admin.lists_every_topic() && admin.lists_every_node()); + let listing = Config::parse(&with( + "[roles.a]\nlist_topics = true\n[roles.b]\nlist_nodes = true\n", + )) + .unwrap(); + let a = listing.permissions(&["a"]).unwrap(); + assert!(a.lists_every_topic() && !a.lists_every_node() && !a.can_query()); + let b = listing.permissions(&["b", "a"]).unwrap(); + assert!(b.lists_every_topic() && b.lists_every_node()); // Replication: each role's topics with that role's nodes, not mixed. let peer = config.permissions(&["peer", "backup"]).unwrap(); diff --git a/crates/bakashrot-server/src/connection.rs b/crates/bakashrot-server/src/connection.rs index 9fe512d..0f858c0 100644 --- a/crates/bakashrot-server/src/connection.rs +++ b/crates/bakashrot-server/src/connection.rs @@ -473,20 +473,41 @@ async fn get_messages(shared: &Arc, peer: &Peer, get: GetMessages) -> Pa } /// Topics: those with messages (pushed here or replicated) that it may -/// push to or read, every one if it may query (queries read everything). -/// Nodes: every one with messages. +/// push to or read; every one with `list_topics` or `query`. Nodes: those +/// with messages in the topics it may read; every one with `list_nodes` +/// or `query`. async fn list_names(shared: &Arc, peer: &Peer, list: ListNames) -> Packet { + let permissions = &peer.permissions; let found = match list.kind { - NameKind::Topic => shared.read(|reader| reader.topics()).await, - NameKind::Node => shared.read(|reader| reader.nodes()).await, + NameKind::Topic => shared + .read(|reader| reader.topics()) + .await + .map(|mut topics| { + if !permissions.lists_every_topic() { + topics + .retain(|topic| permissions.can_push(topic) || permissions.can_read(topic)); + } + topics + }), + NameKind::Node if permissions.lists_every_node() => { + shared.read(|reader| reader.nodes()).await + } + NameKind::Node => shared.read(|reader| reader.heads()).await.map(|heads| { + let mut nodes: Vec = heads + .into_iter() + .filter(|head| permissions.can_read(&head.topic)) + .map(|head| head.node) + .collect(); + // As `reader.nodes()` answers them: in order, none twice. + nodes.sort_by_cached_key(|node| node.to_ascii_lowercase()); + nodes.dedup_by(|a, b| names_equal(a, b)); + nodes + }), }; let mut names = match found { Ok(names) => names, Err(refused) => return refused.into(), }; - if list.kind == NameKind::Topic && !peer.permissions.can_query() { - names.retain(|topic| peer.permissions.can_push(topic) || peer.permissions.can_read(topic)); - } let truncated = names.len() > MAX_NAMES_LIST_ENTRIES; names.truncate(MAX_NAMES_LIST_ENTRIES); NamesList { diff --git a/crates/bakashrot-server/tests/end_to_end.rs b/crates/bakashrot-server/tests/end_to_end.rs index c087585..5c55c7f 100644 --- a/crates/bakashrot-server/tests/end_to_end.rs +++ b/crates/bakashrot-server/tests/end_to_end.rs @@ -41,6 +41,10 @@ read_topics = ["logs"] [roles.analyst] query = true +[roles.explorer] +list_topics = true +list_nodes = true + [roles.peer] send_topics = ["*"] accept_topics = ["*"] @@ -604,8 +608,24 @@ fn names() { assert_eq!(names(&mut sensor, NameKind::Topic), ["logs", "sensors"]); assert_eq!(names(&mut screen, NameKind::Topic), ["logs"]); assert_eq!(names(&mut analyst, NameKind::Topic), ["logs", "sensors"]); - // Nodes: every one with messages. - assert_eq!(names(&mut screen, NameKind::Node), ["garage", "kitchen"]); + // Nodes: those of the topics it may read; every one for a query role. + assert!(names(&mut sensor, NameKind::Node).is_empty()); + assert_eq!(names(&mut screen, NameKind::Node), ["kitchen"]); + assert_eq!(names(&mut analyst, NameKind::Node), ["garage", "kitchen"]); + // `list_topics` and `list_nodes`: every one, without reading any. + let mut explorer = running + .connect(&running.issue("browser", &["explorer"])) + .unwrap(); + assert_eq!(names(&mut explorer, NameKind::Topic), ["logs", "sensors"]); + assert_eq!(names(&mut explorer, NameKind::Node), ["garage", "kitchen"]); + assert_eq!( + code( + explorer + .get_messages("logs", "kitchen", None, 1) + .unwrap_err() + ), + ErrorCode::NotAllowed.code() + ); } /// The query parts of `OldSource/Tests/BigMessages-Test.ps1`. diff --git a/crates/bakashrot-tools/src/bin/bakashrot.rs b/crates/bakashrot-tools/src/bin/bakashrot.rs index a068719..5f9f0d1 100644 --- a/crates/bakashrot-tools/src/bin/bakashrot.rs +++ b/crates/bakashrot-tools/src/bin/bakashrot.rs @@ -101,8 +101,8 @@ enum Command { /// standard input's. sql: String, }, - /// Lists the topics with messages that this client may use (every one - /// if it may query) and the nodes with messages. + /// Lists the topics this client may use and the nodes of those it may + /// read; every one if its roles have list_topics / list_nodes, or query. List { /// What to list. #[arg(value_enum, default_value_t = What::All)] diff --git a/docs/config.md b/docs/config.md index 1af6970..2227c28 100644 --- a/docs/config.md +++ b/docs/config.md @@ -37,11 +37,17 @@ What a role gives: | `push_topics` | none | pushing to these topics, always as its own node | | `read_topics` | none | reading these topics' messages | | `query` | `false` | read-only SQL, which reads every topic | +| `list_topics` | `false` | listing topics answers every one with messages, not only those it may push to or read | +| `list_nodes` | `false` | listing nodes answers every one with messages, not only those of the topics it may read | | `send_topics` | none | replication: these topics sent to it | | `accept_topics` | none | replication: these topics taken from it | | `send_nodes` | `["*"]` | replication: only these nodes' messages sent | | `accept_nodes` | `["*"]` | replication: only these nodes' messages taken | +`list_topics` and `list_nodes` (2026-11-16) only give names, for a tool +that browses a server: reading a topic is still `read_topics`. `query` +gives both, it reads everything anyway. + No `AllowRead` / `AllowWrite`: a list that's empty allows nothing. No default topic: a push always names its topic (protocol 7 has none). diff --git a/docs/known-issues.md b/docs/known-issues.md index ba9efc0..307ef9c 100644 --- a/docs/known-issues.md +++ b/docs/known-issues.md @@ -120,11 +120,11 @@ Reproduce one before fixing it, and move it up (or drop it) once known. taken from anyone. Listing topics also only shows those with messages: a client can't ask what it may push to. Fine for the threat model; names in `push_topics` where the set is closed. -- **Every client sees every node's name.** `ListNames` for nodes - answers every node with messages (`list_names`, `reader.nodes()`), - whatever the client's roles read, to push-only clients too. Topics - are filtered by the roles. Fix: only the nodes of the topics it may read, or none - without `read_topics` or `query`. +- **Whether a node exists can still be probed.** Listing nodes is + filtered by the roles since 2026-11-16 (`list_nodes`), but + `GetMessages` answers `UnknownName` for a node without messages + anywhere and none for one with messages in another topic + (`reader.rs`, `messages`). Fix: none in both cases. - **A CRL file that isn't there.** The server takes it as nothing revoked (`load_revocations`, `bakashrot-server/src/lib.rs`), the `bakashrot` tool refuses to start when `client.toml`'s `crl` names a diff --git a/docs/server.md b/docs/server.md index 9e45658..cba1b50 100644 --- a/docs/server.md +++ b/docs/server.md @@ -201,8 +201,8 @@ the same ids with different messages, undetected. (`MESSAGES_BYTES`, a bigger message alone), `more` if others follow. - **`ListNames`:** topics are those with messages (pushed here or replicated), those the roles may push to or read (every one with - `query`, which reads them all anyway); nodes are every one with - messages. `truncated` past 65,536. + `list_topics` or `query`); nodes are those with messages in the + topics the roles may read (every one with `list_nodes` or `query`). `truncated` past 65,536. - **`Query`:** the roles must allow it (`NotAllowed`); read-only SQL, refused with SQLite's message (`QueryFailed`, with a hint to add `is_json = 1` on "malformed JSON"), over 10,000 rows or 16 MB diff --git a/revamp.md b/revamp.md index 8444ab7..f0882ec 100644 --- a/revamp.md +++ b/revamp.md @@ -168,7 +168,13 @@ Also decided (2026-11-13): peer is read like any other, and a push to a topic nobody names is `NotAllowed` (`UnknownName` is only for nodes now). Before: `topics` since 2026-11-13, `replicated_topics` to filter on edge nodes (now - their peers' `send_topics`). + their peers' `send_topics`); + - **`list_topics` and `list_nodes`** (2026-11-16), two booleans of a + role, for a tool that browses a server: listing answers every topic, + every node with messages. Without them (or `query`, which gives + both): the topics it may push to or read, the nodes of the topics + it may read (every node until then). Names only, reading is still + `read_topics`. - **Schema v9, not compatible with v8** (2026-11-13): old databases aren't opened, their data isn't carried over (the real-life tests start empty). `docs/storage.md`: