System Design · 07. Security · 8 min · Beginner

Authorization

After we know who you are, decide what you are allowed to do, on every request.

In short

Authentication says who; authorization says what, and it is checked on the server for every request. Roles keep the rules short (5,060 entries instead of 100,000), attributes handle the details, and an ownership check keeps Ann to her 3 invoices out of 10,000.

Check at the gateway and in the service, give each credential the least it needs, and log every decision.

Key ideas

  • Authentication: who. Authorization: what. They are two different checks and both are needed. (step 1)
  • Defence in depth: the gateway stops the obvious, the service has the last word. (step 6)

Good to know

  • RBAC is easy to explain and audit. Its weakness is role explosion when rules get specific, such as 'editor, but only for Europe'. (step 3)
  • Many systems mix both: roles for the broad rights, attributes for the details. (step 4)
  • Ask 'what is the worst thing this credential can do?' and make that answer small. (step 7)

Watch out

  • Deny by default: if no rule says yes, the answer is no. Forgetting a rule should lock a door, not open it. (step 2)
  • Random-looking ids do not protect you. The check must be in the code, never in the hope that nobody guesses. (step 5)
  • Logs help only if someone looks. Alert on bursts of denials and on use of admin rights. (step 8)
Ann's browserAPIDatabaseAudit log

Key numbers

Decision
Allow / Deny
Entries to manage
5,060
Invoices Ann can read
3

Terms

Authorization
Deciding what an identified user may do, as opposed to authentication, which says who they are.
Deny by default
If no rule allows an action, the answer is no.
403 Forbidden
The HTTP status for a request from a known caller who is not allowed to do this.
RBAC (role-based access control)
Permissions belong to roles, and people are given roles.
ABAC (attribute-based access control)
Rules decide from attributes of the user, the resource and the situation.
Ownership check
Making sure the resource belongs to the caller, for example with owner_id in the query.
Broken object-level authorization
The bug where a user can read or change other users' objects by changing an id.
Defence in depth
Checking at several layers, such as the gateway and the service.
Least privilege
Giving each person, service or token only the rights it needs.
Audit log
An append-only record of who did what and whether it was allowed.