Security

Browser security boundaries

CORS governs whether a browser allows scripts from another origin to access a response. CSRF protection addresses unwanted actions made through a browser carrying a user’s credentials. They solve different problems, and neither replaces server-side permission checks. 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. Evidence: local teaching fixture, not a production security guarantee. Source: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html

Formal Syntax & Structure

nodejs syntaxnodejs
cors(origin) → response headers (lesson-defined policy)

Working Example

Example Snippetnodejs
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')));
Node.js Reference