Skip to content

Commit 3d4c886

Browse files
committed
identify: Ensure the observed address coresponds to us
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
1 parent c8752c2 commit 3d4c886

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/protocol/libp2p/identify.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ pub(crate) struct Identify {
172172
// Public key of the local node, filled by `Litep2p`.
173173
public: PublicKey,
174174

175+
/// Local peer ID.
176+
local_peer_id: PeerId,
177+
175178
/// Protocol version.
176179
protocol_version: String,
177180

@@ -191,11 +194,15 @@ pub(crate) struct Identify {
191194
impl Identify {
192195
/// Create new [`Identify`] protocol.
193196
pub(crate) fn new(service: TransportService, config: Config) -> Self {
197+
let public = config.public.expect("public key to be supplied");
198+
let local_peer_id = public.to_peer_id();
199+
194200
Self {
195201
service,
196202
tx: config.tx_event,
197203
peers: HashMap::new(),
198-
public: config.public.expect("public key to be supplied"),
204+
public,
205+
local_peer_id,
199206
protocol_version: config.protocol_version,
200207
user_agent: config.user_agent.unwrap_or(DEFAULT_AGENT.to_string()),
201208
pending_inbound: FuturesStream::new(),
@@ -313,6 +320,8 @@ impl Identify {
313320
"outbound substream opened"
314321
);
315322

323+
let local_peer_id = self.local_peer_id.clone();
324+
316325
self.pending_outbound.push(Box::pin(async move {
317326
let payload =
318327
match tokio::time::timeout(Duration::from_secs(10), substream.next()).await {
@@ -361,7 +370,23 @@ impl Identify {
361370
.collect();
362371

363372
let observed_address =
364-
info.observed_addr.and_then(|address| Multiaddr::try_from(address).ok());
373+
info.observed_addr.and_then(|address| {
374+
let address = Multiaddr::try_from(address).ok()?;
375+
376+
if address.is_empty() {
377+
return None;
378+
}
379+
380+
if let Some(multiaddr::Protocol::P2p(peer_id)) = address.iter().last() {
381+
if peer_id != local_peer_id.into() {
382+
tracing::debug!(target: LOG_TARGET, ?peer, ?address, "peer identified provided invalid address");
383+
return None;
384+
}
385+
}
386+
387+
Some(address)
388+
});
389+
365390
let protocol_version = info.protocol_version;
366391
let user_agent = info.agent_version;
367392

0 commit comments

Comments
 (0)