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

Commit 1a81d66

Browse files
committed
fix: refuse to read directories
1 parent 69de2af commit 1a81d66

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/core/read-pull-stream.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const exporter = require('ipfs-unixfs-engine').exporter
44
const pull = require('pull-stream/pull')
55
const collect = require('pull-stream/sinks/collect')
66
const waterfall = require('async/waterfall')
7+
const UnixFs = require('ipfs-unixfs')
78
const {
89
traverseTo
910
} = require('./utils')
@@ -30,9 +31,16 @@ module.exports = (ipfs) => {
3031
parents: false
3132
}, done),
3233
(result, done) => {
34+
const node = result.node
35+
const meta = UnixFs.unmarshal(node.data)
36+
37+
if (meta.type !== 'file') {
38+
return done(new Error(`Error: ${path} was not a file`))
39+
}
40+
3341
waterfall([
3442
(next) => pull(
35-
exporter(result.node.multihash, ipfs._ipld, {
43+
exporter(node.multihash, ipfs._ipld, {
3644
offset: options.offset,
3745
length: options.length
3846
}),

test/read.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ describe('read', function () {
145145
.then((result) => method.collect(result))
146146
.then((buffer) => expect(buffer).to.deep.equal(data.slice(offset, offset + length)))
147147
})
148+
149+
it('refuses to read a directory', () => {
150+
const path = '/'
151+
152+
return method.read(path)
153+
.catch(error => {
154+
expect(error.message).to.contain('was not a file')
155+
})
156+
})
148157
})
149158
})
150159
})

0 commit comments

Comments
 (0)