Headers HTTP de sécurité : HSTS, CSP, X-Frame-Options — guide complet

Les en-têtes HTTP de sécurité sont des instructions que votre serveur envoie au navigateur pour lui indiquer comment se comporter. Correctement configurés, ils bloquent des catégories entières d'attaques — XSS, clickjacking, sniffing de contenu — sans modifier une seule ligne de code de votre application.

Pourquoi ces headers sont critiques

Contrairement aux failles applicatives qui nécessitent des corrections de code, les headers HTTP de sécurité se configurent directement au niveau du serveur web. Un ajout de quelques lignes dans nginx ou Apache suffit à activer des protections que des millions de sites négligent encore.

Selon les analyses automatiques de Wezea, plus de 60 % des domaines scannés n'ont pas de Content-Security-Policy, et 45 % manquent de HSTS. Ces deux headers seuls élimineraient pourtant la majorité des vecteurs d'attaque côté navigateur.

WARN
Un header manquant est une vulnérabilité active
L'absence de ces en-têtes n'est pas un simple défaut de configuration — c'est une porte ouverte aux attaques XSS, au sniffing de connexion et à l'injection de contenu malveillant via des iframes invisibles.

HSTS — forcer le HTTPS

HTTP Strict Transport Security (HSTS) indique au navigateur qu'il ne doit jamais accéder à votre site en HTTP clair — même si l'utilisateur tape l'URL sans "https://". Pendant la durée définie, le navigateur refuse automatiquement toute connexion non chiffrée.

# Header HSTS recommandé
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

Le paramètre max-age=31536000 correspond à un an. includeSubDomains protège également tous vos sous-domaines. preload permet d'inscrire votre domaine dans la liste HSTS Preload de Chrome et Firefox — le navigateur connaît alors votre site comme HTTPS-only avant même d'y avoir accédé.

INFO
Prérequis avant d'activer HSTS
Assurez-vous que tous vos sous-domaines ont un certificat SSL valide avant d'activer includeSubDomains. Un sous-domaine sans HTTPS deviendrait inaccessible.

Content-Security-Policy — bloquer les injections

La Content-Security-Policy (CSP) est le header le plus puissant — et le plus complexe à configurer. Elle définit quelles sources de contenu (scripts, styles, images, iframes) sont autorisées sur votre page. Une CSP bien réglée rend les attaques XSS quasiment inopérantes.

# CSP stricte pour un site statique
Content-Security-Policy: default-src 'self';
  script-src 'self' https://trusted-cdn.com;
  style-src 'self' 'unsafe-inline';
  img-src 'self' data:;
  frame-ancestors 'none';

La directive default-src 'self' interdit tout contenu externe non explicitement autorisé. Chaque directive surcharge ce comportement par défaut pour sa catégorie. Commencez en mode rapport avec Content-Security-Policy-Report-Only pour ne pas casser votre site.

Les directives CSP les plus importantes

Directive Rôle Valeur sécurisée
default-src Fallback pour toutes les ressources 'self'
script-src Sources JavaScript autorisées 'self' (éviter 'unsafe-inline')
frame-ancestors Qui peut intégrer votre page en iframe 'none' ou 'self'
upgrade-insecure-requests Force HTTPS pour toutes les ressources Toujours activer

X-Frame-Options — stopper le clickjacking

Le clickjacking consiste à superposer votre site légitime dans une iframe invisible au-dessus d'une page piégée. L'utilisateur pense cliquer sur un bouton inoffensif mais déclenche une action sur votre site (achat, changement de mot de passe, validation de formulaire).

# Interdire l'intégration en iframe
X-Frame-Options: DENY

# Autoriser uniquement votre propre domaine
X-Frame-Options: SAMEORIGIN

Note : frame-ancestors dans la CSP est la méthode moderne et remplace X-Frame-Options. Configurez les deux pour une compatibilité maximale avec les anciens navigateurs.

Les autres headers essentiels

X-Content-Type-Options

Empêche le navigateur de "deviner" le type MIME d'une réponse. Sans ce header, un attaquant peut servir un fichier malveillant déguisé en image ou texte, que le navigateur exécuterait comme du JavaScript.

X-Content-Type-Options: nosniff

Referrer-Policy

Contrôle quelles informations sont transmises dans l'en-tête Referer quand vos utilisateurs cliquent sur un lien externe. Évite de fuiter des URLs internes ou des tokens de session.

Referrer-Policy: strict-origin-when-cross-origin

Permissions-Policy

Limite l'accès aux APIs du navigateur (caméra, microphone, géolocalisation) pour votre site et les iframes qu'il intègre. Particulièrement important si vous intégrez des widgets ou publicités tiers.

Permissions-Policy: camera=(), microphone=(), geolocation=()

Configurer sur nginx et Apache

nginx

# Dans le bloc server{} de votre vhost nginx
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-ancestors 'none';" always;

Apache

# Dans votre .htaccess ou VirtualHost
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-Frame-Options "DENY"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"

# Activer le module headers si nécessaire
a2enmod headers && systemctl reload apache2
INFO
Le mot-clé always est important sous nginx
Sans always, nginx n'envoie les headers que sur les réponses 200. Avec always, ils sont présents sur toutes les réponses, y compris les erreurs 4xx et 5xx.

Vérifier vos headers

Après avoir déployé vos headers, vérifiez qu'ils sont bien présents et correctement configurés. Plusieurs méthodes s'offrent à vous.

