How the web works for a pentester: HTTP, cookies, sessions
Request and response through an attacker’s eyes: what the client controls
The client controls every element of an HTTP request, and the server cannot tell whether it came from a browser, curl or a script. So checks in the interface (disabled, maxlength, JS validation, hidden fields) are not protection: nothing from the client is trusted. The server itself checks types, limits and permissions and computes the price, role and owner. Requests are changed through a proxy such as Burp or ZAP.
Pentest · HTTP
The page will not let you change the price. Does that mean the server protects it?
An order form shows a price of 100 in a field that cannot be edited, and JavaScript rejects a quantity below 1. A tester who checks only through the interface concludes that the price cannot be changed. An attacker thinks differently: the interface is not a boundary for them, just one of several programs that can send requests. This lesson covers what an HTTP request consists of, what the sender controls in it, how to see and change it through a proxy, and the security rule that follows.
How it works and why. HTTP is a text “request and response” protocol. A request has a request line (method, path with query parameters, version), headers, an empty line and an optional body. A response has a status line, headers and a body.
POST /api/orders?lang=uk HTTP/1.1
Host: shop.example
Content-Type: application/json
Cookie: sid=abc123
{"productId":7,"qty":1}
Everything here is sent by the client: the method, the path, the parameters, any header, the cookie, the body. The server cannot tell whether the request came from a browser, curl or a script. Buttons, disabled, maxlength, JS validation and hidden form fields belong to the interface, that is, to code running in the sender’s hands. Hence the main rule: nothing from the client is trusted. The server checks types, bounds and rights itself and computes what must not be taken from the client (the price, the role, the owner).
A proxy between the browser and the server
Figure 1. Burp Suite or ZAP sit between the browser and the server: every request can be stopped, changed and replayed. The address 127.0.0.1:8080 is typical for both and can be changed.
In Burp Suite these are Proxy (interception), HTTP history and Repeater (resending a request with edits); in ZAP, Break, History and the Manual Request Editor. For HTTPS the proxy issues its own certificate, so its CA is trusted only in a separate test browser and never in your main one.
Example: a price protected only by the interface. A made-up example on a test environment with permission. The interface sends:
POST /api/orders HTTP/1.1
Host: shop.example
Content-Type: application/json
{"productId":7,"qty":1,"price":100}
In Repeater the tester changes the body to {"productId":7,"qty":1,"price":1} and looks at the response:
HTTP/1.1 201 Created
Content-Type: application/json
{"orderId":981,"total":1}
A total of 1 is a defect: the server took the price from the body. The right behaviour is to ignore the price field or reject the request and compute the total from the catalogue on the server:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{"error":"unknown field: price"}
The temptation to fix it “with one more JavaScript check” fails for the same reason: an attacker just bypasses code in the browser. The fix belongs on the server. A safe place to practise is the PortSwigger Web Security Academy labs and OWASP Juice Shop, and only there or on a system with a scope and written permission.
A real case. In 2019 a journalist told the US company First American Financial that its website served documents at a link with a nine-digit number: by changing a digit anyone could open someone else’s document without authentication (this is how KrebsOnSecurity described the flaw). According to the SEC, over 800 million document images were exposed, some with social security numbers and financial data; the company’s security staff had found the problem earlier but did not fix it, and senior management did not know. In 2021 the company agreed to a penalty of 487,616 dollars. The lesson: a parameter in a URL is client input, and the server must check whether this user is allowed to open this document.
Limits. A proxy shows only the exchange with the client: it cannot see what the server does inside, so conclusions about hidden logic are drawn from responses. Mobile apps with certificate pinning refuse to work through a proxy with a foreign certificate, so the test needs a specially prepared build. Burp and ZAP also show WebSocket and HTTP/2, but in a different view.
Common mistakes.
Assuming a disabled field or a hidden input protects a value: anyone who sees the request can change it.
Testing only through the UI and never trying the same request directly: this never finds a missing server-side check.
Trusting headers such as X-Forwarded-For or Referer for access decisions: the client sets them.
Installing the proxy certificate in your main browser: every site you visit becomes visible to the proxy.
Check yourself
The form does not let you send qty: -1, but the server accepts the same request from Repeater (201 Created). What does this say about validation?
Answer
The check exists only in the form’s JavaScript. The server does not repeat it, so anyone who sends the request directly bypasses it. The rule must run on the server.
A tester changes the number in /doc/000417 to 000418 and sees someone else’s file. What is the conclusion and what to repeat before reporting?
Answer
The object-level authorization check is missing (an insecure direct object reference). Repeat the request with two different accounts and without authentication to show exactly who gets access, and mask customer data in the evidence.
Related lessons: “Authorization, scope and rules of engagement: testing legally”, “Cookies and sessions: what keeps you logged in”, “Pentest phases, methodologies and where QA fits”.
In an interview.Junior: “Everything that comes from the client can be changed: URL, parameters, headers, cookies, body. So checks in the interface are not protection, and the server must check everything itself. I use a proxy such as Burp or ZAP for that”. Middle/Senior: I add: UI checks are only for convenience, trust only server-side sources of truth (the price from the catalogue, the owner from the session), test with direct requests next to UI scenarios, and the First American example where a sequential number in the URL opened other people’s documents.
Cookies and sessions: what keeps you logged in
A session rests on an id the server issues after login and the browser sends in a cookie. Whoever has that id is the user as far as the server is concerned, so the id is long and random and the cookie has HttpOnly, Secure and SameSite. Logout destroys the session on the server, not just the cookie. HttpOnly only limits what can be stolen and does not remove XSS.
Pentest · HTTP
A user logged out, but the old token still opens their page. How come?
HTTP does not remember who you are: every request is separate. To make a “login” last, after sign-in the server issues a session identifier and the browser attaches it to every following request. That identifier is the key: whoever has it is the user as far as the server is concerned, even without a password. So most attacks on sessions come down to how to steal the key, guess it, or make the server accept a foreign one.
How it works and why. After sign-in the server creates a session record (user, creation time, expiry) and returns its id in a Set-Cookie header. The browser stores the cookie and sends it in the Cookie header on every request to the matching domain and path. The server finds the record by id and knows who is asking. The alternative is a token that carries data itself (for example a JWT): then the server looks nothing up, but such a token is harder to revoke (see the lesson on sessions and JWT in the authentication chapter).
The life of a session
Figure 1. The password is sent once; after that only the session id in the cookie proves who you are.
Cookie attributes decide where it applies, whether a script can reach it, including a foreign one injected into the page through XSS (cross-site scripting), and whether it goes with a request forged by another page (CSRF); both attacks are covered in detail in the client-side chapter:
Attribute
What it does
Against what
HttpOnly
hides from JavaScript
theft via XSS
Secure
HTTPS only
eavesdropping
SameSite
whether to send from other sites
part of CSRF
Path, Domain
where it applies
excess exposure
__Host-
prefix: Secure, Path=/, no Domain
overwrite from a subdomain
Max-Age
lifetime
long-lived theft
Example: a weak and a stronger cookie. QA opens DevTools (the Application tab, Cookies section) or looks at the response header after sign-in. A weak variant:
HTTP/1.1 200 OK
Set-Cookie: sid=1042; Path=/
There are three defects: the id looks like a sequential number (the neighbour can be guessed: 1041, 1043), there is no HttpOnly (XSS can read the cookie), and no Secure or SameSite. A stronger variant:
HTTP/1.1 200 OK
Set-Cookie: __Host-sid=q8Zk3...long-random;
Path=/; Secure; HttpOnly; SameSite=Lax
Here the value is long and random from a cryptographic generator (OWASP advises at least 64 bits of entropy, in practice people use 128). An explicit SameSite is needed because the default behaviour differs between browsers. One more useful QA test: copy the cookie, log out and repeat a request with the same value.
GET /api/profile HTTP/1.1
Host: shop.example
Cookie: __Host-sid=q8Zk3...long-random
The expected answer is 401 Unauthorized: logout must destroy the session on the server, not just delete the cookie in the browser. If it returns 200, a key stolen earlier keeps working.
A real case. In September 2018 Facebook reported a vulnerability in the “View As” feature (viewing your profile as another person sees it). Attackers stole access tokens, “digital keys that keep people logged in”: at first the company named almost 50 million affected accounts and reset about 40 million more as a precaution, so roughly 90 million people had to log in again; in October it clarified that tokens of about 30 million people were stolen. The issue was found on 25 September; Facebook fixed the code, reset the tokens and temporarily switched the feature off. Lesson for a tester: a session token is equivalent to the password and MFA at once, so the server must be able to revoke it, and stealing one key must not open so many accounts.
Limits.HttpOnly does not stop XSS: the script cannot read the cookie but can send requests on the user’s behalf, and the browser attaches the cookie by itself. SameSite=Lax does not save from every cross-site request, and Secure does not help if the id leaked through a log or a URL. Also check separately: the session id changes after login (otherwise session fixation, covered in the authentication chapter) and there are limits for inactivity and total lifetime.
Common mistakes.
Assuming that logout deletes the session: check by replaying the old cookie.
Putting the session id in the URL: it ends up in history, logs and the Referer header.
Relying on HttpOnly as XSS protection: it only limits what can be stolen.
Setting no session lifetime: a stolen key works indefinitely.
Check yourself
The session cookie has HttpOnly, but the page has stored XSS. What can the attacker still do?
Answer
He cannot read the cookie, but the script runs on the user’s page and can send requests (change the password, e-mail, make a transfer), and the browser attaches the cookie itself. So HttpOnly reduces the damage, but the XSS still has to be fixed.
After logging out, the same request with the old cookie returns 200. What is the defect and why is it dangerous?
Answer
Logout does not destroy the session on the server (it only deletes the cookie). A key stolen earlier (through XSS, a log, a shared computer) keeps working, so logging out gives the user no protection.
Related lessons: “Request and response through an attacker’s eyes: what the client controls”, “Same-origin, CORS and CSP: who allows what to whom”, “Sessions and JWT: fixation, signature, expiry”.
In an interview.Junior: “After sign-in the server issues a session id in a cookie and the browser sends it with every request. The cookie should have HttpOnly, Secure and SameSite, the id should be long and random, and logout should destroy the session on the server”. Middle/Senior: I add: the __Host- prefix, idle and absolute timeouts, session revocation, a new session after login, the difference between a server-side session and a self-contained token, and the Facebook 2018 case where one kind of token opened tens of millions of accounts.
Same-origin, CORS and CSP: who allows what to whom
The same-origin policy stops a script from one origin (scheme, host and port) from reading the responses and DOM of another. CORS deliberately relaxes it through response headers, and CSP complements it as a second line against XSS. The policy limits reading, not sending a request, which is why CSRF exists. CORS works only in the browser, and a classic mistake is reflecting Origin with credentials.
Pentest · HTTP
A foreign page can send a request to your bank but cannot read the answer. When does that break?
A user opened an attacker’s site in one tab and has a banking account open in another. Why can the attacker’s page not simply read the balance? And why does CSRF exist at all? Both questions are answered by the same-origin policy, while CORS and CSP describe how it is relaxed and complemented.
How it works and why. An origin is the scheme, host and port together. The same-origin policy does not let a script from one origin read the responses and DOM of another. It exists because the browser attaches cookies to requests to the bank by itself, and without it any page would read everything a logged-in user can see.
What counts as the same origin
Difference
Example
Same?
path
shop.example/cart
yes
scheme
http://shop.example
no
subdomain
api.shop.example
no
port
shop.example:8443
no
Compared with https://shop.example. An important detail: the policy restricts reading the response, not sending the request. A form or an image from another site still sends the request with the cookie and the server executes it. CSRF rests on exactly this, which is why a token, an Origin check and SameSite are needed against it.
CORS. Sometimes a legitimate front end on app.shop.example has to read an API on api.shop.example. The API then allows it in a response: Access-Control-Allow-Origin: https://app.shop.example. If the request is “non-simple” (a method such as PUT or DELETE, an Authorization header, JSON in Content-Type), the browser first sends an OPTIONS request (a preflight) and asks permission. A “simple” request goes straight away, so CORS does not guarantee that an unwanted request will not reach the server, it only stops a script from reading the response. And CORS protects only in a browser: curl does not notice it.
Example: CORS that opens private data to foreign sites. The tester adds an arbitrary Origin to the request (only on a test system within scope):
GET /api/account HTTP/1.1
Host: api.shop.example
Origin: https://evil.example
Cookie: __Host-sid=...
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://evil.example
Access-Control-Allow-Credentials: true
{"email":"user@shop.example","iban":"UA..."}
The server mirrors any Origin and allows credentials: if the session cookie is sent cross-site (SameSite=None), the page evil.example will read the victim’s data in the victim’s browser. The fix: an allowlist of origins with exact comparison, not a mirror and not an “ends with shop.example” check (evilshop.example matches it), a Vary: Origin header and no acceptance of the value null (sandboxed iframes and local files send it). The value * together with credentials is rejected by browsers, so it cannot be abused the way mirroring can.
Mirroring the Origin against an allowlist
Typical
The server returns whatever Origin arrived plus credentials: true. A foreign site reads the victim’s responses.
Better
An exact list of trusted origins, Vary: Origin, and no Allow-Origin at all for the rest.
Figure 1. The difference between CORS settings that open and close reading of responses.
CSP. The Content-Security-Policy header tells the browser where scripts and other resources may be loaded from and usually forbids inline scripts. For example, default-src 'self'; script-src 'self'; object-src 'none'. It is a second line of defence: if XSS succeeded, the policy often stops the script from running or sending data out. But it does not replace output encoding, and a policy that is too broad (unsafe-inline, *) gives almost nothing.
A real case. In October 2005 Samy Kamkar released the Samy worm on MySpace: code in a profile that added him as a friend and copied itself into the profile of everyone who viewed it. Over a million users ran it within 20 hours. The same-origin policy did not help here: the script ran as part of MySpace’s own page, that is in the same origin, and acted on behalf of the visitor. MySpace closed the vulnerability. The lesson: SOP does not protect from XSS, output encoding and CSP do, and a “joke” without permission is still a crime.
Limits. SOP and CORS work only in a browser: a direct request (curl, a script, a mobile app) does not notice them, so data is protected by authentication and server-side permission checks. SameSite=Lax does not cover GET requests with side effects or requests from subdomains of the same site, and CSP helps little if the policy allows unsafe-inline or domains with arbitrary content.
Common mistakes.
Thinking that CORS blocks the request itself: reading the response is blocked, while the side effects of a simple request have already happened.
Allowing any subdomain with a regular expression: one forgotten or taken-over subdomain gives everyone access.
Treating CORS as authentication: curl and server-side scripts simply do not apply it: CORS works only in a browser.
Adding a CSP “for the checkbox” with unsafe-inline: XSS passes just the same.
Check yourself
An API responds with Access-Control-Allow-Origin: * without credentials. Can a foreign site read a logged-in user’s data?
Answer
Not data that depends on a cookie: even if the script sends the request with the cookie, the browser will not let it read the response (a wildcard does not work with credentials), and without the cookie the server returns only public data. There is a risk if the data is not public and access is granted by network position (an internal network) instead of a cookie.
Why can the attacker’s page send POST /transfer with the victim’s cookie even though it cannot read the response?
Answer
The same-origin policy restricts reading, not sending. A form from another site sends the request and the browser attaches the cookie. That is why a token, an Origin check and SameSite are needed against CSRF.
Related lessons: “Cookies and sessions: what keeps you logged in”, “Request and response through an attacker’s eyes: what the client controls”, “OWASP Top 10:2025: an overview of the list”.
In an interview.Junior: “An origin is the scheme, host and port. The browser does not let a script read responses of another origin, and CORS allows it deliberately through response headers”. Middle/Senior: I add: SOP restricts reading, not sending, which is why CSRF exists; typical CORS mistakes are mirroring the Origin together with credentials and weak regular expressions; CSP as a second barrier against XSS; the 2005 Samy worm example, where code ran in the same origin.