Skip to content

Commit

Permalink
fix: gateway ipfs path error on invalid cid (#1548)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Mar 7, 2022
1 parent e74162a commit 6cc7ec8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion 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 @@ -16,7 +17,12 @@ export async function ipfsGet(request, env) {
const redirectQueryString = reqQueryString ? `?${reqQueryString}` : ''

// Parse and normalize CID
const nCid = normalizeCid(cid)
let nCid
try {
nCid = normalizeCid(cid)
} catch (err) {
throw new InvalidUrlError(`invalid CID: ${cid}: ${err.message}`)
}
const url = new URL(
`https://${nCid}.${env.GATEWAY_HOSTNAME}${redirectPath}${redirectQueryString}`
)
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 6cc7ec8

Please sign in to comment.