Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Module 1 - Web Security Foundations Web Security Web Security: From Zero to Secure Apps

How HTTP Works: What Every Security Engineer Must Know

Reviewed & accurate
AI Summary

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

HeaderWhat It DoesWhy You Need It
Set-Cookie: HttpOnlyJS can't read cookiePrevents XSS cookie theft
Set-Cookie: SecureOnly sent over HTTPSPrevents MITM cookie sniffing
Set-Cookie: SameSite=LaxCookie not sent on cross-site requestsPrevents CSRF
X-Frame-Options: DENYPage can't be framedPrevents clickjacking
Content-Security-PolicyRestrict script originsMitigates XSS impact
Strict-Transport-SecurityForce HTTPS for N secondsPrevents SSL strip attacks
X-Content-Type-Options: nosniffBrowser won't MIME-sniffPrevents content-type confusion

HTTP Methods and Their Security Implications

MethodPurposeSecurity Notes
GETRead resourceShould be idempotent, no side effects. Never put sensitive data in URL (it gets logged).
POSTCreate resourceBody carries data. CSRF protection needed.
PUT/PATCHUpdate resourceSame CSRF concern as POST.
DELETERemove resourceSame CSRF concern. Always confirm in UI.
OPTIONSCORS preflightUsed by browser to check CORS policy. Often misconfigured to allow everything.
TRACEEcho requestShould 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:

  1. Passive sniffing: attacker reads traffic without modifying. Trivial on HTTP.
  2. Active MITM: attacker modifies traffic. Can swap the JS bundle for malicious code, inject scripts, redirect.
  3. SSL stripping: attacker downgrades HTTPS to HTTP without the user noticing. Strict-Transport-Security header prevents this.
HTTP vs HTTPS: What an Attacker Sees Browser HTTP: password\nin cleartext Attacker Seespassword=alice123\nsession=abc HTTP to server Server Plain HTTP: every byte is visible to anyone on the path.
Plain HTTP exposes everything. We'll fix this with HTTPS in Lesson 3.

Status Codes Security Engineers Should Know

CodeMeaningSecurity Implication
200 OKSuccessNormal response.
301/302 RedirectResource movedOpen redirect vulnerability if Location comes from user input.
401 UnauthorizedNot authenticatedShould NOT reveal whether username exists (use same message as 403).
403 ForbiddenAuthenticated but no permissionSame timing/message as 401 to avoid user enumeration.
404 Not FoundResource doesn't existReturning 401 vs 404 leaks whether resource exists.
500 Internal ErrorServer crashedStack traces leak info. Always log details server-side, return generic message to client.
429 Too Many RequestsRate limitedCritical for brute force / abuse prevention.

Common HTTP Pitfalls

  1. Sensitive data in URLs: GET /login?password=alice123 — passwords end up in browser history, server logs, Referer headers. Always use POST body.
  2. Trusting X-Forwarded-For: this header is set by proxies and is trivially spoofable. Don't use it for authentication or session binding.
  3. Verbose 500 errors: stack traces in 500 responses leak file paths, library versions, even DB credentials. Always disable in production.
  4. 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.
Previously: Previously: Lesson 01 introduced web security basics.
Today: Today: you learned HTTP anatomy, security headers, and why plain HTTP is dangerous.
Next: Next: Lesson 03 shows you how HTTPS/TLS fixes HTTP's flaws — and the handshake that secures every connection.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments