Handling requests safely · mid
CORS and security headers (helmet)
CORS headers tell browsers which other origins may read your API's responses, and helmet sets a group of security headers; neither one authenticates users or stops non-browser clients.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- The browser enforces CORS, not the server. curl and Postman ignore it, and the server still receives (and may process) the request.
- A preflight OPTIONS request happens for non-simple requests (a JSON Content-Type, an Authorization header, PUT/DELETE/PATCH). The server answers with Access-Control-Allow-Origin, -Methods, -Headers and optionally -Max-Age.
- With cookies (fetch credentials: 'include') the server must return the exact origin, not *, plus Access-Control-Allow-Credentials: true. Use an allowlist; never reflect any Origin.
- cors package: app.use(cors({ origin: ['https://app.example.com'], credentials: true })).
- helmet() sets headers such as Content-Security-Policy, Strict-Transport-Security, X-Content-Type-Options: nosniff, X-Frame-Options, Referrer-Policy, and removes X-Powered-By.
- For a pure JSON API, CSP matters less than for HTML pages, but nosniff and HSTS still help; frame protections matter for any HTML you serve.
The red pen
Common traps: mistake → fix
Mistake: CORS protects my API from attackers
Fix: it only stops browsers from reading responses; it is not access control.
Mistake: Access-Control-Allow-Origin: * works with cookies
Fix: browsers reject * when credentials are included.
Mistake: CORS errors are fixed in the frontend
Fix: the server must send the headers (or the frontend uses a same-origin proxy/BFF).
Easy to hard
Questions interviewers ask, easy to hard
- Why does the request work in Postman but fail in the browser?
- What is a preflight and when does it happen?
- How do you configure CORS for a SPA on another domain that uses cookies?
- What does helmet do, and which headers matter for an API?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.