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 csp report endpoint #209

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/edge-gateway/src/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface EnvInput {
LOKI_TOKEN?: string
EDGE_GATEWAY: Fetcher
GATEWAY_HOSTNAME: string
CSP_REPORT_URI: string
GOODBITSLIST: KVNamespace
}

Expand Down
33 changes: 22 additions & 11 deletions packages/edge-gateway/src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const IPFS_GATEWAYS = [
'https://ipfs.io/ipfs/',
]
const DOTSTORAGE_APIS = ['https://*.web3.storage', 'https://*.nft.storage']
const ALLOWED_LIST = [
'https://*.githubusercontent.com',
'https://polygon-rpc.com',
'https://rpc.testnet.fantom.network',
]

/**
* Handle gateway requests
Expand Down Expand Up @@ -50,31 +55,37 @@ export async function gatewayGet(request, env) {
return response
}

return getTransformedResponseWithCspHeaders(response)
return getTransformedResponseWithCspHeaders(response, env)
}

/**
* Transforms response with custom headers.
* Content-Security-Policy header specified to only allow requests within same origin.
*
* @param {Response} response
* @param {import('./bindings').Env} env
*/
function getTransformedResponseWithCspHeaders(response) {
function getTransformedResponseWithCspHeaders(response, env) {
const clonedResponse = new Response(response.body, response)
const defaultSrc = `'self' 'unsafe-inline' 'unsafe-eval' blob: data: ${IPFS_GATEWAYS.join(
' '
)} ${DOTSTORAGE_APIS.join(' ')} ${ALLOWED_LIST.join(' ')}`
const connectSrc = `'self' blob: data: ${IPFS_GATEWAYS.join(
' '
)} ${DOTSTORAGE_APIS.join(' ')} ${ALLOWED_LIST.join(' ')}`
const reportUri = env.CSP_REPORT_URI

clonedResponse.headers.set(
'content-security-policy',
`default-src 'self' 'unsafe-inline' 'unsafe-eval' blob: data: ${IPFS_GATEWAYS.join(
' '
)} ${DOTSTORAGE_APIS.join(
' '
)} https://*.githubusercontent.com; form-action 'self'; navigate-to 'self'; connect-src 'self' blob: data: ${IPFS_GATEWAYS.join(
' '
)} ${DOTSTORAGE_APIS.join(
' '
)} https://polygon-rpc.com https://rpc.testnet.fantom.network`
`default-src ${defaultSrc} ; form-action 'self'; navigate-to 'self'; connect-src ${connectSrc} ; report-to csp-endpoint ; report-uri ${reportUri}`
)

reportUri &&
clonedResponse.headers.set(
'reporting-endpoints',
`csp-endpoint="${reportUri}"`
)

return clonedResponse
}

Expand Down
2 changes: 2 additions & 0 deletions packages/edge-gateway/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kv_namespaces = [

[env.production.vars]
GATEWAY_HOSTNAME = 'ipfs.nftstorage.link'
CSP_REPORT_URI = 'https://csp-report-to.web3.storage'
DEBUG = "false"
ENV = "production"

Expand All @@ -58,6 +59,7 @@ kv_namespaces = [

[env.staging.vars]
GATEWAY_HOSTNAME = 'ipfs-staging.nftstorage.link'
CSP_REPORT_URI = 'https://staging.csp-report-to.web3.storage'
DEBUG = "true"
ENV = "staging"

Expand Down