Test API authorization: can user B read user A's data?

Authentication asks "who are you?". Authorization asks "may you touch this?". Most APIs get the first right and slip on the second: GET /invoices/1842 checks that the token is valid, but not that invoice 1842 belongs to the caller. Change the ID, read someone else's data. It tops the OWASP API Security list as Broken Object Level Authorization (BOLA), and it's easy to test once you set up two users.

Set up two users

Add both tokens to the environment as secret variables:

VariableValue
tokenAuser A's token (account 1)
tokenBuser B's token (account 2)

Use users in different accounts or organizations. Two users in the same team may legitimately share data, which hides the bug you're looking for.

Make an object as A

HTTP
POST {{baseUrl}}/invoices
Authorization: Bearer {{tokenA}}
Content-Type: application/json

{ "customer": "ACME", "amount": 1200 }
JavaScript
pm.test('A created an invoice', () => pm.response.to.have.status(201));
pm.environment.set('invoiceIdA', pm.response.json().id);

Touch it as B

Put the attacks in a folder whose auth is Bearer {{tokenB}}, so every request inherits user B. Then one request per operation:

Output
GET    {{baseUrl}}/invoices/{{invoiceIdA}}
PATCH  {{baseUrl}}/invoices/{{invoiceIdA}}           { "amount": 1 }
DELETE {{baseUrl}}/invoices/{{invoiceIdA}}
GET    {{baseUrl}}/invoices/{{invoiceIdA}}/pdf
POST   {{baseUrl}}/invoices/{{invoiceIdA}}/send

Put one test in the folder's post-response script, so every request in it checks the same thing:

JavaScript
pm.test('B is refused', () => {
  pm.expect(pm.response.code).to.be.oneOf([403, 404]);
});

404 is often the better answer: it doesn't confirm the object exists. Pick one for your API and assert it exactly once you've decided.

Then prove it's still there

The DELETE as B must not have deleted anything. Finish with A:

JavaScript
// GET {{baseUrl}}/invoices/{{invoiceIdA}} with tokenA
pm.test('A still has the invoice, unchanged', () => {
  pm.response.to.have.status(200);
  pm.expect(pm.response.json().amount).to.equal(1200);
});

A 403 on the PATCH means nothing if the amount changed anyway.

Places the bug hides

  • Lists and search. GET /invoices?customerId=... with A's customer ID, as B. The list should be empty or refused, not A's invoices.
  • IDs in the body. POST /payments { "invoiceId": "{{invoiceIdA}}" } as B. Checks on path parameters often don't run on body fields.
  • Nested resources. /accounts/{{accountIdB}}/invoices/{{invoiceIdA}}: B's own account in the path, A's invoice at the end.
  • Other methods. GET is checked, PUT was added later and isn't.
  • Exports and files. PDF, CSV and attachment URLs are frequently served by a different handler.
  • Function-level checks. A regular user calling admin endpoints: /admin/users, ?role=admin in a body, "isAdmin": true on profile update.

Keep it in CI

Once the folder exists, it costs nothing to run on every build:

Terminal
requestarium run-collection "Invoices API" --folder "Authorization" --env Staging --junit authz.xml

New endpoint? Add one request to the folder. The folder test does the rest.