X-XSS-Protection
What is X-XSS-Protection?
The X-XSS-Protection header is an HTTP security header that enables built-in cross-site scripting (XSS) protection mechanisms in modern web browsers. This header was primarily supported by older versions of Internet Explorer, Chrome, and Safari to provide basic XSS filtering capabilities.
When enabled, this header instructs the browser to detect and block reflected XSS attacks by sanitizing or preventing the rendering of malicious scripts that appear to be injected through user input.
Header Syntax
The X-XSS-Protection header supports several configuration options:
X-XSS-Protection: 0
X-XSS-Protection: 1
X-XSS-Protection: 1; mode=block
- 0: Disables XSS filtering
- 1: Enables XSS filtering (default behavior)
- 1; mode=block: Enables XSS filtering and blocks the entire page if an attack is detected
Browser Support and Current Status
While X-XSS-Protection was widely supported in older browsers:
- Modern browsers (Chrome 78+, Firefox 52+, Edge 79+) have removed support for this header
- Content Security Policy (CSP) has become the recommended approach for XSS protection
- Many security experts now recommend explicitly disabling this header (
X-XSS-Protection: 0) to prevent potential security vulnerabilities
Security Considerations
Potential vulnerabilities with X-XSS-Protection:
- False positives/negatives: The browser's XSS filter could incorrectly identify legitimate content as malicious or fail to detect actual attacks
- Filter bypass techniques: Attackers could craft payloads specifically designed to bypass the browser's XSS filter
- Information leakage: The filter could potentially be used as an oracle to test for XSS vulnerabilities
- Inconsistent behavior: Different browsers implemented the XSS filter differently, leading to inconsistent protection
Best Practices
- Disable X-XSS-Protection in favor of Content Security Policy:
X-XSS-Protection: 0 Content-Security-Policy: script-src 'self'; object-src 'none' - Implement Content Security Policy (CSP) as the primary XSS protection mechanism
- Use other security headers in combination:
- Follow secure coding practices to prevent XSS vulnerabilities at the source:
- Proper output encoding
- Input validation
- Context-aware escaping
Example Implementation
HTTP Response Header:
X-XSS-Protection: 0
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://trusted.cdn.com
Web Server Configuration Examples:
Apache (.htaccess):
Header set X-XSS-Protection "0"
Header set Content-Security-Policy "default-src 'self'; script-src 'self'"
Nginx:
add_header X-XSS-Protection "0";
add_header Content-Security-Policy "default-src 'self'; script-src 'self'";
Express.js (Node.js):
app.use((req, res, next) => {
res.setHeader('X-XSS-Protection', '0');
res.setHeader('Content-Security-Policy', "default-src 'self'; script-src 'self'");
next();
});
Related Security Concepts
- Cross-Site Scripting (XSS) - The vulnerability this header protects against
- Content Security Policy (CSP) - Modern alternative to X-XSS-Protection
- HTTP Security Headers - Overview of important security headers
- Reflected XSS - Type of XSS attack
- Stored XSS - Type of XSS attack
- Same-Origin Policy (SOP) - Fundamental web security model
- Cross-Origin Resource Sharing (CORS) - Cross-origin resource sharing mechanism
X-Frame-Options
A security header that prevents clickjacking attacks by controlling how a webpage can be embedded in frames.
X.509 Certificate
X.509 is a standard format for public key certificates used in SSL/TLS, code signing, and digital signatures to verify identity and establish secure communications.
