Categories
Application

Guarding Your Web: The Role of Security Headers

Introduction

Internet communications are predominantly carried out using the HTTP protocol. When accessing a website, the browser sends an HTTP request to retrieve a page (e.g. www.artresilia.pt). This request is accompanied by additional information, such as the type of browser used, the content being sent, and the type of content expected in response, among other details. The server’s response, in addition to the page data itself, includes additional information that instructs clients (browsers) on how to behave. These instructions can specify, for instance, the type of data transmitted, the date and time, any cookies that should be stored, and more. All of this additional information is transmitted through HTTP headers.


HTTP headers are fields of information that form part of an HTTP request or response. They are used to transmit additional data between the client and server during a data exchange.


This article will explore security headers, emphasising their importance, the different types of headers that exist, and the impact of the OWASP Secure Headers Project. A practical example of malicious code injection will be presented, comparing how a browser behaves with and without the relevant security header associated with the demonstrated vulnerability. Additionally, we will demonstrate how to test web applications by bypassing the protection offered by security headers to identify vulnerabilities more effectively. Lastly, we will discuss the importance of a layered approach to application security, characterising security headers as just an extra layer of defence, similar to a firewall, rather than a standalone, definitive solution.

What are security headers?

Security headers are configurations implemented in the HTTP headers of a web application to enhance its overall security. These headers help protect against common attacks such as XSS (Cross-Site Scripting), clickjacking and MIME sniffing, while also providing clear directives on how the browser should handle specific content. In practical terms, security headers act as rules sent by the server to the browser, defining boundaries and behaviours for certain resources or functionalities. These instructions can, for example, restrict the execution of scripts from untrusted sources, enforce the exclusive use of HTTPS connections to avoid insecure content, or prevent the rendering of iframes altogether.

Without security headers, the browser operates without these additional safeguards, which can expose vulnerabilities that can be exploited by attackers to carry out attacks with varying impacts. When configured correctly, which aims to be as restrictive as possible without disrupting the application’s functionality, security headers make it possible to significantly increase the security of the web environment. Furthermore, their low implementation cost makes them highly appealing—they are generally simple and quick to configure, providing an excellent cost-benefit ratio for reducing risks effectively.

Types of Security Headers

There are various types of security headers, each designed to protect web applications against specific types of attacks. Below, we highlight the most important ones:

  • Content-Security-Policy (CSP): This header is fundamental for preventing attacks such as XSS (Cross-Site Scripting) and data injection. It allows developers to define which content sources are considered trustworthy, such as scripts, styles, images and other resources. For example, the CSP header can instruct the browser to block any inline scripts or restrict the execution of external resources to those from approved domains.
  • Strict-Transport-Security (HSTS): HSTS enforces the use of HTTPS, protecting against protocol downgrade attacks and significantly reducing the risk of Man-In-The-Middle attacks. It ensures that browsers automatically redirect HTTP requests to HTTPS, enhancing the overall security of the connection.
  • X-Frame-Options: This header prevents the page from being embedded in iframes from unauthorised domains, protecting against clickjacking attacks. X-Content-Type-Options: The X-Content-Type-Options header prevents browsers from attempting to guess the content type of resources, preventing attacks known as MIME sniffing.
  • Content-Type: Although not considered a security header, the Content-Type header specifies the type of data the server is sending, ensuring that content is interpreted correctly. When configured properly, this header can limit the potential damage caused by improper content handling.
  • Permissions-Policy: This header controls access to sensitive browser features such as the camera, microphone or geolocation, reducing the attack surface of the application.

Some security headers used in the past have been found to be inefficient or even counterproductive and have been replaced by more robust alternatives. It is therefore important to stay up to date with the latest recommendations and standards. The OWASP Secure Headers Project is an excellent resource for monitoring updates and best practices.

Practical Impact: Website With and Without Security Headers

To demonstrate the importance of security headers, we carried out a practical test based on a Stored XSS (Cross-Site Scripting) exercise available on the educational platform of the company behind Burp Suite. The exercise can be accessed at the following URL: 

In this exercise, the vulnerability is present in a comment field. To simulate a malicious attacks, we inject the following payload: <script>alert(“XSS Vulnerability Triggered”)</script>. This payload mimics a malicious script designed to execute automatically whenever the page is loaded, demonstrating the exposure of users to this vulnerability. The video below illustrates the payload being triggered automatically every time the page is refreshed. 

Adding a Security Header

Using Burp Suite, we simulate the addition of a security header to the server’s response (or any reverse proxy, firewall, or other technology that can add it): 

Content-Security-Policy: script-src 'self';

Guarding Your Web: The Role of Security Headers

This header restricts the browser to only execute scripts originating from the website itself, effectively blocking any inline or external scripts not explicitly allowed.


Result: After adding the Content-Security-Policy header, the browser successfully blocked the execution of the malicious script, preventing the alert from being displayed. This demonstrates how security headers can act as a critical safeguard against vulnerabilities like XSS, protecting users from malicious payloads.


 

