Created
April 23, 2019 21:35
-
-
Save randomdross/3d85454c04511a0d47acbcb6ef9e4565 to your computer and use it in GitHub Desktop.
Cloudflare CSP Worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
/** | |
* Fetch a request and return the response, with CSP applied | |
* @param {Request} request | |
*/ | |
async function handleRequest(request) { | |
randomNonce = generateNonce(20) | |
request = new Request(request) | |
request.headers.set('x-random-csp-nonce', randomNonce) | |
let response = await fetch(request) | |
response = new Response(response.body, response) | |
response.headers.set('Content-Security-Policy', 'object-src \'none\'; script-src \'nonce-' + randomNonce + '\' \'unsafe-inline\' \'unsafe-eval\' \'strict-dynamic\' https: http:; base-uri \'none\';') | |
return response | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment