| name | missing-security-headers-anti-pattern |
| description | Security anti-pattern for missing security headers (CWE-16). Use when generating or reviewing web application code, server configuration, or HTTP response handling. Detects missing CSP, HSTS, X-Frame-Options, and other protective headers. |
Missing Security Headers Anti-Pattern
Severity: Medium
Summary
HTTP security headers defend against XSS, clickjacking, and man-in-the-middle attacks at the browser level. Applications failing to send these headers rely on insecure browser defaults, missing a powerful declarative security layer.
The Anti-Pattern
The anti-pattern is omitting security headers from HTTP responses. Browsers default to permissive policies; servers must instruct stricter controls.
BAD Code Example
from flask import Flask, make_response
app = Flask(__name__)
@app.route("/")
def index():
response = make_response("<h1>Welcome to the site!</h1>")
return response
GOOD Code Example
from flask import Flask, make_response
app = Flask(__name__)
@app.after_request
def add_security_headers(response):
response.headers['Content-Security-Policy'] = "default-src 'self'"
response.headers['X-Frame-Options'] = 'DENY'
response.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'
response.headers['X-Content-Type-Options'] = 'nosniff'
response.headers['Referrer-Policy'] = 'strict-origin-when-cross-origin'
return response
@app.route("/")
def index_secure():
return make_response("<h1>Welcome to the secure site!</h1>")
Detection
- Use browser developer tools: Open the "Network" tab, inspect a request to your site, and look at the "Response Headers" section. Check for the presence of the headers listed below.
- Use an online scanner: Tools like SecurityHeaders.com can quickly scan a public website and report on its missing headers.
- Review framework configurations: Check your web server or framework's configuration files to see if security headers are being set globally. Many frameworks have dedicated middleware (like
Helmet for Express.js) to handle this.
Prevention
Implement a middleware or a global response filter in your application that adds the following headers to all outgoing responses.
Related Security Patterns & Anti-Patterns
References