How Websites Work
Chapters and transcript
How Websites Work Every time you open a website, your browser sends a request, and the server sends back a page. It all happens in less than a second. Before you can test a website, you need to understand this conversation. In this lesson you will learn about requests, responses, cookies and sessions.
Client and server Two sides talk on the web. The client is your browser. The server is the computer that hosts the website. They talk using HTTP. The browser sends a request, and the server sends back a response.
An HTTP request Let us open up a request. First, the method says what you want to do. GET reads data, and POST sends data. Next, the path says which page or action, like slash login. Then the headers carry extra information, such as cookies. Finally, the body holds the data you are sending, like what you typed in a form.
A request in action Let me show you on the laptop. This is the login page of our practice shop. I type the username ali, then a password, and click Log in. Behind the scenes, the browser sends a POST request to slash login, with what I typed in the body. The server answers, and sends me a session cookie.
An HTTP response The server's response starts with a status code. Two hundred means OK. Three oh one and three oh two are redirects. Four oh one means you need to log in. Four oh three means you are logged in, but not allowed. Four oh four means not found. And five hundred is a server error, which is often interesting to a tester.
Cookies and sessions HTTP has no memory. Each request is separate. So how does a site remember you? You log in. The server creates a session and sends back a session cookie, a long random value. Your browser sends it with every request. But this means whoever has your cookie is you. An attacker with a stolen cookie is logged in as you.
Cookies and sessions Because stealing session cookies is a common goal of attackers, good cookies use protective flags. HttpOnly means JavaScript cannot read the cookie. Secure means it is only sent over HTTPS. And SameSite limits when the cookie is sent to other sites.
The front end Now something every tester does first. I open View page source on the practice shop. The whole page is here: the HTML, the CSS and the JavaScript. Nothing is hidden. Everything the server sends to the browser can be read by the user, and DevTools shows even more.
The front end So here is the rule for developers. The browser is not secret. Never put passwords, keys or private notes in front-end code, and never trust the browser to enforce security. Those jobs belong on the server. Security checks must happen there.
Quick check Quick check. A developer wants to hide a secret from users inside a web page. Where is it safe to put it? Think about what the browser receives, then choose.
Putting it together Let us put it together. The browser sends a request, and the server sends a response. A request has a method, a path, headers and a body. Status codes tell you what happened. A session cookie keeps you logged in, so it must be protected. Everything sent to the browser can be read, so security checks belong on the server. Now read the lesson below, and try the lab.
Before you can hack a website, you need to know how it works. The good news: the basics are simple.
Client and server
- The client is your browser
- The server is the computer that hosts the website
They talk using HTTP. The browser sends a request, and the server sends back a response.
An HTTP request
Here is a simplified request your browser sends when you log in:
POST /login HTTP/1.1
Host: shop.example
Cookie: session=8f2a91c4
Content-Type: application/x-www-form-urlencoded
username=ali&password=secret123
- Method (
POST): what you want to do.GETreads data,POSTsends data. - Path (
/login): which page or action - Headers: extra information, such as cookies
- Body: the data you are sending
An HTTP response
HTTP/1.1 200 OK
Set-Cookie: session=c71e09ab; HttpOnly; Secure
Content-Type: text/html
<html> ... </html>
The status code tells you what happened:
| Code | Meaning |
|---|---|
| 200 | OK |
| 301 / 302 | Redirect to another page |
| 401 | You need to log in |
| 403 | Forbidden: you are logged in but not allowed |
| 404 | Not found |
| 500 | Server error (often interesting to a tester) |
Cookies and sessions
HTTP has no memory. Each request is separate. So how does a website remember that you are logged in?
- You log in with your username and password
- The server creates a session and sends you a session cookie, a long random value
- Your browser sends that cookie with every request
- The server sees the cookie and knows it is you
This means whoever has your session cookie is you, as far as the website is concerned. Stealing session cookies is a common goal of attackers.
Good cookies use flags such as:
HttpOnly: JavaScript cannot read the cookieSecure: only sent over HTTPSSameSite: limits when the cookie is sent to other sites
The front end is not secret
Everything the server sends to the browser can be read by the user: HTML, CSS, JavaScript and data from APIs. You can see it with View page source or browser DevTools.
Rule for developers: never put passwords, keys or private notes in front-end code, and never trust the browser to enforce security. Security checks must happen on the server.
Penetration testers always start by reading the source. In the lab, you will see why.
HANDS-ON LAB
View the Source
A developer left a note in the page. Users cannot see it, but you can.
Check your understanding
Answer every question correctly to complete this lesson. If you miss one, try the quiz again.
1. Which HTTP method is normally used to submit a login form?
2. What does a status code of 403 mean?
3. Where can you safely hide a secret from users in a web page?
Locked: answer every quiz question above correctly to complete this lesson.
Spotted a mistake or something unclear? Report a mistake