mufassall

Cheat sheet · Security

Authorization

12 min · Intermediate · mufassall.dtwin.uz/en/l/authorization/

← Open the lesson

The problem

Knowing who the caller is does not tell you what they may do. An invoicing app shared by many companies learns this the hard way: changed ids in the URL, forgotten checks, and a service account that could do anything.

What you learn

  • Authentication vs authorization, and why a valid token is not permission
  • Ownership checks on every request and the IDOR bug
  • RBAC vs ABAC, with one policy in one place
  • Least privilege for service accounts
Final scene
40%

Diagram as code

The lesson's final design as text: paste it into Jira, GitLab, GitHub, Notion or your docs, where Mermaid renders by itself. There are D2 and PlantUML (Confluence, IntelliJ) versions too.

Mermaid: flowchart

flowchart LR
  n_alice(("Alice (company c_7)"))
  n_web(["Web app"])
  n_api["Invoice API"]
  n_db[("Invoices DB")]
  n_bob(("Bob (company c_9)"))
  n_policy["Policy service"]
  n_reports("Report job")
  n_files[("Export files")]
  n_alice --> n_web
  n_web --> n_api
  n_api --> n_db
  n_bob --x n_api
  n_api --> n_policy
  n_reports --x n_db
  n_reports --> n_files

Mermaid: request sequence

sequenceDiagram
  participant n_web as Web app
  participant n_api as Invoice API
  actor n_bob as Bob (company c_9)
  participant n_db as Invoices DB
  participant n_policy as Policy service
  participant n_reports as Report job
  Note over n_web,n_api: A valid token, and that is all
  n_web->>n_api: GET /invoices/inv_1042 HTTP/1.1
  n_api-->>n_web: HTTP/1.1 200 OK
  Note over n_bob,n_api: Change the id in the URL, get someone else's invoice
  n_bob->>n_api: GET /invoices/inv_1043 HTTP/1.1
  n_api-->>n_bob: HTTP/1.1 200 OK
  Note over n_api,n_db: Check ownership in the query itself
  n_api->>n_db: SELECT id, amount, status FROM invoices
  n_db-->>n_api: (0 rows)
  Note over n_web,n_api: RBAC: what each role may do
  n_web->>n_api: POST /invoices/inv_1042/approve HTTP/1.1
  n_api-->>n_web: HTTP/1.1 403 Forbidden
  Note over n_bob,n_api: Forty endpoints, one forgotten check
  n_bob->>n_api: GET /invoices/export?company_id=c_7 HTTP/1.1
  n_api-->>n_bob: HTTP/1.1 200 OK
  Note over n_api,n_policy: One policy, asked from one place
  n_api->>n_policy: POST /v1/check HTTP/1.1
  n_policy-->>n_api: HTTP/1.1 200 OK
  Note over n_api,n_policy: ABAC: rules about attributes, not just roles
  n_api->>n_policy: POST /v1/check HTTP/1.1
  n_policy-->>n_api: HTTP/1.1 200 OK
  Note over n_reports,n_db: A report job with the keys to everything
  n_reports->>n_db: SET ROLE app_owner#59;
  n_db-->>n_reports: DELETE 18240
  Note over n_reports,n_db: Least privilege: only what the job needs
  n_reports->>n_db: SET ROLE reports_ro#59;
  n_db-->>n_reports: ERROR: permission denied for table invoices

D2

direction: right

n_alice: "Alice (company c_7)" {
  shape: person
}
n_web: "Web app" {
  shape: rectangle
}
n_api: "Invoice API" {
  shape: rectangle
}
n_db: "Invoices DB" {
  shape: cylinder
}
n_bob: "Bob (company c_9)" {
  shape: person
}
n_policy: "Policy service" {
  shape: rectangle
}
n_reports: "Report job" {
  shape: rectangle
  style.multiple: true
}
n_files: "Export files" {
  shape: stored_data
}

n_alice -> n_web
n_web -> n_api
n_api -> n_db
n_bob -> n_api {
  target-arrowhead.shape: cross
}
n_api -> n_policy
n_reports -> n_db {
  target-arrowhead.shape: cross
}
n_reports -> n_files

PlantUML: request sequence

