Skip to content
CORS Write-up Beginner

CORS in action

An API endpoint reflected any supplied Origin in its CORS response while allowing credentials, so a malicious site could read authenticated users' data. The author demonstrated this with a proof-of-concept page that fetched the endpoint with credentials and displayed the response.

geekboy.ninja

Published on geekboy.ninja

few days before noticed a blog post for exploiting Facebook chat and reading all the chats of users so that made me to interested to know about the issues, and basically it was misconfigured CORS configuration where null origin is allowed with credentials true, it was not something heard for the 1st time, @albinowax…

The author, on geekboy.ninja

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

This write-up is about a misconfigured CORS policy found on a target the researcher was authorised to test. The report names the asset as www.geekboy.ninja, but the vulnerable endpoint shown in the commands is written generically as https://my.target.com/api/web/user, an endpoint that returns user data in the response. The report does not describe the product or feature behind that endpoint beyond the fact that it returns all users' data.

The starting point was a blog post about exploiting Facebook chat to read users' messages. That issue was a misconfigured CORS configuration in which the null origin was allowed together with credentials. The researcher used that write-up, and the PortSwigger material by @albinowax on exploiting CORS misconfigurations, as background and then went looking for the same class of bug on targets they were allowed to test.

Setup

The report does not describe any accounts, roles or specific tool versions. The tools mentioned are:

  • curl, used to send a request with a chosen Origin header and inspect the response headers.
  • Burp Suite Professional, whose Active Scan may find this class of issue automatically.
  • A browser, to host and run the HTML proof of concept.

The precondition for exploitation is that the target reflects an arbitrary Origin back in Access-Control-Allow-Origin while also sending Access-Control-Allow-Credentials: true.

Finding it

Before testing, the researcher lays out how to read a CORS response from an attacker's point of view. There are three cases.

Poorly implemented, best case for the attacker:

HTTP
Access-Control-Allow-Origin: https://attacker.com
Access-Control-Allow-Credentials: true

Poorly implemented, exploitable:

HTTP
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true

Bad implementation but not exploitable:

HTTP
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

or just:

HTTP
Access-Control-Allow-Origin: *

The last case is still poor from a development point of view, but the rules of CORS mean that when Access-Control-Allow-Origin is *, the browser does not give the attacker the benefit of Access-Control-Allow-Credentials: true, so there is no access to the victim's cookies.

The researcher also lists two conditions under which the misconfiguration cannot be exploited even when it is present:

  • A custom header in the request that is used to authenticate the user.
  • A unique value, authentication value or key in the request URI.

The Facebook Messenger issue is given as the example of the null origin case. The researcher's own finding, made two days before writing, is the example of the first case: any arbitrary Origin is allowed and the same Origin is reflected back into Access-Control-Allow-Origin with credentials set to true.

The recommended way to check is with curl. Send a request with a chosen Origin header and look at whether that origin comes back in the response:

Bash
curl https://test.victim.com -H "Origin: https://geekboy.ninja" -I

The report includes a screenshot of the curl check here without describing it.

Burp Professional users can also let Burp Active Scan look for this. In this specific case, however, the root URL was not vulnerable when checked manually:

Bash
curl https://my.target.com -H "Origin: https://geekboy.ninja" -I

The origin was not reflected there. The behaviour changed when the request went to the specific endpoint that returns all users' data:

Bash
curl https://my.target.com/api/web/user -H "Origin: https://geekboy.ninja" -I

On that endpoint the host was reflected back with credentials set to true, which the researcher judged sufficient to make the attack work and to steal the data.

Exploitation

Step 1: Confirm the reflection on the data endpoint

The curl request above is the confirmation step. The important parts are the -H "Origin: https://geekboy.ninja" header, which supplies the attacker-controlled origin, and -I, which asks for headers only so the response headers can be read. A positive result is the same origin appearing in Access-Control-Allow-Origin together with Access-Control-Allow-Credentials: true.

Step 2: Serve the proof of concept

The researcher used the following HTML page as the proof of concept:

