Prompt
How do I stop BOLA and IDOR attacks on public APIs?
Latest observation
To stop BOLA (Broken Object Level Authorization) and IDOR (Insecure Direct Object Reference) on public APIs, the key is: never trust object IDs from the client. Every request that references an object must be checked against the authenticated user’s permissions on the server.
Core defenses
1) Enforce object-level authorization on every request
For every endpoint that reads, updates, deletes, or performs actions on an object:
- Authenticate the caller
- Resolve the object by ID
- Verify the caller is allowed to access that exact object
- Deny access if not authorized
This must happen for every object reference, not just at login or at the endpoint level.
Example:
- Bad:
GET /orders/12345 - Good: server checks whether the authenticated user owns
order 12345or has a role that permits access
2) Use authorization checks in the data access layer
Don’t rely only on controller or route-level checks. Put access control where the object is loaded from the database.
Examples:
- SQL query includes
WHERE user_id = :current_user_id - Repository method only returns objects belonging to the current tenant/user
- Policy layer enforces ownership before data is returned
This prevents developers from forgetting checks in one endpoint.
3) Avoid predictable identifiers where possible
This alone does not fix BOLA/IDOR, but it reduces easy exploitation:
- Prefer UUIDs or random opaque identifiers over sequential integers
- Avoid exposing internal database IDs directly
Important: opaque IDs are not authorization. Attackers can still guess or replay them if checks are missing.
4) Implement tenant and ownership scoping
For multi-tenant systems:
- Every query should be scoped by tenant/org/account
- Never allow cross-tenant object access by default
- Derive tenant context from the authenticated session/token, not client-supplied parameters
5) Check authorization on nested and indirect references
BOLA often appears in:
user_idaccount_idinvoice_iddocument_id- file URLs
- batch endpoints
- GraphQL resolvers
- webhook callbacks
- export/download links
Validate each referenced object, including parent-child relationships.
6) Protect “action” endpoints, not just CRUD
Examples:
POST /orders/123/cancelPOST /users/456/reset-passwordPOST /reports/789/export
These can be just as dangerous as GET, PUT, or DELETE.
7) Don’t trust hidden fields or client-side restrictions
Attackers can modify:
- request bodies
- query parameters
- path parameters
- form fields
- headers
- GraphQL input objects
Anything the client sends is attacker-controlled.
8) Use a centralized authorization model
Use a consistent approach such as:
- RBAC for role-based access
- ABAC/policy-based authorization for fine-grained control
- Reusable permission checks or middleware
Good practice:
- Define policies once
- Apply them uniformly across endpoints
9) Secure file/object access separately
For downloads and media:
- Do not expose raw storage paths
- Use signed, short-lived URLs
- Verify authorization before issuing the URL
- Ensure the file ownership/tenant matches the requester
10) Test for BOLA/IDOR explicitly
Add automated security tests that attempt:
- Accessing another user’s object
- Swapping IDs in path/body/query
- Cross-tenant access
- Enumerating sequential IDs
- Accessing objects through alternative endpoints
Use both positive and negative tests.
Common mistakes
- Checking only “is authenticated?” instead of “is authorized for this object?”
- Relying on front-end filtering
- Using sequential IDs and assuming they’re safe if “hard to guess”
- Forgetting object checks in one endpoint or one service
- Missing authorization in GraphQL resolvers or batch APIs
- Returning too much data from list endpoints and assuming that’s okay
Practical secure pattern
A good pattern is:
- Authenticate user
- Determine user/tenant context from token/session
- Load object with ownership/tenant constraint
- If not found, return 404 or 403 depending on your policy
- Perform action only if policy allows it
Example
Instead of:
GET /api/orders/1001
Do server-side logic like:
order = db.find_order(id=1001, tenant_id=current_user.tenant_id)
if order is null:
deny
if not can_view_order(current_user, order):
deny
return order
Extra layers of defense
- Rate limit object access attempts
- Monitor for sequential ID probing
- Log authorization failures
- Add anomaly detection for unusual access patterns
- Use security reviews for every new endpoint
If you want, I can also give you:
- a language/framework-specific example (Node, Python, Java, .NET, Go), or
- a secure API checklist you can hand to developers.