QA Interview Hub · Knowledge base · Quizzes · Українська

API › API security

OWASP API Top 10: access and authentication

The main access and authentication risks in the OWASP API Top 10 (2023) are BOLA (API1), Broken Authentication (API2) and BFLA (API5). BOLA (IDOR) means the server does not check that the requested object belongs to the user, and BFLA means an ordinary role calls a function meant for a higher role. API3 (BOPLA) covers individual fields. Authorization is needed in every handler, and testing it takes two users.

API · Security

A user changed 42 to 43 in the URL and saw someone else's account. Does your test suite catch it?

The test "get my account" is green: the token is valid, the status is 200. But the same token sent to GET /accounts/43 returns another person's account. Authentication works, but nothing checks whose object it is. No test with only one user can find this. This lesson covers which risks of the OWASP API Security Top 10 concern access and authentication, and how a QA engineer checks them without playing a pentester.

How it is built

The OWASP API Security Top 10 (2023 edition) contains three risks about "who are you" and "what may you do":

  • API1 BOLA (Broken Object Level Authorization, IDOR in older terms): the server does not check whether the object with the given id belongs to the user.
  • API2 Broken Authentication: weak login, tokens that are never invalidated, sensitive operations without re-confirmation.
  • API5 BFLA (Broken Function Level Authorization): a regular user calls a function meant for a role with higher rights.

All three have one cause: authentication and authorization happen in different places. Authentication says "the token is genuine, this is user A", and a gateway or middleware usually does it. Authorization asks "may A read this particular object and call this particular function", and the answer depends on data in the database, so a gateway cannot do it. Every handler has to perform it or explicitly call a shared policy, and this is where it gets forgotten.

Where the ownership check lives

GET /accounts/43 + token A 1 token genuine? yes, else 401 2 does object 43 belong to A? check exists: 403 none: 200 + data
Figure 1. Step 1 is done by a gateway, step 2 must be done by every handler. The BOLA bug lives in step 2, which is highlighted because it is the one most often forgotten.

Why it is so. The server cannot "guess" that 43 is someone else's: an id is only a key in the database. So reliable protection means either an owner filter in the database query (WHERE id = 43 AND owner_id = :userId) or an explicit policy check. Hiding other people's ids (replacing sequential numbers with UUIDs) makes guessing harder but does not remove the bug: a UUID can be seen in an email, a log or someone's link, and the missing check is still missing.

Example: from one user to two

Before: one user in the tests, and nobody ever reaches into someone else's resource. The bug shows only when you check what happens with another person's token:

GET /v1/accounts/43 HTTP/1.1
Authorization: Bearer <token of the owner of account 42>

HTTP/1.1 200 OK
Content-Type: application/json

{ "id": 43, "owner": "Petro", "iban": "UA21...", "balance": 18200.50 }

After: two users, A and B. We request A's resource with B's token and check all methods, not just GET:

// tokenA belongs to user A (owner of account 42), tokenB to user B
const as = (token: string) => ({ Authorization: 'Bearer ' + token });

test('user B cannot touch account 42 with any method', async ({ request }) => {
  const url = '/v1/accounts/42';
  const calls = [
    request.get(url, { headers: as(tokenB) }),
    request.patch(url, { headers: as(tokenB), data: { nickname: 'x' } }),
    request.delete(url, { headers: as(tokenB) }),
    request.get(url + '/transactions', { headers: as(tokenB) }),
  ];
  for (const res of await Promise.all(calls)) {
    expect([403, 404]).toContain(res.status());
  }
  // the owner still sees an untouched account
  const own = await request.get(url, { headers: as(tokenA) });
  expect(own.status()).toBe(200);
  expect((await own.json()).nickname).not.toBe('x');
});

The test accepts 403 or 404: a product may choose either, see the lesson on information leaks. The final GET by the owner checks the main thing: even when the server answered 403, the change must not have gone through. That way the test also catches the bug "answered 403 but had already done it". The same rule applies to nested resources (/transactions) and to ids in the request body, not only in the path.

BFLA is checked similarly but with a different question: not "whose object is it" but "does the role have the right to the function at all":

test('a regular user cannot call admin functions', async ({ request }) => {
  const res = await request.delete('/v1/admin/users/5', {
    headers: { Authorization: 'Bearer ' + userToken },
  });
  expect(res.status()).toBe(403);
  // and the target user must still exist
  const check = await request.get('/v1/admin/users/5', {
    headers: { Authorization: 'Bearer ' + adminToken },
  });
  expect(check.status()).toBe(200);
});

The typical cause of the bug: the frontend hides the "Delete user" button for a regular role, while the endpoint trusts that the button is not visible. Hiding in the UI is not protection.

Broken Authentication: what to check

  • Password guessing: after several wrong attempts the answer is 429 or a lockout (details in the lesson on rate limiting).
  • Token invalidation: after logout and after a password change, the old token (or refresh token) stops working.
  • No token in the URL: a query ends up in logs and browser history.
  • Sensitive actions (changing password, email, IBAN) require the current password or a second factor.
  • Login responses do not differ for an existing and a non-existing account.

Limits and alternatives

  • A two-token test finds a missing check but does not prove the check exists in every handler. So the set is repeated for every new endpoint that accepts an id.
  • Manual BOLA checks automate well: from an OpenAPI specification you can generate a loop "for every endpoint with a path parameter try someone else's token". Tools such as ZAP or Burp help, but they do not know business logic (whose object it is).
  • This is only the top of the list. The full Top 10 also includes SSRF, misconfiguration, API inventory and safe consumption of third-party APIs. A pentest and code review cover what a tester cannot see from outside.

Common mistakes

  • One test user. Without a second token BOLA cannot be tested at all.
  • Checking only GET. Someone else's object can often be changed or deleted while reading is protected.
  • Treating a UUID instead of a number as protection. It only makes guessing harder.
  • Checking the role only in the UI. The button is gone, but the endpoint answers 200.
  • Not checking the state after a 403. The response may be right while the side effect has already happened.

Check yourself

A test sends user B's token to DELETE /accounts/42 (A's account) and gets 403. What is the test still missing?

Answer

A check that account A still exists. A 403 is only what the server says: if the check was done after the deletion, the account is already gone. So after a negative call the resource is read by its owner.

In the application the "Delete user" button is not shown to a regular user. Is that enough protection?

Answer

No. The interface only hides the call, while the server accepts any request with a valid token. Call DELETE /admin/users/5 directly with a regular user's token and expect 403.

In an interview. Junior: The most important thing in API security is BOLA, or IDOR: with one user's token I request another user's resource and expect 403 or 404. I check it for all methods, and also admin functions under a regular role. Middle/Senior: Authentication says who you are, while authorization depends on data and has to be in every handler, so two test users are a must. After a negative call I read the resource as its owner to be sure there was no side effect. I also check token invalidation after logout and a password change, the login attempt limit, and sensitive actions without re-confirmation.

More articles in this chapter