Advanced

The wire is not safe

I sent the session over http and it worked. It also worked for anyone sniffing the wire. TLS encrypts the channel and signs the origin, so the transport is part of the contract.

What you will see

Strict-Transport-Security: max-age=31536000; HSTS

About 1 min of reading

Try It

Force the upgrade and read the HSTS header.

1

Request http and see the redirect to https, not a served page.

curl -s -o /dev/null -w "%{http_code} %{redirect_url}" http://yoursite/change

Before you run it, say out loud what it will print. Then open the answer.

What it prints
307 https://yoursite/change
2

The HSTS header tells the browser to stay on https for a year.

curl -sI https://yoursite/ | grep -i strict-transport

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Strict-Transport-Security: max-age=31536000; HSTS
Do not add HSTS until you are sure every asset and every route is https. Once sent, the browser refuses http for that host for the whole max-age, so a single http asset breaks the site.

Data Flow

Over http the request bytes ride in the clear; TLS encrypts the channel and binds the origin, and HSTS makes the browser upgrade before any request is sent.
1 Request over http in clear
↓
2 TLS encrypts channel
↓
3 TLS binds origin
↓
4 HSTS upgrades before send
Why this works
Transport is the contract

A secure app over http is still insecure: the cookie and the body ride in the clear. TLS is not decoration, it is the reason the channel is private and the origin is real. Every state-changing route must be https-only.

HSTS preloads the upgrade

HSTS tells the browser to request https for this host even before you redirect, so a mistyped http link or a stale bookmark cannot send a plain request. It closes the window the redirect alone leaves open.

Examples

Plain http leaks

http://yoursite/change   <- bytes travel in the clear

Over http the whole request, cookie included, is readable by anyone on the wire, a proxy, or a log. TLS is what makes it private.

TLS gives two things

https -> encrypted channel + verified origin

TLS encrypts the bytes so a eavesdropper cannot read them, and it binds the bytes to the domain, so a fake site cannot impersonate you.

Redirect up, never down

http -> 307 -> https

Send a permanent redirect from http to https and add HSTS so the browser upgrades before it ever sends a request. Never downgrade.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The standing checklist
Previous Lesson
The standing checklist
Next Lesson
Locking what the page may load
Locking what the page may load →