Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: improved multiaddr validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
victorb authored and daviddias committed Feb 18, 2018
1 parent 494da7f commit d9744a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/core/components/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
'use strict'

const defaultNodes = require('../runtime/config-nodejs.json').Bootstrap
const Multiaddr = require('multiaddr')
const isMultiaddr = require('mafmt').IPFS.matches
const promisify = require('promisify-es6')

function isValid (ma) {
if (typeof ma === 'string') {
try {
ma = new Multiaddr(ma)
return Boolean(ma)
} catch (err) {
return false
}
} else if (ma) {
return Multiaddr.isMultiaddr(ma)
} else {
function isValidMultiaddr (ma) {
try {
return isMultiaddr(ma)
} catch (err) {
return false
}
}

function invalidMultiaddrError (ma) {
return new Error(`${ma} is not a valid Multiaddr`)
}

module.exports = function bootstrap (self) {
return {
list: promisify((callback) => {
Expand All @@ -35,8 +32,8 @@ module.exports = function bootstrap (self) {
args = { default: false }
}

if (multiaddr && !isValid(multiaddr)) {
return setImmediate(() => callback(new Error('Not valid multiaddr')))
if (multiaddr && !isValidMultiaddr(multiaddr)) {
return setImmediate(() => callback(invalidMultiaddrError(multiaddr)))
}

self._repo.config.get((err, config) => {
Expand Down Expand Up @@ -64,8 +61,8 @@ module.exports = function bootstrap (self) {
callback = args
args = {all: false}
}
if (multiaddr && !isValid(multiaddr)) {
return setImmediate(() => callback(new Error('Not valid multiaddr')))
if (multiaddr && !isValidMultiaddr(multiaddr)) {
return setImmediate(() => callback(invalidMultiaddrError(multiaddr)))
}

self._repo.config.get((err, config) => {
Expand Down
9 changes: 9 additions & 0 deletions test/core/bootstrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,13 @@ describe('bootstrap', () => {
})
})
})

it('fails if passing in a invalid multiaddr', (done) => {
node.bootstrap.add('/funky/invalid/multiaddr', (err, res) => {
expect(err).to.match(/not a valid Multiaddr/)
expect(err).to.match(/funky/)
expect(res).to.not.exist()
done()
})
})
})

0 comments on commit d9744a1

Please sign in to comment.