In July we covered the 2023 OWASP API Security Top 10 and why APIs have become such a large attack surface. This follow-up is for developers. Here are the API vulnerabilities we find most often when reviewing code, what they look like, and how to fix them.
1. Missing object-level authorisation
The most common and most damaging API bug: the endpoint checks that the user is logged in, but not that they are allowed to access this object.
# Vulnerable (FastAPI): any authenticated user can read any order
@app.get("/orders/{order_id}")
def get_order(order_id: int, user: User = Depends(current_user), db: Session = Depends(get_db)):
order = db.get(Order, order_id)
if not order:
raise HTTPException(404)
return order# Fixed: the query is scoped to the caller
@app.get("/orders/{order_id}")
def get_order(order_id: int, user: User = Depends(current_user), db: Session = Depends(get_db)):
order = db.execute(
select(Order).where(Order.id == order_id, Order.customer_id == user.customer_id)
).scalar_one_or_none()
if not order:
raise HTTPException(404) # same response for "missing" and "not yours"
return OrderOut.model_validate(order)Habits: make scoped queries the default through a repository or policy layer; write tests that request other users' objects; never rely on unguessable IDs as the only protection.
2. Mass assignment and over-exposure
Binding request JSON directly onto a model lets clients set fields they should not:
// Vulnerable (Express + an ORM): client can send {"role": "admin", "credit": 99999}
app.patch("/users/me", auth, async (req, res) => {
await User.update(req.body, { where: { id: req.user.id } });
res.json(await User.findByPk(req.user.id)); // also returns every column
});// Fixed: allow-list writable fields, return an explicit shape
const Writable = z.object({ displayName: z.string().max(80), locale: z.enum(["en", "fr", "de"]) }).strict();
app.patch("/users/me", auth, async (req, res) => {
const data = Writable.parse(req.body); // rejects unknown fields
await User.update(data, { where: { id: req.user.id } });
const u = await User.findByPk(req.user.id);
res.json({ id: u.id, displayName: u.displayName, locale: u.locale });
});3. Broken function-level authorisation
Admin endpoints protected only by being "hidden" in the UI. Every endpoint should declare which roles may call it, and the check should run server-side:
@router.post("/admin/users/{user_id}/suspend", dependencies=[Depends(require_role("support_admin"))])
def suspend_user(user_id: int): ...Deny by default: endpoints without an explicit authorisation rule should fail closed.
4. JWT mistakes
JSON Web Tokens are easy to get subtly wrong:
- accepting
alg: none, or letting the token choose its own algorithm, - not verifying the signature at all (decoding is not verifying),
- skipping
iss,audorexpchecks, so tokens for another service or expired tokens are accepted, and - putting sensitive data in the payload, which is only base64-encoded, not encrypted.
import jwt
claims = jwt.decode(
token,
key=jwks_client.get_signing_key_from_jwt(token).key,
algorithms=["RS256"], # fixed list, never taken from the token
audience="https://api.example.com",
issuer="https://login.example.com/",
options={"require": ["exp", "iat", "sub"]},
)Prefer a well-maintained library and your identity provider's SDK over hand-rolled validation.
5. No limits on expensive operations
Search, export, report generation and file uploads can be abused to exhaust resources or run up costs:
- rate-limit per user and per token, not just per IP address,
- paginate with a maximum page size,
- cap upload sizes and processing time, and
- move expensive work to queues with quotas.
6. Server-side request forgery
Webhooks, URL imports and link previews that fetch user-supplied URLs can be pointed at internal services or cloud metadata endpoints. Allow-list destinations where possible, resolve and block private address ranges, disable redirects or re-validate after them, and use IMDSv2 in AWS.
7. Leaky errors and missing logging
Stack traces and database errors in API responses help attackers map your system. Return generic errors to clients, log details server-side with a correlation ID, and log authorisation failures. A spike in 403s or 404s on sequential IDs is a strong signal of enumeration.
A pull request checklist for API changes
- Every data access is scoped to the caller (tenant, owner, role)
- Request bodies validated against strict schemas; writable fields allow-listed
- Responses use explicit output schemas
- Endpoint declares required roles; default deny
- Tokens validated for algorithm, signature, issuer, audience and expiry
- Pagination limits, rate limits and size caps on expensive operations
- Outbound fetches validated against SSRF
- Errors generic to clients, detailed in logs; auth failures logged
- Tests include cross-user and cross-role access attempts
The bottom line
Most API breaches do not need sophisticated exploits; they need one endpoint that forgot to ask "is this caller allowed to do this to this object?". Make authorisation, strict input and output schemas, careful token validation and sensible limits the default in your framework, and check them in every review.
Our application penetration testing focuses heavily on API authorisation, and our security training covers these patterns hands-on for development teams.