Agent Wranglers

ACC-34Required

Check a named permission on the server for every action

Check on the server, for every action, whether the user has a named permission such as invoice.refund. Map roles to permissions in one place, and never compare role names in feature code. Hiding a button is not a check.

How to do it

Write one list of permissions, each an action on a kind of record. Write one map from each role to its permissions, in code or in a table. Feature code calls one function, such as can(membership, 'invoice.refund'). A new or changed role is then one edit. Run the check in one place for each route, such as middleware on the whole admin area, and test each action, not only each page. An unknown role or permission gets no, as deny by default and when a check fails says. Anyone can call the API without the screen, with a browser's developer tools or a script. The interface may hide buttons with the list from Let the server say what each user may do, but the server check is what counts.

Examples

JavaScript example

const ROLE_PERMISSIONS = {
  owner: ['invoice.read', 'invoice.create', 'invoice.refund', 'member.manage', 'billing.manage'],
  admin: ['invoice.read', 'invoice.create', 'invoice.refund', 'member.manage'],
  member: ['invoice.read', 'invoice.create'],
  viewer: ['invoice.read'],
};

export const can = (membership, permission) =>
  ROLE_PERMISSIONS[membership?.role]?.includes(permission) ?? false;

Python example

ROLE_PERMISSIONS = {
    "owner": {"invoice.read", "invoice.create", "invoice.refund", "member.manage", "billing.manage"},
    "admin": {"invoice.read", "invoice.create", "invoice.refund", "member.manage"},
    "member": {"invoice.read", "invoice.create"},
    "viewer": {"invoice.read"},
}

def can(membership, permission: str) -> bool:
    return membership is not None and permission in ROLE_PERMISSIONS.get(membership.role, set())

How to check

Search the code for role comparisons, such as role === 'admin' or role == "admin". Matches appear only in the policy module. A test confirms can answers no for an unknown role. As a normal user, call every admin address and API action found in the code, the scripts and the sitemap. Every call returns 403 or 404. For each action the screen hides from a role, a test calls the API directly as that role, and the data is unchanged.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/accounts/roles-and-permissions/permissions-not-roles">Agent Wranglers accounts and identity playbook</a>. In code you write, add a comment next to it with the same link.