HTML
<!DOCTYPE html>
<html>
<body>
<center>
<h2>CORS POC Exploit</h2>
<h3>Extract SID</h3>

<div id="demo">
<button type="button" onclick="cors()">Exploit</button>
</div>

<script>
function cors() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = alert(this.responseText);
    }
  };
  xhttp.open("GET", "https://target.com/info/", true);
  xhttp.withCredentials = true;
  xhttp.send();
}
</script>

</body>
</html>

The page presents a button that calls cors(). That function creates an XMLHttpRequest, sets withCredentials = true so the victim's cookies are sent with the cross-origin request, and issues a GET to the target endpoint. When the request completes with status 200, the response body is shown in an alert. The endpoint in the proof of concept is written as https://target.com/info/.

Step 3: Run it as the victim

When a logged-in victim loads the attacker's page and the request is made, the browser sends the victim's credentials to the target. Because the target reflects the attacker's origin and allows credentials, the browser lets the script read the response, and the data is displayed in the alert.

The report includes a screenshot at this point without describing its contents.

Root cause

The endpoint reflects an arbitrary Origin value into Access-Control-Allow-Origin and pairs it with Access-Control-Allow-Credentials: true. That combination tells the browser that any origin is allowed to read credentialed responses. The root URL did not behave this way, so the mistake is specific to the data endpoint rather than to the whole host.

The usual defences did not help here. The browser's own CORS rules only protect against the * case, where credentials are not exposed to the reading origin; they do nothing when the server echoes back the caller's origin.

Impact

An attacker who can get a logged-in victim to load a page they control can read the data returned by the affected endpoint, because the request carries the victim's cookies and the response is readable cross-origin. In this case the endpoint was one where all users' data is returned, so the exposure is the data of the users whose sessions are used to make the request. The attack requires the victim to visit the attacker's page while authenticated, and it depends on the endpoint remaining reachable without an authenticating custom header or a unique key in the URI.

Fix

The fix implied by the analysis is to stop reflecting arbitrary origins: only return an explicit allowlist of trusted origins in Access-Control-Allow-Origin, and never combine a reflected or wildcard origin with Access-Control-Allow-Credentials: true.

Variants to test

  1. Send curl https://target -H "Origin: https://attacker.example" -I to the root and to every API endpoint that returns user data. A positive result is the attacker origin echoed in Access-Control-Allow-Origin with Access-Control-Allow-Credentials: true.
  2. Try Origin: null on the same endpoints. A positive result is Access-Control-Allow-Origin: null with credentials true, the Facebook Messenger pattern.
  3. Check whether the reflection is a prefix or suffix match, for example by sending an origin such as https://attacker.example.target.com or https://target.com.attacker.example. A positive result is any of these being accepted and reflected.
  4. Look for endpoints that return sensitive data but are not covered by the same policy as the root, since the root here was not vulnerable while the data endpoint was.
  5. Confirm exploitability in a browser with withCredentials = true and a cross-origin GET, as in the proof of concept, rather than relying on headers alone. A positive result is the response body being readable by the attacker's script.
  6. Check for the two blockers before reporting: an authenticating custom header or a unique key in the URI. If either is required, the reflection alone is not enough to exploit.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

A security researcher read a public blog post about a flaw in Facebook chat that let someone read other people's messages. The flaw was a misconfigured setting called CORS, and the researcher decided to look for the same mistake on websites they were allowed to test. They found one: an endpoint on a target site that returned data about users and reflected an arbitrary origin back with credentials allowed. They confirmed it with a command-line tool, then built a small web page to prove that a real browser would give up the data. This article walks through that work from the beginning, assuming you have never heard of CORS, cookies or APIs before.

Background: the Facebook chat story

A few days before writing, the researcher noticed a blog post about exploiting Facebook chat to read all of a user's chats. What made that possible was a misconfigured CORS setup in which the null origin was allowed together with credentials. This was not the first time such a thing had been heard of. @albinowax from PortSwigger explained the general problem very well in a blog post on exploiting CORS misconfigurations. After reading the Messenger post, the researcher went to test for the same issue on some targets where they were allowed to test.

