PC-015HIGHCORS & API Security

Wildcard CORS Origin (*) Combined with Credentials

Flags CORS configurations that combine a wildcard origin (*) with credentials enabled (Access-Control-Allow-Credentials: true), causing silent browser rejection or dangerous framework origin reflection.

Rule Slug
cors-wildcard-with-credentials
Affected Files
*
Detection Method
Deterministic Regex
Live Rule Tester100% Browser-Local
HIGH: Detected 1 violation
Line 6:origin: '*',

Vulnerable Pattern vs. Hardened Fix

Vulnerable Pattern
// server/app.ts (Express)
import cors from 'cors';

// HIGH RISK: Wildcard origin combined with credentials
app.use(cors({
  origin: '*',
  credentials: true,
}));
Recommended Fix
// server/app.ts (Express)
import cors from 'cors';

const ALLOWED_ORIGINS = new Set([
  'https://app.example.com',
  'https://admin.example.com',
]);

app.use(cors({
  origin: (origin, callback) => {
    if (!origin || ALLOWED_ORIGINS.has(origin)) {
      callback(null, true);
    } else {
      callback(new Error('Origin not allowed by CORS policy'));
    }
  },
  credentials: true,
}));
Vulnerable Pattern (Laravel PHP)
// config/cors.php (Laravel)
return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],
    'allowed_methods' => ['*'],
    'allowed_origins' => ['*'], // HIGH RISK: Wildcard with credentials
    'supports_credentials' => true,
];
Recommended Fix (Laravel PHP)
// config/cors.php (Laravel)
return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],
    'allowed_methods' => ['*'],
    'allowed_origins' => [
        'https://app.example.com',
        'https://admin.example.com',
    ],
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*'],
    'supports_credentials' => true,
];

Why This Fails in Production

Cross-Origin Resource Sharing (CORS) governs which external web origins may read authenticated responses from your API inside a user’s browser. Under the Fetch Standard, browsers strictly forbid combining Access-Control-Allow-Origin: * with Access-Control-Allow-Credentials: true. At best, shipping this combination breaks every authenticated fetch() or XMLHttpRequest call in production with an opaque browser CORS error, often slipping past curl or Postman tests that do not enforce browser Same-Origin Policy.

Even more dangerously, several popular backend CORS middleware libraries attempt to "helpfully" work around the specification ban: when configured with both wildcard origins and credentials enabled, some middleware automatically reflects the incoming Origin request header back as Access-Control-Allow-Origin instead of sending a literal asterisk. That single behavior instantly disables the Same-Origin Policy across your entire authenticated API.

When an API permits credentialed cross-origin reads from any website, any malicious page a logged-in user visits in another browser tab can silently issue authenticated fetch() requests carrying the user’s session cookies or HTTP Basic credentials, read private JSON payloads (such as profile data, CSRF tokens, or account balances), and exfiltrate them directly to an attacker-controlled server.

Always define an explicit allowlist of trusted frontend origins (loaded from environment configuration so staging and production never mix), validate incoming origins using strict exact-match checks, and enable credentials only when cookies or authorization headers are genuinely required.

Remediation Steps

  1. 1Replace wildcard origin: "*" with an explicit allowlist array such as [process.env.FRONTEND_URL] (e.g., ["https://app.example.com"]).
  2. 2Keep credentials: true only on endpoints that genuinely require browser cookies or cross-origin authentication headers.
  3. 3Verify that your CORS middleware sets Vary: Origin whenever returning a dynamic origin from an allowlist so CDNs do not cache one origin’s headers for another.
  4. 4Test preflight OPTIONS and credentialed POST requests across browsers using an explicit origin validator before deploying.
OpsCheck: Test & Generate Valid CORS Headers

Use the OpsCheck Multi-Stack CORS & Preflight Simulator to generate safe origin allowlists for your framework.

Open OpsCheck ↗

Frequently Asked Questions

Why did my API work in Postman and curl with origin: "*" and credentials: true, then fail in Chrome?
CORS is enforced strictly by web browsers, not server-side HTTP clients like curl or Postman. Browsers reject any credentialed response whose Access-Control-Allow-Origin header is a literal *.
Can I allow multiple subdomains safely with credentials enabled?
Yes. Maintain an explicit Set of approved origins or match against a strictly anchored regex (e.g., /^https:\/\/[a-z0-9-]+\.example\.com$/), never substring checks like origin.includes("example.com").
When is Access-Control-Allow-Origin: * actually safe?
Wildcard CORS is safe only for genuinely public, unauthenticated read-only resources (like public CDN fonts or open documentation APIs) where credentials: false and no private user data is returned.

Related CORS & API Security Rules

Scan local project folder →