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

Commit 3df6167

Browse files
committed
Sanitize multihash input.
1 parent e8b5dd5 commit 3df6167

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/clean-multihash.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
const bs58 = require('bs58')
4+
const isIPFS = require('is-ipfs')
5+
6+
module.exports = function (multihash) {
7+
if (!isIPFS.multihash(multihash)) {
8+
throw new Error('not valid multihash')
9+
}
10+
if (Buffer.isBuffer(multihash)) {
11+
return bs58.encode(multihash)
12+
}
13+
return multihash
14+
}

src/exporter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Readable = require('readable-stream').Readable
1010
const pathj = require('path')
1111
const util = require('util')
1212
const fieldtrip = require('field-trip')
13+
const cleanMultihash = require('./clean-multihash')
1314

1415
exports = module.exports = Exporter
1516

@@ -24,6 +25,7 @@ function Exporter (hash, dagService, options) {
2425
if (!isIPFS.multihash(hash)) {
2526
throw new Error('not valid multihash')
2627
}
28+
hash = cleanMultihash(hash)
2729

2830
Readable.call(this, { objectMode: true })
2931

test/test-exporter.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const UnixFS = require('ipfs-unixfs')
1010
const concat = require('concat-stream')
1111
const fs = require('fs')
1212
const path = require('path')
13+
const bs58 = require('bs58')
1314

1415
let ds
1516

@@ -24,6 +25,29 @@ module.exports = function (repo) {
2425
done()
2526
})
2627

28+
it('ensure hash inputs are sanitized', (done) => {
29+
const hash = 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8'
30+
const bs = new BlockService(repo)
31+
const ds = new DAGService(bs)
32+
const mhBuf = new Buffer(bs58.decode(hash))
33+
ds.get(hash, (err, fetchedNode) => {
34+
const unmarsh = UnixFS.unmarshal(fetchedNode.data)
35+
expect(err).to.not.exist
36+
const testExport = exporter(mhBuf, ds)
37+
testExport.on('error', (err) => {
38+
expect(err).to.not.exist
39+
})
40+
testExport.pipe(concat((files) => {
41+
expect(files).to.be.length(1)
42+
expect(files[0].path).to.equal(hash)
43+
files[0].content.pipe(concat((bldata) => {
44+
expect(bldata).to.deep.equal(unmarsh.data)
45+
done()
46+
}))
47+
}))
48+
})
49+
})
50+
2751
it('export a file with no links', (done) => {
2852
const hash = 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8'
2953
const bs = new BlockService(repo)

0 commit comments

Comments
 (0)