Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 4 OF 5 · 3 MIN READ

CORS, Cookies and Important Headers

CORS, cookies and caching headers cause many real production bugs, so interviewers ask about them often. You should explain why the browser blocks a cross-origin call, what a preflight is, and how to set a secure session cookie.

What you will learn

  • Explain the same-origin policy and CORS
  • Know when the browser sends a preflight OPTIONS request
  • Set HttpOnly, Secure and SameSite cookies correctly
  • Use Cache-Control and ETag

Key concepts

Origin

Scheme + host + port. https://app.example.com and https://api.example.com are different origins. The browser blocks JavaScript from reading cross-origin responses unless the server allows it with CORS headers.

Preflight

For non-simple requests (methods like PUT/DELETE, a JSON Content-Type, custom headers such as Authorization) the browser first sends OPTIONS asking permission. The server answers with Access-Control-Allow-Origin, -Methods and -Headers.

Cookie flags

HttpOnly hides the cookie from JavaScript (stops token theft through XSS). Secure sends it only over HTTPS. SameSite=Lax or Strict stops it from being sent on most cross-site requests, which blocks CSRF.

ETag

A version tag for a response. The client sends If-None-Match: "abc"; if nothing changed the server returns 304 with no body, saving bandwidth.

Step by step

CORS is enforced by the browser, not the server

The request still reaches your server. The browser just refuses to hand the response to JavaScript if the headers do not allow it. curl and server-to-server calls ignore CORS completely, so CORS is not a security layer for your API.

Credentials need an exact origin

If the frontend sends cookies (fetch with credentials: "include"), the server must answer with Access-Control-Allow-Credentials: true and a specific origin. The wildcard * is not allowed with credentials.

Examples

CORS and a secure session cookie in Express

import cors from 'cors'

const allowed = ['https://app.example.com']
app.use(cors({
  origin: (origin, cb) => cb(null, !origin || allowed.includes(origin)),
  credentials: true,
  methods: ['GET', 'POST', 'PATCH', 'DELETE'],
  maxAge: 600, // cache the preflight for 10 minutes
}))

res.cookie('sid', sessionId, {
  httpOnly: true,
  secure: true,
  sameSite: 'lax',
  maxAge: 7 * 24 * 3600 * 1000,
  path: '/',
})

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • List three requests that trigger a preflight and one that does not.
  • Explain why Access-Control-Allow-Origin: * breaks a login that uses cookies.
  • Write the Cache-Control header for a hashed JS bundle and for a user profile API.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Does CORS protect my API from attackers?

No. CORS only controls which web pages can read responses in a browser. Anyone can call your API with curl or a script. Protection comes from authentication, authorization and rate limiting.

What is the difference between SameSite Lax and Strict?

Strict never sends the cookie on cross-site requests, even when the user clicks a link to your site, so they arrive logged out. Lax sends it on top-level GET navigations but not on cross-site POSTs, images or fetches, which is the usual default.

What do no-cache and no-store mean?

no-cache lets the browser store the response but it must revalidate with the server (ETag) before each use. no-store forbids storing it at all, used for sensitive data like bank statements.

Summary

CORS is a browser rule: allow specific origins, answer preflights, and use exact origins with credentials. Protect session cookies with HttpOnly, Secure and SameSite, and use Cache-Control and ETag to avoid sending data the client already has.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus