Web Security Headers Cheat Sheet
Quick reference guide for Web Security Headers: CSP, HSTS, X-Frame-Options, CORS, and security policy rules.
Security Tools
security-headers
csp
hsts
HTTP security headers tell browsers how to behave when handling your site's content to protect against XSS, clickjacking, and MIME sniffing.
Essential Security Headers Summary
Table
| Header Name | Recommended Value | Protection Goal |
|---|---|---|
Content-Security-Policy | default-src 'self'; script-src 'self' | Prevent XSS and unauthorized data injection |
Strict-Transport-Security | max-age=31536000; includeSubDomains | Force HTTPS connection (HSTS) |
X-Frame-Options | DENY or SAMEORIGIN | Protect against Clickjacking attacks in <iframe> |
X-Content-Type-Options | nosniff | Prevent MIME type sniffing by browsers |
Referrer-Policy | strict-origin-when-cross-origin | Control URL referrer information leakage |
Permissions-Policy | camera=(), microphone=(), geolocation=() | Restrict browser hardware API capabilities |
Content Security Policy (CSP) Directives
http
Content-Security-Policy:
default-src 'self';
script-src 'self' https://trustedscripts.example.com;
style-src 'self' 'unsafe-inline';
img-src 'self' data: https:;
font-src 'self' https://fonts.gstatic.com;
connect-src 'self' https://api.example.com;
frame-ancestors 'none';
base-uri 'self';
form-action 'self';
CORS Headers Reference
Table
| CORS Header | Direction | Description |
|---|---|---|
Access-Control-Allow-Origin | Response | Whitelisted origin (https://example.com) or * |
Access-Control-Allow-Methods | Response | Allowed HTTP methods (GET, POST, OPTIONS, PUT) |
Access-Control-Allow-Headers | Response | Allowed request headers (Content-Type, Authorization) |
Access-Control-Allow-Credentials | Response | Set to true to allow cookies in cross-origin requests |
Access-Control-Max-Age | Response | Preflight OPTIONS cache duration in seconds (86400) |
Common Pitfalls & Tips
[!WARNING] Setting
Access-Control-Allow-Origin: *while also settingAccess-Control-Allow-Credentials: trueis forbidden by browser CORS security specifications!
[!TIP] Use
Content-Security-Policy-Report-Onlyduring initial deployment to test CSP policies without breaking site functionality.