From d4acccf6140ee9c474c9f85fdbf235cca9b07ea6 Mon Sep 17 00:00:00 2001 From: Daniel Norman Date: Thu, 30 Jul 2026 12:24:19 +0200 Subject: [PATCH] protocol/service: Check visibility on `Subscribe` When `Service::handle_message` handles `Message::Subscribe`, constraints regarding visibility of repositories are not considered, this leads to leaking RIDs of private repositories. Add a corresponding check. Add a test to verify that reference announcements relayed via subscribe correctly apply visibility filtering, there by ensuring references of private repositories are only propagated to nodes on the allow list. The test regarding deduplication needs a fix: Ensure that all mock repositories are public and in storage so that they do not get filtered out in case of unknown visiblity. Co-authored-by: Lorenz Leutgeb --- crates/radicle-node/src/tests.rs | 74 +++++++++++++++++++++++++- crates/radicle-protocol/src/service.rs | 15 ++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/crates/radicle-node/src/tests.rs b/crates/radicle-node/src/tests.rs index 60dd40eb7..d6253acca 100644 --- a/crates/radicle-node/src/tests.rs +++ b/crates/radicle-node/src/tests.rs @@ -482,13 +482,23 @@ fn announcement_rebroadcast() { } #[test] -fn announcement_rebroadcast_duplicates() { +fn announcement_rebroadcast_deduplicates() { let mut cid = Peer::cid(); let mut amy = Peer::amy(); let bob = Peer::bob(); let dan = Peer::dan(); let rids = arbitrary::set::(3..=3); + for rid in &rids { + let mut repo = r#gen::(1); + repo.doc.doc = repo + .doc + .doc + .with_edits(|doc| doc.visibility = Visibility::Public) + .unwrap(); + amy.storage_mut().repos.insert(*rid, repo); + } + amy.connect_to(&bob); amy.receive(*bob.nid(), cid.node_announcement()); @@ -803,6 +813,68 @@ fn refs_announcement_relay_private() { ); } +#[test] +fn refs_announcement_subscribe_private() { + let tmp = tempfile::tempdir().unwrap(); + let mut amy = Peer::amy(); + let cid = Peer::with_storage( + "cid", + CID, + Storage::open(tmp.path().join("cid"), fixtures::user()).unwrap(), + ); + + let bob = { + let signer = SigningKey::mock(BOB as usize); + let storage = fixtures::storage(tmp.path().join("bob"), &signer).unwrap(); + + Peer::with_storage("bob", BOB, storage) + }; + + // Amy holds the repo, and it isn't visible to Cid. + let repo = { + let mut repo = r#gen::(1); + repo.doc.doc = repo + .doc + .doc + .with_edits(|doc| { + doc.visibility = Visibility::Private { + allow: [(*amy.nid()).into(), (*bob.nid()).into()].into(), + }; + }) + .unwrap(); + repo + }; + let private = repo.id; + + amy.seed(&repo.id, policy::Scope::All).unwrap(); + amy.storage_mut().repos.insert(private, repo); + + amy.connect_to(&bob); + amy.connect_to(&cid); + + // Amy stores Bob's announcement *before* Cid subscribes. + amy.receive(*bob.nid(), bob.refs_announcement(private)); + amy.messages(*cid.nid()).for_each(drop); + + // Cid subscribes with the default filter, which matches every repo. + amy.receive(*cid.nid(), Message::Subscribe(Subscribe::all())); + + let private_refs_announcement = amy.messages(*cid.nid()).find(|m| { + matches!( + m, + Message::Announcement(Announcement { + message: AnnouncementMessage::Refs(RefsAnnouncement { rid, .. }), + .. + }) if *rid == private + ) + }); + assert_matches!( + private_refs_announcement, + None, + "The refs announcement for private repo {private} is not sent to Cid" + ); +} + /// Even if Amy is not tracking Bob, Amy will fetch Bob's refs for a repo she doesn't have. #[test] fn refs_announcement_fetch_trusted_no_inventory() { diff --git a/crates/radicle-protocol/src/service.rs b/crates/radicle-protocol/src/service.rs index 75a56badb..be4954b5e 100644 --- a/crates/radicle-protocol/src/service.rs +++ b/crates/radicle-protocol/src/service.rs @@ -1840,6 +1840,21 @@ where if ann.node == *remote { continue; } + // Only send a `RefsAnnouncement` for repositories that are visible to + // the peer. If we do not have the repository, we can not know whether + // it should be visible to the peer or not, so we default to `false`. + if let AnnouncementMessage::Refs(RefsAnnouncement { rid, .. }) = + &ann.message + && !self + .storage + .get(*rid) + .ok() + .flatten() + .map(|doc| doc.is_visible_to(&(*remote).into())) + .unwrap_or(false) + { + continue; + } // Only send messages if we're a relay, or it's our own messages. if relay || ann.node == local { self.outbox.write(peer, ann.into());