Prompt
How do I enforce object-level access control across all APIs?
Latest observation
To enforce object-level access control across all APIs, you want a centralized authorization model where every request is checked against the specific object being accessed, not just the endpoint or user role.
Core principle
Never rely only on:
- “User is authenticated”
- “User has role X”
- “User can call this endpoint”
You must also verify:
- Which object is being accessed
- Whether that user is allowed to act on that exact object
- Whether the action is allowed (read, update, delete, etc.)
Recommended approach
1) Use a centralized authorization layer
Put authorization logic in a shared component used by all APIs:
- middleware
- service layer guard
- policy engine
- authorization library
This prevents one-off checks being missed in individual handlers.
Examples:
- RBAC + ABAC policy layer
- Policy engines like OPA, Casbin, AWS Cedar, Zanzibar-style systems
- Framework guards/interceptors if using a web framework
2) Check permissions against object identity
For every request, resolve the target object and validate access.
Example:
GET /orders/123- Check whether the user can
readorder123 - Not just whether they can access
/orders/*
Typical rule:
- user must be owner, or
- user must belong to same tenant/org, or
- user must have a delegated/shared permission
3) Enforce in the service/domain layer, not only at the controller
API gateways and route middleware are useful, but they are not enough.
Best practice:
- authenticate at the edge
- authorize at the application/service layer
- re-check before any sensitive data operation
This protects against:
- hidden internal calls
- bypasses via alternate endpoints
- insecure direct object references (IDOR)
4) Use a consistent authorization API
Create a single method used everywhere, like:
authorize(user, action, resourceType, resourceId)
Or:
can(user, "read", order)
can(user, "update", project)
This reduces logic duplication and makes audits easier.
5) Tie queries to authorization
Don’t fetch the object first and then hope it’s allowed.
Prefer:
- database queries scoped by user access
- row-level security
- tenant filters
- ownership filters
Example:
- instead of
SELECT * FROM orders WHERE id = 123 - use
SELECT * FROM orders WHERE id = 123 AND tenant_id = user.tenant_id
For sensitive systems, consider:
- Row-Level Security (RLS) in the database
- scoped ORM helpers
- query builders that automatically apply authorization constraints
6) Model object ownership and relationships explicitly
Define who can access what:
- owner
- collaborator
- manager
- team member
- tenant admin
- public/shared access
Then encode those rules centrally.
Example policy:
- owner can read/update/delete
- team members can read
- admins can do all actions
- cross-tenant access is denied unless explicitly granted
7) Make object access checks mandatory for every action
Apply checks to:
- read
- create
- update
- delete
- list/search results
- export/download
- bulk actions
- nested resources
- webhook/event-driven operations
- background jobs if they act on behalf of a user
Especially important:
- listing endpoints must filter by authorization
- bulk endpoints must authorize each object individually or by scoped policy
8) Avoid trusting client-supplied IDs
Never assume that because a client knows an object ID, they may access it.
Common vulnerability:
- user changes
customer_id=1001tocustomer_id=1002 - API returns another user’s data
Always verify the object belongs to the current caller’s access scope.
9) Use deny-by-default
If authorization data is missing or unclear:
- deny access
- log the event
- fail closed
This is safer than permissive fallback behavior.
10) Add testing and observability
To ensure coverage:
- unit test every policy
- integration test every endpoint for unauthorized access
- add security tests for IDOR / broken object-level auth
- log authorization decisions for audit
Useful tests:
- owner can access own object
- non-owner cannot access object
- user from another tenant cannot access
- admin can access as expected
- list endpoints do not leak unauthorized items
A practical implementation pattern
At request time:
- Authenticate user
- Extract resource ID(s)
- Load resource or scope query by access constraints
- Call authorization check
- Proceed only if allowed
Example pseudocode
def get_order(user, order_id):
order = db.find_order(order_id)
if not authz.can(user, "read", order):
raise Forbidden()
return order
Better:
def get_order(user, order_id):
order = db.find_order_for_user(order_id, user)
if not order:
raise NotFound() # or Forbidden depending on your security model
return order
Recommended architecture for scale
For many APIs/microservices:
- Central policy engine
- Shared authorization SDK
- Standard resource model
- Tenant-aware database scoping
- Service-side enforcement
- Audit logging of all decisions
If your environment is complex, consider:
- policy-as-code
- fine-grained authorization service
- attribute-based access control (ABAC)
- relationship-based access control (ReBAC) for sharing/collaboration use cases
Common mistakes to avoid
- Checking only roles, not object ownership
- Authorizing at UI level only
- Forgetting list/search endpoints
- Forgetting nested resources
- Returning 403/404 inconsistently without a clear strategy
- Duplicating auth logic in every controller
- Trusting hidden/internal APIs to be safe by default
- Not testing for IDOR
Short answer
To enforce object-level access control across all APIs, implement a centralized, deny-by-default authorization layer that checks user + action + specific object on every request, and back it with query scoping/row-level security, shared policy logic, and automated tests.
If you want, I can also show:
- a framework-agnostic reference design,
- an example in Node/Express, Django, Spring, or FastAPI, or
- how to do this with OPA/Casbin/Cedar.