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

Commit

Permalink
fix: refuse to read directories
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jul 10, 2018
1 parent 69de2af commit 1a81d66
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/core/read-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const exporter = require('ipfs-unixfs-engine').exporter
const pull = require('pull-stream/pull')
const collect = require('pull-stream/sinks/collect')
const waterfall = require('async/waterfall')
const UnixFs = require('ipfs-unixfs')
const {
traverseTo
} = require('./utils')
Expand All @@ -30,9 +31,16 @@ module.exports = (ipfs) => {
parents: false
}, done),
(result, done) => {
const node = result.node
const meta = UnixFs.unmarshal(node.data)

if (meta.type !== 'file') {
return done(new Error(`Error: ${path} was not a file`))
}

waterfall([
(next) => pull(
exporter(result.node.multihash, ipfs._ipld, {
exporter(node.multihash, ipfs._ipld, {
offset: options.offset,
length: options.length
}),
Expand Down
9 changes: 9 additions & 0 deletions test/read.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ describe('read', function () {
.then((result) => method.collect(result))
.then((buffer) => expect(buffer).to.deep.equal(data.slice(offset, offset + length)))
})

it('refuses to read a directory', () => {
const path = '/'

return method.read(path)
.catch(error => {
expect(error.message).to.contain('was not a file')
})
})
})
})
})

0 comments on commit 1a81d66

Please sign in to comment.