Skip to content

Commit 131d524

Browse files
authored
fix: handle invalid or corrupt protobuf data (#449)
Ensure we throw an error with a better name than 'Error'.
1 parent 42246f9 commit 131d524

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ const unixFsResolver: Resolver = async (cid, name, path, toResolve, resolve, dep
4545
}
4646

4747
const block = await toBuffer(blockstore.get(cid, options))
48-
const node = decode(block)
48+
let node: PBNode
49+
50+
try {
51+
node = decode(block)
52+
} catch (err: any) {
53+
// badly formatted or invalid protobuf
54+
throw new NotUnixFSError(err.message)
55+
}
56+
4957
let unixfs
5058
let next
5159

packages/ipfs-unixfs-exporter/test/exporter.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,4 +1737,14 @@ describe('exporter', () => {
17371737
expect(basicfile).to.not.have.property('unixfs')
17381738
expect(basicfile).to.not.have.property('content')
17391739
})
1740+
1741+
it('should throw NotUnixFSError when data is not parseable', async () => {
1742+
const buf = uint8ArrayFromString('i am not a protobuf')
1743+
const mh = await sha256.digest(buf)
1744+
const cid = CID.createV1(dagPb.code, mh)
1745+
await block.put(cid, buf)
1746+
1747+
await expect(exporter(cid, block)).to.eventually.be.rejected
1748+
.with.property('name', 'NotUnixFSError')
1749+
})
17401750
})

0 commit comments

Comments
 (0)