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: walking dag-cbor paths #39

Merged
merged 7 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
42 changes: 42 additions & 0 deletions packages/verified-fetch/src/utils/get-resolved-accept-header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { isExplicitAcceptHeader, isExplicitFormatQuery, isExplicitIpldAcceptRequest } from './is-accept-explicit.js'
import { queryFormatToAcceptHeader } from './select-output-type.js'
import type { ParsedUrlStringResults } from './parse-url-string.js'
import type { ComponentLogger } from '@libp2p/interface'

export interface ResolvedAcceptHeaderOptions {
query?: ParsedUrlStringResults['query']
headers?: RequestInit['headers']
logger?: ComponentLogger
}

export function getResolvedAcceptHeader ({ query, headers, logger }: ResolvedAcceptHeaderOptions): string | undefined {
const log = logger?.forComponent('helia:verified-fetch:get-resolved-accept-header')
const requestHeaders = new Headers(headers)
const incomingAcceptHeader = requestHeaders.get('accept') ?? undefined

if (incomingAcceptHeader != null) {
log?.('incoming accept header "%s"', incomingAcceptHeader)
}

if (!isExplicitIpldAcceptRequest({ query, headers: requestHeaders })) {
log?.('no explicit IPLD content-type requested, returning incoming accept header %s', incomingAcceptHeader)
return incomingAcceptHeader
}

const queryFormatMapping = queryFormatToAcceptHeader(query?.format)

if (query?.format != null) {
log?.('incoming query format "%s", mapped to %s', query.format, queryFormatMapping)
}

let acceptHeader = incomingAcceptHeader
// if the incomingAcceptHeader is autogenerated by the requesting client (browser/curl/fetch/etc) then we may need to override it if query.format is specified
if (!isExplicitAcceptHeader(requestHeaders) && isExplicitFormatQuery(query)) {
log?.('accept header not recognized, but query format provided, setting accept header to %s', queryFormatMapping)
acceptHeader = queryFormatMapping
}

log?.('resolved accept header to "%s"', acceptHeader)

return acceptHeader
}
32 changes: 32 additions & 0 deletions packages/verified-fetch/src/utils/is-accept-explicit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { FORMAT_TO_MIME_TYPE } from './select-output-type.js'
import type { ParsedUrlStringResults } from './parse-url-string.js'

export interface IsAcceptExplicitOptions {

query?: ParsedUrlStringResults['query']
headers: Headers
}

export function isExplicitAcceptHeader (headers: Headers): boolean {
const incomingAcceptHeader = headers.get('accept')
if (incomingAcceptHeader != null && Object.values(FORMAT_TO_MIME_TYPE).includes(incomingAcceptHeader)) {
return true
}
return false
}

export function isExplicitFormatQuery (query?: ParsedUrlStringResults['query']): boolean {
const formatQuery = query?.format
if (formatQuery != null && Object.keys(FORMAT_TO_MIME_TYPE).includes(formatQuery)) {
return true
}
return false
}

/**
* The user can provide an explicit `accept` header in the request headers or a `format` query parameter in the URL.
* If either of these are provided, this function returns true.
*/
export function isExplicitIpldAcceptRequest ({ query, headers }: IsAcceptExplicitOptions): boolean {
return isExplicitAcceptHeader(headers) || isExplicitFormatQuery(query)
}
13 changes: 13 additions & 0 deletions packages/verified-fetch/src/utils/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ export function notAcceptableResponse (url: string, body?: SupportedBodyTypes, i
return response
}

export function notFoundResponse (url: string, body?: SupportedBodyTypes, init?: ResponseInit): Response {
const response = new Response(body, {
...(init ?? {}),
status: 404,
statusText: 'Not Found'
})

setType(response, 'basic')
setUrl(response, url)

return response
}

