CORS Wildcard Misconfiguration Allows Unauthorized Cross-Origin Access
What Is This Vulnerability?
Cross-Origin Resource Sharing (CORS) controls which external domains can access your API. Setting the Access-Control-Allow-Origin header to a wildcard (*) or dynamically reflecting any origin without validation allows any website to make authenticated requests to your server, potentially leaking sensitive data.
Why It Happens
Developers often set permissive CORS policies during local development to avoid cross-origin errors and then forget to restrict them before deploying to production. Some teams also misconfigure CORS middleware by reflecting the request Origin header without checking it against an allowlist.
Example Code
import express from "express";
import cors from "cors";
const app = express();
// Allows ANY origin to access the API
app.use(cors({ origin: "*", credentials: true }));
app.get("/api/user/profile", (req, res) => {
res.json({ email: req.user.email, balance: req.user.balance });
});import express from "express";
import cors from "cors";
const allowedOrigins = [
"https://myapp.com",
"https://admin.myapp.com",
];
const app = express();
app.use(
cors({
origin(origin, callback) {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error("Blocked by CORS"));
}
},
credentials: true,
}),
);
app.get("/api/user/profile", (req, res) => {
res.json({ email: req.user.email, balance: req.user.balance });
});How Hackers Exploit It
An attacker hosts a malicious page that makes fetch requests to your API. Because the wildcard CORS policy returns permissive headers, the browser allows the attacker's script to read the response. The attacker can steal session tokens, personal data, or financial information from any logged-in user who visits the malicious page.
How to Fix It
Replace the wildcard origin with an explicit allowlist of trusted domains. Validate the incoming Origin header against this list before reflecting it. Never combine credentials: true with a wildcard origin. Use environment-specific CORS configurations so development and production have different policies.