HTTP Security Basics
How cookies, same-origin policy, and security headers form the browser's security model
Skills taught: websecurity.same-origin-policy, websecurity.cookies, websecurity.security-headers, websecurity.https
The Same-Origin Policy
The browser's most fundamental security control is the Same-Origin Policy (SOP). It prevents JavaScript on one origin from reading responses from a different origin. An 'origin' is the combination of scheme + hostname + port: https://myapp.com:443. Without SOP, a malicious site could read your bank's response to a request made using your session cookie — stealing your balance, account number, and transaction history.
Origin: https://myapp.com
SAME origin (allowed):
https://myapp.com/api/users ← same scheme, host, port
https://myapp.com/dashboard ← just a different path
DIFFERENT origin (blocked by SOP):
http://myapp.com ← different scheme (http vs https)
https://api.myapp.com ← different subdomain
https://myapp.com:8080 ← different port
https://othersite.com ← different host
WHAT SOP BLOCKS:
JavaScript reading the RESPONSE body from a cross-origin fetch()
JavaScript accessing an iframe's DOM from a different origin
localStorage and cookies from other origins
WHAT SOP DOES NOT BLOCK (important!):
Sending requests to other origins (you CAN send, just can't read responses)
This is why CSRF is still possible even with SOP
Cookie security attributes
// Set a session cookie with all security attributes
res.cookie('sessionId', token, {
// HttpOnly: JavaScript cannot read this cookie
// Prevents XSS from stealing the session token
httpOnly: true,
// Secure: cookie only sent over HTTPS, never HTTP
// Prevents eavesdropping on unencrypted connections
secure: process.env.NODE_ENV === 'production',
// SameSite: controls when cookie is sent cross-site
// 'strict' — never sent cross-site (breaks OAuth flows)
// 'lax' — sent on top-level navigation (GET), not POST (recommended)
// 'none' — always sent (requires Secure: true) — only for embeds
sameSite: 'lax',
// maxAge: cookie expires after this many milliseconds
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
// path: cookie only sent for this path prefix
path: '/',
});
// INSECURE — never do this:
res.cookie('sessionId', token); // Missing all security attributes
// → JavaScript can steal it (XSS)
// → Sent over HTTP (eavesdropping)
// → Sent cross-site (CSRF possible)
Security headers — your second line of defence
import helmet from 'helmet';
// Helmet sets all important security headers automatically
app.use(helmet());
// What helmet sets:
// Content-Security-Policy: controls what resources the browser loads
// Prevents XSS by blocking inline scripts and scripts from untrusted origins
// Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.myapp.com
// X-Frame-Options: DENY
// Prevents your page being embedded in an iframe (clickjacking prevention)
// Superseded by CSP frame-ancestors, but set both for compatibility
// X-Content-Type-Options: nosniff
// Prevents browser 'MIME sniffing' — a file served as text/plain won't run as JavaScript
// Strict-Transport-Security: max-age=31536000; includeSubDomains
// Forces HTTPS for 1 year — browser will refuse HTTP connections
// Referrer-Policy: strict-origin-when-cross-origin
// Controls how much URL info is sent in the Referer header to other sites
// Permissions-Policy: camera=(), microphone=(), geolocation=()
// Disables browser features your app doesn't need
Content-Security-Policy controls what the browser is allowed to load.
Each directive specifies a resource type and allowed sources.
DIRECTIVES:
default-src fallback for unspecified resource types
script-src where JavaScript can be loaded from
style-src where CSS can be loaded from
img-src where images can be loaded from
connect-src where fetch/XHR/WebSocket can connect
font-src where fonts can be loaded from
frame-ancestors who can embed this page in an iframe
report-uri where CSP violation reports are sent
SOURCE VALUES:
'self' same origin as the document
'none' block everything
https: any HTTPS source
'unsafe-inline' allow inline scripts/styles (weakens XSS protection)
'nonce-{value}' allow a specific inline script with a matching nonce
https://cdn.example.com specific trusted domain
EXAMPLE (tight policy for a React SPA):
Content-Security-Policy:
default-src 'self';
script-src 'self';
style-src 'self' https://fonts.googleapis.com;
font-src 'self' https://fonts.gstatic.com;
img-src 'self' data: https:;
connect-src 'self' https://api.myapp.com;
frame-ancestors 'none'
Frequently Asked Questions
Does my frontend app (React SPA) need security headers?
Yes — headers are set by the server serving your index.html and static assets (Nginx, Vercel, Netlify, CloudFront). Even if your React app has no backend, the file server can and should set security headers. Vercel and Netlify let you configure them in vercel.json and netlify.toml respectively. Without them, your app is vulnerable to clickjacking and your CSP is non-existent.