Skip to content
Securityintermediate

What is HSTS?

Updated Nov 30, 20263 min readBased on official documentation
What is HSTS?

In short

HSTS, short for HTTP Strict Transport Security, is a web security policy that tells a browser to use only secure HTTPS connections with a website. A site declares it with the Strict-Transport-Security response header. Once the browser has seen that header, it refuses plain HTTP for that site for a set period.

The problem HSTS solves

Websites often start on plain HTTP and then redirect visitors to HTTPS. That first plain request is a gap. Someone on the same network can intercept it, block the redirect, and keep the visitor on an unencrypted connection. This kind of attack is often called a downgrade or stripping attack.

HSTS closes that gap. It lets a website state, in advance, that it should only be reached over a secure connection. Browsers that understand the policy then avoid plain HTTP for that site on their own.

The specification defines HSTS as a policy that a site declares through an HTTP response header, and in some cases through browser configuration. The header is named Strict-Transport-Security.

How it works

The server sends the header in a response that arrives over HTTPS. A typical value looks like this:

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

The max-age part is a number of seconds. It tells the browser how long to remember the policy. In this example, the browser remembers it for one year. Each time the browser sees the header again, it refreshes the timer.

The includeSubDomains part is optional. When it is present, the policy also covers every subdomain of the site, such as a name that begins with a further label in front of the main domain.

After the browser stores the policy, it changes any plain http:// address for that host into a secure https:// address before it sends the request. The user does not need to type anything different. The browser does this on its own.

The specification also says that a browser should only store the policy if the connection was set up without errors. If the certificate is invalid or the secure connection fails, the header must be ignored. This prevents a broken or fake connection from setting the policy.

When to use it

HSTS fits any site that serves its pages entirely over HTTPS and wants to stay that way. It is most useful for sites that handle logins, payments, or personal data, but any site that should never be visited over plain HTTP can benefit.

You should be sure HTTPS works for every page and subdomain before you turn on includeSubDomains. If a subdomain still serves plain HTTP, browsers will refuse to load it once the policy applies. For background on the secure connection layer that HSTS depends on, see our TLS guide, if it is listed on the site.

A simple example

Imagine a shop at example-shop.test. Its server responds to a secure request with the header below. Its certificate is valid, so the browser accepts the policy.

HTTP/1.1 200 OK
Strict-Transport-Security: max-age=86400

One day later, the visitor types example-shop.test into the address bar. The browser still remembers the policy, so it connects with https:// directly. No plain HTTP request leaves the computer. If the policy has expired, the browser would fall back to the normal behaviour and the server would need to redirect again.

Note that the browser only learns about the policy after the first secure visit. The very first request, before any header has been seen, can still be exposed. That gap is why the specification discusses a preload list, which browsers can ship with built-in site entries.

Common mistakes

Several mistakes come up often. The first is sending the header over plain HTTP. Browsers ignore it there, because the policy must come from a secure connection. The second is setting a very long max-age before testing. If you later need to drop HTTPS on a host, visitors' browsers may keep forcing it until the timer runs out.

The third mistake is using includeSubDomains without checking every subdomain. Any name under the domain that lacks a working HTTPS setup will stop working for those visitors. The fourth is assuming HSTS protects against everything. It does not defend against phishing sites or malware on a user's machine. The specification lists these as threats it does not address.

The simplest alternative is a plain redirect from HTTP to HTTPS. It is easy to set up, but it does not stop the first unencrypted request, so it protects less than HSTS. Many sites use both: a redirect for new visitors and the header for returning ones.

Another option is the browser's preload list, where a domain is built into the browser before any visit. This removes the first-visit gap, but it needs a formal submission process and a long commitment to HTTPS. Treat it as a later step, once your HTTPS setup is well tested.

FAQ

Does HSTS replace HTTPS?

No. HSTS works on top of HTTPS. It tells the browser to use HTTPS for a site, but the site still needs a valid TLS certificate and a working secure connection.

How long does a browser remember an HSTS policy?

The browser remembers it for the number of seconds set in the max-age value. Each new secure response with the header refreshes that time. A value of zero tells the browser to drop the stored policy.

What happens if my certificate is invalid?

The browser must not store the policy when the secure connection has errors. Users may still see a warning from the browser, and the header is ignored in that case.

Can HSTS stop phishing?

No. HSTS only enforces secure connections to a real site. It does not check whether a site is the one the user meant to visit, so phishing pages still need other defences.

Sources

This page is based on the official documentation below. Read it for full detail.

HSTS — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.