Skip to lesson

HTTPS and why tokens need it

A login request crosses a station Wi-Fi router, an internet provider and many other machines before it reaches the server. On plain HTTP, each of them could read the password and token, or change the reply.

Plain HTTP: every byte is readable on the way

An eavesdropper is someone who reads network traffic that was not meant for them. On plain HTTP, which sends requests and responses as ordinary text, they see the URL, headers, cookies, bodies and tokens.

One login and one later request, exactly as an eavesdropper copies them from plain HTTP.

The same request travels from a phone through a Wi-Fi router and an internet provider to the server. Over plain HTTP, everyone on the path can read and change the email, the password and the token.

Example 1 · A login over plain HTTP, as an eavesdropper sees it

Text
POST /api/v1/auth/login HTTP/1.1Host: shoplite.exampleContent-Type: application/json{"email": "asha.rao@example.com", "password": "Mango-Kite-Rain-2026"}HTTP/1.1 200 OKContent-Type: application/json{"accessToken": "7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c4", "tokenType": "Bearer", "expiresIn": 900}GET /api/v1/me/orders HTTP/1.1Host: shoplite.exampleAuthorization: Bearer 7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c4
What someone on the station Wi-Fi gets
Asha's email and password, in plain text
The access token, usable by anyone for the next 15 minutes
Every order she opens, with her phone number and address
The chance to change any response before it reaches her

What this code does

  • This is the exact text that travels when a client uses http:// instead of https://. Nothing here is hidden: the password is in the body and the token is in the response and in every later request.
  • The eavesdropper does not need the password at all. Copying the Authorization line is enough to call /api/v1/me/orders as Asha until the token expires.

Every machine on the path can read plain HTTP: the free Wi-Fi at a railway station, the hostel router, the internet provider and every router in between. They can also change the traffic, for example swap the UPI ID on a payment page, and neither side would notice. An attacker who sits in the middle and changes traffic is called a man-in-the-middle.

Tools that capture network traffic are common and free. On an open Wi-Fi, reading someone's plain HTTP needs no special skill.

What TLS adds: encryption, integrity and identity

TLS (Transport Layer Security) is the protocol that wraps a connection in protection; HTTPS is HTTP inside TLS, usually on port 443. It gives encryption, integrity and server identity.

The same login over HTTPS: the eavesdropper now sees only encrypted records.

Example 2 · The same login over HTTPS, as an eavesdropper sees it

Text
phone 10.0.0.23  ->  203.0.113.10 port 443server name: shoplite.example        (from the DNS lookup and the TLS hello)TLS 1.3 handshake: client hello, server hello, certificate (encrypted)then only encrypted records:  17 03 03 01 2a 8e 4f c2 91 ...      about 300 bytes, phone -> server  17 03 03 00 9b 3d 7a 05 ee ...      about 160 bytes, server -> phone  17 03 03 00 c4 51 e9 2b 07 ...      about 200 bytes, phone -> server
What TLS protects and what it does not
Hidden    the URL path, headers, cookies, token, email, password and every body
Detected  any change to a record on the way (the connection is dropped)
Proved    the server holds the private key for shoplite.example's certificate
Visible   which server, when, and roughly how many bytes

What this code does

  • After the handshake the eavesdropper sees only encrypted records. 17 03 03 is the standard start of every TLS data record; the bytes after it are ciphertext. The record sizes and the server name are still visible.
  • Even /api/v1/me/orders and the query string are inside the encrypted part. Only the IP address, port and server name stay outside, which is why the URL path is safe from the network but not from logs on the server.

The three guarantees in detail: encryption makes the path, headers, cookies, tokens and bodies unreadable on the way; integrity detects any change and drops the connection; server identity proves you are talking to the real owner of the domain name. Identity comes from a certificate: a small file that says "this public key belongs to shoplite.example", signed by a certificate authority (CA). Browsers and phones come with a list of trusted CAs, and many of them, such as Let's Encrypt, sign for free after the owner proves control of the domain.

TLS has limits. It does not hide which server you connect to, when, or roughly how much data moves; it does not protect data once it arrives; and it does not fix bugs in the app, such as a missing owner check.

The handshake and what the padlock means

The TLS handshake is the short conversation a client and server run before any HTTP is sent, to agree on settings, check the server's certificate and create a shared key. In TLS 1.3 it takes one round trip.

curl -v prints each step of a real TLS handshake. Follow the lines in order.

Example 3 · Reading a real TLS handshake with curl -v

Terminal
curl -sv --http1.1 -o /dev/null https://example.com
Sample output (trimmed; curl on Linux and macOS; addresses, dates and the certificate differ; Windows curl prints fewer TLS lines)
* Host example.com:443 was resolved.
*   Trying 172.66.147.243:443...
* ALPN: curl offers http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519MLKEM768 / id-ecPublicKey
* ALPN: server accepted http/1.1
* Server certificate:
*   subject: CN=example.com
*   start date: Sep 26 22:49:11 2026 GMT
*   expire date: Dec 25 22:56:35 2026 GMT
*   issuer: C=US; O=SSL Corporation; CN=Cloudflare TLS Issuing ECC CA 3
*   subjectAltName: "example.com" matches cert's "example.com"
* SSL certificate verified via OpenSSL.
* Established connection to example.com (172.66.147.243 port 443) from 172.17.0.2 port 44338
> GET / HTTP/1.1
> Host: example.com
> User-Agent: curl/8.17.0
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Date: Fri, 16 Oct 2026 06:26:47 GMT
< Content-Type: text/html; charset=utf-8
< Connection: keep-alive
< Server: cloudflare
<
* Connection #0 to host example.com:443 left intact