To follow the rest of this article you need three ideas.

What a cookie is. When you log in to a website, the site's server sends your browser a small piece of data called a cookie. Your browser stores it and sends it back automatically with every later request to that site. That is how the site knows it is still you and does not ask for your password on every page. A cookie is like a cloakroom ticket: you show it and the staff hand you your coat, without asking who you are again.

What an API endpoint is. A website is not only pages you look at. Behind the scenes, the page's code often asks the server for raw data, for example a list of your messages or your profile details. The address it asks is called an endpoint, and the data comes back in a machine-readable form rather than as a pretty page. Think of a restaurant: the dining room is the website you see, and the kitchen door where the waiter collects dishes is the endpoint. The endpoint in this story is one that returns data about users.

What CORS is. Browsers have a rule called the same-origin policy. A page loaded from one site is not allowed to read data from another site. That stops a malicious page from quietly fetching your bank details while you are logged in elsewhere. CORS (Cross-Origin Resource Sharing) is the controlled way a server can relax that rule: it tells the browser, in response headers, which other sites are allowed to read its data. A header is just a labelled line of text that travels with a request or a response. The server says which origin (which site) is welcome in a header called Access-Control-Allow-Origin, and whether logged-in visitors' cookies may be used in a header called Access-Control-Allow-Credentials.

The danger is when a server is too generous with those two headers. If it says "any site is welcome" and "yes, send the visitor's cookies too", then any website the visitor happens to open can read that visitor's data from the target site.

How to read a CORS response as an attacker

Before testing, the researcher lays out three cases you might see in a server's response, from an attacker's point of view.

Poorly implemented, best case for the attacker

HTTP
Access-Control-Allow-Origin: https://attacker.com
Access-Control-Allow-Credentials: true

Line by line: the first line says the server will let the site https://attacker.com read its responses. The second line says cookies may be included. If a server names the attacker's site explicitly like this, the attacker's page can read the logged-in visitor's data. This is the best case because it is unambiguous and reliable.

Poorly implemented, exploitable

HTTP
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true

Here the allowed origin is the literal word null. Browsers use null as the origin of certain pages, for example a page opened from a local file or from a sandboxed frame. If a server trusts null and also allows credentials, an attacker can arrange for their page to present itself as null and read the data. This is the pattern behind the Facebook Messenger issue mentioned above.

Bad implementation but not exploitable

HTTP
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

or just:

HTTP
Access-Control-Allow-Origin: *

The asterisk means "any site at all". The first of these two looks alarming, but the rules of CORS say that when Access-Control-Allow-Origin is *, the browser does not give the attacker the benefit of Access-Control-Allow-Credentials: true. In other words, no access to the victim's cookies. The researcher notes that this is still not good from a development point of view, but it is not exploitable in the way the earlier cases are.

When you cannot exploit it even so

The researcher lists two conditions under which the misconfiguration cannot be exploited even when it is present:

  • A custom header in the request that is used to authenticate the user.
  • A unique value, authentication value or key in the request URI.

A custom header is an extra labelled line the client must add to the request, beyond the standard ones. If the server only accepts requests that carry a secret header, an attacker's page cannot add it, because the browser will not let a page set arbitrary headers on a cross-site request without the server's permission. Similarly, if the address itself must contain a secret value (the URI is the address of the resource), the attacker cannot guess it. Either one blocks the attack even if the CORS headers look wrong.

Finding the bug

The researcher's own finding, made two days before writing, is an example of the first case: any arbitrary origin is allowed, and the same origin is reflected back into Access-Control-Allow-Origin with credentials set to true. "Reflected" means the server copies whatever value the client sent in its Origin request header straight into its response. So if you ask as https://geekboy.ninja, the server answers that https://geekboy.ninja is allowed.

The best way the researcher found to check for this is with curl, a command-line tool that fetches a URL and prints what comes back. You send a request with a chosen Origin header and look at whether that origin appears in the response:

Bash
curl https://test.victim.com -H "Origin: https://geekboy.ninja" -I

