mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-04-25 12:42:21 +00:00
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: https://github.com/libp2p/js-libp2p-tcp/issues/93 fixes: https://github.com/ipfs/js-ipfs/issues/1447
55 lines
1.1 KiB
JavaScript
55 lines
1.1 KiB
JavaScript
/* eslint-env mocha */
|
|
'use strict'
|
|
|
|
const chai = require('chai')
|
|
const dirtyChai = require('dirty-chai')
|
|
const expect = chai.expect
|
|
chai.use(dirtyChai)
|
|
const getMultiaddr = require('../src/get-multiaddr')
|
|
|
|
const goodSocket4 = {
|
|
remoteAddress: '127.0.0.1',
|
|
remotePort: '9090',
|
|
remoteFamily: 'IPv4'
|
|
}
|
|
|
|
const goodSocket6 = {
|
|
remoteAddress: '::1',
|
|
remotePort: '9090',
|
|
remoteFamily: 'IPv6'
|
|
}
|
|
|
|
const badSocket = {}
|
|
|
|
const badSocketData = {
|
|
remoteAddress: 'aewmrn4awoew',
|
|
remotePort: '234',
|
|
remoteFamily: 'Hufflepuff'
|
|
}
|
|
|
|
describe('getMultiaddr multiaddr creation', () => {
|
|
it('creates multiaddr from valid socket data', (done) => {
|
|
expect(getMultiaddr(goodSocket4))
|
|
.to.exist()
|
|
done()
|
|
})
|
|
|
|
it('creates multiaddr from valid IPv6 socket data', (done) => {
|
|
expect(getMultiaddr(goodSocket6))
|
|
.to.exist()
|
|
done()
|
|
})
|
|
|
|
it('returns undefined multiaddr from missing socket data', (done) => {
|
|
expect(getMultiaddr(badSocket))
|
|
.to.equal(undefined)
|
|
done()
|
|
})
|
|
|
|
it('returns undefined multiaddr from unparseable socket data', (done) => {
|
|
expect(getMultiaddr(badSocketData))
|
|
.to.equal(undefined)
|
|
done()
|
|
})
|
|
})
|