Authorization & Access Control
Once you know who a user is, authorization decides what they may do. Broken access control is number one in the OWASP Top 10: it is the most common serious web vulnerability. The problem is rarely clever cryptography; it is a missing check in one of hundreds of endpoints.
Principles
- Least privilege: give each user, service and process only the permissions it needs, for only as long as it needs them.
- Deny by default: if no rule grants access, the answer is no.
- Check on every request, on the server: hiding a button in the UI is not access control.
- Separation of duties: no single person can complete a sensitive process alone (one person creates a payment, another approves it).
- Centralise the logic: one well-tested authorization function, not ad-hoc
ifstatements scattered through the code.
Access control models
| Model | Decision based on | Example | Good for |
|---|---|---|---|
| DAC (discretionary) | The owner grants access | Sharing a document with a colleague | File systems, collaboration |
| MAC (mandatory) | System-wide labels and clearances | "Secret" documents for "Secret"-cleared staff | Military, high-assurance OSs |
| RBAC (role-based) | The user's roles | Editors can publish, viewers can read | Most business apps |
| ABAC (attribute-based) | Attributes of user, resource, action, context | Doctors may read records of patients in their own ward during their shift | Fine-grained, dynamic rules |
| ReBAC (relationship-based) | Relationships in a graph | You can see photos shared with your friends | Social apps, Google Zanzibar-style systems |
RBAC
In role-based access control permissions are assigned to roles, and roles to users. Changing someone's job means changing their roles, not dozens of individual permissions.
ROLE_PERMISSIONS = {
"viewer": ["article:read"],
"editor": ["article:read", "article:write"],
"admin": ["article:read", "article:write", "article:delete", "user:manage"],
}
USER_ROLES = {
"alice": ["admin"],
"bob": ["editor"],
"carol": ["viewer"],
"dave": [],
}
def can(user, permission):
roles = USER_ROLES.get(user, []) # unknown user -> no roles
return any(permission in ROLE_PERMISSIONS.get(role, []) for role in roles)
checks = [("alice", "user:manage"), ("bob", "article:write"), ("bob", "article:delete"),
("carol", "article:read"), ("dave", "article:read"), ("mallory", "article:read")]
for user, perm in checks:
print(f"{user:<8} {perm:<15} {'ALLOW' if can(user, perm) else 'deny'}")alice user:manage ALLOW
bob article:write ALLOW
bob article:delete deny
carol article:read ALLOW
dave article:read deny
mallory article:read deny
dave (no roles) and mallory (unknown) are denied without any special code: that's deny-by-default.
ABAC
RBAC can't easily express "only during working hours" or "only your own department's records". Attribute-based access control evaluates policies over attributes of the subject, resource, action and environment:
POLICIES = [
{
"name": "doctors read records in their ward during shift",
"effect": "allow",
"when": lambda s, r, a, env: (a == "read" and s["role"] == "doctor"
and s["ward"] == r["ward"]
and s["shift_start"] <= env["hour"] < s["shift_end"]),
},
{
"name": "patients read their own record",
"effect": "allow",
"when": lambda s, r, a, env: a == "read" and s["role"] == "patient" and s["id"] == r["patient_id"],
},
{
"name": "nobody edits a locked record",
"effect": "deny",
"when": lambda s, r, a, env: a == "write" and r["locked"],
},
]
def decide(subject, resource, action, env):
matched = [p for p in POLICIES if p["when"](subject, resource, action, env)]
if any(p["effect"] == "deny" for p in matched): # deny overrides allow
return "deny"
return "allow" if matched else "deny (no policy)"
record = {"patient_id": "p17", "ward": "cardiology", "locked": True}
dr_lee = {"id": "d3", "role": "doctor", "ward": "cardiology", "shift_start": 8, "shift_end": 20}
dr_kim = {"id": "d4", "role": "doctor", "ward": "oncology", "shift_start": 8, "shift_end": 20}
patient = {"id": "p17", "role": "patient"}
print("Dr Lee, 10:00, read: ", decide(dr_lee, record, "read", {"hour": 10}))
print("Dr Lee, 23:00, read: ", decide(dr_lee, record, "read", {"hour": 23}))
print("Dr Kim, 10:00, read: ", decide(dr_kim, record, "read", {"hour": 10}))
print("patient, read own: ", decide(patient, record, "read", {"hour": 23}))
print("Dr Lee, 10:00, write:", decide(dr_lee, record, "write", {"hour": 10}))Dr Lee, 10:00, read: allow
Dr Lee, 23:00, read: deny (no policy)
Dr Kim, 10:00, read: deny (no policy)
patient, read own: allow
Dr Lee, 10:00, write: deny
Two common conventions are visible: deny overrides allow, and no matching policy means deny. Policy engines such as Open Policy Agent (OPA) and AWS IAM work on the same principles.
IDOR: the missing ownership check
Insecure Direct Object Reference (IDOR) is the most common access-control bug. An endpoint like GET /api/invoices/1043 checks that the user is logged in but not that invoice 1043 is theirs. Changing the number reveals other customers' invoices.
INVOICES = {
1042: {"owner": "alice", "total": 120.00},
1043: {"owner": "bob", "total": 89.50},
}
def get_invoice_vulnerable(user, invoice_id):
if user is None:
return "401 login required"
inv = INVOICES.get(invoice_id)
return inv if inv else "404"
def get_invoice_fixed(user, invoice_id):
if user is None:
return "401 login required"
inv = INVOICES.get(invoice_id)
if inv is None or inv["owner"] != user:
return "404" # same answer as "doesn't exist": don't reveal it exists
return inv
print("vulnerable:", get_invoice_vulnerable("alice", 1043))
print("fixed: ", get_invoice_fixed("alice", 1043))
print("fixed: ", get_invoice_fixed("alice", 1042))vulnerable: {'owner': 'bob', 'total': 89.5}
fixed: 404
fixed: {'owner': 'alice', 'total': 120.0}
Random, unguessable IDs (UUIDs) make IDOR harder to exploit but are not a fix: IDs leak through logs, emails and shared links. The ownership check is the fix.
Testing access control
Write tests that try each sensitive endpoint as each kind of user (anonymous, normal user, other user, admin) and assert the expected allow/deny. A table of expected outcomes, checked automatically on every build, catches the "forgot the check on the new endpoint" mistake.
Go further: IDOR and BOLA shows the missing ownership check on a toy API with its detection and fix, and Function-Level Access Control covers the role side.
Test yourself
An API returns any order by ID to any logged-in user. What is this vulnerability called?
- CSRF
- IDOR / broken object-level authorization
- XSS
- SQL injection
What does 'deny by default' mean in an authorization system?
- All users are blocked forever
- Access is refused unless a rule explicitly grants it
- Admins are denied first
- Denied requests are logged
Which model best expresses 'nurses may view records of patients in their own ward during their shift'?
- DAC
- RBAC alone
- ABAC
- No model can