[SECURITY] Nginx Header 1 : The easiest header : HSTS Preload

Bonjour,

Introduction

Le header HSTS (HTTP Strict Transport Security) est un en-tête Nginx très important. C’est un mécanisme de sécurité permettant d’assurer la sécurité des échanges entre le navigateur et le serveur. Il demande au navigateur d’interagir avec le site web à travers une connexion sécurisée (HTTPS). Il permet entres autres de prévenir plusieurs attaques :

  • écoutes passives du réseau
  • Man-In-The-Middle : Même si l’attaque produit un certificat invalide visant à le faire accepter par l’utilisateur, l’attaque échouera car le mécanisme de sécurité empêchera l’utilisateur d’ajouter un exception de sécurité.
  • Les requêtes http seront converties en requêtes https.

Il faut savoir, que le HSTS va permettre de modifier tous les liens http en lien https.

Utilisation

Le header HSTS dans la configuration nginx s’écrit de la manière suivante :

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";

ou :

more_set_headers "Strict-Transport-Security : max-age=63072000; includeSubDomains";

(le nouveau standard)

Cette configuration est déjà présente dans Yunohost.

Le Préchargement (preload)

Le problème du HSTS : il ne devient actif que lors de la première connexion. Cependant, il y a une solution qui permet le préchargement de ce header dans la navigateur: le preload.

Pour pouvoir l’activer, il faut déjà rajouter le préchargement dans sa conf nginx (déjà présent dans Yunohost) :

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";

Mais cela ne suffit pas : il faut ensuite vous inscrire sur https://hstspreload.org/. Cependant, ce site internet exige que le préchargement soit fait sur le domaine tout entier et non pas sur un sous-domaine. Il faut par ailleurs activer le HSTS ensuite pour tous les sous-domaines (mesure de sécurité).

Il est aussi bon de savoir, que si votre domaine, un jour, n’est plus actif, il sera supprimé de la liste des “HSTS Preload Sites”.

Conclusion : Que dois-je faire ?

La configuration avec le préchargement est déjà présente dans la configuration de base de Yunohost. Il n’y a donc rien à ajouter.

Le HSTS nécessite un certificat valide. Vous pouvez donc commencer par ajouter un certificat LE depuis votre interface d’administration.

Pour activer le préchargement, il suffit de quelques étapes supplémentaires :
1 - Vérifier que votre domaine domain.tld est accessible (il ne doit y avoir aucune redirection vers un sous-domaine; par contre, une redirection vers un sous-dossier ne dérangera pas), et qu’il possède un certificat valide.
2 - Inscrivez-vous sur https://hstspreload.org


Hello,

Introduction

The HSTS header (HTTP Strict Transport Security) is a very important Nginx header. It is a security mechanism to ensure the security of exchanges between the browser and the server. It asks the browser to interact with the website through a secure connection (HTTPS). Among other things, it prevents several attacks:

  • passive listening of the network
  • Man-In-The-Middle: Even if the attack produces an invalid certificate to make the user accept it, the attack will fail because the security mechanism will prevent the user from adding a security exception.
  • The http requests will be converted to https requests.

It should be noted that the hsts will allow to modify all http links into https links.

Use

The HSTS header in the nginx configuration is written as follows:

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";

or :

more_set_headers "Strict-Transport-Security : max-age=63072000; includeSubDomains";

(the new standard)

This configuration is already present in Yunohost.

Preload (preload)

The problem with HSTS is that it only becomes active during the first connection. There is a solution that allows the preloading of this header in the browser: the preload.

To be able to activate it, you must already add the prelod in its nginx conf (already present in Yunohost):

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";

But that’s not enough: you must then register at https://hstspreload.org/. However, this website requires that the prelod be done on the entire domain and not on a subdomain. In addition, the HSTS must also be activated afterwards for all sub-domains (security measure).

It is also good to know that if your domain, one day, is no longer active, it will be deleted from the list of “HSTS Preload Sites”.

Conclusion: What should I do?

The configuration with preload is already present in the basic configuration of Yunohost. So there is nothing more to say.

The HSTS requires a valid certificate. So you can start by adding a LE certificate from your administration interface.

To activate the preload, just do a few additional steps:
1 - Check that your domain domain.tld is accessible (there must be no redirection to a subdomain; however, a redirection to a subfolder will not disturb), and that it has a valid certificate.
2 - Register on https://hstspreload.org


Sources :

WIKIPEDIA : https://fr.wikipedia.org/wiki/HTTP_Strict_Transport_Security
OWASP SECURITY : https://www.owasp.org/index.php/HTTP_Strict_Transport_Security_Cheat_Sheet
MOZILLA SECURITY : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security

Test your server header/security :

SSLLABS : https://www.ssllabs.com/ (check security)
SECURITYHEADER : https://securityheaders.com/ (check headers)

1 Like

Would you say it is safe to enable HSTS through an application? Weblate suggests me to do so, but I don’t understand the risks.

Do I have to make sure the domain has a valid certificate? Is there an easy way to do so?

Thanks for your help

Yes, it’s safer. HSTS header is a protection, that mean that you will not have the right to access your website if it has not a valid certificate and your communication with the server will be HTTPS Only.

It has an advantage and a little drawback. Well, if you forget to update certificate, you won’t be able to renew it from the webadmin interface, you will have to connect to SSH and update it through CLI.

It’s high security, and the recommendation from security researcher is to enable it on every production servers, because it avoids MIM attack and make the communication secure.

Preloading is much more security, because in fact, before having to a website, the browser know that the website has HSTS enabled. Explanation : In fact, the first time you go on a website with HSTS, the server has to send the header and then the browser know that this site is in HSTS. As far as I understand the pb, if this first time, there is an MIM attack, it’s possible that this HSTS header doesn’t work in some cases. That’s the reason, there is a list with HSTS preloading, this : https://hstspreload.org/. this list is register in every browser and is updated. So the first time you go on a website, if it’s had preloding enabled, the browser knows before you go to the website that it’s preloaded.

I hop this will help you,

Don’t hesitate if you have any other questions.
I’m not security expert, so it’s what I understand :wink:

PS: Sorry for my very bad english.

Note that it’s clearly something that should happen in the core and not driven by an app installation … and in fact it’s already included in the core …

Indeed. I just answered @jibecfed. This post is very old. It’s what I did before adding it in the core.

2 Likes