Skip to content
Katabench
Try free
8 min read The Katabench team

CORS misconfiguration: how reflected origins leak data

A CORS misconfiguration let a page on another site read a signed-in response in Chrome. Only exact origin matching blocked all three attacker origins.

A frontend on https://app.shop.test calls its API, and the console reports that the fetch "has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." Anyone who has built a frontend against a separate API has read that line. Searching for it tends to turn up middleware like the following, the error disappears, and the ticket closes:

C#
app.Use(async (context, next) =>
{
    var origin = context.Request.Headers.Origin.ToString();
    if (origin.Length > 0)
    {
        context.Response.Headers.AccessControlAllowOrigin = origin;
        context.Response.Headers.AccessControlAllowCredentials = "true";
    }

    await next();
});

This is the most damaging CORS misconfiguration there is. It tells the browser that any website may read this API's responses with the visitor's credentials, whenever the browser attaches them.

A reflected origin in Chrome

Test bed: a Kestrel app over HTTPS, headless Chrome with made-up host names mapped to loopback, and a login page that sets two cookies, sess_none with SameSite=None; Secure and sess_lax with SameSite=Lax; Secure. Output from a page on https://evil.test calling the API with credentials: "include":

Text
GET reflect: READ {"user":"alice","sess_none":true,"sess_lax":false}

The attacker's script read a response belonging to Alice. Only the SameSite=None cookie travelled, which matches MDN's description of SameSite: Lax cookies stay off cross-site fetch calls. Every result in this post is from Chrome, which by default does not block third-party cookies. Firefox and Safari ship third-party cookie protections on by default, so the same cross-site read may arrive with no session there. Neither was tested here.

Those defenses address cross-site requests. Served from https://forgotten.shop.test, a sibling subdomain and therefore the same site, the page received both cookies in Chrome. A credential in an Authorization header is a different case, because the attacker's page holds no token to attach, so this particular read needs a cookie or another credential the browser sends by itself.

Which pages got to read a signed-in response

https://app.shop.test

The real frontend

Reflect

Read

EndsWith

Read

StartsWith

Read

Exact list

Read

https://evil.test

Unrelated site

Reflect

Read, a leak

EndsWith

Blocked

StartsWith

Blocked

Exact list

Blocked

https://notshop.test

Registered look-alike

Reflect

Read, a leak

EndsWith

Read, a leak

StartsWith

Blocked

Exact list

Blocked

https://app.shop.test.evil.test

Attacker's subdomain

Reflect

Read, a leak

EndsWith

Blocked

StartsWith

Read, a leak

Exact list

Blocked

Measured in headless Chrome with credentials included. The frontend reading is the access you intended, and any other read is a leak. Only the exact list separates the frontend from all three attackers.

What the browser enforces and what the server still runs

The same-origin policy stops a page from reading responses from another origin. CORS is how a server relaxes that rule, and Microsoft's CORS documentation says it makes an API less restricted, not safer. The server handles the request either way. Enforcement happens in the browser, which is why curl never sees a CORS error.

Take a correctly configured policy that trusts only the real frontend, and a form-style POST with Content-Type: text/plain from the evil page:

Text
browser: post BLOCKED TypeError
server:  {"transfer:text/plain:none=True:lax=False:origin=https://evil.test":1}

A text/plain POST from https://evil.test ran a handler behind an exact-origin policy, with the SameSite=None cookie attached. Chrome hid only the response.

MDN explains why: requests shaped like a form submission need no preflight. Changing the content type to application/json did trigger a preflight. The server answered 204 with no CORS headers, Chrome stopped there, and the handler's counter never moved. PortSwigger's CORS material makes the same point about CSRF, and it is why every state-changing handler still needs its own authentication, authorization and antiforgery work.

Name exact origins and let the framework compare

List full origins, send credentials only where you need them, and name the methods and headers:

C#
builder.Services.AddCors(options =>
{
    options.AddPolicy("spa", policy => policy
        .WithOrigins("https://app.shop.test", "https://admin.shop.test")
        .AllowCredentials()
        .WithMethods("GET", "POST", "PUT")
        .WithHeaders("Content-Type", "Authorization"));
});

Apply it with app.UseCors() and .RequireCors("spa") on the endpoints that need it. Against this policy, both real origins received their own value in Access-Control-Allow-Origin along with Vary: Origin. https://notshop.test, http://app.shop.test, null and https://app.shop.test.evil.test got no CORS headers at all. If you write this by hand in any language, the shape is the same: an exact, full-string comparison against a fixed set, and that Vary header, which MDN recommends so a cache does not replay one origin's answer to another.

