API security: the UI hides it, the API sends it
APIs often return whole database objects and let the frontend pick what to show. Anyone with DevTools sees the rest. The OWASP API Top 10 risks that matter most for product teams and how to design responses that leak nothing.
In one sentence
Decide on the server which fields each audience may see and change, return only those, and authorize every object and every property.
Three fields on screen, eleven on the wire
The profile page shows a name, an avatar and a bio. The endpoint behind it returns the full user row: email, phone, password hash, internal flags. The frontend just doesn't render them.
{
"id": 7,"name": "Ana López","avatar": "🦊","bio": "Climber, coffee nerd","email": "ana@mail.test","phone": "+34 600 000 111","passwordHash": "$argon2id$v=19$m=19456…","role": "customer","lastLoginIp": "88.12.4.201","stripeCustomerId": "cus_FAKE91","isAdmin": false,}⚠ 7 fields the UI hides but anyone can read in DevTools
The API risks to know
| OWASP API (2023) | What it looks like |
|---|---|
| API1 Broken object level authorization | GET /orders/1042 works for someone who doesn't own it (IDOR) |
| API2 Broken authentication | Tokens that never expire, unprotected password reset, API keys in URLs |
| API3 Broken object property level authorization | Responses with extra fields; mass assignment on writes |
| API4 Unrestricted resource consumption | No pagination limits, no rate limits, expensive queries on demand |
| API5 Broken function level authorization | A normal user calls /admin/refunds |
| API9 Improper inventory | An old /v1 endpoint, still live, without the new checks |
// Serialize per audience instead of returning the DB row
const PublicUser = UserSchema.pick({ id: true, name: true, avatar: true, bio: true });
const SelfUser = PublicUser.extend({ email: z.string(), phone: z.string() });
return Response.json(isSelf ? SelfUser.parse(user) : PublicUser.parse(user));Key takeaways
- 01Never return raw database objects from an API.
- 02Define response schemas per audience on the server.
- 03Authorize objects, properties and functions on every request.
- 04Keep an inventory of endpoints and retire old versions.
Check your understanding
1 of 3
Why shouldn't an API return raw database objects?
Related concepts
Access control & IDOR
If `/api/orders/1042` works for a user who doesn't own order 1042, that's an IDOR. Every query that loads a record by ID must also filter by who's asking.
7 min read →CORS
CORS decides which other websites can read your responses from a user's browser. Never reflect any Origin with Allow-Credentials: true, and remember CORS is not authentication.
6 min read →AuthN vs AuthZ
Authentication answers *who are you?* Authorization answers *are you allowed to do this, to this thing, right now?* Most breaches of web apps are the second one missing.
6 min read →