Skip to content

Commit 40855f4

Browse files
authored
fix: only send ip/domain observed address in identify (#2201)
Some addresses are not naturally routable - for example incoming webrtc addresses, so do not send them as observed addresses in identify responses.
1 parent 051154d commit 40855f4

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

packages/libp2p/src/identify/identify.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { logger } from '@libp2p/logger'
44
import { peerIdFromKeys } from '@libp2p/peer-id'
55
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
66
import { type Multiaddr, multiaddr, protocols } from '@multiformats/multiaddr'
7+
import { IP_OR_DOMAIN } from '@multiformats/multiaddr-matcher'
78
import { pbStream } from 'it-protobuf-stream'
89
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
910
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
@@ -345,6 +346,12 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
345346
signedPeerRecord = envelope.marshal().subarray()
346347
}
347348

349+
let observedAddr: Uint8Array | undefined = connection.remoteAddr.bytes
350+
351+
if (!IP_OR_DOMAIN.matches(connection.remoteAddr)) {
352+
observedAddr = undefined
353+
}
354+
348355
const pb = pbStream(stream).pb(Identify)
349356

350357
await pb.write({
@@ -353,7 +360,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
353360
publicKey,
354361
listenAddrs: multiaddrs.map(addr => addr.bytes),
355362
signedPeerRecord,
356-
observedAddr: connection.remoteAddr.bytes,
363+
observedAddr,
357364
protocols: peerData.protocols
358365
}, {
359366
signal

packages/libp2p/src/upgrader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ export class DefaultUpgrader implements Upgrader {
432432
throw new CodeError('Stream is not multiplexed', codes.ERR_MUXER_UNAVAILABLE)
433433
}
434434

435-
log('%s: starting new stream on %s', direction, protocols)
435+
log('%s-%s: starting new stream on %s', connection.id, direction, protocols)
436436
const muxedStream = await muxer.newStream()
437437

438438
try {

packages/libp2p/test/identify/index.spec.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { TypedEventEmitter } from '@libp2p/interface/events'
55
import { start, stop } from '@libp2p/interface/startable'
6-
import { mockConnectionGater, mockRegistrar, mockUpgrader, connectionPair, mockStream } from '@libp2p/interface-compliance-tests/mocks'
6+
import { mockConnectionGater, mockRegistrar, mockUpgrader, connectionPair, mockStream, streamPair } from '@libp2p/interface-compliance-tests/mocks'
77
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
88
import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record'
99
import { PersistentPeerStore } from '@libp2p/peer-store'
@@ -13,6 +13,7 @@ import { MemoryDatastore } from 'datastore-core/memory'
1313
import delay from 'delay'
1414
import drain from 'it-drain'
1515
import * as lp from 'it-length-prefixed'
16+
import { duplexPair } from 'it-pair/duplex'
1617
import { pipe } from 'it-pipe'
1718
import { pbStream } from 'it-protobuf-stream'
1819
import { pushable } from 'it-pushable'
@@ -581,4 +582,29 @@ describe('identify', () => {
581582

582583
expect(localPeerStorePatchSpy.called).to.be.false('patch was called when public key was invalid')
583584
})
585+
586+
it('should not send unroutable observed addresses', async () => {
587+
const localIdentify = new DefaultIdentifyService(localComponents, defaultInit)
588+
589+
const duplex = duplexPair<any>()
590+
const streams = streamPair({
591+
duplex: duplex[0]
592+
}, {
593+
duplex: duplex[1]
594+
})
595+
596+
const data: IncomingStreamData = {
597+
stream: streams[0],
598+
connection: stubInterface<Connection>({
599+
remoteAddr: multiaddr('/webrtc/p2p/QmR5VwgsL7jyfZHAGyp66tguVrQhCRQuRc3NokocsCZ3fA')
600+
})
601+
}
602+
603+
await localIdentify._handleIdentify(data)
604+
605+
const pb = pbStream(duplex[1])
606+
const result = await pb.read(Identify)
607+
608+
expect(result.observedAddr).to.be.undefined()
609+
})
584610
})

0 commit comments

Comments
 (0)