Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: @helia/* modules validate CID codec #643

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/json/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class DefaultJSON implements JSON {
}

async get <T> (cid: CID, options: Partial<GetOptions> = {}): Promise<T> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random question, was looking at the GetOptions type. It contains codec: BlockCodec property which is passed to the blockstore.get call. Is this ever used?

if (cid.code !== jsonCodec.code) {
throw new TypeError('The passed CID had an incorrect codec, it may correspond to a non-JSON block')
}

const buf = await this.components.blockstore.get(cid, options)
Comment on lines +118 to 122
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks that the codec codes match inside of the JSON.get implementation.


return jsonCodec.decode(buf)
Expand Down
8 changes: 7 additions & 1 deletion packages/json/test/get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { expect } from 'aegir/chai'
import { MemoryBlockstore } from 'blockstore-core'
import { CID } from 'multiformats/cid'
import { identity } from 'multiformats/hashes/identity'
import { json, type JSON } from '../src/index.js'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'

describe('get', () => {
let blockstore: Blockstore
Expand Down Expand Up @@ -39,4 +39,10 @@ describe('get', () => {

await expect(j.get(cid)).to.eventually.deep.equal(input)
})

it('rejects if CID codec is not equal to JSON codec', async () => {
const rawCID = CID.createV1(0x55, cid.multihash)
await expect(j.get(rawCID)).to.eventually.be.rejected
.with.property('message', 'The passed CID had an incorrect codec, it may correspond to a non-JSON block')
})
})
Loading