When it comes to keeping your website secure, there’s a lot to consider. One of the most important, yet often overlooked, aspects is website security headers. These headers play a crucial role in protecting your site from a range of threats, including cross-site scripting (XSS), clickjacking, and even man-in-the-middle attacks.
Website security headers are like the first line of defense for your website. They tell the browser how to handle certain types of requests, such as what content to trust or which features to block. Setting them up properly can make a huge difference in keeping both your site and your users safe from potential harm.
In this post, we’ll explain what website security headers are, why they’re essential, and how you can implement them on your site.
Read Also: How to Scan Vulnerabilities on WordPress Using Virtualbox (WPScan Github and OpenVas).
Types of Website Security Headers

1. Content-Security-Policy (CSP)
The Content-Security-Policy (CSP) header is one of the most powerful security headers you can implement. It protect your website from cross-site scripting (XSS) attacks and other forms of code injection by allowing web administrators to specify which content sources are considered safe.
With CSP, you can define a set of trusted domains for things like images, scripts, styles, and other resources. This way, even if an attacker tries to inject malicious code into your website, the browser will block any content that doesn’t come from a trusted source.
2. Strict-Transport-Security (HSTS)
Strict-Transport-Security (HSTS) is a security header that forces browsers to only connect to your website using HTTPS, not HTTP.
This helps prevent downgrade attacks, where an attacker might try to intercept or modify communications by tricking the browser into using an insecure connection. HSTS ensures that all connections to your site are encrypted, protecting sensitive data such as login credentials and personal information.
It also guards against cookie hijacking, as cookies transmitted over HTTPS are much harder for attackers to steal.
3. X-Content-Type-Options
The X-Content-Type-Options header is designed to prevent browsers from interpreting files as a different MIME type than what is specified in the response. This is important for preventing attacks where an attacker might upload a malicious file that the browser mistakenly treats as a different type, such as executing a JavaScript file disguised as an image or a document.
By setting this header to nosniff, you ensure that the browser will strictly follow the declared content type and won’t attempt to “sniff” or guess the type of the content.
4. X-Frame-Options
The X-Frame-Options header is crucial for preventing a type of attack called clickjacking. Clickjacking occurs when a malicious website embeds your site in an invisible iframe and tricks users into clicking on something different from what they think they’re clicking on—like a hidden button that performs an unintended action. By using this header, you can control whether your website can be embedded in an iframe at all. There are a few settings for X-Frame-Options:
- DENY: This setting prevents your site from being embedded in any frame or iframe, regardless of where the request is coming from.
- SAMEORIGIN: This setting allows your site to be framed, but only by pages from the same origin (domain).
- ALLOW-FROM: This allows your website to be framed only by a specified URL or domain.
Implementing X-Frame-Options helps ensure that your site can’t be used in clickjacking attacks, adding an important layer of protection for user interactions.
5. X-XSS-Protection
The X-XSS-Protection header is a basic form of protection against reflected cross-site scripting (XSS) attacks. These attacks occur when an attacker tricks a website into reflecting malicious JavaScript back to the user’s browser.
By setting the X-XSS-Protection header to 1; mode=block, the browser will attempt to detect reflected XSS attacks and block the page from rendering if such an attack is detected. While this is a basic level of protection, it is useful in preventing simple XSS attacks from being executed. Modern browsers and more comprehensive security measures like Content-Security-Policy (CSP) are recommended for stronger XSS protection.
6. Referrer-Policy
The Referrer-Policy header controls what information the browser sends as the “referrer” when making requests to other websites. The referrer is the URL of the page from which a user navigated to another page. It sometimes reveal sensitive information such as query strings, session tokens, or personal data.
By setting a strict referrer policy, you can control how much of that information is sent. Some common settings for the Referrer-Policy header include:
- no-referrer: No referrer information is sent.
- no-referrer-when-downgrade: Referrer information is sent unless navigating from HTTPS to HTTP.
- same-origin: Referrer information is sent only to the same origin (domain).
- strict-origin-when-cross-origin: Referrer information is sent only for same-origin requests, or for cross-origin requests over HTTPS.
7. Feature-Policy (now Permissions-Policy)
The Feature-Policy header, which has now been replaced by the Permissions-Policy header, allows web administrators to restrict certain browser features that pose security or privacy risks. Features like geolocation, camera access, microphone access. By setting this header, you can control which features are available on your site, enhancing both security and user privacy.
For example, if your website doesn’t need the user’s camera, you can disable it for all users by specifying camera ‘none’ in your Permissions-Policy header. Some common settings in Permissions-Policy include:
- geolocation ‘self’: Restricts access to geolocation to the same origin.
- camera ‘none’: Disables camera access entirely.
- microphone ‘self’: Restricts microphone access to the same origin.
8. Cache-Control and Pragma
The Cache-Control and Pragma headers control how resources on your site are cached by the browser or intermediary caches (like content delivery networks or proxy servers). While caching improves site speed, improper caching settings leave data privacy concerns.
For example, sensitive information like authentication tokens, personal data, or payment details should not be cached, as doing so could expose it to unintended parties.
By using the Cache-Control header, you can specify directives to manage caching behavior. Common directives include:
- no-cache: Prevents caching of sensitive resources.
- no-store: Ensures that sensitive information is not stored in the browser’s cache.
- private: Allows caching only for the user’s browser and not for shared caches.
- max-age: Specifies the maximum amount of time a resource is considered fresh.
The Pragma header is an older HTTP header that is typically used for backward compatibility, particularly with HTTP/1.0 clients. It’s often set to no-cache to prevent caching of sensitive data.
Why Are Website Security Headers Important?

