mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-28 21:50:26 +08:00
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 <lorenz.leutgeb@radicle.dev>
This commit is contained in:
committed by
Lorenz Leutgeb
co-authored by
Lorenz Leutgeb
parent
6ba619f9a6
commit
d4acccf614
@@ -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::<RepoId>(3..=3);
|
||||
|
||||
for rid in &rids {
|
||||
let mut repo = r#gen::<MockRepository>(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::<MockRepository>(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() {
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user