Line by line: curl is the program. https://test.victim.com is the address being fetched. -H "Origin: https://geekboy.ninja" adds a request header named Origin with the value https://geekboy.ninja, pretending to be a page from that site. -I asks for headers only, not the page body, so the response headers can be read quickly. You then look for your chosen origin in the response.

The report includes a screenshot of the curl check here without describing it.

If you use Burp Suite Professional, a commercial tool for inspecting and testing web traffic, its Active Scan feature may find this class of issue for you automatically. In this specific case, however, the root URL was not vulnerable when checked manually:

Bash
curl https://my.target.com -H "Origin: https://geekboy.ninja" -I

This is the same command as before, aimed at the front page of the target. The origin was not reflected there. The behaviour changed when the request went to the specific endpoint that returns all users' data:

Bash
curl https://my.target.com/api/web/user -H "Origin: https://geekboy.ninja" -I

Again the same command, but the address now points at /api/web/user, the endpoint that hands back user data. On that endpoint the host was reflected back with credentials set to true. The researcher judged that sufficient to make the attack work and to steal the data. The lesson is that a site can be safe on its front page and unsafe on a data endpoint, so you have to test the endpoints that matter.

Exploitation

Step 1: Confirm the reflection on the data endpoint

The curl request above is the confirmation step. The important parts are the -H "Origin: https://geekboy.ninja" header, which supplies the attacker-controlled origin, and -I, which asks for headers only so the response headers can be read. A positive result is the same origin appearing in Access-Control-Allow-Origin together with Access-Control-Allow-Credentials: true. You do this first because there is no point building a browser attack until you know the server will cooperate.

Step 2: Serve the proof of concept

A proof of concept is a small, harmless demonstration that the problem is real. The researcher used the following HTML page:

HTML
<!DOCTYPE html>
<html>
<body>
<center>
<h2>CORS POC Exploit</h2>
<h3>Extract SID</h3>

<div id="demo">
<button type="button" onclick="cors()">Exploit</button>
</div>

<script>
function cors() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = alert(this.responseText);
    }
  };
  xhttp.open("GET", "https://target.com/info/", true);
  xhttp.withCredentials = true;
  xhttp.send();
}
</script>

</body>
</html>