Website security headers are essential for protecting your site from common web vulnerabilities, enhancing privacy, and ensuring regulatory compliance.
Prevention of Common Web Vulnerabilities
Security headers help prevent issues like XSS, clickjacking, and mixed content. For example, Content-Security-Policy (CSP) blocks malicious scripts, while X-Frame-Options prevents clickjacking by blocking iframing. HSTS ensures secure HTTPS connections, protecting against downgrade attacks and mixed content issues.
Improved Privacy
Security headers also protect user privacy. For instance, the Referrer-Policy controls what information is shared with third-party sites, preventing the exposure of sensitive data. Permissions-Policy restricts access to sensitive browser features like geolocation and camera, safeguarding user privacy.
Helps Achieve Regulatory Compliance
Security headers are important for meeting regulations like GDPR and PCI DSS. For example, Cache-Control prevents sensitive data from being cached, while HSTS and CSP contribute to secure data transmission and user protection, helping your site comply with security standards.
How to configure and Implement Website Security Headers
Implementing website security headers involves configuring your web server to send the appropriate headers with each HTTP response. Here’s how you can set them up in popular web servers:
Setting up Website Security Headers in Apache
To configure security headers in Apache, you can add the following directives to your site’s .htaccess file or the server’s configuration file (httpd.conf or apache2.conf):
Header set Content-Security-Policy "default-src 'self'; script-src 'self';"
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header set X-Frame-Options "DENY"
Header set X-Content-Type-Options "nosniff"
Header set Referrer-Policy "no-referrer"
Setting up Website Security Headers in Nginx
In Nginx, you can add security headers by editing the nginx.conf file or the server block configuration:
add_header Content-Security-Policy "default-src 'self'; script-src 'self';" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer" always;
These configurations enable key security headers like CSP, HSTS, X-Frame-Options, X-Content-Type-Options, and Referrer-Policy, which enhance your site’s protection.
Testing and Monitoring
Once you’ve set up your security headers, it’s important to test and monitor their implementation to ensure they are working correctly:
- SecurityHeaders.com: This tool scans your website and gives you a detailed report on which headers are present and how securely they are configured.
- Mozilla Observatory: Another great tool for testing security headers, providing a comprehensive security score and offering suggestions for improvement.
Ongoing monitoring is also critical. You should regularly review the security headers on your site to ensure they remain up to date and compliant with the latest best practices. There are also automated tools and scanners that can check these headers periodically to alert you to any changes or misconfigurations.
Common Pitfalls to Avoid
When configuring security headers, it’s easy to make misconfigurations that could lead to vulnerabilities. Here are some common pitfalls:
- A poorly configured Content-Security-Policy might allow too many external sources, which could expose your site to XSS attacks. For example, a CSP that allows script-src * (all sources) would defeat the purpose of the header. A more secure setting would be script-src ‘self’, restricting scripts to your own domain.
- Not including the includeSubDomains directive in your Strict-Transport-Security header can leave subdomains vulnerable to downgrade attacks. Make sure your HSTS policy is comprehensive and includes max-age and includeSubDomains.
- If you set X-Frame-Options to ALLOW-FROM but forget to specify a valid URL, the header will be ignored by most browsers, leaving your site open to clickjacking. Always test that your iframe embedding settings are correct.
How to Fix Missing Security Headers?
- Use tools like SecurityHeaders.com or Mozilla Observatory to scan your site and get a report of the missing security headers.
- Once you’ve identified which headers are missing, configure your web server (Apache, Nginx, etc.) to add them.
- For example, if Content-Security-Policy (CSP) is missing, you can add it using these configurations:
Apache: Header set Content-Security-Policy "default-src 'self'; script-src 'self';"
Nginx: add_header Content-Security-Policy "default-src 'self'; script-src 'self'" always;
- After configuring the missing headers, use the same testing tools to confirm that they are now being sent with your site’s responses.
- Check for correct implementation by reviewing your site’s headers in your browser’s developer tools (under the “Network” tab) or using header-checking services.
- Regularly scan your site to ensure headers are still correctly configured, as server or software updates could alter the configuration.
What Are the Effects of Missing Security Headers?
- Cross-Site Scripting (XSS): Without a Content-Security-Policy (CSP) header, malicious scripts can be injected into your site and executed in users’ browsers, leading to data theft, session hijacking, or malware installation.
- If the X-Frame-Options header is missing, your site can be embedded in malicious iframes, tricking users into clicking on hidden buttons or links that perform unintended actions.
- Man-in-the-Middle (MITM) Attacks: Without Strict-Transport-Security (HSTS), attackers can intercept and manipulate communication between the user and your website.
- Without a Referrer-Policy, sensitive data such as session tokens or personal information are leaked to third-party sites when users navigate away from your website.
- A website missing security headers appear untrustworthy to users and security scanners, leading to a loss of confidence and decreased traffic.
- Missing headers hinder compliance with regulations like GDPR or PCI DSS, which require strict data protection and secure browsing practices.
Faqs on website security headers
What is HTTP security header?
An HTTP security header is a part of the HTTP response sent from a web server to a browser, instructing the browser on how to behave in terms of security.
What are the 4 types of HTTP headers?
- Request headers: Sent by the browser to the server, containing information like user-agent and accepted languages.
- Response headers: Sent by the server to the browser, including security headers like Content-Security-Policy and X-Frame-Options.
- Entity headers: Provide metadata about the content, such as Content-Type and Content-Length.
- General headers: Provide general information about the communication, such as Cache-Control and Date.
Where do I find the header of a website?
To find the headers of a website, you can use your browser’s developer tools. In Chrome, open Developer Tools (press F12 or right-click > Inspect), go to the Network tab, and refresh the page. Click on the website’s request (usually the first one) to view its headers under the Headers section.
How do I add an HTTP header in Chrome browser?
you cannot directly modify headers in the browser itself. However, you can use Chrome extensions like ModHeader to simulate adding custom HTTP headers for testing purposes. For permanent changes, you need to configure headers on your web server.
How to add security headers in web config?
- Open your web.config file located in the root directory of your ASP.NET application.
- Inside the <configuration> element, add the <system.webServer> section (if it’s not already there). Within this section, you’ll place your security headers inside the <httpProtocol> element.
Useful links
- Test your website’s HTTP security headers and get recommendations on improving security.
- documentation on HTTP headers, CSP, STS and more.
- Microsoft documentation on how to add custom headers in web.config for IIS.
- Content-Security-Policy Generator
- tool for scanning your website for security headers and getting a detailed security report.

