Skip to content

Commit

Permalink
fix: check deny list before cache and fix blocked CIDs (#1748)
Browse files Browse the repository at this point in the history
This PR moves the check for blocked CIDs before the cache retrieval code since we may have already retrieved content we later block.

It also fixes the anchor values in our local denylist, which were generated before interop was checked and then I forgot to update them 🤦‍♂️.

A couple of other minor tweaks. We need to refactor the log stuff a little in a future PR - I had an error that was thrown before `env.log` was set...
  • Loading branch information
Alan Shaw authored Apr 1, 2022
1 parent a257c42 commit fbd2922
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
9 changes: 3 additions & 6 deletions packages/gateway/denylist.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
[
{
"anchor": "83d6b787245835ca357e405725a49493fd4552774355cf647b210b3bece2"
"anchor": "363783d6b787245835ca357e405725a49493fd4552774355cf647b210b3bece2"
},
{
"anchor": "f6f1c0a3c3f9d20741645e911abdc01dd46f5086b8eb5e1803ba011c8e23"
"anchor": "d7daf6f1c0a3c3f9d20741645e911abdc01dd46f5086b8eb5e1803ba011c8e23"
},
{
"anchor": "e665be4831882ae4137b07b8b98d80c3b742b72a491da5fdff4e4b4afc6c"
},
{
"anchor": "86f92f56ce525091b625a67d942360b15a5797b75b8b33d57ae91d2b7223"
"anchor": "0684e665be4831882ae4137b07b8b98d80c3b742b72a491da5fdff4e4b4afc6c"
}
]
24 changes: 12 additions & 12 deletions packages/gateway/src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ import {
*/
export async function gatewayGet(request, env, ctx) {
const startTs = Date.now()
const cache = caches.default
let res = await cache.match(request.url)

if (res) {
// Update cache metrics in background
const responseTime = Date.now() - startTs

ctx.waitUntil(updateSummaryCacheMetrics(request, env, res, responseTime))
return res
}

const reqUrl = new URL(request.url)
const cid = getCidFromSubdomainUrl(reqUrl)
const pathname = reqUrl.pathname
Expand All @@ -65,6 +54,17 @@ export async function gatewayGet(request, env, ctx) {
}
}

const cache = caches.default
const res = await cache.match(request.url)

if (res) {
// Update cache metrics in background
const responseTime = Date.now() - startTs

ctx.waitUntil(updateSummaryCacheMetrics(request, env, res, responseTime))
return res
}

// Prepare IPFS gateway requests
const shouldPreventRateLimit = await getGatewayRateLimitState(request, env)
const gatewayReqs = env.ipfsGateways.map((gwUrl) =>
Expand Down Expand Up @@ -153,7 +153,7 @@ export async function gatewayGet(request, env, ctx) {
// Gateway timeout
if (
responses[0].value?.aborted &&
responses[0].value?.reason == TIMEOUT_CODE
responses[0].value?.reason === TIMEOUT_CODE
) {
throw new TimeoutError()
}
Expand Down
7 changes: 5 additions & 2 deletions packages/gateway/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ export default {
env.log.timeEnd('request')
return env.log.end(res)
} catch (error) {
env.log.timeEnd('request')
return env.log.end(serverError(error, request, env))
if (env.log) {
env.log.timeEnd('request')
return env.log.end(serverError(error, request, env))
}
return serverError(error, request, env)
}
},
}

0 comments on commit fbd2922

Please sign in to comment.