Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/protocol-autonat-v2/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InvalidParametersError, ProtocolError, serviceCapabilities, serviceDependencies } from '@libp2p/interface'
import { peerSet } from '@libp2p/peer-collections'
import { createScalableCuckooFilter, isGlobalUnicast, isPrivate, PeerQueue, repeatingTask, trackedMap, pbStream, getNetConfig } from '@libp2p/utils'
import { createScalableCuckooFilter, isGlobalUnicast, isNetworkAddress, isPrivate, PeerQueue, repeatingTask, trackedMap, pbStream, getNetConfig } from '@libp2p/utils'
import { anySignal } from 'any-signal'
import { setMaxListeners } from 'main-event'
import { DEFAULT_CONNECTION_THRESHOLD, DIAL_DATA_CHUNK_SIZE, MAX_DIAL_DATA_BYTES, MAX_INBOUND_STREAMS, MAX_MESSAGE_SIZE, MAX_OUTBOUND_STREAMS, TIMEOUT } from './constants.ts'
Expand Down Expand Up @@ -288,6 +288,11 @@ export class AutoNATv2Client implements Startable {
return false
}

if (!isNetworkAddress(addr.multiaddr)) {
// skip non-network addresses
return false
}

const options = getNetConfig(addr.multiaddr)

if (options.type === 'ip6') {
Expand Down Expand Up @@ -402,10 +407,25 @@ export class AutoNATv2Client implements Startable {
// if the remote peer has IPv6 addresses, we can probably send them an IPv6
// address to verify, otherwise only send them IPv4 addresses
const supportsIPv6 = peer.addresses.some(({ multiaddr }) => {
if (!isNetworkAddress(multiaddr)) {
return false
}

return getNetConfig(multiaddr).type === 'ip6'
})

// get multiaddrs this peer is eligible to verify
if (!isNetworkAddress(connection.remoteAddr)) {
return
}

const remoteAddrConfig = getNetConfig(connection.remoteAddr)

// only ip4/ip6 remote addrs can be mapped to a stable network segment
if (remoteAddrConfig.type !== 'ip4' && remoteAddrConfig.type !== 'ip6') {
return
}

const segment = this.getNetworkSegment(connection.remoteAddr)
const results = this.getUnverifiedMultiaddrs(segment, supportsIPv6)

Expand Down
22 changes: 22 additions & 0 deletions packages/protocol-autonat-v2/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,28 @@ describe('autonat v2 - client', () => {
.to.be.true('Did not verify external multiaddr')
})

it('should skip non-network addresses', async () => {
const nonNetworkAddress = multiaddr('/')

addressManager.getAddressesWithMetadata.returns([{
multiaddr: nonNetworkAddress,
verified: false,
type: 'observed',
expires: 0
}])

const connection = await stubPeerResponse({
host: '123.123.123.123',
messages: {}
})

await service.client.verifyExternalAddresses(connection)
await delay(100)

expect(addressManager.confirmObservedAddr.called)
.to.be.false('Attempted to verify a non-network address')
})

it('should time out when verifying an observed address', async () => {
const observedAddress = multiaddr('/ip4/123.123.123.123/tcp/28319')
addressManager.getAddressesWithMetadata.returns([{
Expand Down
Loading