Secure administration: permissions by action, not role

Why an “if role === admin” check eventually fails and how to design permissions by module and action, verify them on the server and separate work from configuration.

Tomáš Renčík5 min read
Illustration for the article: Secure administration: permissions by action, not role
Contents
  1. 1. Permissions by action
  2. 2. Separate work from configuration
  3. 3. Always check on the server
  4. 4. Data belongs to a company, not just to a module
  5. 5. When a role by name is fine
  6. 6. Test it like an attacker
  7. 7. Practice exercise
  8. 8. Frequently asked questions
  9. 9. Beware of “shortcuts” in the interface
  10. 10. In closing
  11. 11. Related articles

In short: Secure administration doesn't ask “is this an admin?” but “may this person perform this action in this module?”. Design permissions as module + action (for example view, edit, delete, manage settings), always check them on the server, and separate configuring a module from everyday work.

In almost every admin panel I've seen, this turns up somewhere in the code:

if (user.role === 'admin') {
  // show settings
}

At the start that's fine. The problem comes when the company says: “We want a Technician role that may edit tickets but not change their settings.” Or “The sales manager should see invoices but not delete them.” Suddenly you have nowhere to record that.

Permissions by action

A better model: permission = module + action.

{
  "tikety":  ["zobrazit", "vytvorit", "upravit"],
  "faktury": ["zobrazit"],
  "crm":     ["zobrazit", "vytvorit", "upravit", "zmazat", "spravovat"]
}

A role is just a named bundle of permissions. “Technician”, “Salesperson” or “Accountant” — the code knows nothing about their names. It only knows whether the user has a given action for a module.

The check in the code then looks like this:

await vyzadujPravo(event, 'faktury', 'upravit')

Separate work from configuration

The most common mistake I fix: whoever may edit a record automatically may also configure the module — change statuses, e-mail templates, numbering sequences. For an ordinary person, these are dangerous buttons they don't need and that confuse them.

The solution: a separate manage action. Whoever doesn't have it doesn't see the cog icon or the settings pages. The user interface instantly becomes calmer and safer.

When introducing a new permission, make sure nothing changes for anyone after deployment: every role that could edit until now also gets manage automatically. It can be narrowed down afterwards, deliberately.

Always check on the server

Hiding a button in the interface is not security. It's convenience. Anyone who knows the API address can send the request without the button.

So the rules are:

  • The server checks the permission on every request. Always.
  • The interface only uses permissions to hide things the person cannot use — so they don't click into the void.
  • Both must use the same rule. If the interface shows a settings tab that the server rejects, the person clicks and nothing happens. If the interface hides the tab but the server allows it, you have a hole you don't know about.

Data belongs to a company, not just to a module

In multi-company systems, it isn't enough to know that a person may “view invoices”. They must be allowed to view them in this company. So filter every database query by the companies the user has access to as well:

SELECT * FROM faktury WHERE firma_id IN (?, ?) AND id = ?

A classic mistake: a record's detail is loaded by id alone. Anyone who changes the number in the address gets to see someone else's invoice.

When a role by name is fine

There are things that genuinely belong only to the owner of the system: licences, switching modules on, backups of the whole system. These can be tied to a single super-role. But there are few of them, and they are the exception, not the rule.

Test it like an attacker

Every so often, create a role with minimal permissions and try to:

  1. open directly the addresses of pages it can't see in the menu,
  2. send requests to APIs it has no permission for,
  3. change the id in the address to a record from another company.

All of it must end in a rejection (403 or 404), not in data.

Practice exercise

Design permissions for a small “Bike Service” application with the modules Jobs, Inventory and Invoices:

  1. List the actions for each module (view, create, edit, delete, manage).
  2. Put together three roles: Mechanic, Salesperson, Owner.
  3. For each role, write down what it may do — and above all, what it may not do and why.
  4. On the server, write a single function vyzadujPravo(modul, akcia) and use it in every endpoint.
  5. As the Mechanic, try to get to the invoices — through the interface and directly via the API.

Frequently asked questions

Where should permissions be stored?

The simplest is in the database with the role, for example as JSON. Load them when checking and cache them briefly so they don't have to be read on every request.

What if one person has different permissions in different companies?

Then the role belongs not just to the user but to the pair user + company. It's worth thinking about this before the system grows.

Beware of “shortcuts” in the interface

A common bug arises when the interface has its own shortcut that the server doesn't know about. For example: “an admin sees all settings tabs”. The server, however, requires the manage permission for a specific page. An admin without it clicks the tab and the page sends them back — nothing happens and no one knows why.

The solution is simple: the interface and the server both ask about the same permission using the same function. If you need an exception for the super-role, make it the same in both places. Such inconsistencies are easy to find with a test using a role that has exactly one permission — you'll immediately see what is shown and what actually works.

In closing

Permissions by action are a little more work at the start. They pay off on the day a customer says “and we'd like one more role” — and you just tick boxes instead of rewriting code.

  • #bezpečnosť
  • #práva
  • #administrácia
  • #backend
  • #návrh