Skip to content

Commit

Permalink
fix(Swarm.RegisterPeer): check policy
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 21, 2019
1 parent a493add commit 828f328
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Routing/Dht1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ public DhtMessage ProcessAddProvider(Peer remotePeer, DhtMessage request, DhtMes
.Select(p => p.TryToPeer(out Peer peer) ? peer : (Peer)null)
.Where(p => p != null)
.Where(p => p == remotePeer)
.Where(p => p.Addresses.Count() > 0);
.Where(p => p.Addresses.Count() > 0)
.Where(p => Swarm.IsAllowed(p));
foreach (var provider in providers)
{
Swarm.RegisterPeer(provider);
Expand Down
4 changes: 2 additions & 2 deletions src/Routing/DistributedQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void ProcessProviders(DhtPeerMessage[] providers)
{
if (provider.TryToPeer(out Peer p))
{
if (p == Dht.Swarm.LocalPeer)
if (p == Dht.Swarm.LocalPeer || !Dht.Swarm.IsAllowed(p))
continue;

p = Dht.Swarm.RegisterPeer(p);
Expand All @@ -236,7 +236,7 @@ void ProcessCloserPeers(DhtPeerMessage[] closerPeers)
{
if (closer.TryToPeer(out Peer p))
{
if (p == Dht.Swarm.LocalPeer)
if (p == Dht.Swarm.LocalPeer || !Dht.Swarm.IsAllowed(p))
continue;

p = Dht.Swarm.RegisterPeer(p);
Expand Down
15 changes: 14 additions & 1 deletion src/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace PeerTalk
/// <summary>
/// Manages communication with other peers.
/// </summary>
public class Swarm : IService, IPolicy<MultiAddress>
public class Swarm : IService, IPolicy<MultiAddress>, IPolicy<Peer>
{
static ILog log = LogManager.GetLogger(typeof(Swarm));

Expand Down Expand Up @@ -276,6 +276,9 @@ public IEnumerable<Peer> KnownPeers
/// is raised.
/// </para>
/// </remarks>
/// <exception cref="Exception">
/// The <see cref="BlackList"/> or <see cref="WhiteList"/> policies forbid it.
/// </exception>
public Peer RegisterPeer(Peer peer)
{
if (peer.Id == null)
Expand All @@ -286,6 +289,10 @@ public Peer RegisterPeer(Peer peer)
{
throw new ArgumentException("Cannot register self.");
}
if (!IsAllowed(peer))
{
throw new Exception($"Communication with '{peer}' is not allowed.");
}

var isNew = false;
var p = otherPeers.AddOrUpdate(peer.Id.ToBase58(),
Expand Down Expand Up @@ -1053,5 +1060,11 @@ public bool IsAllowed(MultiAddress target)
&& WhiteList.IsAllowed(target);
}

/// <inheritdoc />
public bool IsAllowed(Peer peer)
{
return peer.Addresses.All(a => IsAllowed(a));
}

}
}
4 changes: 2 additions & 2 deletions test/PeerManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public void BlackListsThePeer()
Assert.AreEqual(0, manager.DeadPeers.Count);

manager.SetNotReachable(peer);
Assert.IsFalse(manager.Swarm.IsAllowed("/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb"));
Assert.IsFalse(manager.Swarm.IsAllowed((MultiAddress)"/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb"));

manager.SetReachable(peer);
Assert.IsTrue(manager.Swarm.IsAllowed("/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb"));
Assert.IsTrue(manager.Swarm.IsAllowed((MultiAddress)"/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb"));
}

[TestMethod]
Expand Down
40 changes: 40 additions & 0 deletions test/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,46 @@ public void DeregisterPeer()
Assert.IsFalse(swarm.KnownPeers.Contains(other));
Assert.AreEqual(other, removedPeer);
}

[TestMethod]
public void IsAllowed_Peer()
{
var swarm = new Swarm();
var peer = new Peer
{
Id = "QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h",
Addresses = new MultiAddress[]
{
"/ip4/127.0.0.1/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"
}
};

Assert.IsTrue(swarm.IsAllowed(peer));

swarm.BlackList.Add(peer.Addresses.First());
Assert.IsFalse(swarm.IsAllowed(peer));

swarm.BlackList.Clear();
swarm.BlackList.Add("/p2p/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h");
Assert.IsFalse(swarm.IsAllowed(peer));
}

[TestMethod]
public void RegisterPeer_BlackListed()
{
var swarm = new Swarm { LocalPeer = self };
var peer = new Peer
{
Id = "QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h",
Addresses = new MultiAddress[]
{
"/ip4/127.0.0.1/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"
}
};

swarm.BlackList.Add(peer.Addresses.First());
ExceptionAssert.Throws<Exception>(() => swarm.RegisterPeer(peer));
}
}

/// <summary>
Expand Down

0 comments on commit 828f328

Please sign in to comment.