Prompt
How do I implement ABAC for tenant-level permissions in SaaS?
Latest observation
Implementing ABAC (Attribute-Based Access Control) for tenant-level permissions in SaaS means making authorization decisions based on attributes like:
- User attributes: role, department, job level, tenant membership
- Resource attributes: tenant_id, owner_id, classification
- Action attributes: read, write, delete, invite
- Environment attributes: time, IP, device, region
For SaaS, the key requirement is usually:
a user can only access resources belonging to their tenant, unless explicitly allowed otherwise.
1) Model your tenancy and attributes
At minimum, every protected resource should carry a tenant_id.
Example attributes
User
user_idtenant_idrole(admin, member, viewer)status(active, suspended)
Resource
resource_idtenant_idowner_idtypesensitivity
Action
readcreateupdatedeleteinvite_user
Environment
request_timeip_addressmfa_verified
2) Define authorization rules as policies
A simple tenant-level ABAC policy might be:
Allow access if user.tenant_id == resource.tenant_id
and user.status == active
and action is allowed for user.role
Example policy logic
- Viewer: can
readresources in their tenant - Member: can
read,create,updateown tenant resources - Admin: can do all actions within their tenant
- Support staff: can access multiple tenants only if
support_access = true
You can express this in code, policy engines, or a rules DSL.
3) Decide where enforcement happens
You want authorization enforced in multiple layers:
A. API / service layer
Check every request before business logic runs.
B. Data layer
Ensure queries are filtered by tenant, not just checked in memory.
Bad:
SELECT * FROM documents WHERE id = :id;
Good:
SELECT * FROM documents WHERE id = :id AND tenant_id = :tenant_id;
This prevents cross-tenant leaks if application checks are bypassed.
4) Use a policy engine if complexity grows
For non-trivial ABAC, use a dedicated policy layer such as:
- Open Policy Agent (OPA)
- AWS Cedar
- Casbin
- Custom policy service
These let you centralize rules and keep them maintainable.
Example policy in pseudo-code
allow if
user.status == "active" and
user.tenant_id == resource.tenant_id and
action in role_permissions[user.role]
More advanced example
allow if
user.status == "active" and
(
user.tenant_id == resource.tenant_id
or user.support_access == true
) and
not (resource.sensitivity == "restricted" and user.role != "admin")
5) Separate tenant isolation from tenant permissions
These are related but not identical:
Tenant isolation
A user should not see another tenant’s data by default.
Tenant permissions
Within a tenant, users may have different capabilities:
- admin
- billing admin
- read-only
- project manager
ABAC handles both by combining:
tenant_idmatch- role/department/ownership rules
- context checks
6) Keep permissions coarse, attributes fine-grained
For maintainability:
- Use tenant_id + role for most checks
- Add more attributes only when needed
Example:
- “Only users in tenant A can access tenant A’s invoices.”
- “Only billing admins can modify invoices.”
- “Only document owners can edit drafts unless admin.”
This avoids exploding policy complexity.
7) A practical policy structure
A common structure is:
Permit if:
- User is active
- Tenant matches
- Action is allowed for role
- Additional resource constraints pass
Example in pseudo-code
function can(user, action, resource):
if user.status != "active":
return false
if user.tenant_id != resource.tenant_id and not user.support_access:
return false
if action not in permissions_for_role(user.role):
return false
if resource.sensitivity == "high" and user.role != "admin":
return false
return true
8) Make tenant scoping a default in your ORM/query layer
A very common SaaS bug is forgetting tenant filtering in one endpoint.
Mitigations
- Add a tenant scope helper
- Require
tenant_idin repository methods - Use row-level security if your database supports it
- Add integration tests for cross-tenant access
Example pattern:
documents = DocumentRepo.for_tenant(user.tenant_id).get(document_id)
Not:
documents = DocumentRepo.get(document_id)
9) Use JWT/session claims carefully
You can put tenant info in tokens, but don’t trust them blindly without verification.
Useful claims:
sub= user idtid= tenant idrolescp= scopes
Caution
If roles or tenant membership change, stale tokens can grant access longer than intended.
Mitigations:
- short-lived access tokens
- token revocation
- re-check critical membership at authz time
- use token introspection for high-security cases
10) Test authorization thoroughly
You should test:
- same-tenant access allowed
- cross-tenant access denied
- suspended users denied
- role restrictions enforced
- ownership-based exceptions
- support/admin overrides
- stale token edge cases
Example test matrix
| User | Resource tenant | Action | Expected |
|---|---|---|---|
| member | same | read | allow |
| member | other | read | deny |
| admin | same | delete | allow |
| viewer | same | update | deny |
| support | other | read | allow only if policy says so |
11) Recommended implementation approach
If you want a pragmatic path:
Start simple
- Enforce
user.tenant_id == resource.tenant_id - Add role-based permissions inside each tenant
- Put tenant filters in every query
Then evolve
- Add ownership checks
- Add resource classification/sensitivity
- Add environment-based conditions
- Move policies to a central engine
12) Example real-world SaaS policy
Policy:
- Users can read any resource in their tenant if their role is viewer, member, or admin
- Only member/admin can create
- Only owner or admin can update
- Only admin can delete
- Support can access cross-tenant only through a special support workflow
Pseudo-code:
allow read if user.tenant_id == resource.tenant_id
allow create if user.tenant_id == resource.tenant_id
and user.role in ["member", "admin"]
allow update if user.tenant_id == resource.tenant_id
and (user.role == "admin" or user.user_id == resource.owner_id)
allow delete if user.tenant_id == resource.tenant_id
and user.role == "admin"
13) Common mistakes to avoid
- Relying only on UI to hide data
- Checking permissions only at the controller layer
- Forgetting tenant filters in queries
- Overloading roles instead of using attributes
- Using stale tenant membership from long-lived tokens
- Not testing cross-tenant access
If you want, I can also provide:
- a sample ABAC policy schema,
- an OPA/Cedar example, or
- a reference implementation in Python/Node/Java.