What this code does

  • Lines starting with * are curl's notes on the connection. You can follow the handshake from the section above: client hello, server hello, the certificate, CERT verify (the server's signature) and Finished. The SSL connection using line shows the agreed TLS version and cipher; AES_256_GCM is the fast symmetric encryption used for the data.
  • The Server certificate block shows the checks: the certificate names example.com, it is in date, a CA signed it and curl verified it. Only then do the > request lines and < response lines appear, and all of them travel encrypted. -o /dev/null throws the page away so only this trace remains.

The handshake in four steps:

  1. Client hello: the client says which TLS versions and ciphers it supports and sends its half of a key exchange.
  2. Server hello and certificate: the server picks the options, sends its half of the key exchange and its certificate, and signs the handshake with its private key.
  3. Checks: the client verifies the certificate is signed by a trusted CA, is in date and names this host, and that the server's signature is valid.
  4. Shared key: both sides now hold the same fresh symmetric key that never crossed the network. Every HTTP request and response is encrypted with it.

The browser's padlock means two things only: the connection is encrypted, and the server proved it owns that exact domain name. It does not mean the site is honest: a fake site at shopIite.example (a capital I instead of an l) can get a valid certificate for its own name and show a padlock too.

Bearer tokens: whoever holds the token is you

A bearer token is an access token that works for whoever bears (carries) it. The server checks only that the token is valid, not who sends it or from which phone.

A token travels as plain text in one header line. Anyone who copies that line can send it.

Example 4 · A bearer token is just text in a header

Terminal
curl -s https://httpbin.org/headers -H "Authorization: Bearer demo-7f3a9c2e"
Sample output (the curl version and trace id differ)
{
  "headers": {
    "Accept": "*/*", 
    "Authorization": "Bearer demo-7f3a9c2e", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/8.8.0", 
    "X-Amzn-Trace-Id": "Root=1-6ad1c3c9-5ed5f7186345e84943035add"
  }
}

What this code does

  • httpbin.org/headers echoes back the headers it received. The server got the token as plain text in one header line; nothing in it says which phone or person sent it.
  • Anyone who copies Bearer demo-7f3a9c2e can send this exact line from any computer, and a real API would treat them as the token's owner. Here the token travelled over HTTPS, so nobody on the network could copy it on the way.

A bearer token is like a metro token: the gate opens for anyone who drops it in. So a leaked token is a stolen account until it expires. Tokens leak through plain HTTP, through URLs (query strings end up in browser history and server logs), through log files that print headers, through screenshots and through code pushed to GitHub.

The defences are simple habits. Send tokens only over HTTPS, only in the Authorization header, never in a URL. Keep access tokens short-lived, such as 15 minutes, and never write them to logs.

Find the bug

Find the leaking token

Each snippet can leak the access token. Tap the risky line, then check the fix.

The app asks for the learner's orders.

Round 1 of 3
A password is sent once at login; a token is sent with every request. That makes the token the thing an eavesdropper is most likely to catch.

HTTPS in practice: load balancers, HSTS and localhost

HSTS (HTTP Strict Transport Security) is a response header that tells the browser: for the next N seconds, only ever use HTTPS for this site. It closes the gap where the very first request goes out as plain HTTP.

A plain HTTP request is redirected to HTTPS, and the HTTPS answer carries the HSTS header. Then comes the picture of where TLS ends in a real deployment.

Example 5 · Redirect to HTTPS, then HSTS

Terminal
curl -s -o /dev/null -w "%{http_code} %{redirect_url}\n" http://github.comcurl -sI https://github.com | grep -i strict-transport-security
Sample output (header names are lower case over HTTP/2)
301 https://github.com/
strict-transport-security: max-age=31536000; includeSubdomains; preload

What this code does

  • Plain HTTP gets nothing but a 301 redirect to the HTTPS address. A careful server never serves real pages or accepts logins on plain HTTP.
  • The HTTPS response carries HSTS: max-age=31536000 is 365 days, includeSubdomains covers every sub-domain and preload asks browsers to ship the rule built in, so even the very first visit uses HTTPS.

Example 6 · Where TLS starts and ends in a deployment

Text
Phone app / browser   |  HTTPS (TLS 1.3), certificate for shoplite.example   vLoad balancer or reverse proxy           <- TLS ends here (TLS termination)   |  private network: plain HTTP, or HTTPS again   |  adds X-Forwarded-Proto: https   vShopLite API servers on port 8080 (three copies)   |   vMySQL databaseDeveloper laptop: http://localhost:8080  (never leaves the machine)
The rule for each part
Public internet          always HTTPS; plain HTTP only redirects
Inside the private network  plain HTTP is common; HTTPS again is safer
On your own laptop       plain HTTP on localhost is fine for development

What this code does

  • The load balancer holds the certificate and its private key, so the app servers do not each need one. It also spreads requests over the three copies of the API.
  • The app must trust X-Forwarded-Proto only from its own load balancer. That header is how the app knows the original request was HTTPS, for example when it builds Location URLs.

In a real deployment TLS often ends at a load balancer or reverse proxy: a server in front of the app that decrypts every request and passes it to one of several app servers. This is called TLS termination. Behind it, traffic travels inside a private network, as plain HTTP or encrypted again, and the app learns the original scheme from a header such as X-Forwarded-Proto: https.

The usual setup: plain HTTP answers only with a redirect to HTTPS, and HTTPS responses carry the HSTS header. Plain http://localhost:8080 is fine while you develop, because the traffic never leaves your own computer; a real server reachable from the internet must use HTTPS, with no exceptions for "just a test".

Next step

Check your understanding

11 quick questions