Skip to main content

Free 30-min security demo Book Now

Academy CORS Misconfiguration
Intermediate · 20 min

CORS Misconfiguration

Learn how wildcard CORS and reflected Origin headers allow cross-origin requests to read sensitive API responses.

1 Wildcard CORS and Reflected Origin

CORS (Cross-Origin Resource Sharing) headers tell browsers which origins may access API responses. Misconfigured CORS allows malicious sites to read sensitive API data from authenticated users.

Wildcard with credentials — impossible but attempted:

Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

Browsers reject this combination, but developers often "fix" it by reflecting the Origin header blindly.

Reflected Origin (critical vulnerability):

# VULNERABLE: mirrors any Origin header back
origin = request.headers.get("Origin", "")
response.headers["Access-Control-Allow-Origin"] = origin  # Mirrors attacker!
response.headers["Access-Control-Allow-Credentials"] = "true"

An attacker creates a page at https://evil.com that sends a credentialed CORS request to your API. Your API responds with Access-Control-Allow-Origin: https://evil.com, allowing the attacker to read the victim's private data.

2 Strict Origin Allowlist

Only allow specific, pre-approved origins. Validate the Origin header against an allowlist before reflecting it.

Safe CORS middleware (Node.js):

const ALLOWED_ORIGINS = new Set([
  "https://myapp.com",
  "https://www.myapp.com",
  "https://admin.myapp.com"
]);

app.use((req, res, next) => {
  const origin = req.headers.origin;
  if (origin && ALLOWED_ORIGINS.has(origin)) {
    res.setHeader("Access-Control-Allow-Origin", origin);
    res.setHeader("Access-Control-Allow-Credentials", "true");
    res.setHeader("Vary", "Origin");  // Important for caching!
  }
  next();
});

Defense checklist:

  • Validate Origin against an explicit allowlist before reflecting
  • Never combine wildcard * with credentials
  • Set Vary: Origin to prevent incorrect cache serving
  • Return null or no ACAO header for disallowed origins
  • Use CORS only for public APIs; prefer same-origin for sensitive APIs

Knowledge Check

0/3 correct
Q1

What makes a "reflected Origin" CORS configuration dangerous?

Q2

Why must you set Vary: Origin alongside dynamic CORS headers?

Q3

Can Access-Control-Allow-Origin: * be used with Access-Control-Allow-Credentials: true?

Code Exercise

Validate Origin Against Allowlist

The CORS middleware reflects any Origin header. Fix it to only allow origins in the approved list.

javascript

Put it into practice

Find CORS Misconfiguration in your own code

Offensive360 SAST traces the tainted data flow behind this class of bug across 60+ languages and ships a secure fix with every finding. Public open-source projects scan free; teams can see it on their own repository in a 30-minute demo.