| name | open-cors-anti-pattern |
| description | Security anti-pattern for open Cross-Origin Resource Sharing (CORS) policies (CWE-942). Use when generating or reviewing server configurations, API backends, or any code that sets CORS headers. Detects overly permissive Access-Control-Allow-Origin headers, including wildcard, null origin, and reflected origin. |
Open CORS Policy Anti-Pattern
Severity: Medium
Summary
Misconfigured CORS policies allow any website to make authenticated requests on behalf of users. Servers responding with Access-Control-Allow-Origin: * or reflecting client Origin headers enable data theft and unauthorized actions.
The Anti-Pattern
The anti-pattern is overly permissive Access-Control-Allow-Origin headers: wildcard (*) or reflecting client Origin values.
BAD Code Example
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.after_request
def add_cors_headers(response):
origin = request.headers.get('Origin')
if origin:
response.headers['Access-Control-Allow-Origin'] = origin
return response
@app.route("/api/user/profile")
def get_profile():
user = get_user_from_session()
return jsonify(user.to_dict())
GOOD Code Example
from flask import Flask, request, jsonify
app = Flask(__name__)
ALLOWED_ORIGINS = {
"https://www.yourapp.com",
"https://yourapp.com",
"https://staging.yourapp.com"
}
@app.after_request
def add_secure_cors_headers(response):
origin = request.headers.get('Origin')
if origin in ALLOWED_ORIGINS:
response.headers['Access-Control-Allow-Origin'] = origin
response.headers['Access-Control-Allow-Credentials'] = 'true'
response.headers['Vary'] = 'Origin'
return response
@app.route("/api/user/profile")
def get_profile_secure():
user = get_user_from_session()
return jsonify(user.to_dict())
Detection
- Use browser developer tools: Open the "Network" tab, make a cross-origin request to your API, and inspect the response headers. Look for
Access-Control-Allow-Origin. Is it *? Does it match the Origin of your request even if that origin is untrusted?
- Use
curl: Make a request and set a custom Origin header to see if the server reflects it:
curl -H "Origin: https://evil.com" -I https://yourapp.com/api/some-endpoint
Check if the response contains Access-Control-Allow-Origin: https://evil.com.
- Review CORS configuration: Check your application's code or framework configuration for how CORS headers are being set. Look for wildcards or reflected origins.
Prevention
Related Security Patterns & Anti-Patterns
References