HTTP Strict Transport Security

Description
HTTP Strict Transport Security (HSTS) is an opt-in security enhancement that is specified by a web application through the use of a special response header. Once a supported browser receives this header that browser will prevent any communications from being sent over HTTP to the specified domain and will instead send all communications over HTTPS. It also prevents HTTPS click through prompts on browsers.

The specification has been released and published end of 2012 as RFC 6797 (HTTP Strict Transport Security (HSTS)) by the IETF. (Reference see in the links at the bottom.)

Examples
Simple example, using a long (1 year) max-age:

Strict-Transport-Security: max-age=31536000

If all present and future subdomains will be HTTPS:

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

Recommended: If the site owner would like their domain to be included in the HSTS preload list maintained by Chrome (and used by Firefox and Safari), then use:

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

The `preload` flag indicates the site owner's consent to have their domain preloaded. The site owner still needs to then go and submit the domain to the list.

Excessively Strict STS
Use caution when setting excessively strict STS policies. Including subdomains should only be used in environments where all sites within your organization for the given domain name require ssl. Max-age limits should be carefully considered as infrequent visitors may find your site inaccessible if you relax your policy.

Before enabling includeSubDomains, also consider the impact of any existing DNS CNAME records for CDNs, email services, or other 3rd party services. Since includeSubDomains will force such CNAME subdomains to https:// it's likely the browser will throw a domain-mismatch error, which is hard to reverse because of the browser caching nature of HSTS.

Browser Support
A detailed overview of supporting browsers can be found at caniuse.com.

Server Side
The web server side needs to inject the HSTS header.

For HTTP sites on the same domain it is not recommended to add a HSTS header but to do a permanent redirect (301 status code) to the HTTPS site. An Apache HTTPd example that will permanently redirect a URL to the identical URL with a HTTPS scheme, is as follows:

 ServerAlias * RewriteEngine On       RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [redirect=301] 

On the HTTPS site configuration the following is needed to add the header as recommended by the standard: Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

The following links show how to set response headers in other web servers:
 * NGINX
 * Lighttpd
 * HTTPd

IIS
Whilst custom headers can be configured in IIS without any extensions, it is not possible to restrict these headers to secure transport channels as per the HSTS specification. This leaves the following options.

Custom Module
HSTS has been implemented, per the specification, as an open source IIS module.

URL Rewrite
Install Microsoft URL Rewrite and use following rewrite rules.

              </outboundRules> </system.webServer>

Threats
HSTS addresses the following threats:
 * User bookmarks or manually types http://example.com and is subject to a man-in-the-middle attacker
 * HSTS automatically redirects HTTP requests to HTTPS for the target domain
 * Web application that is intended to be purely HTTPS inadvertently contains HTTP links or serves content over HTTP
 * HSTS automatically redirects HTTP requests to HTTPS for the target domain
 * A man-in-the-middle attacker attempts to intercept traffic from a victim user using an invalid certificate and hopes the user will accept the bad certificate
 * HSTS does not allow a user to override the invalid certificate message

Links

 * AppSecTutorial Series - Episode 4
 * Chromium Projects/HSTS
 * HSTS Spec
 * Wikipedia
 * Mozilla Developer Network
 * OWASP TLS Protection Cheat Sheet
 * Firefox STS Support
 * Google Chrome STS Support
 * Moxie Marlinspike's Black Hat 2009 talk on sslstrip, that demonstrates why you need HSTS