Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

How to Test Your Own API for Broken Access Control (Before a Pentester Does)

Broken access control is one of the most common serious issues in web apps and APIs. It's #1 in the OWASP Top 10, and in APIs it usually shows up as BOLA (Broken Object Level Authorization): one user can read or change a

Broken access control is one of the most common serious issues in web apps and APIs. It's #1 in the OWASP Top 10, and in APIs it usually shows up as BOLA (Broken Object Level Authorization): one user can read or change another user's data just by changing an ID.

The tricky part is that nothing looks broken. The request succeeds, the response is valid JSON. It's just the wrong user's data. Scanners often miss it for exactly that reason.

Here's a simple way to check your own API before a pentest.

1. Create two normal users

Make two test accounts with the same role, ideally in different organizations/tenants if your app is multi-tenant:

  • User A → owns order 1001
  • User B → owns order 2002

Log in as each and grab their tokens.

TOKEN_A="eyJ...userA"
TOKEN_B="eyJ...userB"

2. Request your own object (baseline)

curl -s https://api.example.com/v1/orders/1001 \
  -H "Authorization: Bearer $TOKEN_A"

You should get order 1001. Good.

3. Request the other user's object

Same token, other user's ID:

curl -s -o /dev/null -w "%{http_code}\n" \
  https://api.example.com/v1/orders/2002 \
  -H "Authorization: Bearer $TOKEN_A"

Expected: 403 or 404.
Vulnerable: 200 with order 2002's data.

4. Don't stop at GET

Reading data is bad; changing it is worse. Repeat with the methods that modify data:

# Can User A edit User B's order?
curl -s -o /dev/null -w "%{http_code}\n" -X PATCH \
  https://api.example.com/v1/orders/2002 \
  -H "Authorization: Bearer $TOKEN_A" \
  -H "Content-Type: application/json" \
  -d '{"shipping_address":"test"}'

# Can User A delete it?
curl -s -o /dev/null -w "%{http_code}\n" -X DELETE \
  https://api.example.com/v1/orders/2002 \
  -H "Authorization: Bearer $TOKEN_A"

Only run write/delete tests in a test or staging environment, never against real customer data.

5. Check the places people forget

  • Nested routes: /orgs/{orgId}/users/{userId}. Is orgId checked, or only userId?
  • IDs in the body, not just the URL: {"account_id": 2002}
  • File and export URLs: /files/{id}/download, /reports/{id}.pdf
  • Bulk and search endpoints that accept a list of IDs
  • Role changes: after a user is demoted, does their old token still work on admin routes?

6. Fix it in one place

The usual cause is checking authentication ("is this a valid user?") but not authorization ("does this user own this object?"). The fix is to scope every lookup to the current user or tenant:

// ❌ Finds any order by ID
const order = await Order.findById(req.params.id);

// ✅ Only finds orders that belong to the caller's tenant
const order = await Order.findOne({
  _id: req.params.id,
  tenantId: req.user.tenantId,
});
if (!order) return res.status(404).end();

Put this in a shared helper or middleware so every new endpoint gets it by default.

7. Turn it into a test

Once you've fixed an endpoint, add an automated test: "User A requesting User B's order returns 404". That stops the bug from coming back in the next refactor.

This won't replace a full pentest (business logic, chained issues and edge cases need a human with context), but it catches the single most common serious API bug in about 20 minutes.

Written by the team at Penva Security, human-led penetration testing for startups and SaaS teams.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.