Securing REST & GraphQL APIs: OWASP Top 10 API Vulnerabilities
APIs serve as the connectivity layer for modern web applications, routing data between client frontends, databases, and third-party services. Consequently, they are a primary target for attackers.
Let's look at the top API vulnerabilities and how to address them.
1. Broken Object Level Authorization (BOLA / IDOR)
BOLA occurs when an endpoint accepts user-supplied identifiers (such as UUIDs or sequential IDs) to access resource models without validating that the logged-in user owns the requested resource.
Mitigation:
Implement object-level authorization checks in your controllers:
```javascript
const resource = await Database.getResource(req.params.id);
if (resource.userId !== req.user.id) {
return res.status(403).send("Unauthorized Access");
}
```
2. Broken Object Property Level Authorization (Mass Assignment)
This vulnerability occurs when endpoints allow users to modify database properties they shouldn't access (such as setting an `isAdmin: true` flag) by submitting unexpected JSON parameters.
Mitigation:
Enforce input validation using transfer schemas or select parameters explicitly:
```javascript
const safePayload = {
username: req.body.username,
bio: req.body.bio
};
await User.update(req.user.id, safePayload);
```