Line by line: <!DOCTYPE html> tells the browser this is an HTML page. <html>, <body>, <center> and the closing tags are the page structure. The two headings, CORS POC Exploit and Extract SID, are just labels on the page. The <div id="demo"> block holds a button labelled Exploit; onclick="cors()" means that clicking it runs the function named cors. Inside <script>, function cors() { starts that function. var xhttp = new XMLHttpRequest(); creates the browser object used to make a request in the background without reloading the page. xhttp.onreadystatechange = function() { sets up code that runs whenever the request's state changes. if (this.readyState == 4 && this.status == 200) checks that the request has finished and the server answered successfully. document.getElementById("demo").innerHTML = alert(this.responseText); shows the server's answer in a pop-up box. xhttp.open("GET", "https://target.com/info/", true); prepares a GET request (the ordinary kind used to fetch something) to the target address. xhttp.withCredentials = true; is the crucial line: it tells the browser to include the visitor's cookies with this cross-site request. xhttp.send(); actually sends it. The endpoint in the proof of concept is written as https://target.com/info/.

Step 3: Run it as the victim

When a logged-in victim loads the attacker's page and the request is made, the browser sends the victim's credentials to the target. Because the target reflects the attacker's origin and allows credentials, the browser lets the script read the response, and the data is displayed in the alert. In everyday terms: the victim walks into the attacker's shop, the attacker's page quietly phones the victim's bank using the cloakroom ticket the victim is carrying, and the bank, having been told to trust the caller, reads out the account details.

The report includes a screenshot at this point without describing its contents.

Root cause

The endpoint reflects an arbitrary Origin value into Access-Control-Allow-Origin and pairs it with Access-Control-Allow-Credentials: true. That combination tells the browser that any origin is allowed to read credentialed responses. The root URL did not behave this way, so the mistake is specific to the data endpoint rather than to the whole host. The report does not show the server-side code, so the exact implementation that produces the reflection is not stated.

The usual defences did not help here. The browser's own CORS rules only protect against the * case, where credentials are not exposed to the reading origin; they do nothing when the server echoes back the caller's origin. The researcher lists two conditions that would block exploitation, an authenticating custom header or a unique key in the URI; the report does not say whether either was present here.

Impact

An attacker who can get a logged-in victim to load a page they control can read the data returned by the affected endpoint, because the request carries the victim's cookies and the response is readable cross-origin. In this case the endpoint was one where all users' data is returned, so the exposure is the data of the users whose sessions are used to make the request. The attack requires the victim to visit the attacker's page while authenticated, and it depends on the endpoint remaining reachable without an authenticating custom header or a unique key in the URI.

Fix

The fix implied by the analysis is to stop reflecting arbitrary origins: only return an explicit allowlist of trusted origins in Access-Control-Allow-Origin, and never combine a reflected or wildcard origin with Access-Control-Allow-Credentials: true.

Variants to test

  1. Send curl https://target -H "Origin: https://attacker.example" -I to the root and to every API endpoint that returns user data. A positive result is the attacker origin echoed in Access-Control-Allow-Origin with Access-Control-Allow-Credentials: true.
  2. Try Origin: null on the same endpoints. A positive result is Access-Control-Allow-Origin: null with credentials true, the Facebook Messenger pattern.
  3. Check whether the reflection is a prefix or suffix match, for example by sending an origin such as https://attacker.example.target.com or https://target.com.attacker.example. A positive result is any of these being accepted and reflected.
  4. Look for endpoints that return sensitive data but are not covered by the same policy as the root, since the root here was not vulnerable while the data endpoint was.
  5. Confirm exploitability in a browser with withCredentials = true and a cross-origin GET, as in the proof of concept, rather than relying on headers alone. A positive result is the response body being readable by the attacker's script.
  6. Check for the two blockers before reporting: an authenticating custom header or a unique key in the URI. If either is required, the reflection alone is not enough to exploit.

Glossary

  • API: a way for one piece of software to ask another for data or actions, usually over the web at a specific address.
  • Endpoint: the specific address of a piece of an API or website that returns a particular thing, such as user data.
  • Cookie: a small piece of data a site stores in your browser after you log in, sent back automatically with later requests so the site recognises you.
  • Session: the period during which a site treats you as logged in, identified by your cookie.
  • Header: a labelled line of text sent with a web request or response, carrying information such as what is allowed or what format the data is in.
  • Origin: the combination of scheme, host and port that identifies which site a page came from, for example https://geekboy.ninja.
  • Same-origin policy: the browser rule that a page from one site may not read data from another site.
  • CORS: Cross-Origin Resource Sharing, the set of response headers by which a server deliberately relaxes the same-origin policy for chosen other sites.
  • Access-Control-Allow-Origin: the CORS response header naming which origin may read the response, or * for any.
  • Access-Control-Allow-Credentials: the CORS response header saying whether cookies may be included in the cross-site request and response.
  • null origin: the literal origin value null, used by browsers for pages such as local files or sandboxed frames.
  • Reflection: when a server copies a value the client sent straight back into its response.
  • Credentialed request: a cross-site request that carries the visitor's cookies, enabled in the browser with withCredentials = true.
  • curl: a command-line program that fetches a URL and prints the response, useful for inspecting headers.
  • Burp Suite Professional: a commercial tool for inspecting and testing web traffic, whose Active Scan can look for issues automatically.
  • Proof of concept (POC): a small demonstration showing that a vulnerability is real and exploitable.
  • XMLHttpRequest: the browser object a page's script uses to make a request in the background.
  • GET: the ordinary kind of web request used to fetch something.
  • URI: the address of a resource on the web, such as the path part of a URL.
  • Custom header: an extra header beyond the standard ones, which a page cannot freely add to a cross-site request without the server's permission.
  • Allowlist: an explicit list of the only values that are permitted.

Similar reports

View all

Same bug class and techniques, clearest first.

CORS misconfiguration

The lesson behind this report.

A permissive cross-origin policy lets another site read responses with the user's credentials.