@startuml
participant "Web app" as n_web
participant "Invoice API" as n_api
actor "Bob (company c_9)" as n_bob
database "Invoices DB" as n_db
participant "Policy service" as n_policy
participant "Report job" as n_reports
note over n_web, n_api : A valid token, and that is all
n_web -> n_api : GET /invoices/inv_1042 HTTP/1.1
n_api --> n_web : HTTP/1.1 200 OK
note over n_bob, n_api : Change the id in the URL, get someone else's invoice
n_bob -> n_api : GET /invoices/inv_1043 HTTP/1.1
n_api --> n_bob : HTTP/1.1 200 OK
note over n_api, n_db : Check ownership in the query itself
n_api -> n_db : SELECT id, amount, status FROM invoices
n_db --> n_api : (0 rows)
note over n_web, n_api : RBAC: what each role may do
n_web -> n_api : POST /invoices/inv_1042/approve HTTP/1.1
n_api --> n_web : HTTP/1.1 403 Forbidden
note over n_bob, n_api : Forty endpoints, one forgotten check
n_bob -> n_api : GET /invoices/export?company_id=c_7 HTTP/1.1
n_api --> n_bob : HTTP/1.1 200 OK
note over n_api, n_policy : One policy, asked from one place
n_api -> n_policy : POST /v1/check HTTP/1.1
n_policy --> n_api : HTTP/1.1 200 OK
note over n_api, n_policy : ABAC: rules about attributes, not just roles
n_api -> n_policy : POST /v1/check HTTP/1.1
n_policy --> n_api : HTTP/1.1 200 OK
note over n_reports, n_db : A report job with the keys to everything
n_reports -> n_db : SET ROLE app_owner;
n_db --> n_reports : DELETE 18240
note over n_reports, n_db : Least privilege: only what the job needs
n_reports -> n_db : SET ROLE reports_ro;
n_db --> n_reports : ERROR: permission denied for table invoices
@enduml

PlantUML: components

@startuml
left to right direction
actor "Alice (company c_7)" as n_alice
agent "Web app" as n_web
component "Invoice API" as n_api
database "Invoices DB" as n_db
actor "Bob (company c_9)" as n_bob
component "Policy service" as n_policy
collections "Report job" as n_reports
storage "Export files" as n_files
n_alice --> n_web
n_web --> n_api
n_api --> n_db
n_bob -[#c62828]-> n_api
n_api --> n_policy
n_reports -[#c62828]-> n_db
n_reports --> n_files
@enduml

Recap

Four rules for authorization.

1) A valid token is identity, not permission. 2) Check ownership on every request, with the tenant from the token, and answer 404 for foreign ids. 3) Keep one policy in one place, deny by default: RBAC for roles, ABAC for conditions. 4) Give every service account its own role with only the permissions it needs.

Numbers: at the start and at the end

MetricStartEnd
Foreign records read0/day0/day
API p9545 ms54 ms
Job permissions4123

Steps

  1. Case A valid token, and that is all

    Alice from company c_7 opens invoice inv_1042.

  2. Problem Change the id in the URL, get someone else's invoice

    Bob works at another company, c_9.

  3. Fix Check ownership in the query itself

    The API now loads an invoice only together with the caller's company, taken from the token, never from the URL.

  4. Case RBAC: what each role may do

    Inside a company not everyone may approve invoices.

  5. Problem Forty endpoints, one forgotten check

    The ownership check is copied into every endpoint by hand.

  6. Fix One policy, asked from one place

    All rules move to one policy: given a subject, an action and a resource, answer allow or deny.

  7. Fix ABAC: rules about attributes, not just roles

    New rules: an accountant may approve invoices only up to 50 million, never one they created, and only during work hours.

  8. Problem A report job with the keys to everything

    A nightly job builds export files.

  9. Fix Least privilege: only what the job needs

    The job gets its own role, reports_ro: read invoices, write only to its temp table, and put files only under its own folder in storage.

  10. Recap Recap

    Four rules for authorization.

Terms

RBAC and ABAC
RBAC grants permissions by role, ABAC by rules about attributes of the user, the resource and the context. Both live in one policy in one place, denying by default.
IDOR (insecure direct object reference)
The server returns a record by the id in the request but never checks that it belongs to this user. Changing a number in the URL opens someone else's order or document.