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:
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.
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":
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
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:
browser: post BLOCKED TypeError
server: {"transfer:text/plain:none=True:lax=False:origin=https://evil.test":1}A
text/plainPOSTfromhttps://evil.testran a handler behind an exact-origin policy, with theSameSite=Nonecookie 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.
List full origins, send credentials only where you need them, and name the methods and headers:
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()throwsInvalidOperationExceptionwhen the policy is built.SetIsOriginAllowed(_ => true).AllowCredentials()walks around that guard, and it let the page onhttps://evil.testread Alice's response. Microsoft's documentation calls this insecure too.- A trailing slash in
WithOrigins("https://app.shop.test/")blocks your own frontend with theNo 'Access-Control-Allow-Origin'console error. SetIsOriginAllowedToAllowWildcardSubdomainsrespected the dot boundary, rejectinghttps://notshop.test, while admittinghttps://forgotten.shop.testandhttps://a.b.shop.test. Each of those also receivesSameSite=Laxcookies, 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.
Each of these tries to avoid reflection, and the comment names an origin that read the response anyway:
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 aboveThe 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:
<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:
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'
doneAgainst 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.
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
- Open in the editor: Comment Renderer (HTML Injection)
Secure coding Easy Free, no account needed
Comment Renderer (HTML Injection)
Fix a stored-XSS hole in a comment renderer without breaking its output.
- Open the Pro kata: CORS Origin Reflection
Secure coding Medium Katabench Pro
CORS Origin Reflection
Decide the Access-Control-Allow-Origin value by exact allowlist, not a suffix match an attacker can satisfy.
More like this: C# secure coding exercises →