Why It Matters for AI-Coded Apps
AI-generated backends frequently set CORS to Access-Control-Allow-Origin: * to make development easier, then ship this to production. This allows any website to make API requests to your backend, enabling data theft, CSRF-like attacks, and credential exposure. Misconfigured CORS is one of the most common issues in vibe-coded APIs.
Real-World Example
A backend sets Access-Control-Allow-Origin: * with Access-Control-Allow-Credentials: true. An attacker’s website makes a fetch request to https://yourapi.com/api/user/profile and receives the authenticated user’s data because the browser sends cookies and the server allows any origin.
How to Detect and Prevent It
Never use * for Access-Control-Allow-Origin in production. Explicitly allowlist your frontend domain(s). Never combine * with Access-Control-Allow-Credentials: true (browsers block this, but reflecting the Origin header is equally dangerous). Validate the Origin header against an allowlist on the server.
Frequently Asked Questions
What is a CORS preflight request?
Why does CORS only apply to browsers?
How do I configure CORS in Express.js?
cors package: app.use(cors({ origin: 'https://yourfrontend.com', credentials: true })). For multiple origins, pass an array or a function that validates against an allowlist. Never use origin: true which reflects any origin.