
Security headers are directives web applications use to configure protection features in web browsers. Based on these directives, browsers make client-side vulnerabilities such as Cross-Site Scripting (XSS), clickjacking and code injection harder to exploit.
HSTS (HTTP Strict Transport Security)
HTTP Strict Transport Security (HSTS) is an important security feature that helps protect websites and their users against several forms of attack, including man-in-the-middle (MITM) attacks and session and cookie theft.
HSTS works by instructing web browsers to use only secure HTTPS connections when communicating with a website, even if the user manually types an HTTP URL or clicks an insecure link. By forcing traffic over HTTPS, HSTS helps prevent the interception or modification of traffic and the unintentional exposure of sensitive data.
HSTS also protects against an attack called SSL stripping, in which an attacker downgrades the connection to HTTP and then intercepts the traffic. By telling the browser to always use HTTPS, HSTS prevents this attack and ensures user data is always encrypted.
Overall, HSTS should be implemented by every website that needs a high level of security.
Remediation:
- Edit "httpd.conf" and enable the line
LoadModule headers_module modules/mod_headers.so. - Add the following line to the VirtualHost:
Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload" - Restart the web server
Content Security Policy (CSP)
The Content Security Policy (CSP) response header helps prevent several types of attacks, such as cross-site scripting (XSS) and data injection, by letting developers define the sources from which the browser may execute scripts, load resources and interact with the page.
We wrote about CSP in more detail in: Content Security Policy (CSP): defining where resources can be loaded from
X-XSS-Protection (deprecated - use CSP instead)
The X-XSS-Protection response header was designed to help prevent cross-site scripting (XSS), one of the most common web application vulnerabilities.
It instructed the browser to enable its built-in XSS filter, which tried to detect and block attempts to inject malicious scripts. Modern browsers have removed this filter, and in some cases it introduced new problems, so today the recommendation is to rely on a strict Content Security Policy and, if the header is still sent, to set it to 0. Older guidance recommended:
Legacy configuration:
- Add the following line to the VirtualHost:
Header set X-XSS-Protection "1; mode=block" - Restart the web server
Referrer-Policy
The Referrer-Policy HTTP response header helps protect web applications against information leakage and makes some attacks, such as Cross-Site Request Forgery (CSRF), harder.
It controls how much information is included in the HTTP Referer header when a user clicks a link or submits a form that takes them from one page to another. The Referer header usually contains the URL of the page where the user clicked the link or submitted the form.
If Referrer-Policy is set to “no-referrer”, the Referer header is not sent at all. This can, however, break legitimate uses of the header, such as analytics or debugging.
If it is set to “strict-origin-when-cross-origin” or “same-origin”, the full Referer is sent only for same-origin requests, and cross-origin requests receive at most the origin.
Overall, Referrer-Policy should be implemented by every web application: by controlling how much information goes into the Referer header, it helps prevent leaks and protects user data.
Remediation:
- Add the following line to the VirtualHost:
Header always set Referrer-Policy "strict-origin" - Restart the web server
X-Frame-Options
The X-Frame-Options header is a security feature that helps protect web applications against clickjacking.
Clickjacking is an attack in which the attacker tricks users into clicking a button or link on a web page that is hidden or disguised as a legitimate element.
We wrote about X-Frame-Options and how to set it in: X-Frame-Options: protecting web applications against clickjacking
X-Content-Type-Options
The X-Content-Type-Options header is an important security feature that helps protect web applications against MIME type confusion attacks.
MIME confusion attacks happen when the browser interprets content as a different type than intended, which can let an attacker run malicious code in the user’s browser. For example, an attacker could upload a file that looks like an image but actually contains script; if the browser “sniffs” the content and treats it as executable, the attacker’s code may run.
X-Content-Type-Options lets developers prevent this by telling the browser not to guess the content type. Setting the header to “nosniff” instructs the browser to always use the MIME type declared in the Content-Type header instead of detecting it from the response body.
This is particularly important for web applications that let users upload files or submit data, as they are more exposed to this type of attack.
Remediation:
- Add the following line to the VirtualHost:
Header always set X-Content-Type-Options "nosniff" - Restart the web server
