This Cloudflare Worker automatically injects Content Security Policy (CSP) headers with dynamic nonces into HTML responses, helping to secure your website against XSS attacks.
- Automatically injects CSP headers with secure nonces
- Adds nonce attributes to all script tags
- Supports both enforcement and report-only modes
- Uses
strict-dynamicfor enhanced security - Compatible with modern browsers
- Zero performance impact - uses Cloudflare's HTMLRewriter for efficient streaming transformations
- No blocking or render delays - nonces are injected on-the-fly without buffering content
- Clone this repository:
git clone https://github.com/your-username/csp-nonce-worker.git
cd csp-nonce-worker- Install Wrangler, Cloudflare's CLI tool:
npm install -g @cloudflare/wrangler- Login to your Cloudflare account:
wrangler login- Set up your environment variables (optional):
wrangler secret put ENFORCE_CSP- Deploy the worker:
wrangler deployYou can configure the worker to run on specific routes in your Cloudflare dashboard:
- Go to your domain in Cloudflare dashboard
- Click "Workers Routes"
- Click "Add Route"
- Enter your route pattern (e.g.,
example.com/*) - Select your CSP worker
- Start with report-only mode by setting
ENFORCE_CSP = "false". This will add aContent-Security-Policy-Report-Onlyheader. - Monitor your browser's console for CSP violations.
- Address any legitimate scripts that are being blocked.
- Test thoroughly across different pages and functionality.
Once you're confident that all legitimate scripts are working:
- Update the environment variable:
ENFORCE_CSP = "true" - Deploy the updated configuration
- The worker will now use
Content-Security-Policyheader to enforce the policy
The CSP configuration includes:
script-src:'strict-dynamic': Allows scripts loaded by trusted scripts'nonce-[random]': Dynamic nonce for inline scripts'unsafe-inline': Fallback for older browsershttps:: Fallback for browsers not supporting strict-dynamic
style-src:'self': Allows loading stylesheets from same origin'nonce-[random]': Dynamic nonce for inline styles when STYLE_NONCE is enabled
object-src 'none': Prevents injection of pluginsbase-uri 'none': Prevents base tag hijackingupgrade-insecure-requests: Upgrades HTTP requests to HTTPS
Common issues you might encounter:
- Blocked Scripts: Check the browser console for CSP violation reports
- Third-party Scripts: Ensure they're loaded via HTTPS
- Inline Scripts: All inline scripts need the nonce attribute (automatically handled by the worker)
To run locally:
wrangler devThis allows you to test changes before deployment.
- Always start with report-only mode
- Monitor CSP violations in your browser's console
- Maintain a list of legitimate scripts that need to be allowed
- Test thoroughly across different pages and user scenarios
- Consider implementing CSP violation reporting to track issues
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.