Course contentsShow
← Web Development
Lesson 2880 of 2,98251. Content Security PolicyFree lesson

CSP Headers in Development vs Production

Configure different CSP policies for development and production environments.

CSP Headers in Development vs Production

What you'll learn: How to configure different Content Security Policy rules for local development versus live production to balance security and developer experience.

Why Different Policies Matter

Just as you wouldn't use the same lock-and-key system for your home office and a bank vault, your CSP policies should reflect different security needs and workflows. In development, you need flexibility for hot-reloading, local debugging tools, and rapid iteration. In production, you need maximum security with no compromises.

Common Development Relaxations

During development, you might temporarily allow:

// Development CSP (more permissive)
Content-Security-Policy: 
  default-src 'self'; 
  script-src 'self' 'unsafe-inline' 'unsafe-eval' localhost:* ws:; 
  style-src 'self' 'unsafe-inline'; 
  connect-src 'self' localhost:* ws: wss:;

Notice 'unsafe-inline', 'unsafe-eval', and wildcards for localhost ports — these help with hot module replacement, dev servers, and debugging tools.

Production Hardening

Your production CSP should be strict and fully tested:

// Production CSP (locked down)
Content-Security-Policy: 
  default-src 'self'; 
  script-src 'self' 'nonce-{random}'; 
  style-src 'self' 'nonce-{random}'; 
  connect-src 'self' https://api.yourdomain.com;

All unsafe-* keywords are removed, nonces or hashes are used exclusively, and third-party domains are explicitly listed.

Implementation Strategy

Use environment variables to switch policies:

const cspHeader = process.env.NODE_ENV === 'production' 
  ? strictProductionCSP 
  : relaxedDevCSP;

Always test your production CSP in Report-Only mode first using real traffic before enforcing.

Key Takeaway: Maintain separate CSP configurations for development (permissive, dev-tool friendly) and production (strict, security-focused), and automate the switch based on your deployment environment.