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

QA Fundamentals › Manual API testing

How do you test authentication and authorisation in an API?

Authentication answers who the caller is (failure gives 401), authorization what they may do (failure gives 403). Authorization is checked at three levels: function, object (whose record) and field. A 200 with someone else's data is always a bug (BOLA, also called IDOR). A matrix of role × endpoint × whose resource × operation is built, and after a write the other user's resource is checked unchanged, since a 403 alone does not guarantee that.

QA Fundamentals · Manual API testing

A request with someone else’s ID returned 200 with data. Is it a bug or by design?

A tester checked API security: no token returns 401, an ordinary user on an admin method returns 403. All as planned. Then they put another user's order number in the address and got 200 and someone else's data. Authentication and role work, but access to an individual object does not. This lesson is about how authentication differs from authorisation, which three levels of checks the server has and how to build a test matrix that does not let others' data through.

How it works and why. Authentication answers "who are you?": a token, an API key, a session. A failure is 401 (per RFC 9110 a 401 response must carry a WWW-Authenticate header; for Bearer tokens RFC 6750 describes error="invalid_token"). Authorisation answers "what are you allowed to do?". A failure is 403: the identity is known but the action is forbidden. Authorisation has several levels, and each is needed separately, because a violation at each gives its own class of vulnerability. In the OWASP API Security Top 10 (2023) these are API1 Broken Object Level Authorization, API3 (the property level) and API5 Broken Function Level Authorization.

Three checks, in this order

1. Is the token valid?2. Does the role allow it?3. Is it their resource?200 and data401403403 or 404
Figure 1. The server asks in turn: who are you, what may you do and is it yours. The third check is the one most often missing.

The levels are: a function check (may the role perform this operation: an ordinary user does not call admin methods), an object check (does this record belong to the user: BOLA, informally IDOR) and a field check (may they see or change this field: not setting role: admin for themselves through the request body). For someone else's object the server returns 403 or 404. 404 is often chosen on purpose so as not to confirm that such a resource exists. Both are fine as long as the team has set a policy and follows it. What matters is something else: a 200 with someone else's data is always a bug.

Example: from three checks to a matrix. The first test plan had three lines: no token, an expired token, an ordinary user on an admin method. All passed. The fourth test added a new dimension, "resource owner":

# the token of user A (user-a), order 9001 belongs to user B
GET /api/orders/9001 HTTP/1.1
Authorization: Bearer <token of user-a>

HTTP/1.1 200 OK
{"id": 9001, "owner": "user-b", "total": 1280.00, "card": "**** 4242"}

This is a typical BOLA: the server checked the token and the role (an ordinary user is allowed to read orders) but did not compare whose record it is. The fix is obvious in code: the database query gets an owner condition. For the tester the conclusion is different: the matrix must contain not only "who" but also "whose resource" and "which operation". A minimal matrix for one resource:

RequestExpectation
No token401
An expired, forged or truncated token401
A token from another environment or for another service (audience)401
An ordinary user, an admin method403
User A reads, changes (PUT, PATCH) or deletes the resource of B403 or 404, the data is unchanged
A list of resources for user Aonly their records, none of someone else's
Their own resource with the right role200 and the right fields

A write test needs an extra step: after user A's request to the resource of B, check that the resource of B did not change (a GET as B), because a 403 response alone guarantees nothing if the change happened anyway.

Limits and alternatives. Tokens deserve separate attention. For a JWT you check the signature, the expiry (exp), the issuer (iss) and the audience (aud). After a user logs out, a short-lived access token that is validated without a store lookup usually stays valid until it expires. So "the token does not work after logout" depends on the implementation: check that the refresh token is revoked, and if the team promises immediate revocation, that a denylist works. There are other models too: API keys, cookie sessions (where the CSRF question is added) and OAuth 2.0 with scopes: a token with the read scope must not pass a write. Automating the matrix of roles and endpoints fits parameterised tests well. Manual checking remains for new endpoints. Brute force and lockout on login belong to security separately.

Common mistakes.

  • Checking only 401 and 403 at the function level: BOLA at the object level stays outside the tests.
  • Testing only reads: someone else's resource may be unreadable by GET but changeable via PUT or DELETE.
  • Taking a 403 as proof without checking the state: after the request the other user's resource must remain unchanged.
  • Substituting only sequential IDs: unpredictable UUIDs make enumeration harder but do not replace the owner check.
  • Mixing up 401 and 403: "no or invalid token" and "a token but no rights" are different bugs with different fixes.
  • Not checking lists and search: a single resource may be protected while the collection returns others' records.

Check yourself

A request with an ordinary user’s token to an admin endpoint returned 401 instead of 403. Is it a bug?

Answer

Most likely yes. The token is valid, the identity is established, so the correct response is 403: the action is forbidden. 401 means authentication failed. But first find out: is the token not expired and has the policy not changed (some services deliberately answer the same way so as not to reveal the API structure). If the specification says 403 and the API gives 401, that is a mismatch to raise as a bug.

User A sent a PUT to the resource of user B and got 403. Is that enough to conclude "protected"?

Answer

No. You need to make sure the resource of B did not change: a GET as B and, where possible, a database check. A 403 response may hide a change that has already happened if the owner check runs after the write or the access axis is broken in another method.

In an interview. Junior: I check requests with no token and with an expired token: it must be 401, and with a token without the needed rights 403. The most important thing is whether a user can get someone else's data by substituting another ID: that is an IDOR vulnerability. And that after logout the session cannot be renewed: the refresh token is revoked. Middle/Senior: I distinguish three levels of authorisation: function, object, field. I build a matrix "role × endpoint × whose resource × operation" and for writes I check that the other user's resource did not change. For a JWT I look at the signature, exp, aud and iss, and after logout I clarify how revocation is implemented: an access token often lives until it expires. I agree the 403 or 404 policy for others' resources with the team and check that it is the same on all endpoints.

More articles in this chapter