Advertisement
Intermediate ASP.NET Core, CORS (Cross-Origin Resource Sharing)

What security risk does dynamically reflecting the Origin header back as Access-Control-Allow-Origin introduce, and why is it a dangerous anti-pattern?

Short answer

While AllowAnyOrigin combined with AllowCredentials is blocked by the spec, some developers work around it by dynamically 'reflecting' whatever Origin header the request sends back as the allowed origin value (via SetIsOriginAllowed(_ => true)), effectively allowing every origin…

Save ASP.NET Core mock interview
Advertisement
AModel answer
While AllowAnyOrigin combined with AllowCredentials is blocked by the spec, some developers work around it by dynamically 'reflecting' whatever Origin header the request sends back as the allowed origin value (via SetIsOriginAllowed(_ => true)), effectively allowing every origin while still technically satisfying the credentials requirement -- this completely defeats CORS's security purpose, allowing any malicious website to make authenticated cross-origin requests using a logged-in user's cookies.
// DANGEROUS anti-pattern: reflecting any origin defeats CORS's entire purpose
policy.SetIsOriginAllowed(origin => true).AllowCredentials();

// SAFE: explicit allowlist of known, trusted origins only
policy.WithOrigins("https://app.mycompany.com", "https://admin.mycompany.com").AllowCredentials();
Real-world example
A security audit flags a CORS configuration using SetIsOriginAllowed(_ => true) combined with AllowCredentials as a critical vulnerability, since it effectively allows any malicious website to make authenticated API calls using a victim's session cookies.
Likely follow-ups
  • How would an attacker actually exploit an overly permissive CORS + credentials configuration?
  • What's the safe way to support multiple known frontend origins instead?
Related topics