web-security-cheatsheet
Table of Contents
The Big Picture: Why These Exist
The Web Security Problem
Quick Comparison: At a Glance
Feature
What It Controls
Enforced By
Use Case
Same-Origin Policy (SOP)
The Foundation of Web Security
What is an "Origin"?
What SOP Actually Does
✅ Allows (No Restrictions):
Blocks (SOP Restrictions):
Real-World Example: Why SOP Matters
Common Misconceptions
Wrong Belief
Reality
Cross-Origin Resource Sharing (CORS)
The Controlled Bridge Between Origins
The Problem CORS Solves
How CORS Works: The Complete Flow
Simple Requests (GET, POST with simple headers)
Preflight Requests (PUT, DELETE, custom headers)
Essential CORS Headers Explained
Server Response Headers
Browser Request Headers (Automatic)
Implementation Examples
Example 1: Node.js/Express - Whitelist Specific Origins
Example 2: Using CORS Middleware
Example 3: Nginx Configuration
CORS Security: Common Vulnerabilities
Vulnerability 1: Reflecting Origin Without Validation
Vulnerability 2: Wildcard with Credentials
Vulnerability 3: Over-Permissive for Sensitive Data
Frontend: Making CORS Requests
Fetch API
XMLHttpRequest
What CORS Does NOT Protect Against
Content Security Policy (CSP)
Your Page's Bouncer: Controls What Gets In
The XSS Problem CSP Solves
CSP Directives: The Complete List
Resource Loading Directives
Directive
What It Controls
Example
Document Directives
Directive
Purpose
Example
Reporting Directives
Directive
Purpose
Source Values: The Keywords
Value
Meaning
Use Case
CSP Levels: From Basic to Bulletproof
Level 1: Minimal Protection
Level 2: Practical Website
Level 3: Maximum Security with Nonces
Real-World Implementation
Method 1: HTTP Header (Recommended)
Method 2: Meta Tag (Limited)
Testing CSP: Report-Only Mode
Setting Up Violation Reporting
Client-Side Violation Monitoring
Common CSP Mistakes & Fixes
Mistake 1: Using 'unsafe-inline' Everywhere
Mistake 2: Overly Permissive Wildcards
Mistake 3: Forgetting frame-ancestors
CSP Compatibility: Progressive Enhancement
HttpOnly Cookies
Protecting Your Session Tokens
The Cookie Theft Problem
Cookie Security Flags: Complete Guide
All Security Flags Explained
Last updated