# Via curl en ligne de commande
curl -I https://votre-domaine.com

# Résultat attendu
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-frame-options: DENY
x-content-type-options: nosniff
content-security-policy: default-src 'self'; ...

Wezea analyse automatiquement vos headers HTTP lors de chaque scan de domaine et signale les manquants avec leur niveau de risque associé.

Vérifiez vos headers HTTP maintenant

Wezea analyse en 60 secondes la présence et la validité de vos en-têtes de sécurité.

Lancer le scan gratuit →

Gratuit · Sans inscription · Résultat immédiat

HTTP security headers: HSTS, CSP, X-Frame-Options — complete guide

HTTP security headers are instructions your server sends to the browser to tell it how to behave. When properly configured, they block entire categories of attacks — XSS, clickjacking, content sniffing — without changing a single line of your application code.

Why these headers are critical

Unlike application vulnerabilities that require code fixes, HTTP security headers are configured directly at the web server level. Adding just a few lines to nginx or Apache enables protections that millions of sites still neglect.

According to Wezea's automated analysis, over 60% of scanned domains lack a Content-Security-Policy, and 45% are missing HSTS. These two headers alone would eliminate most browser-side attack vectors.

WARN
A missing header is an active vulnerability
The absence of these headers is not just a configuration oversight — it's an open door to XSS attacks, connection sniffing, and malicious content injection via invisible iframes.

HSTS — enforce HTTPS

HTTP Strict Transport Security (HSTS) tells the browser it should never access your site over plain HTTP — even if the user types the URL without "https://". For the specified duration, the browser automatically refuses any unencrypted connection.

# Recommended HSTS header
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

The max-age=31536000 parameter corresponds to one year. includeSubDomains also protects all your subdomains. preload allows you to register your domain in the HSTS Preload list of Chrome and Firefox — the browser will know your site is HTTPS-only before even visiting it.

INFO
Prerequisites before enabling HSTS
Ensure that all your subdomains have valid SSL certificates before enabling includeSubDomains. A subdomain without HTTPS would become inaccessible.

Content-Security-Policy — block injections

Content-Security-Policy (CSP) is the most powerful header — and the most complex to configure. It defines which content sources (scripts, styles, images, iframes) are allowed on your page. A well-tuned CSP makes XSS attacks virtually inoperative.

# Strict CSP for a static site
Content-Security-Policy: default-src 'self';
  script-src 'self' https://trusted-cdn.com;
  style-src 'self' 'unsafe-inline';
  img-src 'self' data:;
  frame-ancestors 'none';

The default-src 'self' directive blocks all external content not explicitly allowed. Each directive overrides this default behavior for its category. Start in report mode with Content-Security-Policy-Report-Only to avoid breaking your site.

The most important CSP directives

Directive Role Secure value
default-src Fallback for all resources 'self'
script-src Allowed JavaScript sources 'self' (avoid 'unsafe-inline')
frame-ancestors Who can embed your page in an iframe 'none' or 'self'
upgrade-insecure-requests Force HTTPS for all resources Always enable

X-Frame-Options — stop clickjacking

Clickjacking involves overlaying your legitimate site in an invisible iframe above a malicious page. Users think they're clicking an innocent button but trigger an action on your site (purchase, password change, form submission).

# Forbid iframe embedding
X-Frame-Options: DENY

# Allow only your own domain
X-Frame-Options: SAMEORIGIN

Note: frame-ancestors in CSP is the modern method and replaces X-Frame-Options. Configure both for maximum backward compatibility.

Other essential headers

X-Content-Type-Options

Prevents the browser from "guessing" the MIME type of a response. Without this header, an attacker can serve a malicious file disguised as an image or text that the browser would execute as JavaScript.

X-Content-Type-Options: nosniff

Referrer-Policy

Controls what information is transmitted in the Referer header when your users click external links. Prevents leaking internal URLs or session tokens.

Referrer-Policy: strict-origin-when-cross-origin

Permissions-Policy

Restricts access to browser APIs (camera, microphone, geolocation) for your site and the iframes it embeds. Particularly important if you integrate third-party widgets or ads.

Permissions-Policy: camera=(), microphone=(), geolocation=()

Configure on nginx and Apache

nginx

# Inside the server{} block of your nginx vhost
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-ancestors 'none';" always;

Apache

# In your .htaccess or VirtualHost
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-Frame-Options "DENY"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"

# Enable headers module if necessary
a2enmod headers && systemctl reload apache2
INFO
The always keyword is important in nginx
Without always, nginx only sends headers on 200 responses. With always, they are present on all responses, including 4xx and 5xx errors.

Verify your headers

After deploying your headers, verify they are present and properly configured. Several methods are available.

# Via curl command line
curl -I https://your-domain.com

# Expected output
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-frame-options: DENY
x-content-type-options: nosniff
content-security-policy: default-src 'self'; ...

Wezea automatically analyzes your HTTP headers on every domain scan and flags missing ones with their associated risk level.

Check your HTTP headers now

Wezea analyzes in 60 seconds the presence and validity of your security headers.

Run the free scan →

Free · No sign-up · Instant results

Vous gérez les domaines de vos clients ? Wezea propose des rapports PDF en marque blanche et un monitoring multi-domaines pour les agences web et MSP.

Découvrir Wezea Agences →

Managing your clients's domains? Wezea offers white-label PDF reports and multi-domain monitoring for web agencies and MSPs.

Discover Wezea for Agencies →