CHAPTER 08
Intermediate
Authorization and Access Control
Updated: May 13, 2026
20 min read
# CHAPTER 8
Authorization and Access Control
1. Introduction
Authentication proves *who* you are. Authorization determines *what* you are allowed to do. Once your API has validated a user's JWT or session, the security battle is only half won. If an authenticated user attempts to executeDELETE /api/users/99, the API must rigorously check if the user has the explicit right to delete user 99. In this chapter, we will explore Access Control logic, focusing on Role-Based Access Control (RBAC) and mitigating Broken Object Level Authorization (BOLA), the number one vulnerability in APIs today.
2. Learning Objectives
By the end of this chapter, you will be able to:- Clearly define Authorization logic.
- Understand and implement Role-Based Access Control (RBAC).
- Identify Broken Object Level Authorization (BOLA / IDOR) vulnerabilities.
- Write secure PHP logic to verify ownership before modifying resources.
- Apply the principle of "Least Privilege".
3. Beginner-Friendly Explanation
Imagine a high school.- Authentication: The security guard checks your ID badge at the front door. You are verified as a student. You are allowed inside.
- Authorization: You walk down the hallway and try to open the door to the Principal's Office. The door is locked. Your student badge doesn't work here; you need an "Admin" badge. Furthermore, if you go to the locker room, your student badge only opens *your specific locker*, not the locker next to it.
Authorization in APIs is building the locks for the Principal's Office (Role validation) and building the locks for the individual lockers (Ownership validation).
4. Real-World Attack Scenarios
-
BOLA (Broken Object Level Authorization): A popular ridesharing app had an endpoint
GET /api/receipts?receiptid=1001. A user logged in, viewed their own receipt, and noticed the ID in the URL. They changed the URL to?receiptid=1002. Because the API failed to check if receipt 1002 *belonged* to the logged-in user, the API happily returned a stranger's ride history and credit card details.
-
Privilege Escalation: A user creates an account. They intercept the POST request and add
"role": "admin"to the JSON body. The API blindly saves the data. The standard user is now an administrator and deletes the database.
5. Access Control Models
-
1.
RBAC (Role-Based Access Control): Users are assigned roles (e.g.,
User,Editor,Admin). Endpoints are restricted based on these roles.
- 2. ABAC (Attribute-Based Access Control): More complex. Access is granted based on attributes (e.g., "Managers can only edit documents created in their specific department during business hours").
6. Vulnerable vs Secure Code Examples (BOLA/IDOR)
The most common and devastating API flaw occurs when developers trust the ID provided in the URL without checking ownership.Vulnerable PHP (BOLA Flaw):
php
Secure PHP (Ownership Validation):
php
7. PHP Examples (Role-Based Access)
Securing an administrative endpoint.
php
8. The Principle of Least Privilege
Always grant the minimum level of access required to complete a task.-
If an API key is used by a script to backup database files, that key should only have
READaccess, neverDELETEaccess.
- If a user is a "Guest", they should not have access to endpoints that modify data.
9. Best Practices
- Never Trust the Client: The frontend UI hiding the "Delete" button is an illusion. Hackers bypass the UI. Authorization MUST be enforced on the backend.
-
Use UUIDs instead of Sequential IDs: If your API uses sequential IDs (
/users/1,/users/2), it is trivial for a hacker to guess IDs and test for BOLA vulnerabilities. If you use UUIDs (/users/550e8400-e29b-41d4-a716-446655440000), they are impossible to guess, adding an extra layer of defense (though ownership checks are still mandatory).
-
Centralize Authorization Logic: Don't scatter
if ($user == owner)statements throughout hundreds of files. Create a centralized middleware or function (e.g.,checkownership($resourceid, $userid)) to handle it uniformly.
10. Common Mistakes
- Confusing 401 and 403:
-
401 Unauthorizedmeans "I don't know who you are. Please log in."
-
403 Forbiddenmeans "I know who you are, but you are not allowed to do this."
-
Mass Assignment: Failing to define which fields a user is allowed to update. If your
UPDATEquery blindly takes all JSON data and writes it to the database, a user can update theirbalanceorrolefields.
11. Mini Exercises
- 1. Define the acronym RBAC.
- 2. If an API uses long, complex UUIDs instead of sequential numbers for user IDs, do you still need to write backend code to verify ownership? (Yes or No).
12. Practice Challenges
Challenge: Review the following vulnerable SQL query designed to fetch a user's private message based on an ID from the URL:"SELECT content FROM messages WHERE messageid = " . $GET['id']
Rewrite this query conceptually to include a check against $currentuserid to prevent a BOLA attack.
13. MCQs with Answers
Question 1
What is the difference between Authentication and Authorization?
Question 2
An attacker changes a URL from /api/invoice/10 to /api/invoice/11 and successfully downloads another user's invoice. What is this vulnerability called?
Question 3
What HTTP status code should an API return if a logged-in User attempts to access an Admin-only endpoint?
14. Interview Questions
- Q: What is Broken Object Level Authorization (BOLA), and how is it mitigated at the code level?
- Q: Explain Role-Based Access Control (RBAC). How would you architect this in a MySQL database?
- Q: What is Mass Assignment, and how can it lead to Privilege Escalation?
15. FAQs
Q: Should I return a 403 Forbidden or a 404 Not Found if a user tries to access a document they don't own? A: Returning a 403 tells the attacker "This document exists, but you can't see it." In highly secure systems, it is often better to return a 404 Not Found. This hides the existence of the document entirely, preventing attackers from probing to see which IDs exist in your database.16. Summary
In this chapter, we tackled the critical logic of Authorization. We explored Role-Based Access Control to separate Admins from Users, and tackled Broken Object Level Authorization (BOLA)—the catastrophic flaw where developers blindly trust IDs provided by the client. We learned that robust API security requires appending ownership checks (e.g.,WHERE ownerid = ?) directly into our database queries, strictly enforcing the principle of least privilege.