Course contentsShow
← Software Engineering
Lesson 1071 of 1,93332. Security EngineeringFree lesson

HTTP Strict Transport Security (HSTS)

Forcing browsers to always use HTTPS, preventing protocol downgrade attacks and SSL stripping.

HTTP Strict Transport Security (HSTS)

What you'll learn: How to use HSTS to force browsers to always communicate over HTTPS, protecting users from protocol downgrade and SSL stripping attacks.

The Problem: Protocol Downgrade Attacks

Even with TLS deployed, users might type http://example.com (no "s") in their browser, or click on an old HTTP link. A man-in-the-middle attacker can intercept that initial HTTP request and keep the user on an insecure connection — a technique called SSL stripping. The browser never gets upgraded to HTTPS, and the attacker can eavesdrop on or manipulate the traffic.

What HSTS Does

HTTP Strict Transport Security is a security header your server sends that instructs browsers: "For the next X seconds, only communicate with me over HTTPS, even if the user types http:// or clicks an HTTP link."

Once a browser receives this header from your HTTPS site, it automatically upgrades all future requests to HTTPS before sending them — the HTTP request never leaves the user's machine. This eliminates the window for downgrade attacks.

The HSTS Header

Your server sets HSTS with:

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
  • max-age: How long (in seconds) to remember this policy. 31536000 = 1 year.
  • includeSubDomains: Apply the policy to all subdomains too.
  • preload: Opt-in to browser preload lists (baked-in HSTS before first visit).

First-Visit Problem and Preloading

HSTS only protects after the first successful HTTPS visit. To protect that first visit, you can submit your domain to the HSTS Preload List, which browsers ship with. This provides protection from day one.

Key Takeaway: HSTS tells browsers to enforce HTTPS-only access for your domain, preventing protocol downgrade and SSL stripping attacks by never allowing plain HTTP requests to leave the user's browser.