Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 4b04b17

Browse files
TomCodedJacob Heun
authored and
Jacob Heun
committed
fix: invalid ip address and daemon can be crashed by remote user
Per the nodeJS documentation, a Net socket.remoteAddress value may be undefined if the socket is destroyed, as by a client disconnect. A multiaddr cannot be created for an invalid IP address (such as the undefined remote address of a destroyed socket). Currently the attempt results in a crash that can be triggered remotely. This commit catches the exception in get-multiaddr and returns an undefined value to listener rather than throwing an exception when trying to process defective or destroyed socket data. Listener then terminates processing of the incoming p2p connections that generate this error condition. fixes: #93 fixes: ipfs/js-ipfs#1447
1 parent 6c36a46 commit 4b04b17

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

src/get-multiaddr.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22

33
const multiaddr = require('multiaddr')
44
const Address6 = require('ip-address').Address6
5+
const debug = require('debug')
6+
const log = debug('libp2p:tcp:get-multiaddr')
57

68
module.exports = (socket) => {
79
let ma
810

9-
if (socket.remoteFamily === 'IPv6') {
10-
const addr = new Address6(socket.remoteAddress)
11+
try {
12+
if (socket.remoteFamily === 'IPv6') {
13+
const addr = new Address6(socket.remoteAddress)
1114

12-
if (addr.v4) {
13-
const ip4 = addr.to4().correctForm()
14-
ma = multiaddr('/ip4/' + ip4 +
15-
'/tcp/' + socket.remotePort
16-
)
15+
if (addr.v4) {
16+
const ip4 = addr.to4().correctForm()
17+
ma = multiaddr('/ip4/' + ip4 +
18+
'/tcp/' + socket.remotePort
19+
)
20+
} else {
21+
ma = multiaddr('/ip6/' + socket.remoteAddress +
22+
'/tcp/' + socket.remotePort
23+
)
24+
}
1725
} else {
18-
ma = multiaddr('/ip6/' + socket.remoteAddress +
19-
'/tcp/' + socket.remotePort
20-
)
26+
ma = multiaddr('/ip4/' + socket.remoteAddress +
27+
'/tcp/' + socket.remotePort)
2128
}
22-
} else {
23-
ma = multiaddr('/ip4/' + socket.remoteAddress +
24-
'/tcp/' + socket.remotePort)
29+
} catch (err) {
30+
log(err)
2531
}
26-
2732
return ma
2833
}

src/listener.js

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ module.exports = (handler) => {
2525
socket.on('error', noop)
2626

2727
const addr = getMultiaddr(socket)
28+
if (!addr) {
29+
if (socket.remoteAddress === undefined) {
30+
log('connection closed before p2p connection made')
31+
} else {
32+
log('error interpreting incoming p2p connection')
33+
}
34+
return
35+
}
36+
2837
log('new connection', addr.toString())
2938

3039
const s = toPull.duplex(socket)

test/get-multiaddr.spec.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
const getMultiaddr = require('../src/get-multiaddr')
9+
10+
const goodSocket4 = {
11+
remoteAddress: '127.0.0.1',
12+
remotePort: '9090',
13+
remoteFamily: 'IPv4'
14+
}
15+
16+
const goodSocket6 = {
17+
remoteAddress: '::1',
18+
remotePort: '9090',
19+
remoteFamily: 'IPv6'
20+
}
21+
22+
const badSocket = {}
23+
24+
const badSocketData = {
25+
remoteAddress: 'aewmrn4awoew',
26+
remotePort: '234',
27+
remoteFamily: 'Hufflepuff'
28+
}
29+
30+
describe('getMultiaddr multiaddr creation', () => {
31+
it('creates multiaddr from valid socket data', (done) => {
32+
expect(getMultiaddr(goodSocket4))
33+
.to.exist()
34+
done()
35+
})
36+
37+
it('creates multiaddr from valid IPv6 socket data', (done) => {
38+
expect(getMultiaddr(goodSocket6))
39+
.to.exist()
40+
done()
41+
})
42+
43+
it('returns undefined multiaddr from missing socket data', (done) => {
44+
expect(getMultiaddr(badSocket))
45+
.to.equal(undefined)
46+
done()
47+
})
48+
49+
it('returns undefined multiaddr from unparseable socket data', (done) => {
50+
expect(getMultiaddr(badSocketData))
51+
.to.equal(undefined)
52+
done()
53+
})
54+
})

0 commit comments

Comments
 (0)