What is X-XSS Protection?

The HTTP X-XSS-Protection response header is a security feature supported by Internet Explorer, Chrome, and Safari that helps prevent pages from loading when a reflected cross-site scripting (XSS) attack is detected.

This header activates the built-in XSS filter in most modern browsers. It is typically enabled by default, but this header ensures the filter is active for a specific website even if a user has disabled it. The header is supported in IE 8+ and in Chrome (since Chrome 4, although support for the header may vary in older versions).

Header Values and Behavior:

  • X-XSS-Protection: 0 – Disables the XSS filter.
  • X-XSS-Protection: 1 – Enables the filter; scripts coming from the request are filtered, but the page still renders.
  • X-XSS-Protection: 1; mode=block – Enables the filter and, when an attack is detected, blocks the page from being rendered entirely.

Examples of Implementation:

Django:

SECURE_BROWSER_XSS_FILTER = True

Nginx:

add_header X-XSS-Protection "1; mode=block";

Apache:

Header always set X-XSS-Protection "1; mode=block"

Important Considerations:

While the XSS filter can provide a layer of defense, it is not foolproof. There are known bypasses, and the filter may have limitations in certain contexts. Relying solely on the X-XSS-Protection header is not sufficient for comprehensive XSS prevention; proper input validation and output encoding are essential.

References and Further Reading: