Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean addresses with peer id before adding to addrbook #2007

Merged
merged 3 commits into from Jan 25, 2023

Conversation

sukunrt
Copy link
Member

@sukunrt sukunrt commented Jan 23, 2023

Fixes: #2001

@sukunrt
Copy link
Member Author

sukunrt commented Jan 23, 2023

I'm cleaning the address before adding it to addressbook. The other option is to change the check in all transports. This seemed cleaner to me.

Copy link
Contributor

@MarcoPolo MarcoPolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up! This is really helpful 🎉. There's a couple of things we should do before merging this:

  • The changes mentioned in the review.
  • We should update the implementations of Host.Connect so that they check that the multiaddrs with a p2p component in the Addrs field match the ID field. Filter out any mismatching multiaddrs (maybe log a warning). And return an error if no addresses are dialable because of mismatched peer ids. As well as removing the p2p component of the multiaddr before passing it down the call stack.

p2pAddr := addr[0].Encapsulate(Multiaddr("/p2p/" + peerId.String()))
ab.AddAddr(peerId, p2pAddr, time.Hour)
AssertAddressesEqual(t, addr, ab.Addrs(peerId))
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a case that fails if the peer id in the multiaddr is != to the peer id passed in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The annoying thing is that AddAddr doesn't return an error. So the best we can do here is to fail silently and log an error in the implementations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added another test case with filtering on invalid peerId

@@ -601,10 +601,12 @@ func (ab *dsAddrBook) deleteAddrs(p peer.ID, addrs []ma.Multiaddr) (err error) {
func cleanAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now take a peer id as well, and filter out multiaddrs that have an id component but don't match (and log an error when this happens)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm logging a warning which was previously logged for nil addresses, should I make it an error?

@@ -238,19 +238,21 @@ func (mab *memoryAddrBook) addAddrsUnlocked(s *addrSegment, p peer.ID, addrs []m

exp := mab.clock.Now().Add(ttl)
for _, addr := range addrs {
if addr == nil {
// Remove suffix of /p2p/peer-id from address
taddr, _ := peer.SplitAddr(addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above about the peer id check. Also I think it's okay to shadow the addr variable with the new multiaddr.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed

@@ -283,17 +285,18 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du

exp := mab.clock.Now().Add(ttl)
for _, addr := range addrs {
if addr == nil {
taddr, _ := peer.SplitAddr(addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed now.

@sukunrt
Copy link
Member Author

sukunrt commented Jan 24, 2023

  • We should update the implementations of Host.Connect so that they check that the multiaddrs with a p2p component in the Addrs field match the ID field. Filter out any mismatching multiaddrs (maybe log a warning). And return an error if no addresses are dialable because of mismatched peer ids. As well as removing the p2p component of the multiaddr before passing it down the call stack.

All Host.Connect implementations use PeerStore.AddAddrs which will filter out for invalid ID and log the warning, should I do it in the connect implementation as well?
I was thinking of doing this:

func (h *BasicHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
	// absorb addresses into peerstore
	h.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.TempAddrTTL)
    addrs := h.Peerstore().Addrs(pi.ID)
	if len(addrs) == 0 {
		return fmt.Errorf("no valid address found for peer %s", pi.ID)
	}

Add error reporting from Host.Connect
@sukunrt
Copy link
Member Author

sukunrt commented Jan 24, 2023

@MarcoPolo Addressed review comments. Do the changes in Host.Connect look okay?

@MarcoPolo
Copy link
Contributor

All Host.Connect implementations use PeerStore.AddAddrs which will filter out for invalid ID and log the warning,

Good point. If there are no available addresses we'll still get that error from Connect. I think that's okay.

Copy link
Contributor

@MarcoPolo MarcoPolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small nits. I pushed a change. Thanks a ton for fixing this :)

@@ -709,6 +709,11 @@ func (h *BasicHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
}
}

addrs := h.Peerstore().Addrs(pi.ID)
if len(addrs) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought we already return a similar error here https://github.com/libp2p/go-libp2p/blob/master/p2p/net/swarm/swarm_dial.go#L340. So this change is not required. Sorry I added confusion here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fair. Thanks for fixing this!

@MarcoPolo
Copy link
Contributor

Will merge after CI passes.

@MarcoPolo MarcoPolo merged commit 084de06 into libp2p:master Jan 25, 2023
@masih
Copy link
Member

masih commented Jan 26, 2023

Thanking you folks for getting this issue resolved so quickly 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multiaddrs with peer ID are not considered dialable
3 participants