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

feat: add content security policy header #172

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Changes from all 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
21 changes: 19 additions & 2 deletions packages/edge-gateway/src/gateway.js
Original file line number Diff line number Diff line change
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
}