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

protocols/identify Check multiaddr has valid peer id component prior to caching #2338

Conversation

MarcoPolo
Copy link
Contributor

Context

Resolves #2302.

#2232 introduced a change to the Identify protocol implementation that caches the discovered listenAddrs from the Identify message. The cache is then used in the addresses_of_peer method that is called by swarm code to find possible addresses to dial for a given peer.

This lets the Identify assist in peer discovery of a given network behavior automatically. See #2216 for more information.

This cache was disabled in #2312 because we weren't doing any validation. This PR adds the peer id validation.

Proposed Solution

This adds a simple validation before we cache the multiaddr. We don't cache a multiaddr with a wrong final peer-id component. e.g. something that ends with .../p2p/Qmsome-other-peer. While this should fail to dial because we check this before dialing, it's probably better to not cache this in the first place.

If the multiaddr doesn't have a final .../p2p/Qmfoo component, we consider it valid.

Testing this locally, it seems like all listenAddrs in Identify messages generally don't include the .../p2p/ component (except for dnsaddr, but do these even show up in identify messages?)

Rollback strategy

This is safe to revert. Without this, multiaddrs with a wrong peerid will fail to dial.

Next steps

This doesn't enable the caching, I can do that in this PR or another one if we want to keep things smaller.

@MarcoPolo MarcoPolo changed the title Check multiaddr has valid peer id component protocols/identify Check multiaddr has valid peer id component prior to caching Nov 12, 2021
@MarcoPolo
Copy link
Contributor Author

tagging @mxinden here on GH as well

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

🚀 couple of minor comments. Other than that this looks good to me. Thanks for the help.

Good job getting started so quickly!

protocols/identify/src/identify.rs Outdated Show resolved Hide resolved
fn check_multiaddr_matches_peer_id() {
let peer_id = PeerId::random();
let other_peer_id = PeerId::random();
let mut addr: Multiaddr = ("/ip4/147.75.69.143/tcp/4001".to_string())
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let mut addr: Multiaddr = ("/ip4/147.75.69.143/tcp/4001".to_string())
let mut addr: Multiaddr = "/ip4/147.75.69.143/tcp/4001".to_string()

Are these needed?

protocols/identify/src/identify.rs Outdated Show resolved Hide resolved
protocols/identify/src/identify.rs Outdated Show resolved Hide resolved
@mxinden
Copy link
Member

mxinden commented Nov 13, 2021

except for dnsaddr, but do these even show up in identify messages?

Yes, a node could advertise a dnsaddr as their listen address.

@mxinden
Copy link
Member

mxinden commented Nov 15, 2021

Thanks for the follow-up. Just missing a changelog entry (see release docs).

@MarcoPolo
Copy link
Contributor Author

Done, should I also enable this by default or do that in another PR? (essentially undoing this PR: #2312)

@mxinden mxinden mentioned this pull request Nov 15, 2021
... or none at all. We don't want to cache a multiaddr with a purposely
wrong multiaddr. e.g. something that ends with .../p2p/some-other-peer.
While this should fail to dial because we [check this before
dialing](https://github.com/libp2p/rust-libp2p/blob/master/core/src/connection/pool/concurrent_dial.rs#L144),
it's probably better to not cache this in the first place.
@MarcoPolo MarcoPolo force-pushed the check-multiaddr-has-correct-peer-or-none-at-all branch from 27340f8 to 314c0f0 Compare November 15, 2021 19:00
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

Thanks @MarcoPolo.

Done, should I also enable this by default or do that in another PR? (essentially undoing this PR: #2312)

I think it is fine for now. I would suggest enabling it by default once people had the chance to test it out on a larger deployment.

@mxinden mxinden merged commit c4f7877 into libp2p:master Nov 16, 2021
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.

protocols/identify: Validate addresses before caching
2 participants