Browser security mechanism that controls which resources a webpage can load. Prevents code injection attacks (XSS, clickjacking) by restricting sources of scripts, styles, images, and other content.
How It Works
Server sends CSP headers or webpage includes CSP meta tags. Browser enforces the policy by blocking unauthorized resource loads and reporting violations.
<!-- Via meta tag --><meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' https://trusted.com"><!-- Via HTTP header (preferred) -->Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted.com
Key Directives
Content Types
default-src - Fallback for all other directives
script-src - JavaScript sources
style-src - CSS sources
img-src - Image sources
connect-src - AJAX, WebSocket, fetch() sources
font-src - Font sources
media-src - Audio/video sources
frame-src - iframe sources
Common Source Values
'self' - Same origin only
'none' - Block all sources
'unsafe-inline' - Allow inline scripts/styles (avoid if possible)
XSS Prevention: Blocks malicious script injection
Data Theft Protection: Controls where data can be sent Clickjacking Defense: Restricts iframe embedding
Mixed Content Protection: Enforces HTTPS usage
Common Gotchas
Inline scripts/styles blocked: Use nonces or external files
eval() blocked: Avoid dynamic code execution
Third-party widgets: May require policy adjustments
Legacy code: Often relies on ‘unsafe-inline’
Debugging
Check browser console for CSP violation messages
Use Report-Only mode during development
Browser dev tools show blocked resources
Test with strict policy first, then relax as needed
Browser security mechanism that allows servers to specify which origins can access their resources, bypassing the same-origin policy.
The Problem
Same-origin policy blocks cross-domain requests by default:
// ❌ Blocked without CORS// Page: https://myapp.comfetch('https://api.external.com/data')
How It Works
Browser sends request with Origin header
Server responds with CORS headers (or doesn’t)
Browser allows/blocks based on server response
For complex (non-simple) requests, browser sends preflight OPTIONS request first.
Complex/Non-Simple Requeset that triggers OPTIONS preflight
Complex/Non-Simple CORS Requeset that triggers OPTIONS preflight
Complex/Non-Simple CORS Requests & OPTIONS Preflight
What is a Non-Simple CORS Request?
A non-simple (or complex) CORS request is any cross-origin request that doesn’t meet the criteria for a “simple request”. These requests trigger an automatic OPTIONS preflight request from the browser before the actual request is sent.
Simple vs Non-Simple Requests
Simple CORS Requests (No Options Preflight)
Simple requests must meet ALL of the following criteria:
curl does NOT send preflight OPTIONS requests. It only sends the exact request you specify. To see the complete CORS preflight behavior, you must test from:
A web browser
Browser developer tools
Testing tools that simulate browser CORS behavior
curl is useful for testing the server’s response to individual requests, but won’t replicate the full browser CORS flow.