Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: treat /dns, /dns4, and /dns6 addrs as public (#406)
Browse files Browse the repository at this point in the history
Updates filtering rules to treat dns addresses as public.

Closes #377

Co-authored-by: Alex Potsides <alex@achingbrain.net>
  • Loading branch information
joeltg and achingbrain committed Dec 7, 2022
1 parent e333e47 commit e27747a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ export function removePrivateAddresses (peer: PeerInfo): PeerInfo {
multiaddrs: peer.multiaddrs.filter(multiaddr => {
const [[type, addr]] = multiaddr.stringTuples()

// treat /dns, /dns4, and /dns6 addrs as public
if (type === 53 || type === 54 || type === 55) {
// localhost can be a dns address but it's private
if (addr === 'localhost') {
return false
}

return true
}

if (type !== 4 && type !== 6) {
return false
}
Expand All @@ -45,6 +55,10 @@ export function removePublicAddresses (peer: PeerInfo): PeerInfo {
multiaddrs: peer.multiaddrs.filter(multiaddr => {
const [[type, addr]] = multiaddr.stringTuples()

if (addr === 'localhost') {
return true
}

if (type !== 4 && type !== 6) {
return false
}
Expand All @@ -53,7 +67,14 @@ export function removePublicAddresses (peer: PeerInfo): PeerInfo {
return false
}

return isPrivateIp(addr)
const isPrivate = isPrivateIp(addr)

if (isPrivate == null) {
// not an ip address
return false
}

return isPrivate
})
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/kad-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { expect } from 'aegir/chai'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { multiaddr } from '@multiformats/multiaddr'
import * as utils from '../src/utils.js'
import { createPeerId, createPeerIds } from './utils/create-peer-id.js'

Expand Down Expand Up @@ -60,4 +61,38 @@ describe('kad utils', () => {
})
})
})

describe('removePrivateAddresses', () => {
it('filters private multiaddrs', async () => {
const id = await createPeerId()

const multiaddrs = [
multiaddr('/dns4/example.com/tcp/4001'),
multiaddr('/ip4/192.168.0.1/tcp/4001'),
multiaddr('/ip4/1.1.1.1/tcp/4001'),
multiaddr('/dns4/localhost/tcp/4001')
]

const peerInfo = utils.removePrivateAddresses({ id, multiaddrs, protocols: [] })
expect(peerInfo.multiaddrs.map((ma) => ma.toString()))
.to.eql(['/dns4/example.com/tcp/4001', '/ip4/1.1.1.1/tcp/4001'])
})
})

describe('removePublicAddresses', () => {
it('filters public multiaddrs', async () => {
const id = await createPeerId()

const multiaddrs = [
multiaddr('/dns4/example.com/tcp/4001'),
multiaddr('/ip4/192.168.0.1/tcp/4001'),
multiaddr('/ip4/1.1.1.1/tcp/4001'),
multiaddr('/dns4/localhost/tcp/4001')
]

const peerInfo = utils.removePublicAddresses({ id, multiaddrs, protocols: [] })
expect(peerInfo.multiaddrs.map((ma) => ma.toString()))
.to.eql(['/ip4/192.168.0.1/tcp/4001', '/dns4/localhost/tcp/4001'])
})
})
})

0 comments on commit e27747a

Please sign in to comment.