diff --git a/packages/verified-fetch/src/utils/parse-resource.ts b/packages/verified-fetch/src/utils/parse-resource.ts index a65a1904..ea2045df 100644 --- a/packages/verified-fetch/src/utils/parse-resource.ts +++ b/packages/verified-fetch/src/utils/parse-resource.ts @@ -32,6 +32,7 @@ export async function parseResource (resource: Resource, { ipns, logger }: Parse protocol: 'ipfs', path: '', query: {}, + ipfsPath: `/ipfs/${cid.toString()}`, ttl: 29030400 // 1 year for ipfs content } satisfies ParsedUrlStringResults } diff --git a/packages/verified-fetch/src/utils/parse-url-string.ts b/packages/verified-fetch/src/utils/parse-url-string.ts index 63959365..ef39bb4f 100644 --- a/packages/verified-fetch/src/utils/parse-url-string.ts +++ b/packages/verified-fetch/src/utils/parse-url-string.ts @@ -27,6 +27,16 @@ interface ParsedUrlStringResultsBase extends ResolveResult { protocol: 'ipfs' | 'ipns' query: ParsedUrlQuery + /** + * The value for the IPFS gateway spec compliant header `X-Ipfs-Path` on the + * response. + * The value of this header should be the original requested content path, + * prior to any path resolution or traversal. + * + * @see https://specs.ipfs.tech/http-gateways/path-gateway/#x-ipfs-path-response-header + */ + ipfsPath: string + /** * seconds as a number */ @@ -248,7 +258,8 @@ export async function parseUrlString ({ urlString, ipns, logger }: ParseUrlStrin cid, path: joinPaths(resolvedPath, urlPath ?? ''), query, - ttl + ttl, + ipfsPath: `/${protocol}/${cidOrPeerIdOrDnsLink}${urlPath != null && urlPath !== '' ? `/${urlPath}` : ''}` } satisfies ParsedUrlStringResults } diff --git a/packages/verified-fetch/src/verified-fetch.ts b/packages/verified-fetch/src/verified-fetch.ts index 83231062..0a1d9cb9 100644 --- a/packages/verified-fetch/src/verified-fetch.ts +++ b/packages/verified-fetch/src/verified-fetch.ts @@ -490,6 +490,7 @@ export class VerifiedFetch { let query: ParsedUrlStringResults['query'] let ttl: ParsedUrlStringResults['ttl'] let protocol: ParsedUrlStringResults['protocol'] + let ipfsPath: string try { const result = await parseResource(resource, { ipns: this.ipns, logger: this.helia.logger }, options) cid = result.cid @@ -497,6 +498,7 @@ export class VerifiedFetch { query = result.query ttl = result.ttl protocol = result.protocol + ipfsPath = result.ipfsPath } catch (err: any) { options?.signal?.throwIfAborted() this.log.error('error parsing resource %s', resource, err) @@ -558,8 +560,7 @@ export class VerifiedFetch { response.headers.set('etag', getETag({ cid, reqFormat, weak: false })) setCacheControlHeader({ response, ttl, protocol }) - // https://specs.ipfs.tech/http-gateways/path-gateway/#x-ipfs-path-response-header - response.headers.set('X-Ipfs-Path', resource.toString()) + response.headers.set('X-Ipfs-Path', ipfsPath) // set Content-Disposition header let contentDisposition: string | undefined diff --git a/packages/verified-fetch/test/parse-resource.spec.ts b/packages/verified-fetch/test/parse-resource.spec.ts index 05a30192..e903a0aa 100644 --- a/packages/verified-fetch/test/parse-resource.spec.ts +++ b/packages/verified-fetch/test/parse-resource.spec.ts @@ -1,17 +1,20 @@ import { defaultLogger } from '@libp2p/logger' +import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { expect } from 'aegir/chai' import { CID } from 'multiformats/cid' import sinon from 'sinon' -import { stubInterface } from 'sinon-ts' +import { stubInterface, type StubbedInstance } from 'sinon-ts' import { parseResource } from '../src/utils/parse-resource.js' import type { IPNS } from '@helia/ipns' +const testCID = CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') +const peerId = await createEd25519PeerId() + describe('parseResource', () => { it('does not call @helia/ipns for CID', async () => { - const testCID = CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') const shouldNotBeCalled1 = sinon.stub().throws(new Error('should not be called')) const shouldNotBeCalled2 = sinon.stub().throws(new Error('should not be called')) - const { cid, path, query } = await parseResource(testCID, { + const { cid, path, query, ipfsPath } = await parseResource(testCID, { ipns: stubInterface({ resolveDNSLink: shouldNotBeCalled1, resolve: shouldNotBeCalled2 @@ -23,10 +26,49 @@ describe('parseResource', () => { expect(cid.toString()).to.equal(testCID.toString()) expect(path).to.equal('') expect(query).to.deep.equal({}) + expect(ipfsPath).to.equal(`/ipfs/${testCID.toString()}`) }) it('throws an error if given an invalid resource', async () => { // @ts-expect-error - purposefully invalid input await expect(parseResource({}, stubInterface())).to.be.rejectedWith('Invalid resource.') }) + + describe('ipfsPath', () => { + let ipnsStub: StubbedInstance + + beforeEach(async () => { + ipnsStub = stubInterface({ + resolveDNSLink: sinon.stub().returns({ cid: testCID }), + resolve: sinon.stub().returns({ cid: testCID }) + }) + }); + + [ + // resource without paths + { resource: testCID, expectedValue: `/ipfs/${testCID}` }, + { resource: `ipfs://${testCID}`, expectedValue: `/ipfs/${testCID}` }, + { resource: `http://example.com/ipfs/${testCID}`, expectedValue: `/ipfs/${testCID}` }, + { resource: `ipns://${peerId}`, expectedValue: `/ipns/${peerId}` }, + { resource: `http://example.com/ipns/${peerId}`, expectedValue: `/ipns/${peerId}` }, + { resource: 'ipns://specs.ipfs.tech', expectedValue: '/ipns/specs.ipfs.tech' }, + { resource: 'http://example.com/ipns/specs.ipfs.tech', expectedValue: '/ipns/specs.ipfs.tech' }, + // resources with paths + { resource: `ipfs://${testCID}/foobar`, expectedValue: `/ipfs/${testCID}/foobar` }, + { resource: `http://example.com/ipfs/${testCID}/foobar`, expectedValue: `/ipfs/${testCID}/foobar` }, + { resource: `ipns://${peerId}/foobar`, expectedValue: `/ipns/${peerId}/foobar` }, + { resource: `http://example.com/ipns/${peerId}/foobar`, expectedValue: `/ipns/${peerId}/foobar` }, + { resource: 'ipns://specs.ipfs.tech/foobar', expectedValue: '/ipns/specs.ipfs.tech/foobar' }, + { resource: 'http://example.com/ipns/specs.ipfs.tech/foobar', expectedValue: '/ipns/specs.ipfs.tech/foobar' } + ].forEach(({ resource, expectedValue }) => { + it(`should return the correct ipfsPath for "${resource}"`, async () => { + const { ipfsPath } = await parseResource(resource, { + ipns: ipnsStub, + logger: defaultLogger() + }) + + expect(ipfsPath).to.equal(expectedValue) + }) + }) + }) }) diff --git a/packages/verified-fetch/test/verified-fetch.spec.ts b/packages/verified-fetch/test/verified-fetch.spec.ts index 9e202749..bb617e3a 100644 --- a/packages/verified-fetch/test/verified-fetch.spec.ts +++ b/packages/verified-fetch/test/verified-fetch.spec.ts @@ -157,6 +157,7 @@ describe('@helia/verifed-fetch', () => { expect(ipfsResponse).to.be.ok() expect(ipfsResponse.status).to.equal(301) expect(ipfsResponse.headers.get('location')).to.equal(`ipfs://${res.cid}/foo/`) + expect(ipfsResponse.headers.get('X-Ipfs-Path')).to.equal(`/ipfs/${res.cid}/foo`) expect(ipfsResponse.url).to.equal(`ipfs://${res.cid}/foo`) })