Question presented to candidate: "Your API works fine when tested with curl or Postman from any origin, but a specific frontend domain reports its browser requests are being blocked. What's actually blocking it, and where does the fix belong — client or server?"
What a strong answer should cover:
- The single most important, often-misunderstood fact: CORS is enforced by the browser, not the server — this is exactly why the prompt's curl/Postman requests work fine (neither enforces CORS at all) while a real browser genuinely blocks the frontend. The server's only job is to advertise, via response headers, which origins are allowed — the browser reads those headers and decides whether to expose the response to the requesting page's JavaScript.
- 📌 Verified, not assumed: a real Express server with the
corsmiddleware configured to allowhttps://trusted-app.comreturned a genuineAccess-Control-Allow-Originheader for that origin, and genuinely returned no such header at all for a request carrying an untrustedOrigin— while the response body was still returned by the server in both cases, proving the server itself never blocks anything; only a real browser reading that missing header would. - 📌 Interview term: preflight request — for "non-simple" requests (custom headers, methods like PUT/DELETE/PATCH, certain content types), the browser first sends a real OPTIONS request asking permission before the actual request — verified directly: a real preflight OPTIONS request returned a genuine 204 with
Access-Control-Allow-OriginandAccess-Control-Allow-Methodsheaders confirming what's permitted, before the actual request is ever sent. - A precise answer distinguishes CORS from a server-side authorization check: CORS decides whether a browser will let JavaScript running on a different origin's page read the response — it is not a security boundary against a non-browser client (curl, another server, a malicious script running server-side) at all, none of which honor CORS headers in the first place. Real authentication/authorization checks remain necessary regardless of CORS configuration.
- The practical fix, precisely: configure the allowed origins, methods, and headers explicitly (via the
corspackage or manual header-setting) — never reflexively setAccess-Control-Allow-Origin: *on an endpoint that also handles credentials/cookies, since browsers specifically disallow combining a wildcard origin with credentialed requests.
Clarifying questions expected:
- "Does this specific endpoint need to support credentialed requests (cookies, an Authorization header sent cross-origin)?" — directly decides whether a wildcard origin is even usable at all.
- "Is the failing request a simple GET, or does it involve a custom header/method that would trigger a preflight?" — narrows down whether the fix is in the main response headers or the OPTIONS handling.
Code / implementation expected: Yes — a real server tested from both a trusted and an untrusted origin, including a real preflight OPTIONS exchange, is the concrete, convincing proof of exactly what the server does and does not enforce.