The framework's guard rails each have an edge:

  • AllowAnyOrigin().AllowCredentials() throws InvalidOperationException when the policy is built.
  • SetIsOriginAllowed(_ => true).AllowCredentials() walks around that guard, and it let the page on https://evil.test read Alice's response. Microsoft's documentation calls this insecure too.
  • A trailing slash in WithOrigins("https://app.shop.test/") blocks your own frontend with the No 'Access-Control-Allow-Origin' console error.
  • SetIsOriginAllowedToAllowWildcardSubdomains respected the dot boundary, rejecting https://notshop.test, while admitting https://forgotten.shop.test and https://a.b.shop.test. Each of those also receives SameSite=Lax cookies, so a neglected staging host or one cross-site scripting bug on any subdomain gets read access. Listing the origins you run costs one line per frontend.

If you are replacing a reflecting policy in production, find out who depends on it first. Log the Origin values the API receives for a while, sort them, and decide about each one, since a forgotten partner frontend will break the moment the list goes live.

Look-alike checks that leaked in Chrome

Each of these tries to avoid reflection, and the comment names an origin that read the response anyway:

C#
origin.EndsWith("shop.test")                             // https://notshop.test
origin.StartsWith("https://app.shop.test")               // https://app.shop.test.evil.test
Regex.IsMatch(origin, "^https://[a-z0-9.-]+.shop.test")  // both of the above

The suffix check has no dot boundary, so anyone can register a name that ends the same way. It also ignored the scheme and reflected http://app.shop.test, a page a network attacker can rewrite. The prefix check accepts a subdomain of the attacker's own domain. The regular expression has an unescaped dot and no end anchor, which gives it both bugs at once. An origin is a scheme, a host and a port, and a string test on part of it answers a different question.

The null origin is a subtler trap. Developers allowlist it after seeing Origin: null during local testing from a file: page. An attacker can produce that value on demand with a sandboxed iframe:

HTML
<iframe sandbox="allow-scripts" srcdoc="<script>
  fetch('https://api.shop.test/account', { credentials: 'include' })
    .then(r => r.text()).then(t => parent.postMessage(t, '*'));
</script>"></iframe>

Hosted on https://evil.test, that frame read {"user":"alice","sess_none":true,"sess_lax":false} from an endpoint whose list held the real frontend and the string null. The server logged Origin: null, indistinguishable from the case the developer meant to allow.

The server shows its decision in a response header, so these bugs are visible from a terminal. Swap in your own API and domain:

Bash
for origin in https://evil.test https://notshop.test \
  https://app.shop.test.evil.test http://app.shop.test null; do
  echo "== $origin"
  curl -s -o /dev/null -D - -H "Origin: $origin" https://api.shop.test/account \
    | grep -i '^access-control-allow'
done

Against the reflecting endpoint this printed both Access-Control-Allow headers for all five origins. Against the spa policy it printed nothing under any of them, which is the result you want. Keep the same hostile cases in an integration test.

That EndsWith line is where the Katabench exercise attached to this post begins. It is finished when a wrong scheme, an extra prefix and an unlisted subdomain all come back empty. The free C# secure coding exercises have more validation mistakes of this kind, and the track guide describes how they are graded. The open redirect post covers the same family of look-alike strings, and the OWASP Top 10 guide puts this bug beside other misconfigurations.

When * is fine, and what a private network changes

A literal * with Access-Control-Allow-Credentials: true, sent by hand, was refused by Chrome. Without credentials, * is reasonable for data that is public anyway. "Public" has to mean reachable from the internet, though. PortSwigger describes the exception: a server on a private network that answers * trusts the network instead of the caller, so an external page could read it through the browser of an employee inside that network.

Browsers are narrowing this path. Chrome 142 put requests from public sites to local network or loopback addresses behind a permission prompt, so in current Chrome that read now also needs the user to grant the site access. A prompt is a decision a person can get wrong, and it covers one browser, so an internal API should still authenticate its callers. The test bed mapped every host to loopback, so these results do not exercise that prompt.

Practice Security & Reliability

More like this: C# secure coding exercises →

Get new katas and .NET tips in your inbox

A short note when fresh kata land, plus the C# and performance tricks behind the grading. No spam, unsubscribe anytime.