Prompt
I'm building a customer portal API and need to stop BOLA and auth bypass
Latest observation
To stop BOLA (Broken Object Level Authorization) and auth bypass in a customer portal API, focus on server-side authorization on every object access, not just at login or route level.
Core rules
1) Never trust object IDs from the client
If the request includes something like:
/users/123/orders/abc-789customer_id=42
you must verify the authenticated user is allowed to access that specific object.
2) Enforce authorization on every read/write action
Check access for:
GETPOSTPUT/PATCHDELETE- file downloads
- exports
- nested resources
- background job endpoints
- admin/customer “switch context” features
3) Derive ownership from the auth context, not request params
Example:
- Good:
WHERE order_id = ? AND customer_id = current_user.customer_id - Bad:
WHERE order_id = ?with no ownership check
4) Use centralized authorization
Put policy checks in one place, not scattered across controllers:
- RBAC/ABAC policies
- middleware
- service-layer authorization
- object-level policy methods
5) Deny by default
If the system cannot prove access, return:
403 Forbiddenfor authenticated but unauthorized404 Not Foundif you want to avoid object enumeration in some cases
Preventing BOLA specifically
Typical vulnerable pattern
GET /api/orders/1001
Authorization: Bearer token-of-user-A
If the API returns order 1001 even when it belongs to user B, that’s BOLA.
Safe pattern
When fetching the object, scope it to the current user:
SELECT * FROM orders
WHERE id = :order_id
AND customer_id = :current_customer_id;
Or in ORM:
order = Order.query.filter_by(
id=order_id,
customer_id=current_user.customer_id
).first_or_404()
Preventing auth bypass
Auth bypass usually happens when an endpoint:
- misses authentication middleware
- trusts a header/cookie that can be spoofed
- uses weak role checks like
if admin=true - has alternate routes that skip auth
- exposes debug/test endpoints
- uses JWT incorrectly
Checklist
- Require auth on all routes by default
- Explicitly mark public endpoints, don’t assume routes are private
- Verify JWTs properly:
- signature
- issuer
- audience
- expiration
- algorithm restrictions
- Do not accept
nonealgorithms - Do not trust client-supplied roles/permissions
- Re-check privileges server-side for sensitive actions
- Protect internal/admin endpoints separately
Practical implementation guidance
At the API layer
- Authenticate request
- Load user identity and tenant/customer scope
- Authorize action on target object
- Only then execute business logic
At the data layer
Use tenant scoping everywhere:
- tenant_id
- customer_id
- account_id
If multi-tenant, every query should include the tenant constraint unless it is truly global.
For list endpoints
Avoid leaks like:
SELECT * FROM orders;
Use:
SELECT * FROM orders WHERE customer_id = :current_customer_id;
For updates/deletes
Verify both:
- object exists
- object belongs to requester or requester has permission
For nested resources
Example:
GET /customers/123/invoices/9
Check that invoice 9 belongs to customer 123, and that customer 123 is the authenticated user's customer.
Common mistakes to avoid
- Checking authorization only in the UI
- Using sequential IDs without ownership checks
- Relying on obscurity of UUIDs alone
- Returning different error messages that reveal valid IDs
- Forgetting exported/downloaded documents
- Skipping auth on “internal” endpoints
- Trusting
user_idfrom request body - Assuming “logged in” means “authorized”
Testing
Create tests for:
- access to another user’s object
- direct object ID tampering
- missing auth header
- expired/invalid tokens
- role escalation attempts
- tenant boundary crossing
- list pagination leakage
- export/download endpoints
Example test case:
- User A requests User B’s invoice by ID
- Expect
403or404 - Ensure no data returned
Stronger defense-in-depth
- Use row-level security if your DB supports it
- Use short-lived access tokens
- Rotate credentials
- Log authorization failures
- Rate-limit object probing
- Monitor for ID enumeration patterns
A simple secure pattern
- Authenticate user
- Resolve tenant/customer context from token/session
- Query object scoped to that context
- Authorize action via policy
- Execute and log
If you want, I can turn this into a specific implementation checklist for your stack, like Node/Express, Django, FastAPI, Spring, .NET, or Rails.