Skip to content

Web security basics · Part 10 of 10

Least privilege and role-based access control

Give every user and service only the access it needs. Roles make permissions manageable, and server-side checks make them real.

· 2 min read

securityauthorization

The principle of least privilege says that a person, a process or a service should have only the permissions it needs to do its job, and no more. When an account is compromised or a mistake is made, the damage is limited by what that account could do.

Authentication vs authorisation

Authentication answers "who are you?". Authorisation answers "what may you do?". Many serious bugs come from doing the first correctly and forgetting the second.

Role-based access control

Instead of assigning permissions to each user, define roles that bundle permissions, and assign roles to users. A viewer can read, an editor can read and change content, an admin can also manage users. Roles keep the model small and make audits easier. When roles are not enough, for example "authors may edit only their own posts", combine them with ownership rules or attribute-based checks.

Ruby
PERMISSIONS = {
  viewer: [:read],
  editor: [:read, :write],
  admin:  [:read, :write, :manage_users]
}.freeze

def allowed?(user, action, resource)
  return false unless PERMISSIONS.fetch(user[:role], []).include?(action)
  # Ownership rule: editors may only change their own records.
  return true if user[:role] == :admin || action == :read
  resource[:owner_id] == user[:id]
end

puts allowed?({ id: 1, role: :editor }, :write, { owner_id: 1 })
puts allowed?({ id: 1, role: :editor }, :write, { owner_id: 2 })

Enforce on the server, every time

  • Hiding a button in the interface is convenience, not security. The check must run on the server for every request.
  • Deny by default. A new endpoint should be forbidden until a rule allows it.
  • Beware of insecure direct object references: GET /invoices/1234 must verify that invoice 1234 belongs to the caller, not only that the caller is logged in.

Apply it beyond users

  • Database accounts with only the tables and operations they need.
  • Cloud roles scoped to specific resources.
  • Tokens with narrow scopes and short lifetimes.
  • Containers running as a non-root user.

Review regularly

Permissions accumulate. Review who has access, remove accounts that left, and expire temporary elevated access automatically. Log sensitive actions so you can answer "who did this?".

Takeaway

Define roles, check them on the server, deny by default and prune access over time. A small blast radius turns many incidents into minor events.

A knowledge article, drafted with AI assistance and checked with automated tests. It is general background, not a description of my own experience or of any client's work.

Comments

Leave a comment

No account or email needed. Comments appear after review. A one-way hash of your IP address is kept for 30 days to fight spam, then erased.