Prompt
BOLA IDOR API protection
Latest observation
BOLA/IDOR API protection means making sure users can only access their own authorized resources via an API, even if they guess or change IDs in requests.
What BOLA/IDOR is
- BOLA: Broken Object Level Authorization
- IDOR: Insecure Direct Object Reference
Example:
GET /api/orders/12345
If a user can change 12345 to 12346 and view another customer’s order, that’s an IDOR/BOLA issue.
How to protect against it
1) Enforce authorization on every object access
Check permissions server-side for each resource:
- Does this user own the object?
- Is the user allowed to read/update/delete it?
- Is the action allowed for this role?
2) Never trust client-supplied IDs alone
Don’t assume an ID in the URL or body is valid just because it was sent by the client.
Bad:
order = db.get_order(request.params["id"])
return order
Good:
order = db.get_order(request.params["id"])
if order.owner_id != current_user.id:
return 403
return order
3) Use indirect references when appropriate
Instead of exposing sequential IDs, use:
- UUIDs
- random opaque identifiers
- short-lived access tokens tied to the user/session
Note: UUIDs help reduce guessing, but do not replace authorization.
4) Apply object-level checks in all CRUD paths
Check authorization for:
- GET
- POST
- PUT/PATCH
- DELETE
- export/download endpoints
- nested resources like:
/users/{id}/addresses/projects/{id}/members
5) Filter by tenant/user at the data layer
Prefer queries scoped to the authenticated user/tenant:
SELECT * FROM orders WHERE id = ? AND user_id = ?
This is safer than fetching first and checking later.
6) Don’t expose unnecessary object IDs
If possible:
- avoid returning internal IDs in responses
- avoid using predictable sequences
- keep sensitive relationships server-side
7) Test for BOLA/IDOR
Create tests that try:
- accessing another user’s resource
- modifying IDs in requests
- cross-tenant access
- unauthorized delete/update attempts
8) Centralize authorization logic
Use a consistent policy layer rather than scattered if checks throughout the app.
Examples:
- middleware
- policy engine
- RBAC/ABAC rules
- service-layer authorization
Common mistakes
- Checking only login status, not ownership
- Relying on hidden fields in HTML/JSON
- Using predictable sequential IDs without authorization
- Missing checks on PATCH/DELETE endpoints
- Assuming “frontend won’t show it” is enough
Practical secure pattern
- Authenticate the user
- Load the requested object with a scope filter
- Verify authorization
- Return only allowed data
Example:
order = db.query("""
SELECT * FROM orders
WHERE id = ? AND tenant_id = ?
""", [order_id, current_user.tenant_id])
if not order:
return 404
Quick checklist
- Every object access is authorized server-side
- IDs are scoped to user/tenant
- No direct trust in client input
- Read/update/delete all protected
- Tests cover cross-user access
- Logs and alerts for suspicious access patterns
If you want, I can also provide:
- a language-specific example (Node, Python, Java, Go, etc.)
- a BOLA testing checklist
- a secure API design pattern for multi-tenant apps