Understanding CORS Preflight, Credentials, and Origin Allow Lists
Configure cross-origin browser access deliberately and understand why CORS is not an authorization or CSRF defense.
Cross-Origin Resource Sharing is a browser-enforced mechanism that lets a server describe which origins may read a response. Some requests trigger an OPTIONS preflight so the browser can ask whether the server permits the intended method and headers.
THE PRODUCTION PROBLEM STATEMENT
Teams often treat CORS errors as a frontend-only issue or allow every origin to avoid them. A broad policy can expose responses to unintended websites, while CORS does not stop direct requests from scripts, command-line clients, or servers.
Know when the browser sends a preflight request
A cross-origin request using certain methods, headers, or content types causes the browser to send OPTIONS first. The preflight names the intended method and headers; the server’s response decides whether the browser may send the actual request.
The preflight request itself is not the application mutation. Configure the server or proxy to answer OPTIONS with the allowed origin, method, and headers, then return appropriate CORS headers on the actual response too.
OPTIONS /api/orders HTTP/1.1
Origin: https://dashboard.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, x-request-id
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://dashboard.example
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: content-type, x-request-id
Vary: OriginHandle cookies and dynamic origins as a deliberate allow list
Credentialed cross-origin requests require an explicit allowed origin and Access-Control-Allow-Credentials: true; the wildcard origin is not valid with credentials. Browsers also apply cookie SameSite and third-party cookie policies independently of CORS.
If the server reflects Origin dynamically, compare it to a configured allow list first and send Vary: Origin. Never copy arbitrary Origin values into Access-Control-Allow-Origin simply because a request included them.
const allowedOrigins = new Set([
'https://dashboard.example',
'https://admin.example',
]);
function applyCors(req, res) {
const origin = req.headers.origin;
if (allowedOrigins.has(origin)) {
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Vary', 'Origin');
}
}Keep CORS separate from authentication and CSRF controls
CORS controls whether browser JavaScript can read a response under cross-origin rules. It does not authenticate callers, authorize actions, or prevent non-browser clients from making requests.
Cookie-authenticated state changes still need appropriate authentication, authorization, and CSRF protections. A simple cross-origin request may reach a server even when the browser later withholds the response from JavaScript.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
No. CORS is enforced by browsers. APIs still need authentication, authorization, validation, and abuse controls.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Implementing Strict Content Security Policy (CSP) with Nonces and 'strict-dynamic' in Modern SPAs
How to move beyond brittle domain allowlists to cryptographic nonces, trust propagation for dynamic code-splitting, and XSS defense.
Modern OAuth 2.0 and OpenID Connect for SPAs: The Authorization Code Flow with PKCE and Token Rotation
A security engineer’s guide to single-page authentication: why the Implicit Flow is deprecated, code verifiers, and defending against token theft via XSS.