Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 38 ยท Getting Started

HTTP, Sessions and Where Web Apps Break

Understand the parts of an HTTP request an attacker controls, how cookies and sessions work, and why server-side checks matter, with Python parsers you can run in the browser.

To understand web vulnerabilities you must know exactly what travels between browser and server. Every Top 10 category starts from the same fact: the server receives bytes that someone else produced. This lesson dissects an HTTP request, explains cookies and sessions, and lists the places where input enters an application.

Anatomy of a request

An HTTP/1.1 request is plain text: a request line, headers, a blank line and an optional body.

TEXT
POST /transfer?from=savings HTTP/1.1
Host: bank.example
Cookie: session=9f3a1c...
Content-Type: application/x-www-form-urlencoded
Content-Length: 24

to=alice&amount=50.00

Every part is attacker-controlled input when the sender is not your own front end:

Part Examples Typical risk
Method and path /orders/17, DELETE Access control, traversal
Query string ?from=savings Injection, IDOR
Headers Host, Referer, User-Agent, X-Forwarded-For Spoofing, log injection, cache poisoning
Cookies session, role Session hijack, tampering
Body form fields, JSON, uploaded files Injection, mass assignment, upload abuse

The browser's address bar, the forms you build and the JavaScript you ship are only suggestions. Anyone can send any request with a command-line tool or script.

Python Parse a raw request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Statelessness, cookies and sessions

HTTP has no memory: each request stands alone. To remember a logged-in user, the server sets a cookie, a small value the browser sends back on later requests to the same site. Two common designs exist:

  • Server-side session: the cookie holds a random session id; the server stores the data (user id, role) in memory, a database or Redis. The id is a secret key to the account, so it must be unguessable and protected.
  • Signed token (for example a JWT): the cookie or header holds the data itself, signed so it cannot be altered. Convenient, but the signature check is critical and tokens are harder to revoke.

Whichever you use, cookies need the right flags:

Flag Effect
HttpOnly JavaScript cannot read the cookie, limiting theft via XSS
Secure Sent only over HTTPS
SameSite=Lax or Strict Not sent on most cross-site requests, a CSRF defense
Path, Domain, Max-Age Limit where and how long it lives
TEXT
Set-Cookie: session=9f3a1c...; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600
Python Check cookie flags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The request lifecycle and where checks belong

flowchart LR
  R["Request"] --> A["Authenticate: who is this?"]
  A --> Z["Authorize: may they do this to this object?"]
  Z --> V["Validate input"]
  V --> L["Business logic"]
  L --> O["Encode output"]
  O --> S["Response"]

Every request should pass every stage on the server. Skipping a stage is how Top 10 bugs are born.

  • Authentication missing or weak leads to A07.
  • Authorization skipped leads to A01.
  • Validation and safe handling of input prevent A03 (injection).
  • Output encoding stops XSS, a form of injection.
  • Logging at each stage feeds detection (A09).

HTTPS, caching and other transport details

HTTP over TLS (HTTPS) encrypts traffic and proves the server's identity. Without it, anyone on the network path can read cookies and passwords. Pair it with HSTS (Strict-Transport-Security) so browsers refuse to downgrade. Be careful with caches too: a page that shows personal data should carry Cache-Control: no-store, or a shared cache may serve one user's page to another.

Client-side checks are not security

JavaScript validation (a required attribute, a disabled button, a hidden admin link) improves usability only. The server must repeat every check. A simple test: could a user with curl skip this step? If yes, the control belongs on the server.

Python Why the client check is not enough

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The tampered value would fail even the weak client check here, but a script simply never runs it. The server's range check is what protects the application.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which part of an HTTP request can an attacker NOT control when they write their own client?

    A custom client can send any method, path, header, cookie or body. Treat every part as untrusted input.
  2. 2. What does the HttpOnly cookie flag do?

    HttpOnly hides the cookie from document.cookie, which limits session theft through XSS. Secure forces HTTPS and SameSite limits cross-site sending.
  3. 3. A page hides the admin menu with JavaScript for normal users. Is the admin page protected?

    Hiding a link is cosmetic. Anyone can request the URL directly, so the server needs an authorization check.
  4. 4. Which header should accompany pages that show private data to avoid shared-cache leaks?

    no-store tells browsers and caches not to keep the response, so private data cannot be served to someone else.

Summary

  • A request consists of a method, path, query, headers, cookies and body, and all are attacker-controlled.
  • Sessions use cookies; protect them with HttpOnly, Secure and SameSite, and make ids unguessable.
  • Every request should be authenticated, authorized, validated, processed and encoded on the server.
  • Use HTTPS with HSTS, and no-store for private pages.
  • Client-side checks are usability features, never security controls.

Next: A Safe Practice Lab and the Rules of Ethical Testing