/**
* if body is an Error, it will be converted to a string containing the error message.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/verified-fetch/src/utils/select-output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const CID_TYPE_MAP: Record<number, string[]> = {
'application/octet-stream',
'application/vnd.ipld.raw',
'application/vnd.ipfs.ipns-record',
'application/vnd.ipld.dag-json',
'application/vnd.ipld.car',
'application/x-tar'
]
Expand Down Expand Up @@ -145,7 +146,7 @@ function parseQFactor (str?: string): number {
return factor
}

const FORMAT_TO_MIME_TYPE: Record<RequestFormatShorthand, string> = {
export const FORMAT_TO_MIME_TYPE: Record<RequestFormatShorthand, string> = {
raw: 'application/vnd.ipld.raw',
car: 'application/vnd.ipld.car',
'dag-json': 'application/vnd.ipld.dag-json',
Expand Down
9 changes: 7 additions & 2 deletions packages/verified-fetch/src/utils/walk-path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { walkPath as exporterWalk, type ExporterOptions, type ReadableStorage, type UnixFSEntry } from 'ipfs-unixfs-exporter'
import { CodeError } from '@libp2p/interface'
import { walkPath as exporterWalk, type ExporterOptions, type ReadableStorage, type ObjectNode, type UnixFSEntry } from 'ipfs-unixfs-exporter'
import type { CID } from 'multiformats/cid'

export interface PathWalkerOptions extends ExporterOptions {
Expand All @@ -24,11 +25,15 @@
}

if (terminalElement == null) {
throw new Error('No terminal element found')
throw new CodeError('No terminal element found', 'NO_TERMINAL_ELEMENT')

Check warning on line 28 in packages/verified-fetch/src/utils/walk-path.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/utils/walk-path.ts#L28

Added line #L28 was not covered by tests
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
}

return {
ipfsRoots,
terminalElement
}
}

export function objectNodeGuard (node: UnixFSEntry): node is ObjectNode {
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
return node.type === 'object'
}
70 changes: 47 additions & 23 deletions packages/verified-fetch/src/verified-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
import { dagCborToSafeJSON } from './utils/dag-cbor-to-safe-json.js'
import { getContentDispositionFilename } from './utils/get-content-disposition-filename.js'
import { getETag } from './utils/get-e-tag.js'
import { getResolvedAcceptHeader } from './utils/get-resolved-accept-header.js'
import { getStreamFromAsyncIterable } from './utils/get-stream-from-async-iterable.js'
import { tarStream } from './utils/get-tar-stream.js'
import { parseResource } from './utils/parse-resource.js'
import { setCacheControlHeader } from './utils/response-headers.js'
import { badRequestResponse, movedPermanentlyResponse, notAcceptableResponse, notSupportedResponse, okResponse, badRangeResponse, okRangeResponse, badGatewayResponse } from './utils/responses.js'
import { selectOutputType, queryFormatToAcceptHeader } from './utils/select-output-type.js'
import { walkPath } from './utils/walk-path.js'
import { badRequestResponse, movedPermanentlyResponse, notAcceptableResponse, notSupportedResponse, okResponse, badRangeResponse, okRangeResponse, badGatewayResponse, notFoundResponse } from './utils/responses.js'
import { selectOutputType } from './utils/select-output-type.js'
import { objectNodeGuard, walkPath } from './utils/walk-path.js'
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
import type { CIDDetail, ContentTypeParser, Resource, VerifiedFetchInit as VerifiedFetchOptions } from './index.js'
import type { RequestFormatShorthand } from './types.js'
import type { ParsedUrlStringResults } from './utils/parse-url-string'
import type { Helia } from '@helia/interface'
import type { DNSResolver } from '@multiformats/dns/resolvers'
import type { UnixFSEntry } from 'ipfs-unixfs-exporter'
import type { ObjectNode, UnixFSEntry } from 'ipfs-unixfs-exporter'
import type { CID } from 'multiformats/cid'

interface VerifiedFetchComponents {
Expand Down Expand Up @@ -93,6 +94,7 @@
* skipped and set to these values.
*/
const RAW_HEADERS = [
'application/vnd.ipld.dag-json',
'application/vnd.ipld.raw',
'application/octet-stream'
]
Expand All @@ -103,8 +105,9 @@
* type. This avoids the user from receiving something different when they
* signal that they want to `Accept` a specific mime type.
*/
function getOverridenRawContentType (headers?: HeadersInit): string | undefined {
const acceptHeader = new Headers(headers).get('accept') ?? ''
function getOverridenRawContentType ({ headers, accept }: { headers?: HeadersInit, accept?: string }): string | undefined {
// accept has already been resolved by getResolvedAcceptHeader, if we have it, use it.
const acceptHeader = accept ?? new Headers(headers).get('accept') ?? ''

// e.g. "Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8"
const acceptHeaders = acceptHeader.split(',')
Expand Down Expand Up @@ -233,8 +236,33 @@

private async handleDagCbor ({ resource, cid, path, accept, options }: FetchHandlerFunctionArg): Promise<Response> {
this.log.trace('fetching %c/%s', cid, path)
let terminalElement: ObjectNode | undefined
let ipfsRoots: CID[] | undefined

// need to walk path, if it exists, to get the terminal element
try {
const pathDetails = await walkPath(this.helia.blockstore, `${cid.toString()}/${path}`, options)
ipfsRoots = pathDetails.ipfsRoots
const potentialTerminalElement = pathDetails.terminalElement
if (potentialTerminalElement == null) {
return notFoundResponse(resource.toString())

Check warning on line 248 in packages/verified-fetch/src/verified-fetch.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L248 was not covered by tests
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
}
if (objectNodeGuard(potentialTerminalElement)) {
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
terminalElement = potentialTerminalElement
}
} catch (err: any) {
if (options?.signal?.aborted === true) {
throw new AbortError('signal aborted by user')
}

Check warning on line 256 in packages/verified-fetch/src/verified-fetch.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/verified-fetch.ts#L255-L256

Added lines #L255 - L256 were not covered by tests
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
if (['ERR_NO_PROP', 'NO_TERMINAL_ELEMENT'].includes(err.code)) {
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
return notFoundResponse(resource.toString())
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
}

this.log.error('error walking path %s', path, err)
return badGatewayResponse(resource.toString(), 'Error walking path')

Check warning on line 262 in packages/verified-fetch/src/verified-fetch.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/verified-fetch.ts#L260-L262

Added lines #L260 - L262 were not covered by tests
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
}
const block = terminalElement?.node ?? await this.helia.blockstore.get(cid, options)

const block = await this.helia.blockstore.get(cid, options)
let body: string | Uint8Array

if (accept === 'application/octet-stream' || accept === 'application/vnd.ipld.dag-cbor' || accept === 'application/cbor') {
Expand Down Expand Up @@ -274,9 +302,14 @@

response.headers.set('content-type', accept)

if (ipfsRoots != null) {
response.headers.set('X-Ipfs-Roots', ipfsRoots.map(cid => cid.toV1().toString()).join(',')) // https://specs.ipfs.tech/http-gateways/path-gateway/#x-ipfs-roots-response-header
}

return response
}

// eslint-disable-next-line complexity
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
private async handleDagPb ({ cid, path, resource, options }: FetchHandlerFunctionArg): Promise<Response> {
let terminalElement: UnixFSEntry | undefined
let ipfsRoots: CID[] | undefined
Expand All @@ -291,6 +324,9 @@
if (options?.signal?.aborted === true) {
throw new AbortError('signal aborted by user')
}
if (['ERR_NO_PROP', 'NO_TERMINAL_ELEMENT'].includes(err.code)) {
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
return notFoundResponse(resource.toString())

Check warning on line 328 in packages/verified-fetch/src/verified-fetch.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L328 was not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

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

404 on bad paths for unixfs now too

}
this.log.error('error walking path %s', path, err)

return badGatewayResponse(resource.toString(), 'Error walking path')
Expand Down Expand Up @@ -385,7 +421,7 @@
}
}

private async handleRaw ({ resource, cid, path, options }: FetchHandlerFunctionArg): Promise<Response> {
private async handleRaw ({ resource, cid, path, options, accept }: FetchHandlerFunctionArg): Promise<Response> {
const byteRangeContext = new ByteRangeContext(this.helia.logger, options?.headers)
const result = await this.helia.blockstore.get(cid, options)
byteRangeContext.setBody(result)
Expand All @@ -396,7 +432,7 @@
// if the user has specified an `Accept` header that corresponds to a raw
// type, honour that header, so for example they don't request
// `application/vnd.ipld.raw` but get `application/octet-stream`
const overriddenContentType = getOverridenRawContentType(options?.headers)
const overriddenContentType = getOverridenRawContentType({ headers: options?.headers, accept })
if (overriddenContentType != null) {
response.headers.set('content-type', overriddenContentType)
} else {
Expand Down Expand Up @@ -499,20 +535,8 @@

options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:resolve', { cid, path }))

const requestHeaders = new Headers(options?.headers)
const incomingAcceptHeader = requestHeaders.get('accept')

if (incomingAcceptHeader != null) {
this.log('incoming accept header "%s"', incomingAcceptHeader)
}

const queryFormatMapping = queryFormatToAcceptHeader(query.format)

if (query.format != null) {
this.log('incoming query format "%s", mapped to %s', query.format, queryFormatMapping)
}
const acceptHeader = getResolvedAcceptHeader({ query, headers: options?.headers, logger: this.helia.logger })

const acceptHeader = incomingAcceptHeader ?? queryFormatMapping
const accept = selectOutputType(cid, acceptHeader)
this.log('output type %s', accept)

Expand All @@ -523,7 +547,7 @@
let response: Response
let reqFormat: RequestFormatShorthand | undefined

const handlerArgs = { resource: resource.toString(), cid, path, accept, options }
const handlerArgs: FetchHandlerFunctionArg = { resource: resource.toString(), cid, path, accept, options }

if (accept === 'application/vnd.ipfs.ipns-record') {
// the user requested a raw IPNS record
Expand Down
90 changes: 90 additions & 0 deletions packages/verified-fetch/test/verified-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,94 @@ describe('@helia/verifed-fetch', () => {
expect(output).to.deep.equal(obj)
})
})

describe('?format', () => {
let helia: Helia
let verifiedFetch: VerifiedFetch
let contentTypeParser: Sinon.SinonStub

beforeEach(async () => {
contentTypeParser = Sinon.stub()
helia = await createHelia()
verifiedFetch = new VerifiedFetch({
helia
}, {
contentTypeParser
})
})

afterEach(async () => {
await stop(helia, verifiedFetch)
})

it('cbor?format=dag-json should be able to override curl/browser default accept header when query parameter is provided', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)

const resp = await verifiedFetch.fetch(`http://example.com/ipfs/${cid}?format=dag-json`, {
headers: {
// see https://github.com/ipfs/helia-verified-fetch/issues/35
// accept: '*/*'
}
})
expect(resp.headers.get('content-type')).to.equal('application/vnd.ipld.dag-json')
const data = ipldDagJson.decode(await resp.arrayBuffer())
expect(data).to.deep.equal(obj)
})

it('raw?format=dag-json should be able to override curl/browser default accept header when query parameter is provided', async () => {
const finalRootFileContent = uint8ArrayFromString(JSON.stringify({
hello: 'world'
}))
const cid = CID.createV1(raw.code, await sha256.digest(finalRootFileContent))
await helia.blockstore.put(cid, finalRootFileContent)

const resp = await verifiedFetch.fetch(`http://example.com/ipfs/${cid}?format=dag-json`, {
headers: {
// see https://github.com/ipfs/helia-verified-fetch/issues/35
accept: '*/*'
}
})
expect(resp).to.be.ok()
expect(resp.status).to.equal(200)
expect(resp.statusText).to.equal('OK')
const data = await resp.arrayBuffer()
expect(resp.headers.get('content-type')).to.equal('application/vnd.ipld.dag-json')
expect(new Uint8Array(data)).to.equalBytes(finalRootFileContent)
})
})

describe('404 paths', () => {
let helia: Helia
let verifiedFetch: VerifiedFetch
let contentTypeParser: Sinon.SinonStub

beforeEach(async () => {
contentTypeParser = Sinon.stub()
helia = await createHelia()
verifiedFetch = new VerifiedFetch({
helia
}, {
contentTypeParser
})
})

afterEach(async () => {
await stop(helia, verifiedFetch)
})

it('returns a 404 when walking dag-cbor for non-existent path', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)

const resp = await verifiedFetch.fetch(`http://example.com/ipfs/${cid}/foo/i-do-not-exist`)
expect(resp.status).to.equal(404)
})
})
})
Loading