Skip to content

Commit 183cf99

Browse files
committed
fix: add allow provider parameter option to have provider query parameter as experimental
1 parent c30629a commit 183cf99

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

packages/verified-fetch/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,18 @@ export interface VerifiedFetchInit extends RequestInit, ProgressOptions<BubbledP
10341034
*/
10351035
allowInsecure?: boolean
10361036

1037+
/**
1038+
* By default we will not parse provider query parameters, and will not
1039+
* connect to any hosts over their multiaddresses. Instead, we will use the
1040+
* default discovery mechanism to find providers for the content.
1041+
* This is an experimental feature, and may become the default in the future.
1042+
* If you pass `true` here, we will parse the `provider` query parameter and
1043+
* connect to the provider specified in the query parameter to retrieve the content.
1044+
*
1045+
* @default false
1046+
*/
1047+
allowProviderParameter?: boolean
1048+
10371049
/**
10381050
* Whether to include server-timing headers in the response for an individual request.
10391051
*

packages/verified-fetch/src/verified-fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export class VerifiedFetch {
376376
accept,
377377
options: {
378378
...options,
379-
providers: parsedResult.providers
379+
providers: options?.allowProviderParameter ? parsedResult.providers : undefined
380380
},
381381
withServerTiming,
382382
onProgress: options?.onProgress,

packages/verified-fetch/test/verified-fetch.spec.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,15 +811,14 @@ describe('@helia/verified-fetch', () => {
811811
blockBrokerRetrieveCalledWithProviders = pDefer()
812812
blockRetriever = stubInterface<Required<Pick<BlockBroker, 'retrieve' | 'createSession'>>>({
813813
retrieve: sandbox.stub().callsFake(async (cid, options) => {
814-
blockBrokerRetrieveCalledWithProviders.resolve(options.providers)
814+
blockBrokerRetrieveCalledWithProviders.resolve(options.providers || [])
815815

816816
// attempt to read from the provider
817817
// eslint-disable-next-line
818818
const stringProviders = options.providers.map((p: import('@multiformats/multiaddr').Multiaddr) => p.toString())
819819
if (stringProviders.includes(provider)) {
820820
return helia.blockstore.get(cid, options)
821821
}
822-
throw new Error('not found')
823822
}),
824823
createSession: () => {
825824
return blockRetriever
@@ -848,7 +847,9 @@ describe('@helia/verified-fetch', () => {
848847
// broker retriever
849848
await helia.blockstore.put(cid, rawData)
850849

851-
const resp = await stubbedVerifiedFetch.fetch(`ipfs://${cid}?provider=${provider}`)
850+
const resp = await stubbedVerifiedFetch.fetch(`ipfs://${cid}?provider=${provider}`, {
851+
allowProviderParameter: true
852+
})
852853
expect(resp).to.be.ok()
853854
expect(resp.status).to.equal(200)
854855
expect(resp.statusText).to.equal('OK')
@@ -880,7 +881,9 @@ describe('@helia/verified-fetch', () => {
880881
const url = `ipfs://${cid}?${query}`
881882

882883
// Verify response expectations
883-
const resp = await stubbedVerifiedFetch.fetch(url)
884+
const resp = await stubbedVerifiedFetch.fetch(url, {
885+
allowProviderParameter: true
886+
})
884887

885888
expect(resp).to.be.ok()
886889
expect(resp.status).to.equal(200)
@@ -896,6 +899,37 @@ describe('@helia/verified-fetch', () => {
896899
expect(received).to.include(p)
897900
}
898901
})
902+
903+
it('should not pass providers unless allowProviderParameter option is set', async () => {
904+
const rawData = new Uint8Array([0x01, 0x02, 0x03])
905+
const cid = CID.createV1(raw.code, await sha256.digest(rawData))
906+
// Add raw data to helia (provider instance), which will be called by
907+
// broker retriever
908+
await helia.blockstore.put(cid, rawData)
909+
910+
// prepare URL
911+
const providers = [
912+
provider,
913+
'/dns4/provider2.io/tcp/8000/ws'
914+
]
915+
const query = providers
916+
.map(p => `provider=${p}`)
917+
.join('&')
918+
919+
const url = `ipfs://${cid}?${query}`
920+
921+
// Verify response expectations
922+
// Default behaviour with disabled passing providers from query parameter
923+
const resp = await stubbedVerifiedFetch.fetch(url)
924+
925+
expect(resp).to.be.ok()
926+
expect(resp.status).to.not.equal(200)
927+
expect(resp.statusText).to.not.equal('OK')
928+
929+
// Verify block broker is called without providers
930+
const providerParams = await blockBrokerRetrieveCalledWithProviders.promise
931+
expect(providerParams.length).to.equal(0)
932+
})
899933
})
900934

901935
describe('?format', () => {

0 commit comments

Comments
 (0)