← Foundations path
.NETFoundations 28 / 37Reported by candidates

Resource-based authorization and BOLA

“How do you make sure a user can only read or change their own records?”

Checking the user against the record they asked for, after loading it. Skipping this check is the most common API hole: BOLA (Broken Object Level Authorization).

See it

1/7Dealer B, signed in with a valid token, changes the id in the address: loan 7, which is dealer A's.

In plain words

[Authorize] and roles answer "who are you, and what kind of user are you?". They run before your action, when the record has not been loaded yet, so they can't know which record the user asked for. Say a dealer user may read loan applications. Dealer B asks for GET /api/applications/7, and application 7 belongs to dealer A. The role check passes: B is a dealer. If nothing else checks, B reads A's customer. This hole is called BOLA, Broken Object Level Authorization: the API checks the user, but not the object (the record). It is number one on the OWASP API Security Top 10, a well-known list of the most common API holes. The fix is resource-based authorization: load the record first, then ask .NET's "may this user use this record?". Three pieces do the work. A policy is a named rule (here "SameDealer"); the controller asks for the rule by its name. A requirement is an empty class that stands for the rule. A handler is the class that runs the check: it compares a claim on the user (dealer_id) with a field on the record (). If they differ, the check fails and the controller returns 404 Not Found, not 403, so the API does not even confirm the record exists.

Say it in the interview

In the lending API

The controller, the handler, then the set-up. Program.cs runs once at start-up, so the walk goes back to the check step there. Tap Next line to walk dealer B's request.

Lending.sln · where these 3 files sit
  • src/Api/
    • Controllers/
    • Auth/
Diagram · step 2[Authorize(Roles = "Dealer")] passes: B really is a dealer. It can't know whose loan 7 is.
LoansController.cs · line 2 · 1 of 4Checks the kind of user only. It runs before the loan is loaded.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

Read more: Resource-based authorization in ASP.NET Core · OWASP API Security Top 10 (2023): API1 Broken Object Level Authorization