Command Palette

Search for a command to run...

Back to skills
skill № 39836

content-security-policy

Design and deploy Content-Security-Policy (CSP) to prevent XSS attacks and unauthorized resource loading.

FreeNo executable scripts
sethdfordby sethdford★ 51 GitHub stars

Install content-security-policy in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

About this skill

Content-Security-Policy

Design and deploy CSP to prevent XSS and unauthorized resource loading.

Context

You are a senior security engineer implementing CSP for $ARGUMENTS. CSP is the browser's defense against XSS by controlling which scripts, styles, and resources can load. A well-designed CSP blocks inline scripts and restricts external resources to trusted domains, making XSS significantly harder to exploit.

Domain Context

  • CSP Directives: default-src, script-src, style-src, img-src, font-src, connect-src, frame-src, form-action, base-uri
  • Values: 'self' (same origin), 'none' (block all), domain whitelist, nonces (random tokens per request), hashes (SHA-256)
  • Violation Reporting: report-uri or report-to for CSP violations; use to detect XSS attempts and policy issues
  • Browser Support: Widely supported in modern browsers; older browsers ignore CSP (graceful degradation)

Instructions

  1. Start with Report-Only Mode:

    • Deploy CSP with Content-Security-Policy-Report-Only header first
    • Set report-uri to collect violations
    • Monitor for 1-2 weeks; fix legitimate violations (misconfigured resources)
    • Then enforce with Content-Security-Policy header (report-only + enforcement can coexist)
  2. Design Base Policy:

    • default-src 'self' — allow same-origin resources by default
    • script-src 'self' — allow same-origin scripts only (no inline scripts)
    • style-src 'self' — allow same-origin styles only (no inline styles)
    • img-src 'self' data: https: — allow same-origin, data URIs, and HTTPS images
    • font-src 'self' — allow same-origin fonts only
    • connect-src 'self' — allow same-origin API calls only
  3. Allow Trusted External Resources:

    • If you need external CDNs: script-src 'self' cdn.jsdelivr.net
    • If you need Google Fonts: font-src 'self' fonts.googleapis.com; style-src 'self' fonts.googleapis.com
    • Always prefer HTTPS for external resources
    • Whitelist specific subdomains, not entire domains (e.g., trusted-cdn.com not *.example.com)
  4. Secure Inline Scripts & Styles:

    • Option 1 (Nonces): Generate random nonce per request; include in <script nonce="abc123"> and CSP header
      • script-src 'nonce-abc123' — only that script with matching nonce executes
    • Option 2 (Hashes): Hash inline content; include in CSP header
      • script-src 'sha256-hash' — only scripts matching hash execute
    • Option 3 (Refactor): Move inline scripts to external files (best practice)
    • Never use 'unsafe-inline' or 'unsafe-eval' unless absolutely necessary (defeats XSS protection)
  5. Monitor & Iterate:

    • Review CSP violations in logs; identify patterns (third-party analytics, ads, etc.)
    • Add trusted domains as needed, but conservatively
    • Tighten policy over time (remove unnecessary directives)
    • Test CSP in different browsers (Chrome, Firefox, Safari, Edge)
    • Document policy rationale for future maintainers

Anti-Patterns

  • Setting default-src * or overly permissive policies; this provides no protection against XSS
  • Using 'unsafe-inline' to fix CSP violations; refactor code instead; inline scripts defeat CSP's purpose
  • Not using nonces/hashes; without them, you must rely on domain whitelisting, which is fragile
  • Ignoring CSP violation reports; violations indicate XSS attempts or misconfigured resources
  • Setting CSP on sensitive endpoints only; apply consistently across entire application

Further Reading

Full SKILL.md on GitHub

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

FAQ

What does the content-security-policy skill do?

Design and deploy Content-Security-Policy (CSP) to prevent XSS attacks and unauthorized resource loading.

How do I install the content-security-policy skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the content-security-policy skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare content-security-policy with