CommandOpen
Security (csp)
Generate Content Security Policy headers for a web application.
Generate Content Security Policy headers for a web application.
Steps
- Scan the project for frontend assets and their sources:
- JavaScript files: inline scripts, external CDN scripts, dynamic imports.
- CSS files: inline styles, external stylesheets, CSS-in-JS libraries.
- Images: local assets, external image CDNs, data URIs.
- Fonts: Google Fonts, self-hosted, CDN-hosted.
- API calls:
fetch,XMLHttpRequest, WebSocket connections. - Frames: iframes, embedded content.
- Identify all external domains referenced in the codebase.
- Build CSP directives:
default-src: Fallback policy.script-src: JavaScript sources with nonce or hash strategy.style-src: CSS sources.img-src: Image sources.connect-src: API endpoints, WebSocket URLs.font-src: Font sources.frame-src: Iframe sources.object-src: Plugin sources (should be'none').
- Add reporting configuration:
report-uriorreport-to. - Generate both enforcing and report-only headers.
- Output as HTTP header format and as meta tag format.
Format
Content-Security-Policy:
default-src 'self';
script-src 'self' 'nonce-{random}' https://cdn.example.com;
style-src 'self' 'unsafe-inline';
img-src 'self' data: https://images.example.com;
connect-src 'self' https://api.example.com;
font-src 'self' https://fonts.gstatic.com;
object-src 'none';
frame-ancestors 'none';
report-uri /csp-report;Rules
- Never use
unsafe-inlinefor scripts; prefer nonces or hashes. - Always include
object-src 'none'andframe-ancestors 'self'. - Start with a strict policy and relax only as needed.
- Provide a
Content-Security-Policy-Report-Onlyheader for testing. - Document each allowed domain with a comment explaining why it is needed.
Source: https://github.com/rohitg00/awesome-claude-code-toolkit/blob/main/commands/security/csp.md
Author: rohitg00, Claude Code toolkit (rohitg00). License: Apache-2.0.
Shown here with formatting for the web. Keep the author and license notice when you reuse it.
Related entries
- Security Checklist
- Frontend JavaScript/TypeScript Web Security Spec (Vanilla Browser JS/TS, Modern Browsers)
- Backend Security Practices
- jQuery Frontend Security Spec (jQuery 4.0.x, modern browsers)
- Express (Node.js) Web Security Spec (Express 5.x / 4.19.2+, Node.js LTS)
- Single-File HTML Discipline - Why Inline + CDN-Only Externals
There are more like this in the members library
The Codex library holds 7,009 entries. Members can search all of them by meaning and read every one in full.