Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to enable HSTS in addition to forcing HTTPS in .htaccess?

I know nothing about this stuff so please ELI5 in your replies.

Following the instructions from my provider, Dreamhost, I installed an SSL certificate and then added these lines to my .htaccess file to force HTTP requests to be rewritten to HTTPS requests.

# Redirect http requests to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Everything seems to be working correctly, ie: every time I try to access a page under that domain with HTTP, it is rewritten to HTTPS and the "Secure" icon shows in the address bar.

My question is, do I need to also enable HSTS? Reading about it, it seems to do the exact same thing as the previous changes to the .htaccess file. Here's an excerpt from A2 Hosting (not my provider):

Enabling HSTS

When HSTS is enabled for a site, web browsers automatically change any insecure requests (http://) to secure requests (https://). All you need to do to enable HSTS is add a header to your site's .htaccess file. Web browsers recognize this header, and then take care of the rest without any further intervention on your part.

They suggest adding this to .htaccess:

Header set Strict-Transport-Security "max-age=31536000" env=HTTPS

Another tutorial, this time specific to Dreamhost, says to enable HSTS along with forcing HTTPS in the .htaccess file, but doesn't really say why. This page suggests something slightly different:

Header set Strict-Transport-Security "max-age=31415926; includeSubDomains; preload" env=HTTPS

Do I need the "https rewrite" code snippet AND HSTS? Or is having only the "https rewrite" code snippet good enough? Do I need the HSTS code at all, and if so, what's the difference between the two lines of HSTS code in my post?

like image 584
AlwaysLearning Avatar asked Mar 17 '17 19:03

AlwaysLearning


People also ask

Is HSTS required for HTTPS?

HTTP Strict Transport Security (HSTS) is a simple and widely supported standard to protect visitors by ensuring that their browsers always connect to a website over HTTPS. HSTS exists to remove the need for the common, insecure practice of redirecting users from http:// to https:// URLs.

Should HSTS be enabled?

Why Enable HTTP Strict Transport Security (HSTS)? Enabling HSTS will revoke SSL protocol attacks and cookies hijacking. It will also allow websites to load faster by removing a step in the loading procedure.

What happens if HSTS is not enabled?

The lack of HSTS allows downgrade attacks, SSL-stripping man-in-the-middle attacks, and weakens cookie-hijacking protections. Impact: SSL-stripping man-in-the-middle attacks, and weakens cookie-hijacking protections.


1 Answers

HSTS lets the browser know to only connect over https by default but each one of the different flags does something a bit different:

  • includeSubdomains

That means that if your site is on mydomain.com, the policy will apply to all subdomains (i.e. foo.mydomain.com, bar.mydomain.com, etc). Without this included. the policy only applies for the exact domain in question.

  • preload

While HSTS is great conceptually, the first time someone types mydomain.com the browser will try to contact your site on http scheme since it doesn't know that you have your site on https which gives a MITM attacker room to serve you malicious version of the site (aka TOFU problem). To work around this, there is a centralized list for browsers for sites that should be contacted on https by default but to be able to get yourself on that list, you have to have the preload flag in that header. You can see more about this here.

like image 168
Srdjan Grubor Avatar answered Oct 29 '22 00:10

Srdjan Grubor