PC-016HIGHCORS & API Security

Unvalidated Dynamic Origin Reflection in CORS Middleware

Detects server and proxy code that blindly echoes req.headers.origin, $http_origin, or Express cors({ origin: true }) without an allowlist check.

Rule Slug
cors-reflected-origin-header
Affected Files
*
Detection Method
Deterministic Regex
Live Rule Tester100% Browser-Local
HIGH: Detected 1 violation
Line 4:const origin = req.headers.origin;

Vulnerable Pattern vs. Hardened Fix

Vulnerable Pattern
// middleware/cors.ts
export function setCors(req: Request, res: Response) {
  // HIGH RISK: Blindly trusts any caller origin!
  const origin = req.headers.origin;
  if (origin) {
    res.setHeader('Access-Control-Allow-Origin', origin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
  }
}
Recommended Fix
// middleware/cors.ts
const TRUSTED_ORIGINS = new Set([
  'https://app.yourdomain.com',
  'https://dashboard.yourdomain.com',
]);

export function setCors(req: Request, res: Response) {
  const origin = req.headers.origin;
  if (origin && TRUSTED_ORIGINS.has(origin)) {
    res.setHeader('Access-Control-Allow-Origin', origin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
    res.setHeader('Vary', 'Origin');
  }
}

Why This Fails in Production

When developers hit the browser restriction prohibiting Access-Control-Allow-Origin: * with credentials, the most common (and most hazardous) quick fix copied from forum answers is dynamically reflecting the request’s Origin header straight back into the response (such as res.setHeader("Access-Control-Allow-Origin", req.headers.origin), Express cors({ origin: true }), PHP header("Access-Control-Allow-Origin: " . $_SERVER["HTTP_ORIGIN"]), or Nginx add_header Access-Control-Allow-Origin $http_origin).

Unlike a literal wildcard, which browsers refuse to pair with cookies, blindly reflecting the caller’s Origin header satisfies the Fetch specification while still trusting every domain on the internet. If an attacker hosts a page at https://evil-attacker.example and sends fetch("https://api.yourdomain.com/v1/account", { credentials: "include" }), your server echoes back Access-Control-Allow-Origin: https://evil-attacker.example alongside Access-Control-Allow-Credentials: true. The browser concludes that your API explicitly trusts evil-attacker.example and hands over the full private JSON response.

This vulnerability is even worse than standard Cross-Site Request Forgery (CSRF) because CSRF typically allows only blind state-changing writes, whereas unvalidated CORS origin reflection grants both full read access to private API data and CSRF token extraction, enabling complete account takeover across any authenticated user who visits a link.

Even if your API uses Bearer tokens instead of cookies today, blind origin reflection breaks defense-in-depth and becomes immediately exploitable the moment any endpoint adds cookie-based sessions, SSO flows, or internal network access.

Remediation Steps

  1. 1Never echo req.headers.origin, $http_origin, or cors({ origin: true }) directly in production code.
  2. 2Check the incoming Origin header against a strict allowlist (exact string match in a Set or map) before setting Access-Control-Allow-Origin.
  3. 3Always include Vary: Origin in responses when returning a matched origin from a multi-origin allowlist.
  4. 4In Nginx, use a map $http_origin $cors_origin block that maps only explicitly trusted origins to themselves and defaults everything else to an empty string.
OpsCheck: Validate Your CORS Origin Policy

Simulate browser preflight requests and test origin validation rules on OpsCheck.

Open OpsCheck ↗

Frequently Asked Questions

Why does Express cors({ origin: true }) trigger this warning?
In the Node.js cors package, setting origin: true instructs the middleware to reflect whatever Origin header the browser sends back in Access-Control-Allow-Origin, effectively trusting every domain on the web.
Is checking origin.endsWith("yourdomain.com") safe?
No! An attacker can register notyourdomain.com or yourdomain.com.evil.net to bypass naive suffix or substring checks. Always compare against exact full origins (protocol + hostname + optional port).
How do I safely support localhost during development without opening production?
Load allowed origins from an environment variable (ALLOWED_ORIGINS) so development sets http://localhost:3000 while production strictly lists your HTTPS production origins.

Related CORS & API Security Rules

Scan local project folder →