2 Hours
2. HTTP Security Fundamentals
HTTP Security
Authentication and authorization happen over HTTP. Understanding HTTP security fundamentals is essential for building secure applications. This module covers HTTPS, secure cookies, and HTTP authentication headers.
Why HTTPS Matters
Without HTTPS, all data including passwords, tokens, and session cookies are sent in plain text. Anyone on the network can read them.
Text
HTTP (Port 80): HTTPS (Port 443): - Data sent as plain text - Data encrypted with TLS - Vulnerable to MITM attacks - Protected against MITM - No certificate required - Requires SSL/TLS certificate - Anyone can read your traffic - Only the server can decrypt
Best Practice: Always use HTTPS in production. Never send credentials or tokens over HTTP. Let's Encrypt provides free SSL certificates.
Secure Cookies: The Flags That Protect You
Cookies are commonly used for session management. Three critical flags make them secure.
Text
httpOnly → Prevents JavaScript access (mitigates XSS)
Secure → Cookies only sent over HTTPS
SameSite → Controls cross-site requests (mitigates CSRF)
- Lax: Sent for top-level navigations
- Strict: Never sent cross-site
- None: Sent cross-site (requires Secure)
JavaScript
// Node.js/Express example - setting a secure cookie res.cookie('sessionId', token, { httpOnly: true, // JavaScript cannot read it secure: true, // HTTPS only sameSite: 'strict', // CSRF protection maxAge: 3600000 // 1 hour expiration });
Try it: In your browser's developer tools, look at cookies for any website. Notice which flags are set.
The Authorization Header
The Authorization header is the standard way to send credentials to a server. Different authentication methods use different formats.
Text
Basic Auth: Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ= Bearer Token: Authorization: Bearer eyJhbGciOiJIUzI1NiIs... API Key: Authorization: ApiKey abc123xyz
JavaScript
// How to send a Bearer token fetch('https://api.example.com/user', { headers: { 'Authorization': 'Bearer ' + accessToken } });
Note: Basic auth sends username:password encoded in base64. Base64 is NOT encryption — anyone can decode it. Always use Basic auth over HTTPS only.
Knowledge Check
Ready to test your understanding of 2. HTTP Security Fundamentals?