Prompt
How do I enforce object-level authorization on APIs?
Latest observation
To enforce object-level authorization on APIs, you need to make sure a user can access only the specific resource instance they’re allowed to access, not just the endpoint in general.
What it means
Example:
GET /orders/123- Even if the user is authenticated, they should only see order
123if they own it or have permission to view it.
This is different from:
- Authentication: “Who are you?”
- Object-level authorization: “Are you allowed to access this particular object?”
Core approach
1) Never trust client-provided IDs alone
If an API accepts a resource ID like /users/42, /projects/99, or order_id=123, always verify the requesting user’s permission against that object on the server.
Bad:
- Return object directly by ID
Good:
- Fetch object
- Check whether current user has access to it
- Then return it
2) Check permissions in the data access layer or policy layer
Do the authorization check as close to the data fetch as possible so you don’t accidentally leak data in later code.
Common patterns:
- Policy-based authorization:
can_view(user, object) - Scoped queries: only query objects the user is allowed to access
- Row-level security in the database for defense in depth
3) Use ownership/tenant checks
Typical rules:
- User can access objects they own
- User can access objects in their organization/tenant
- Admins may access all objects
- Shared-access rules may apply
Example:
order = Order.objects.get(id=order_id)
if order.owner_id != request.user.id:
raise PermissionDenied
Better:
order = Order.objects.filter(id=order_id, owner_id=request.user.id).first()
if not order:
raise NotFound() # Often better than revealing existence
4) Prefer “filter then fetch” over “fetch then check” when possible
This avoids accidental information disclosure.
Example:
SELECT * FROM orders
WHERE id = ? AND owner_id = ?;
If no row is returned, treat it as not found.
This prevents:
- “This order exists but belongs to someone else” leakage
- Timing or error-message based inference
5) Protect every object-accessing endpoint
Object-level auth must be enforced for:
GET /resource/{id}PUT /resource/{id}PATCH /resource/{id}DELETE /resource/{id}- nested resources like
/projects/{id}/tasks/{taskId}
Also remember:
- Search endpoints
- Export/download endpoints
- Bulk update/delete endpoints
- Admin/debug endpoints
- Relationship endpoints (
add user to project,approve invoice, etc.)
6) Use centralized authorization logic
Don’t duplicate permission checks all over controllers.
Options:
- Authorization service
- Policy classes
- Middleware + per-object checks
- Framework hooks/decorators
Example pattern:
if (!authz.can(user, "read", resource)) {
return res.status(403).send("Forbidden");
}
Or, if resource should be hidden:
if (!resource) return 404;
if (!authz.can(user, "read", resource)) return 404;
7) Be careful with indirect object references
If your API uses IDs, UUIDs, file paths, slugs, or tokens as references, they are all object references and require authorization.
Examples:
invoice_iddocument_uuidreport_slugs3_key/download?file=...
Even if IDs are hard to guess, auth is still required.
8) Consider database-level enforcement
For high-security systems, use:
- Row-Level Security (RLS) in PostgreSQL
- Tenant-based filters
- Views with restricted access
This helps prevent mistakes in application code from exposing data.
9) Audit and test for IDOR
A common vulnerability here is IDOR (Insecure Direct Object Reference).
Test:
- Change the resource ID in the request
- Try other users’ IDs
- Try nested object IDs
- Try bulk endpoints with mixed authorized/unauthorized IDs
Add automated tests like:
- user A can’t read user B’s object
- user A can’t update user B’s object
- user A can only list their own objects
Practical implementation pattern
For each endpoint:
- Authenticate user
- Look up the requested object with an access filter
- If not found, return
404 - If found but not permitted, return
403or404depending on your disclosure policy - Proceed only after authorization succeeds
Example pseudo-code
def get_invoice(request, invoice_id):
invoice = Invoice.objects.filter(
id=invoice_id,
org_id=request.user.org_id
).first()
if not invoice:
return Response(status=404)
return Response(invoice.to_dict())
For update:
def update_invoice(request, invoice_id):
invoice = Invoice.objects.filter(
id=invoice_id,
org_id=request.user.org_id
).first()
if not invoice:
return Response(status=404)
if not request.user.can_edit(invoice):
return Response(status=403)
invoice.amount = request.data["amount"]
invoice.save()
return Response(invoice.to_dict())
Good default rule
A strong default is:
Every request that references a specific object must verify the caller’s access to that exact object before returning or modifying it.
If you want, I can also show:
- framework-specific examples for Django, FastAPI, Express, Spring, or .NET
- a policy-based design for object-level auth
- a checklist to prevent IDOR in your API