Masking vs Mitigating

Although the Content-Security-Policy has prevented the payload from being executed, it is important to note that the vulnerability is still present in the application. The header merely masks the issue, protecting modern browsers and their users from the immediate effects. However, the malicious code remains stored on the server and accessible. This reinforces the idea that security headers do not fix vulnerabilities in the application itself but add an extra layer of protection, reducing the impact and making it harder to exploit vulnerabilities.

Bypasses exist, and a good example can be found in this website: https://cspbypass.com/. This website is specialised in bypassing the Content-Security-Policy (CSP), and is a good example of why we can’t only trust in security headers.

Relying solely on security headers can also create a false sense of security if the underlying issues are not resolved. For example, in the case of Stored XSS, while the browser blocked the script due to the CSP header, the vulnerability persists on the input and the server still sends the payload every time a request is made. This poses risks in scenarios where the header may not be applied consistently or supported, such as older browsers, proxy configurations, or non-browser-based tools interacting with the application. Additionally, attackers often adapt their methods to bypass misconfigured or permissive headers, further diminishing their effectiveness.

Security headers are an essential part of a defence-in-depth strategy, but they must complement secure development practices. To really mitigate vulnerabilities, it is crucial to address the root cause within the application’s source code. This involves implementing secure coding principles, such as input validation, output encoding, and proper handling of user-provided data. By fixing the vulnerability directly in the source, you ensure comprehensive protection that goes beyond the limitations of browser-dependent defences.

Testing Without Security Headers: Configurations in Burp Suite for Reliable Pentests

One of the main objectives of a Pentest is to comprehensively identify vulnerabilities, evaluating the behaviour of the application in different scenarios. To achieve the best results within the time constraints, it is essential to test the application without additional protections, such as firewalls, EDRs and, in the specific case of this article, security headers.

 

While testing in production environments with all the protections enabled may be valuable to simulate real-world conditions, these security layers can interfere with or mask the identification of application-level flaws. This is particularly relevant when the goal of the pentest is to assess the application’s vulnerabilities rather than the effectiveness of the infrastructure protecting it. By temporarily removing security headers during the pentest, it becomes possible to uncover weakness that may be concealed by this additional layer of protection. This approach ensures that vulnerabilities are identified and documented, allowing for more effective remediation at the application level.

Configuration in Burp Suite to Remove Security Headers

Burp Suite offers powerful tools for modifying HTTP requests and responses, allowing you to test specific scenarios. While manipulating response headers is rarely applied in standard testing, as it mimics an effective Man-In-The-Middle attack, it can be useful when testing reflected payloads introduced by the auditor. Removing  Security Headers from responses helps ensure that the browser does not block certain behaviours, allowing vulnerabilities such as reflected XSS to be accurately identified.

The following image demonstrates how to configure Burp Suite to remove a specific security header from server responses:

Guarding Your Web The Role of Security Headers 1

 

To complete the setup, you need to create a rule for each security header you wish to remove. Below are the most common regex patterns used for this purpose:

  • (?i)^content-security-policy\s*:\s*(.+)?$
  • (?i)^strict-transport-security\s*:\s*(.+)?$
  • (?i)^x-content-type-options\s*:\s*(.+)?$
  • (?i)^x-frame-options\s*:\s*(.+)?$
  • (?i)^x-xss-protection\s*:\s*(.+)?$
  • (?i)^referrer-policy\s*:\s*(.+)?$
  • (?i)^permissions-policy\s*:\s*(.+)?$
  • (?i)^access-control-allow-origin\s*:\s*(.+)?$
  • (?i)^x-permitted-cross-domain-policies\s*:\s*(.+)?$

If the application includes other security headers, you can create additional regex patterns to match and remove them accordingly.

By removing these headers during a security test, the browser will no longer obstruct potential vulnerabilities, resulting in a more precise and reliable assessment of the application’s behaviour. This configuration is an essential step in thoroughly evaluating reflected payloads without interference from browser-side protections.

Final Considerations

This article is designed to demonstrate the power of security headers in enhancing the overall security of web applications. By limiting the impact of existing vulnerabilities, these headers protect both the application and its users with minimal effort. The implementation of security headers is a relatively quick and straightforward task, and it can play a key role in safeguarding against attacks, often preventing the exploitation of vulnerabilities.

However, it is important to stress that this article does not advocate that application security should be based solely on security headers or any other single technology. True vulnerability mitigation must always occur at the source of the problem: in the sanitation and validation of inputs, in the proper use of encoding and in the implementation of good development practices. Security headers should be seen as an extra layer of security, easy and quick to implement, and therefore a must-have with an extremely low cost-benefit ratio. They do not replace fundamental security practices, but offer additional protection that can make all the difference in protecting against attacks.

Links


Author : Sylvain Júlio