API Scopes, Roles, and Object-Level Permissions
Compare API scopes, roles, and object-level permissions, then combine them to grant callers only the access each operation and resource requires.
API scopes limit actions a token may perform, roles group capabilities, and object-level policies check access to a specific record. This guide explains how to combine them with verified identity and trusted tenant or ownership data, including checks for list, bulk, and nested routes. Use its examples and tests to catch cross-tenant access gaps and keep authorization decisions observable without logging private data.
API Scopes, Roles, and Object-Level Permissions
Introduction
Authentication establishes the caller’s identity; the authentication versus authorization guide covers that prerequisite. Here, assume identity and tenant context have already been verified. Authorization decides whether this caller may perform this action on this record.
A projects:read scope lets a client call read operations, but the scope alone does not grant access to every project. This request still needs an object-level tenant check:
GET /projects/project_42
Authorization: Bearer <token with projects:read>
Resolve the tenant from verified identity, then check the stored project’s tenant or owner before returning data. A broad permission and access to one object are separate checks.
Roles and object-level rules
Roles work well for stable duties such as analyst or support agent. Object-level rules add context that roles and scopes cannot express by themselves, such as whether a project belongs to the caller’s tenant or a user is assigned to a case. A scope permits an operation. A role grants a capability. The object policy checks whether the caller may act on a specific record.
One policy matrix for a project API
Assume Alice belongs to tenant t_17 and has the analyst role with projects:read. Bob belongs to t_28 and has the project_exporter role with projects:export. Each project has a tenant recorded by the service. The active tenant comes from verified identity, not a request parameter.
| User and tenant | Action | Resource | Decision |
|---|---|---|---|
Alice, t_17, analyst, projects:read |
Read | project_42, tenant t_17 |
Allow. Scope, role, and tenant match. |
Alice, t_17, analyst, projects:read |
Read | project_88, tenant t_28 |
Deny. The object belongs to another tenant. |
Alice, t_17, analyst, projects:read |
Export | Projects in t_17 |
Deny. She lacks the export scope and role. |
Bob, t_28, project exporter, projects:export |
Export | Projects in t_28 |
Allow after checking each project against t_28. |
The same rule applies to every route that can return a project:
GET /v1/projects/project_42requires the read scope and role, then verifies the stored project’s tenant before returning it.GET /v1/projectsderives the tenant from verified identity and constrains the query to that tenant. AtenantIdquery parameter cannot widen the result set.POST /v1/projects/exportrequires export permission, derives the tenant from verified identity, and checks every selected project before creating or delivering the export.
OWASP’s API1:2023 guidance recommends object-level checks whenever an endpoint uses a client-supplied object ID to access or modify a record.
Test the policy across routes
Use two tenant fixtures so a test cannot pass by checking only the user’s broad role or scope:
| Case | Request | Expected result |
|---|---|---|
| Allow | Alice reads project_42 in t_17 |
Return the project. |
| Deny | Alice requests an export without export scope and role | Reject the request; create no export job. |
| Cross-tenant detail | Alice reads project_88 in t_28 |
Return the API’s documented concealment response, with no project data. This example uses 404. |
| Cross-tenant list | Alice requests tenantId=t_28 |
Return only t_17 projects, or reject the unsupported filter; never return t_28 records. |
| Cross-tenant export | Alice selects a t_17 and a t_28 project |
Reject before creating a file or job. |
If the API hides cross-tenant object existence with 404, use that policy consistently on detail, list, and export routes. The status choice does not replace the object-level check.
Authorization flow
flowchart TD
A[Request includes verified identity] --> B[Check required scope and role]
B --> C[Load resource and trusted tenant context]
C --> D[Check ownership or object policy]
D -->|Allowed| E[Return permitted data]
D -->|Denied| F[Return consistent safe response]
E --> G[Record decision without private payload]
Implementation practice
Apply authorization before returning each resource, including items in list, export, nested, and bulk routes. Derive tenant and ownership from verified identity and stored relationships, never from untrusted request fields. Test both allowed and denied cases across users and tenants, including alternate routes that expose the same records.
Production failure scenarios and mitigations
- A list or export route bypasses the detail authorization check. Apply the same object policy to every route that returns the resource, and test list, bulk, nested, and export behavior.
- A caller changes a tenant or owner ID in the request. Resolve those values from verified identity and stored relationships rather than trusting client-supplied fields.
- A valid scope exposes another tenant’s record. Treat scope as permission for an action, then verify access to each requested object before returning data.
- Different routes return 403 and 404 for the same protected object. Apply a consistent denial policy so responses do not reveal whether another tenant’s record exists.
Authorization model trade-offs
Scopes, roles, and object policies solve different problems. Choose the simplest combination that expresses the access rules without relying on client-supplied claims.
| Model | Best fit | Trade-off |
|---|---|---|
| Broad OAuth scopes | Separating capabilities between clients | Easy to issue and review, but too coarse for tenant or record boundaries |
| Fine-grained scopes | Integrations with distinct, narrow operations | Better least-privilege control, but token and consent configuration can grow quickly |
| RBAC | Stable job duties shared by many users | Straightforward assignment, but exceptions can multiply roles |
| ABAC or object policy | Access depends on tenant, ownership, or resource state | Precise decisions, but policy evaluation needs trusted context and careful testing |
Observability checklist
- Record allow and deny decisions by operation, policy, and resource type without logging protected payloads.
- Track repeated cross-tenant denials and unusual access to list, export, or bulk routes.
- Correlate denied requests with request IDs while keeping credentials and personal data out of logs.
- Monitor authorization latency and alert on sudden changes in denial rates after releases.
Security and Compliance Notes
- Resolve user, tenant, and service identity from a verified credential; treat request-body ownership fields as untrusted input.
- Record the actor, action, resource identifier, decision, and request ID for sensitive access decisions. Keep private payload fields out of the audit event and restrict who can read it.
- Review permission grants and service credentials periodically, and remove access when an integration or user no longer needs it.
- Map access and audit controls to the data and obligations in scope. Retention, access review, and breach-record requirements vary by jurisdiction and contract, so confirm them with the organization’s compliance owner.
Common Pitfalls / Anti-Patterns
- Treating a valid scope as proof of access to every object in that scope.
- Checking permissions only on detail routes while list, export, bulk, or nested routes return the same records unchecked.
- Trusting a client-provided role, tenant ID, or owner ID instead of deriving it from verified identity and stored resource relationships.
- Building a large collection of nearly identical roles to represent per-record exceptions; use a resource policy for those decisions.
- Returning different denial behavior across endpoints in a way that reveals whether another tenant’s record exists.
Quick Recap Checklist
- Use scopes for delegated capabilities and roles for application-level duties.
- Check access to each requested object on the server.
- Derive tenant and ownership context from trusted data.
- Test both allowed and denied cases across user and tenant boundaries.
Interview Questions
Further Reading
- API Authentication vs. Authorization — distinguish identity checks from permission decisions.
- API Keys, Sessions, and Service Credentials — choose and manage credentials for different callers.
- Input Validation, Secrets, and Sensitive Data — validate untrusted inputs and protect sensitive values.
- OWASP Authorization Cheat Sheet — practical access-control design and testing guidance.
- OWASP API Security: Broken Object Level Authorization — risks from missing per-object checks in APIs.
Conclusion
A valid token or role does not prove access to every object. Check the caller’s scope and role, then verify tenant, ownership, or other resource rules using trusted data on every route. Tests across users, tenants, list operations, and bulk actions help catch gaps before they expose another caller’s records.
Category
Related Posts
GraphQL Subscriptions: Transports, Security, and Scaling
Choose GraphQL subscription transports, plan authorization and reconnects, and scale event delivery across servers with security and deployment checks.
Logging, Processes, and Permissions on Hosts
Learn how host users, process identities, file permissions, services, and system logs fit together, with safe read-only checks for Linux systems.
CSRF, CORS, and SSRF: Defending Web Request Boundaries
Learn how CSRF, CORS, and SSRF differ, then apply cookie, origin, allowlist, and egress controls to protect browser and server request boundaries.