Authorization

  • Lesson 33 of 40
  • 13 min
  • Intermediate
  • 13 steps

Authentication answers “who are you?”; authorization answers “may you do this, to this data, right now?”. And it must be asked on every request, in every service. Names, invoice numbers and amounts in the scene are example values.

Others’ data exposed
no
Permission checks
central policy
Service access
least
Sofía · AndinaInvoicesAPIInvoices APIdatabaseBruno · AustralPolicyPolicyReportsReportsaudit loginvoice 2208 · Andina · $31,500checks: logged in? yesGET /invoices/2208 → Andina’s!checks: owner = caller’s org/invoices/2208 → 404Scope every querySELECT * FROM invoicesWHERE id = 2208AND org_id = :caller_orgBruno (Austral) → 0 rows → 404PATCH /me { "role": "admin" }saves every field sentbruno · role = adminAllow-list writable fieldsbeforeupdate(u, body)afterpick(body, name, email)roleadmin-only endpointrow-level security: org_idSET app.org = caller’s orgAustral rows onlyThe database filters tooENABLE ROW LEVEL SECURITYCREATE POLICY per_org ON invoicesUSING (org_id = current_org())→ only the caller’s rowscurrent_org(): example function reading app.orgrole: accountantrole allows “edit”? yesRoles → permissionsviewerreadaccountantread · editapproverread · approveadmineverything + usersmember of team Financefollows the relationship chainWhy can Sofía view 2207?invoice 2207is infolder Q3folder Q3shared withFinanceFinancehas membersofía→ sofía may view 2207role: accountantallow: edit · deny: approvemay Sofía edit 2207?admin credentials · injection bugusers · payments · invoices leakedread-only · invoices viewusers table: deniedreport includes Austral’s invoices!own identity · every tenantrun reportonly Andina’s invoicesacts as Sofía · Andina onlySofía’s tokensofía viewed 2207 · 09:14every access recordedmay Sofía edit 2207?every access recorded
  • request
  • response
  • failed request
  • event

Step 1 of 13

Problem

Signed in does not mean allowed

Bruno works at Austral and is a legitimate user of the invoicing app. He opens his invoice at /invoices/2207, changes the number to 2208, and an invoice from another company, Andina, shows up.

The API checked that Bruno was signed in, but never that the invoice was his. This flaw is called broken object-level authorization (BOLA), and it tops the OWASP list of API security risks.

Step 2 of 13

Solution

Check ownership on every request

Every lookup is scoped to whoever is asking: fetch invoice 2208 only if it belongs to the caller’s organisation. For Bruno the query returns nothing and the API answers 404 Not Found.

Deny by default: if an endpoint forgets the check, it must fail closed, never open.

What changed

  • Every query filtered by the caller’s organisation
  • Bruno’s request gets 404

Step 3 of 13

Problem

Case: one field too many in the JSON

Bruno edits his own profile, which is allowed. But the API takes the whole JSON body and saves every field in it. Bruno adds "role": "admin" to the request and promotes himself.

This is mass assignment. The fix: each endpoint declares the fields it may change, and the role changes only through an endpoint reserved for administrators.

What changed

  • Bruno sends an extra “role” field
  • The API saves it and Bruno becomes admin
  • Fix: an allow-list of writable fields

Step 4 of 13

Technique

Let the database separate tenants too

The ownership check lives in application code, so one query that forgets AND org_id = … brings the leak back. Add a second layer: row-level security in the database.

The app tells each connection which organisation it serves, and the database filters every query by it. Even SELECT * FROM invoices returns only the caller’s rows.

What changed

  • Row-level security on the invoices table
  • The app sets the organisation on each connection

Step 5 of 13

Technique

Roles: what each kind of user may do

Inside Andina people need different things: some only look at invoices, some edit them, and a few approve the large ones. Instead of granting permissions person by person, group them into roles and give each person one. This is role-based access control (RBAC).

Sofía is an accountant: she can edit invoices but not approve them.

What changed

  • Roles: viewer, accountant, approver and admin

Step 6 of 13

Technique

Relationships: sharing and teams

Roles fall short for sharing: “Sofía sees this folder because it was shared with her team”. Relationship-based access control (ReBAC) stores single facts such as “invoice 2207 is in folder Q3”, “Q3 is shared with Finance” and “Sofía is a member of Finance”, and walks the chain to decide.

This is how shared documents work in cloud office suites, across millions of objects.

What changed

  • Permissions derived from relationships

Step 7 of 13

Technique

Decide in one place

When checks are if statements spread over twenty services, they drift apart and nobody can answer “who may approve invoices?”. Instead, each service asks a policy service: “may Sofía edit invoice 2207?”, and enforces the yes or no.

The rules live in one place, are tested like the rest of the code, and every decision can be logged.

What changed

  • Central policy service
  • The API asks, then enforces

Step 8 of 13

Problem

Case: a service with a key to everything

The reports service connects to the database with the administrator account, because it was quickest to set up. One day a SQL injection bug turns up in a report filter.

Since that service could read every table, the attacker also walks away with users, password hashes and payment data, not just invoices.

What changed

  • Reports service with admin credentials
  • The injection exposes every table

Step 9 of 13

Solution

Least privilege

Every service, and every person, gets only the access its job needs. Reports now uses a read-only account that sees the invoices view and nothing else; each service has its own credentials.

The same injection bug now exposes at most what reports could already read, and any attempt on another table fails and raises an alert.

What changed

  • Reports: read-only, invoices view only
  • Separate credentials for each service

Step 10 of 13

Problem

Case: the confused deputy

Sofía asks for a report of invoices over $10,000. The API hands the job to the reports service, which queries with its own identity, and that identity can read every organisation’s invoices. Sofía’s report includes Austral’s invoices.

Reports used its authority on someone else’s behalf without checking what she may see: a confused deputy.

What changed

  • The report runs with the service’s identity
  • Sofía sees other organisations’ invoices

Step 11 of 13

Solution

Act as the user, not as yourself

The user’s identity travels with the request: the API forwards Sofía’s token, or swaps it for a narrower one, and the reports service queries as Sofía, with her organisation and her permissions.

The service’s own credentials are kept for work no user asked for, such as nightly maintenance.

What changed

  • The API passes Sofía’s token to reports
  • The query runs with Sofía’s permissions
  • Other organisations’ data

Step 12 of 13

Technique

Audit: who saw what

Finally, record every access to sensitive data (who, what, when and from where) in an append-only audit log that none of the people being audited can edit.

It answers “who looked at this customer’s invoices?” after an incident, and it powers alerts: a support agent opening 3,000 invoices in 10 minutes deserves an alarm.

What changed

  • Append-only audit log
  • Alerts on unusual access

Step 13 of 13

Summary

Recap

On every request, check that the caller may touch that object.

Roles for kinds of users; relationships for sharing.

Decide in a central policy and enforce in each service.

Limit writable fields and add row-level security as a second layer.

Grant least privilege, act as the user rather than the service, and keep an audit log.

You will learn

  • Broken object-level authorization
  • Checking ownership and limiting writable fields
  • Row-level security in the database
  • Roles, relationships and a central policy
  • Least privilege and the confused deputy
  • Audit logs