Question presented to candidate: "An API endpoint accepts a JSON body and returns data from a database. Walk through every layer of security that should exist between the raw incoming request and your business logic actually running."
What a strong answer should cover:
- Securing an API endpoint is a layered problem, not one control: authentication (who is making this request), authorization (are they allowed to do this specific thing), input validation (is the request body/params/query actually well-formed and safe), rate limiting (is this client making requests at an acceptable rate), and transport/header-level protections (HTTPS, security headers) — each addressing a genuinely different failure mode.
- Authentication commonly uses a JWT or session-based token, verified on every request — the dedicated JWT-vs-session-based-authentication question covers the specific trade-offs between the two approaches and where refresh tokens fit.
- Input validation with a schema library (
zod/joi, covered in its own dedicated question) should run before any business logic touches the request body — rejecting a malformed or unexpected request shape immediately, rather than letting invalid data reach deeper code paths where it could enable SQL injection, prototype pollution, or other injection classes (each covered with a real demonstrated exploit in their own dedicated questions). - 📌 Verified, not assumed: HTTP security headers via
helmet()add real, checkable protection with a single middleware call — confirmed directly: a server without it exposed onlyx-powered-by: Express; with it, a full protective header set (CSP, HSTS,X-Frame-Options, and others) appeared automatically. - Rate limiting (covered fully in its own dedicated question) protects against both brute-force credential attacks and basic denial-of-service, and should apply per-identity (per API key/user, not just per IP) where the endpoint is authenticated, since a single IP is not a reliable proxy for a single real client at scale.
- CORS and CSRF (both covered in their own dedicated questions, with real tested browser behavior for CSRF specifically) address genuinely different threats from the layers above — a complete answer names them as distinct, not folds them into "authentication" vaguely.
- A precise answer treats endpoint security as this specific ordered pipeline — headers/transport, rate limiting, authentication, authorization, input validation, then business logic — rather than a flat, unordered list of good ideas.
Clarifying questions expected:
- "Is this endpoint authenticated at all, or intentionally public?" — decides how much of the pipeline (auth, per-identity rate limiting) actually applies.
- "Is the concern a specific layer (auth, input validation) or the complete pipeline end to end?"
Code / implementation expected: Yes — the real, verified helmet() header difference grounds the headers layer concretely; the rest of the pipeline cross-links to its own dedicated, individually-verified question.