Misconfigured CORS
- CWE 942
Cross-Origin Resource Sharing (CORS) is a browser mechanism that allows controlled access to resources on a server from scripts running on other origins (domains). Misconfigured CORS occurs when a web application improperly configures its CORS policy, allowing unintended or malicious origins to access sensitive resources. Attackers can exploit such misconfigurations to bypass the Same-Origin Policy (SOP) and perform unauthorized operations, steal sensitive data, or execute malicious scripts.
Common misconfiguration patterns:
- Using
Access-Control-Allow-Origin: *on endpoints that return sensitive data or support authentication. - Reflecting the
Originheader from requests directly inAccess-Control-Allow-Originwithout validation. - Allowing credentials (
Access-Control-Allow-Credentials: true) for untrusted or all origins. - Missing proper validation of allowed HTTP methods, headers, or exposed response headers.
Impacts of misconfigured CORS:
- Data Exfiltration: Attacker-controlled websites can read sensitive responses from vulnerable endpoints if credentials (cookies, tokens) are sent automatically.
- Account Takeover: If sensitive session tokens are exposed through XHR/fetch requests, attackers may perform actions on behalf of authenticated users.
- Cross-Site Request Forgery Enhancement: Misconfigured CORS can bypass SOP protections, enabling attackers to combine CSRF with data exfiltration.
- Sensitive Function Exposure: APIs or endpoints that should be internal-only may be accessible to external origins.
Detection indicators:
- Inspect response headers for improper
Access-Control-Allow-Originvalues, especially*in combination withAccess-Control-Allow-Credentials: true. - Use automated scanners to test CORS endpoints with malicious origins and verify if responses are accessible.
- Review server-side CORS implementation and frameworks for default behaviors that may override explicit policies.
- Monitor for anomalous cross-origin requests or unauthorized data access patterns.
Remediation
Properly configuring CORS requires validating origins, limiting credentials, and exposing only necessary resources to trusted domains.
Restrict Allowed Origins
Explicitly define a whitelist of trusted domains inAccess-Control-Allow-Origin. Never echo the incomingOriginheader directly without validation.Limit Credentials Exposure
Only allowAccess-Control-Allow-Credentials: truefor trusted origins. Avoid combining wildcard*with credentialed requests.Validate HTTP Methods
LimitAccess-Control-Allow-Methodsto only the required methods (GET, POST, etc.) and avoid allowing unsafe or unnecessary verbs.Restrict Headers
UseAccess-Control-Allow-Headersto permit only required headers. Do not allow all headers by default unless necessary.Expose Only Necessary Response Headers
ControlAccess-Control-Expose-Headersto avoid revealing sensitive headers (e.g., session tokens, internal identifiers) to untrusted origins.Environment-Specific Policies
Apply stricter CORS policies in production environments. Development environments may require broader access, but ensure production is locked down.Test and Monitor
Include automated tests for CORS misconfigurations and monitor logs for suspicious cross-origin requests.Framework-Specific Guidance
Use framework-provided CORS middleware and ensure proper configuration rather than implementing custom CORS logic from scratch.Principle of Least Privilege
Only expose endpoints to external origins when absolutely necessary. Treat all external requests as untrusted.Defense-in-Depth
Combine CORS configuration with authentication, authorization, and input validation. CORS misconfiguration should not be the sole line of defense.