How to test role-based access controls
Role-Based Access Control helps organisations to secure sensitive information while ensuring access management scales, by providing a security layer that governs who can interact with what data and how. In this article, I’ll show that permissions and access are two related but separate areas. Why it is so important. And that your focus should be on the API and data layer, not just the UI.
What is RBAC?
RBAC, pronounced “R-back," is an acronym which stands for “role-based access control”. It is defined by IDM as “a model for authorizing end-user access to systems, applications and data based on a user’s predefined role”. In other words, RBAC restricts system access based on users' assigned roles, making data access policies consistent rather than issuing individual permissions per person, which may result in complex permission structures. In an RBAC system, an admin assigns each individual user one or more roles. Each new role represents a collection of permissions for the user.
RBAC follows authentication (defined as a means of proving that you are who you say you are) and is a format for providing authorization (the required access or permissions to perform certain actions or see certain data). Permissions grant users the ability to perform actions such as creating, reading, updating, or deleting a resource (CRUD). Permissions can be scoped, meaning a user role may only be able to read resources at the project level, while another role may be able to read and update the same resource type at the org level. This means that the same role can have different effects depending on where it is applied.
Using WonderSwitcher as an example, a tester might validate that users in EU locations (with the role EU) are shown (the read permission) an “AI modified” icon when a human-made image or video has been edited by AI, whereas in the US, Canada and the UK, the same icon is not required by law.
Why would you implement this feature?
Organizations often use RBAC to simplify access management and maintain information security, often matching a job function, such as an admin or project manager. In the example above, users are also given roles based on their location to ensure they have appropriate access to resources and information for their region. This follows the principle of least privilege. Users start with no access, and admins grant rights as needed. RBAC makes it easier for admins to modify or remove permissions for all users in a group at once, or quickly change a single user's access level by assigning or removing a role.
Why is testing RBAC difficult?
It is harder to test than it first appears because permissions and access are two separate but connected areas. Because RBAC failures can be found “publicly” by customers and audits, it is something teams should prioritise before deployments. Testing is multi-faceted, meaning it takes on many types of validation that can be time-consuming, especially if there are many roles, actions, and objects to be tested. Each role, resource, action, and permission boundary adds another risk to verify.
What and when should you test it? Building a test strategy
Authorization testing should be tested at numerous points across the SDLC (software development life cycle). RBAC should be tested (typically as API tests, more on test types further down) to validate access and data boundaries. Broken access control is consistently ranked as a security risk in the OWASP Top 10.
Identify the resources available or unavailable to users and roles and define expected access
The first step is being able to identify feature changes that will result in a new API endpoint or UI route. Especially one that's powered by a database query, will enable shift-left testing and planning for the permissions that should be enforced.
I often use the mnemonic “Mary had a little lamb” to help think about system configurations, where each word represents something that could change in the data or feature under test:
- “Mary”, the user or actor
- ”had”, the status, i.e. had (holds), hasn’t (loaded yet), won’t have (permissions)
- ”a”, the quantity (one)
- ”little”, the quality characteristic
- ”lamb” representing the data object or feature under test
Design for the rollout of role or permission changes to prevent access leakage and state transition authorization
The main reason organisations are likely to implement RBAC is ease. So next, try to think about the state transition when new roles are added, or existing roles are changed. This can happen at any point after roles or permissions are implemented, which is why you should consider it during product development.
State transition authorization vulnerabilities occur when a user can perform an action they should no longer be able to perform. For example, in WonderProxy's ability to manage and configure users, if a WonderProxy user was previously an administrator + a billing contact, can they now receive account notifications after their billing rights were revoked? If a user's access was revoked while they were on a billing page, would their access persist while they remained on the page (cookie auth) or would access persist while the user who was previously authorized remains logged in?
Another worked example is that WonderProxy implements RBAC where users may have one or a combination of the following privileges:
- Proxy User: Able to use the network of proxies and VPNs.
- Billing Contact: Receives account notifications, including expiry warnings.
- Administrator: Able to manage users & servers.
Imagine users provide feedback that administrators must also be users by default so they can test the servers they manage. To test RBAC, you must ensure role and permission changes apply as expected, especially when permissions update and conflict with other user groups. How is this rolled out to existing admins? What are the expected permissions in these cases? Overlooking multiple permissions is too serious a risk to ignore.
Test every endpoint and authorisation boundaries with different user roles and data combinations.
Access controls should be enforced on objects too. One way to skirt around access control checks, or access resources that may not otherwise be available, is to change parameters in the URL or request body directly. For example, if you can legitimately access project 100 via a URL or a GET request to the endpoint `/project?id=100`, then what happens when you try the same means of access against different project IDs like `/project?id=101` or `/project?id=102`? This vulnerability is called Insecure Direct Object Reference (IDOR). It can be tested programmatically by looping through known projects that the user should not have access to, using a variety of tools.
Malicious actors may also try to manipulate client-side security tokens and session identifiers, such as JSON Web Tokens (JWTs) or cookies. JWTs contain encoded claims about a user, including their role (e.g. `role: “viewer”`), which can be read via tools such as jwt.io. Changing the decoded payload and copying the encoded JWT means it is possible to manipulate the JWT to have the role parameter `admin`. Manipulating the client-side tokens works as an attack vector due to poorly implemented validation, and the servers failure to verify signatures.
OWASP recommends preventing this by using the deny-by-default principle, which states that you should deny all requests that are not explicitly allowed. This can include validating a user's client-side role on each request. The endpoint must have a rule enforcing a specific role, or it rejects the request by default.
This tests against a broader category of vulnerabilities called privilege escalation. Privilege escalation can happen directionally. Vertical privilege escalation is where a lower-tier role performs actions they shouldn’t have permission to do, and horizontal privilege escalation is where a user can access or update resources they should not have access to. This is often combined with impersonating other users or admins, or trying to access admin-related URLs such as WonderProxy.com/admin and WonderProxy.com/docs/admin.
While testing, ensure error messages do not reveal information about the resource a user cannot access. For example, the difference between showing “access denied” (/admin) and “page not found” (/docs/admin) tells me that /admin is a valid resource I just don’t have the correct permissions, whereas /docs/admin is not a valid resource. Similarly, this is why login pages don't show just “password not recognised” vs “incorrect username and password”, which can reveal that a user exists but has different credentials.
These sorts of authorization tests should be run in a pipeline to prevent regression issues. For added security, teams should log access-control failures and alert on repeated attempts above a threshold, even in lower environments. As well as the obvious ones, such as access being granted or denied, log any sensitive actions to provide robust audit trails. If they are available outside a VPN, malicious actors may use a test or a staging environment before targeting production.
How would you test that?
Test every negative auth case in the API
Although issues may appear in the UI, for example, when an unverified user tries to view an age-restricted product, validation should not happen at the UI layer. Part of the reason behind this is that authorisation should be enforced at both the UI and API layers. Therefore, issues are more commonly found in the data layer, the API, or at permission boundaries. As such, RBAC should be predominantly tested at the API layer, which can be done using tools like Bruno or Postman. Or in automation frameworks such as Playwright. It will depend on your product how often these tests need to run and whether they block releases.
RBAC should be enforced at both the UI and data (API) layers
Although it’s always worth validating that error messages are presented to users on the UI, and that certain URLs cannot be accessed, I would always avoid writing UI tests to ensure page elements are hidden for certain user roles, or that a certain copy is shown to users based on their region, as the UI is likely to change continuously.
Resources a user doesn’t have access to shouldn’t leave holes in the UI
Another mistake I’ve seen teams making is merging snapshot testing with access-control assertions to verify that unauthorized items are hidden.
As long as you’ve validated the UI layout once and ensured it handles missing data gracefully, you typically don’t need to add snapshot tests based on users' roles. For example, if a user doesn’t have access to resource 13 and the UI leaves a gaping hole, or the layout or pagination is broken, that is not a security test, but it is something that could be verified in an independent snapshot test.
Summary
Implementing RBAC is an effective way to simplify access management, but testing it requires enumerating the variety of actors and resources in your organisation, application, or project. It is not just whether a user can access a page. It is verifying that people have the access they need and nothing more.
Testing should focus on the security of the API and data layer, and you can simplify it by implementing deny-by-default policies. I’d much rather a user reach out to say they should have access to a resource than find out users could access information that should be private! The most serious defects often appear at the boundaries: when permissions change, roles overlap, users access resources they don't own, or systems trust the client too much. By focusing on those boundaries and validating permissions at the API layer, teams can significantly reduce the risk of broken access control reaching production.