Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit cd682c2

Browse files
committed
fix: throw on invalid multiaddr to constructor
- dont assume that invalid multiaddrs are valid hosts - if multiaddrOrHost starts with a /, assume its a multiaddr - throw if that multiaddr is invalid. Previously, we'd try and use invalid multiaddrs as the host, which would lead to requests like http:///dnsaddr/foobar.com:5001/api License: MIT Signed-off-by: Oli Evans <oli@tableflip.io>
1 parent af6ba05 commit cd682c2

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const sendRequest = require('./utils/send-request')
88

99
function ipfsClient (hostOrMultiaddr, port, opts) {
1010
const config = getConfig()
11-
12-
try {
13-
const maddr = multiaddr(hostOrMultiaddr).nodeAddress()
14-
config.host = maddr.address
15-
config.port = maddr.port
16-
} catch (e) {
17-
if (typeof hostOrMultiaddr === 'string') {
11+
if (typeof hostOrMultiaddr === 'string') {
12+
if (hostOrMultiaddr[0] === '/') {
13+
// throws if multiaddr is malformed or can't be converted to a nodeAddress
14+
const maddr = multiaddr(hostOrMultiaddr).nodeAddress()
15+
config.host = maddr.address
16+
config.port = maddr.port
17+
} else {
1818
config.host = hostOrMultiaddr
1919
config.port = port && typeof port !== 'object' ? port : config.port
2020
}

test/constructor.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,13 @@ describe('ipfs-http-client constructor tests', () => {
7474

7575
clientWorks(ipfsClient(splitted[2], splitted[4], { protocol: 'http' }), done)
7676
})
77+
78+
it('error in invalid mutliaddr', () => {
79+
const splitted = apiAddr.split('/')
80+
81+
expect(() => ipfsClient('/dns4')).to.throw('invalid address')
82+
expect(() => ipfsClient('/hello')).to.throw('no protocol with name')
83+
expect(() => ipfsClient('/dns4/ipfs.io')).to.throw('multiaddr must have a valid format')
84+
})
7785
})
7886
})

0 commit comments

Comments
 (0)