Skip to content

Commit

Permalink
feat: add content security policy header (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 15, 2022
1 parent 182641a commit 69d0258
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/edge-gateway/src/gateway.js
Expand Up @@ -117,7 +117,7 @@ export async function gatewayIpfs(request, env, ctx, options = {}) {
const responseTime = Date.now() - startTs

options.onCdnResolution && options.onCdnResolution(res, responseTime)
return res
return getTransformedResponseWithCustomHeaders(res)
} else if (
(request.headers.get('Cache-Control') || '').includes('only-if-cached')
) {
Expand Down Expand Up @@ -167,7 +167,7 @@ export async function gatewayIpfs(request, env, ctx, options = {}) {
)

// forward winner gateway response
return winnerGwResponse.response
return getTransformedResponseWithCustomHeaders(winnerGwResponse.response)
} catch (err) {
const responses = await pSettle(gatewayReqs)

Expand Down Expand Up @@ -525,3 +525,20 @@ function getDurableRequestUrl(request, route, data) {
body: data && JSON.stringify(data),
})
}

/**
* Transforms race response with custom headers.
* Content-Security-Policy header specified to only allow requests within same origin.
*
* @param {Response} response
*/
function getTransformedResponseWithCustomHeaders(response) {
const clonedResponse = new Response(response.body, response)

clonedResponse.headers.set(
'content-security-policy',
"default-src 'self' 'unsafe-inline' blob: data: ; form-action 'self' ; navigate-to 'self' "
)

return clonedResponse
}

0 comments on commit 69d0258

Please sign in to comment.