Skip to content

Commit 258a0be

Browse files
olizillarvagg
authored andcommitted
fix: throw on CID.parse v0 string with multibase prefix
Add a check to CID.parse to throw on a CID v0 string with explict multibase, e.g. `zQmPr755CxWUwt39C2Yiw4UGKrv16uZhSgeZJmoHUUS9TSJ` We're seeing pinning service requests comming in from the wild with the undesriable multibase prefix, and the expectation was that CID.parse would have thrown and spared us from having to deal with them. Fixes: #240 See also: ipfs/kubo#9556 License: MIT Signed-off-by: Oli Evans <[email protected]>
1 parent 4b484bb commit 258a0be

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/cid.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,10 @@ export class CID {
484484

485485
const cid = CID.decode(bytes)
486486

487+
if (cid.version === 0 && source[0] !== 'Q') {
488+
throw Error('Version 0 CID string must not include multibase prefix')
489+
}
490+
487491
// Cache string representation to avoid computing it on `this.toString()`
488492
baseCache(cid).set(prefix, source)
489493

test/test-cid.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ describe('CID', () => {
8585
assert.throws(() => cid.toString(base32), msg)
8686
})
8787

88+
it('throws on CIDv0 string with explicit multibase prefix', async () => {
89+
const str = 'zQmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
90+
const msg = 'Version 0 CID string must not include multibase prefix'
91+
assert.throws(() => CID.parse(str), msg)
92+
})
93+
8894
it('.bytes', async () => {
8995
const hash = await sha256.digest(textEncoder.encode('abc'))
9096
const codec = 112

0 commit comments

Comments
 (0)