Skip to content

Commit

Permalink
fix: only send ip/domain observed address in identify (#2201)
Browse files Browse the repository at this point in the history
Some addresses are not naturally routable - for example incoming
webrtc addresses, so do not send them as observed addresses in
identify responses.
  • Loading branch information
achingbrain authored Nov 6, 2023
1 parent 051154d commit 40855f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/libp2p/src/identify/identify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { logger } from '@libp2p/logger'
import { peerIdFromKeys } from '@libp2p/peer-id'
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
import { type Multiaddr, multiaddr, protocols } from '@multiformats/multiaddr'
import { IP_OR_DOMAIN } from '@multiformats/multiaddr-matcher'
import { pbStream } from 'it-protobuf-stream'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
Expand Down Expand Up @@ -345,6 +346,12 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
signedPeerRecord = envelope.marshal().subarray()
}

let observedAddr: Uint8Array | undefined = connection.remoteAddr.bytes

if (!IP_OR_DOMAIN.matches(connection.remoteAddr)) {
observedAddr = undefined
}

const pb = pbStream(stream).pb(Identify)

await pb.write({
Expand All @@ -353,7 +360,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
publicKey,
listenAddrs: multiaddrs.map(addr => addr.bytes),
signedPeerRecord,
observedAddr: connection.remoteAddr.bytes,
observedAddr,
protocols: peerData.protocols
}, {
signal
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p/src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export class DefaultUpgrader implements Upgrader {
throw new CodeError('Stream is not multiplexed', codes.ERR_MUXER_UNAVAILABLE)
}

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

try {
Expand Down
28 changes: 27 additions & 1 deletion packages/libp2p/test/identify/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

expect(localPeerStorePatchSpy.called).to.be.false('patch was called when public key was invalid')
})

it('should not send unroutable observed addresses', async () => {
const localIdentify = new DefaultIdentifyService(localComponents, defaultInit)

const duplex = duplexPair<any>()
const streams = streamPair({
duplex: duplex[0]
}, {
duplex: duplex[1]
})

const data: IncomingStreamData = {
stream: streams[0],
connection: stubInterface<Connection>({
remoteAddr: multiaddr('/webrtc/p2p/QmR5VwgsL7jyfZHAGyp66tguVrQhCRQuRc3NokocsCZ3fA')
})
}

await localIdentify._handleIdentify(data)

const pb = pbStream(duplex[1])
const result = await pb.read(Identify)

expect(result.observedAddr).to.be.undefined()
})
})

0 comments on commit 40855f4

Please sign in to comment.