Skip to content

Commit

Permalink
fix: gateway ipfs path error on invalid cid
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Mar 3, 2022
1 parent 3daefd5 commit 41b5022
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/gateway/src/ipfs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { normalizeCid } from './utils/cid.js'
import { InvalidUrlError } from './errors.js'

/**
* Handle gateway request
Expand All @@ -10,8 +11,14 @@ export async function ipfsGet(request, env) {
const cid = request.params.cid
const path = request.url.split(cid)[1] || ''

// Parse and normalize CID
const nCid = normalizeCid(cid)
// Parse and normalize CID!
let nCid
try {
nCid = normalizeCid(cid)
} catch (err) {
throw new InvalidUrlError(`invalid CID: ${cid}: ${err.message}`)
}

const url = `https://${nCid}.${env.GATEWAY_HOSTNAME}${path}`

return Response.redirect(url, 302)
Expand Down
16 changes: 16 additions & 0 deletions packages/gateway/test/ipfs-path.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test from 'ava'
import { createErrorHtmlContent } from '../src/errors.js'

import { getMiniflare } from './utils.js'

Expand All @@ -9,6 +10,21 @@ test.beforeEach((t) => {
}
})

test('Fails when invalid cid with IPFS canonical resolution', async (t) => {
const { mf } = t.context

const response = await mf.dispatchFetch(
'https://localhost:8787/ipfs/bafy.../path'
)
t.is(response.status, 400)

const textResponse = await response.text()
t.is(
textResponse,
createErrorHtmlContent(400, 'invalid CID: bafy...: Non-base32 character')
)
})

test('should resolve a cid v0 with IPFS canonical resolution', async (t) => {
const { mf } = t.context

Expand Down

0 comments on commit 41b5022

Please sign in to comment.