Overview
This vulnerability occurs when a web application implements Cross-Origin Resource Sharing (CORS) policies that are too permissive, particularly by setting theAccess-Control-Allow-Origin header to overly broad values like the wildcard (*) or by dynamically reflecting the requesting Origin header without proper validation. This allows malicious websites, visited by an authenticated user, to make requests to the vulnerable application and read the responses, potentially stealing sensitive user data or performing unauthorized actions via the user’s session. 🌐🔓➡️😈
Business Impact
Permissive CORS policies undermine the browser’s Same-Origin Policy, leading to:- Sensitive Data Exposure: Malicious websites can make authenticated requests (using the victim’s cookies) to the vulnerable application’s API endpoints and read sensitive data returned in the response (e.g., user profile details, messages, financial information).
- Unauthorized Actions: While the primary risk is data reading, permissive CORS can sometimes facilitate actions if combined with other vulnerabilities or if the endpoint relies solely on cookies for authorization and performs state changes via GET requests (though this is less common).
- Trust Exploitation: It abuses the trust relationship between the user and the vulnerable application.
Reference Details
CWE ID: CWE-942 (Related: CWE-346 Origin Validation Error, CWE-264 Permissions/Privileges)
OWASP Top 10 (2021): A05:2021 - Security Misconfiguration
Severity: High (especially if sensitive data is exposed)
Framework-Specific Analysis and Remediation
CORS is typically configured either at the web server/proxy level (Nginx, Apache) or within the application framework using middleware or filters. The vulnerability is allowing origins that should not be trusted. Key Remediation Principles:- Avoid Wildcard (
*) if Credentials Allowed: Never setAccess-Control-Allow-Origin: *ifAccess-Control-Allow-Credentials: trueis also set. Browsers generally block this combination anyway, but relying on it is insecure. - Use Strict Allow-lists: Maintain an explicit list of trusted origin domains that are permitted to make cross-origin requests.
- Validate Dynamic Origins: If dynamically reflecting the
Originheader, strictly validate it against the allow-list. Do not simply echo back any origin. - Least Privilege: Only allow the specific HTTP methods (
Access-Control-Allow-Methods) and headers (Access-Control-Allow-Headers) required by the trusted origins. - Vary Header: Consider using the
Vary: Originheader to prevent caching issues when serving differentAccess-Control-Allow-Originheaders based on the request origin.
- Python
- Java
- .NET(C#)
- PHP
- Node.js
- Ruby
Framework Context
Using libraries likedjango-cors-headers (Django) or Flask-CORS (Flask) with insecure configurations.Vulnerable Scenario 1: Django CORS_ALLOW_ALL_ORIGINS = True
Vulnerable Scenario 2: Flask-CORS Allowing Any Origin
Mitigation and Best Practices
- Django: Set
CORS_ALLOW_ALL_ORIGINS = False. DefineCORS_ALLOWED_ORIGINS(list of specific domains likehttps://trusted.example.com) orCORS_ALLOWED_ORIGIN_REGEXES. SetCORS_ALLOW_CREDENTIALS = Trueonly if necessary and origins are strictly controlled. - Flask: Replace
"origins": "*"with a specific list:"origins": ["https://trusted.example.com", "https://another.trusted.com"]. Only setsupports_credentials=Trueif absolutely needed and origins are restricted.
Secure Code Example
Testing Strategy
Usecurl or browser developer tools to send requests to your API endpoints from a different origin (e.g., set the Origin header manually in curl).- Check
Access-Control-Allow-Origin. Is it*? Is it reflecting an untrusted origin? - Check
Access-Control-Allow-Credentials. Is ittrue? - If both
Origin: *andCredentials: trueare returned (or if an untrusted origin is reflected withCredentials: true), the configuration is vulnerable. Test by making a JavaScriptfetchrequest from a dummy HTML page served on a different domain to see if you can read authenticated data.

