API Security Best Practices Beyond Authentication
On this page
- Beyond authentication: the OWASP API Security Top 10
- Mass assignment and how to prevent it
- Excessive data exposure in API responses
- Unrestricted resource consumption
- Server-side request forgery (SSRF) in APIs
- Input validation and injection
- CORS, preflight requests and CSRF
- API inventory and security logging
- Check yourself
Beyond authentication: the OWASP API Security Top 10
Most API breaches don't involve broken login. They involve a valid user doing something the API didn't expect: reading another user's object (Day 13), setting a field they shouldn't, or making the server fetch an internal URL. The OWASP API Security Top 10 (2023 edition) catalogs these; here are the ones to design out.
Mass assignment and how to prevent it
A handler that copies the request body straight onto a database record lets clients set any field, including ones the UI never shows:
// VULNERABLE: applies every field the client sent
app.patch('/users/:id', (req, res) => {
Object.assign(user, req.body); // { "role": "admin" } works too
});Fix it with an allow-list of fields each endpoint accepts. From the tested Express API:
const UPDATABLE = ['name'];
const rejected = Object.keys(req.body).filter(k => !UPDATABLE.includes(k));
if (rejected.length)
return problem(res, 422, 'validation-error', 'Fields cannot be updated', { fields: rejected });PATCH /users/1 {"name":"Ana B","role":"admin"}
{"type":"https://api.example.com/errors/validation-error","title":"Fields cannot be updated","status":422,"fields":["role"]}
PATCH /users/1 {"name":"Ana B"}
{"id":1,"email":"ana@example.com","name":"Ana B","role":"member"}Rejecting unknown fields loudly (rather than silently dropping them) also catches client bugs. Schema validation libraries (Zod, Joi, JSON Schema validators, Pydantic) make the allow-list the schema itself.
Excessive data exposure in API responses
The mirror image: returning whole database records and trusting the client to show only some fields. A mobile app hides passwordResetToken, but anyone reading the raw response sees it. Use explicit response serializers that list the fields to return, per endpoint and per viewer. OWASP groups this and mass assignment as Broken Object Property Level Authorization.
Unrestricted resource consumption
- Cap page sizes (Day 7), request body size, upload size, and GraphQL query cost (Day 19).
- Set timeouts on every handler and outbound call.
- Rate limit expensive operations and anything that sends messages or costs money (SMS, email, AI calls), per user and per target.
Server-side request forgery (SSRF) in APIs
Features that fetch a URL the client provides (webhook targets, image imports, link previews) can be pointed at internal addresses: http://169.254.169.254/ (cloud instance metadata, which can hold credentials), http://localhost:6379, internal admin panels.
- Allow only
httpsand expected ports. - Resolve the hostname and reject private, loopback and link-local IP ranges at connection time, since DNS can resolve differently later.
- Don't follow redirects blindly; re-check each hop.
- Make the fetch from an isolated network without access to internal services, and require IMDSv2 on cloud instances (see the AWS course).
Input validation and injection
- Validate types, ranges, lengths and formats at the boundary, with a schema.
- Use parameterized queries; never build SQL, shell commands or LDAP queries with string concatenation.
- Treat file names, paths and content types from clients as untrusted.
CORS, preflight requests and CSRF
- CORS decides which web origins can call your API from a browser. Allow specific origins; never reflect any
Originback together withAccess-Control-Allow-Credentials: true.
How it works in practice: before a cross-origin request that isn't a simple GET or form post, such as a PATCH with a JSON body, the browser first sends a preflight OPTIONS request asking permission, and only sends the real request if the answer allows it:
OPTIONS /users/1
Origin: https://app.example.com
Access-Control-Request-Method: PATCH
204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, PATCH
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 600An "origin" is the scheme, host and port together, so https://app.example.com and http://app.example.com are different origins. Max-Age lets the browser skip the preflight for 10 minutes.
- CORS doesn't protect against non-browser clients; it isn't authentication.
- For cookie-authenticated APIs, use
SameSitecookies and CSRF protection.
API inventory and security logging
OWASP also lists improper inventory management: old versions, debug endpoints and forgotten staging APIs that nobody patches. Keep a list of every API and version that's reachable, retire old ones (Day 23), and log security-relevant events: authentication failures, authorization denials, rate-limit hits.
Check yourself
Binding all client-supplied fields onto a record, letting users set fields like role; prevent it with an allow-list of accepted fields per endpoint.
The server may be tricked into requesting internal addresses, such as cloud metadata or admin services, that the attacker can't reach directly.
No. CORS is enforced by browsers to protect users; curl, scripts and servers ignore it completely. It only controls which web pages can read responses, so authentication and authorization are still required.
The CORS preflight: the browser's OPTIONS request didn't get a response allowing that origin, method or headers, so it never sent the PATCH. Curl doesn't do preflights.