What You Will Learn
BEGINNER- HTTP request/response anatomy (and security-relevant parts)
- Headers that matter for security
- Why HTTP is insecure by default — and what HTTPS adds
HTTP Request Anatomy
Every web request has the same structure. Here's a typical GET with all the parts security engineers care about:
http_request.txthttp
GET /api/users/123 HTTP/1.1
Host: imcseian.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
Accept: application/json
Authorization: Bearer eyJhbGc...
Cookie: session=abc123; csrf_token=xyz789
X-Forwarded-For: 203.0.113.42
Referer: https://imcseian.com/dashboard
Three security-critical parts here: (1) Authorization header carries credentials — leak it and you're owned. (2) Cookie header carries session IDs and CSRF tokens. (3) X-Forwarded-For is spoofable — don't trust it for authentication.
HTTP Response Anatomy
http_response.txthttp
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 247
Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax
X-Frame-Options: DENY
Content-Security-Policy: default-src 'self'
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
{"id": 123, "name": "Alice", "email": "alice@example.com"}
The 7 Security-Critical Headers
| Header | What It Does | Why You Need It |
|---|---|---|
| Set-Cookie: HttpOnly | JS can't read cookie | Prevents XSS cookie theft |
| Set-Cookie: Secure | Only sent over HTTPS | Prevents MITM cookie sniffing |
| Set-Cookie: SameSite=Lax | Cookie not sent on cross-site requests | Prevents CSRF |
| X-Frame-Options: DENY | Page can't be framed | Prevents clickjacking |
| Content-Security-Policy | Restrict script origins | Mitigates XSS impact |
| Strict-Transport-Security | Force HTTPS for N seconds | Prevents SSL strip attacks |
| X-Content-Type-Options: nosniff | Browser won't MIME-sniff | Prevents content-type confusion |
HTTP Methods and Their Security Implications
| Method | Purpose | Security Notes |
|---|---|---|
| GET | Read resource | Should be idempotent, no side effects. Never put sensitive data in URL (it gets logged). |
| POST | Create resource | Body carries data. CSRF protection needed. |
| PUT/PATCH | Update resource | Same CSRF concern as POST. |
| DELETE | Remove resource | Same CSRF concern. Always confirm in UI. |
| OPTIONS | CORS preflight | Used by browser to check CORS policy. Often misconfigured to allow everything. |
| TRACE | Echo request | Should be DISABLED. Enables XST (Cross-Site Tracing) attacks. |
Why HTTP Is Insecure By Default
Plain HTTP sends everything in cleartext. Anyone on the path — your ISP, the coffee shop WiFi, a malicious exit node — can read your password, session cookie, and credit card. Three attack categories:
- Passive sniffing: attacker reads traffic without modifying. Trivial on HTTP.
- Active MITM: attacker modifies traffic. Can swap the JS bundle for malicious code, inject scripts, redirect.
- SSL stripping: attacker downgrades HTTPS to HTTP without the user noticing.
Strict-Transport-Securityheader prevents this.
Plain HTTP exposes everything. We'll fix this with HTTPS in Lesson 3.
Status Codes Security Engineers Should Know
| Code | Meaning | Security Implication |
|---|---|---|
| 200 OK | Success | Normal response. |
| 301/302 Redirect | Resource moved | Open redirect vulnerability if Location comes from user input. |
| 401 Unauthorized | Not authenticated | Should NOT reveal whether username exists (use same message as 403). |
| 403 Forbidden | Authenticated but no permission | Same timing/message as 401 to avoid user enumeration. |
| 404 Not Found | Resource doesn't exist | Returning 401 vs 404 leaks whether resource exists. |
| 500 Internal Error | Server crashed | Stack traces leak info. Always log details server-side, return generic message to client. |
| 429 Too Many Requests | Rate limited | Critical for brute force / abuse prevention. |
Common HTTP Pitfalls
- Sensitive data in URLs:
GET /login?password=alice123— passwords end up in browser history, server logs, Referer headers. Always use POST body. - Trusting X-Forwarded-For: this header is set by proxies and is trivially spoofable. Don't use it for authentication or session binding.
- Verbose 500 errors: stack traces in 500 responses leak file paths, library versions, even DB credentials. Always disable in production.
- Allowing TRACE: enables Cross-Site Tracing (XST) attacks. Disable on your web server.
🌍 REAL WORLD: The GitHub OAuth Leak (2020)
GitHub accidentally logged OAuth tokens in plain HTTP URLs (in Referer headers and browser history). Attackers who got access to logs had access to user accounts. Always pass tokens in headers (Authorization: Bearer ...), never in URL query strings.Open your browser DevTools → Network tab. Visit any website. Inspect a request.
Look for the security headers above. Most sites are missing at least 2-3.
Check your own app's responses. Are Set-Cookie headers using HttpOnly, Secure, SameSite?
If you see plain HTTP anywhere sensitive, fix it today. Lesson 3 explains how.
Bonus: try curl with -v (verbose) on an HTTP site. See every header in the conversation.
🎯 Key Takeaways
- HTTP request: method, path, headers, body. Response: status, headers, body.
- 7 security-critical headers: HttpOnly, Secure, SameSite, X-Frame-Options, CSP, HSTS, nosniff.
- HTTP methods: GET (read), POST (create), PUT/PATCH (update), DELETE (remove). TRACE must be disabled.
- Plain HTTP exposes everything to anyone on the path — passive sniffing, MITM, SSL strip.
- Never put secrets in URLs. Always log details server-side, return generic messages to clients.
🧠 Quick Check
Q1. Why should you never put passwords in URL query strings?
→ URLs get logged in browser history, server logs, Referer headers, and proxy caches. Anyone with log access gets the password. Use POST body + HTTPS.
Q2. What's wrong with returning different messages for 401 vs 403 vs 404?
→ Different responses leak information. An attacker learns whether a username exists (401) vs doesn't (404) — user enumeration. Use same timing and message.
Q3. What does X-Forwarded-For tell you, and why can't you trust it?
→ It claims to be the original client IP, set by proxies. But it's trivially spoofable — anyone can add the header. Don't use it for authN or session binding. Use only for analytics with caveats.
Comments
Comments
Post a Comment