Skip to content

Commit

Permalink
fix(Dht): ProcessFindNode bad peer IDs
Browse files Browse the repository at this point in the history
Some random walkers generate a random Key that is not hashed.
  • Loading branch information
richardschneider committed Jun 10, 2019
1 parent 87718aa commit ba0317b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Routing/Dht1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,16 @@ DhtMessage ProcessPing(DhtMessage request, DhtMessage response)
/// </summary>
public DhtMessage ProcessFindNode(DhtMessage request, DhtMessage response)
{
var peerId = new MultiHash(request.Key);
// Some random walkers generate a random Key that is not hashed.
MultiHash peerId;
try
{
peerId = new MultiHash(request.Key);
}
catch (Exception)
{
peerId = MultiHash.ComputeHash(request.Key);
}

// Do we know the peer?.
Peer found = null;
Expand Down
28 changes: 28 additions & 0 deletions test/Routing/Dht1Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,34 @@ public async Task ProcessFindNodeMessage_Closest()
}
}

[TestMethod]
public async Task ProcessFindNodeMessage_BadNodeId()
{
var swarm = new Swarm { LocalPeer = self };
await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1a");
await swarm.RegisterPeerAsync("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1b");
await swarm.RegisterPeerAsync("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1c");
await swarm.RegisterPeerAsync("/ip4/127.0.0.4/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1d");
await swarm.RegisterPeerAsync("/ip4/127.0.0.5/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1e");
var dht = new Dht1 { Swarm = swarm, CloserPeerCount = 3 };
await dht.StartAsync();
try
{
dht.RoutingTable.Add(other);
var request = new DhtMessage
{
Type = MessageType.FindNode,
Key = new byte[] {0xFF, 1, 2, 3 }
};
var response = dht.ProcessFindNode(request, new DhtMessage());
Assert.AreEqual(3, response.CloserPeers.Length);
}
finally
{
await dht.StopAsync();
}
}

[TestMethod]
public async Task ProcessFindNodeMessage_NoOtherPeers()
{
Expand Down

0 comments on commit ba0317b

Please sign in to comment.