CORS (Cross-Origin Resource Sharing) Vulnerability Explained
CORS is a security mechanism that allows web applications to make requests to different domains while controlling which domains are permitted. However, misconfigurations in CORS policies can lead to security vulnerabilities, enabling attackers to perform malicious actions.
CORS Vulnerability Types
Overly Permissive CORS Policies
If a server allows any origin (Access-Control-Allow-Origin: *), attackers can exploit this to steal sensitive data.
If the server reflects the Origin header without validation, attackers can bypass restrictions.
Improper Handling of Credentials
If Access-Control-Allow-Credentials: true is set but Access-Control-Allow-Origin is not properly restricted, attackers can steal cookies or session tokens.
Wildcard with Credentials
A server should never allow Access-Control-Allow-Origin: * with Access-Control-Allow-Credentials: true because it allows any site to make authenticated requests.
Attack Scenario: Exploiting Misconfigured CORS
Scenario: Stealing User Data via Malicious Website
Vulnerable Server Configuration
The target website (https://vulnerable-bank.com) has a misconfigured CORS policy:
Ensure the server checks the Origin header against a whitelist of allowed domains.
Use Vary: Origin Header
Prevents caching of CORS responses, ensuring proper validation per request.
Restrict Sensitive Endpoints
Avoid exposing sensitive APIs to CORS unless absolutely necessary.
Use CSRF Tokens
Even with proper CORS, implement CSRF protection to prevent unauthorized actions.
Conclusion
CORS vulnerabilities arise from misconfigurations that allow unauthorized cross-origin requests. Attackers can exploit these weaknesses to steal sensitive data or perform actions on behalf of victims. Properly configuring CORS policies and validating origins can prevent such attacks.