CORS CSRF and browser security
Local fixture: Node.js 22.16.0, ECMAScript modules, synthetic data only. Run node main.mjs in a disposable directory. This is not a production security implementation.
Select CORS headers from an allowlist
const origins = new Set(['https://catalog.example']);function cors(origin) { if (!origins.has(origin)) return {Vary:'Origin'}; return {'Access-Control-Allow-Origin':origin, 'Access-Control-Allow-Credentials':'true',Vary:'Origin'};}console.log(JSON.stringify(cors('https://catalog.example')));console.log(JSON.stringify(cors('https://catalog.example:8443')));Expected output:
{"Access-Control-Allow-Origin":"https://catalog.example","Access-Control-Allow-Credentials":"true","Vary":"Origin"}{"Vary":"Origin"}A different port is a different origin; the response model does not grant CORS access.
How it works
An origin combines scheme, host and port. Configure a precise origin allowlist when cross-origin browser clients are necessary; credentialed CORS requires an explicit allowed origin rather than a wildcard, and responses that vary by Origin should account for caches with Vary: Origin. A preflight checks certain cross-origin requests, but not every request is preflighted. A cookie-authenticated application should use a maintained CSRF mechanism for state changes, validate origin information as appropriate, and set cookie attributes. SameSite uses site semantics, not exactly origin semantics. Use safe HTTP methods for read-only actions. The examples are policy reasoning models, not browser execution, cookie issuance or complete CSRF middleware.
Require a verified CSRF result for writes
// All booleans are results from trusted server checks, not request fields.function decision({authenticated, authorized, csrfVerified}) { if (!authenticated) return 401; if (!authorized || !csrfVerified) return 403; return 200;}console.log(decision({authenticated:true,authorized:true,csrfVerified:false}));console.log(decision({authenticated:true,authorized:false,csrfVerified:true}));console.log(decision({authenticated:true,authorized:true,csrfVerified:true}));Expected output:
403403200Both permission and CSRF verification are independently necessary in this cookie-authenticated write policy.
Common mistakes
Do not treat an omitted CORS header as denial of a server-side action. Non-browser clients do not enforce browser CORS. Do not reflect arbitrary Origin values or treat HttpOnly as a CSRF prevention mechanism.
Practice and next steps
Add an approved staging origin to the allowlist. Check that the exact production and staging origins match while different schemes or ports do not.
Further reading: Primary documentation. Related staged reference: Browser security boundaries.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CORS CSRF and browser security — knowledge check
Apply the security boundary and distinguish the example from a production control.
What does CORS primarily regulate?
Which condition should a cookie-authenticated write satisfy?
Why return Vary: Origin when CORS headers depend on origin?