In multi-user ERP, DMS, and e-commerce platforms, a single missing authorization check on an API endpoint can cause a serious cross-account data breach. If an authenticated dealer or customer changes a URL parameter from /api/v1/orders/10482 to /api/v1/orders/10483, or swaps an invoice_id inside a PDF export call, an endpoint without ownership validation will return another company's orders, discount tiers, or internal approvals.
According to the OWASP API Security Project, Broken Object Level Authorization (BOLA), widely encountered in practice as Insecure Direct Object Reference (IDOR), ranks as the leading API security risk. The flaw rarely stems from missing authentication; instead, the backend trusts a client-supplied identifier without verifying that the caller has rights over that specific record.
1. Why route-level role checks and UUIDs fail alone
Teams often confuse role-based access control (RBAC) with object-level authorization. Middleware can confirm that a caller holds a valid JWT and belongs to the dealer or customer role. However, role checks only verify whether a user may call the order endpoint, never whether that user or tenant owns the specific order ID requested.
Another common mistake is assuming that replacing auto-increment integers (10482, 10483) with random UUIDv4 or ULID strings stops IDOR. Avoiding predictable sequential IDs is useful because it blocks trivial numeric scraping. Yet opaque IDs are not an access-control boundary. UUIDs regularly surface in browser history, proxy logs, shared email links, webhook payloads, and list responses. Once an attacker captures a valid identifier, an API without ownership checks still leaks the record.
2. Enforcing ownership on every query and PostgreSQL Row Level Security
A resilient backend should never fetch a record by primary key alone (SELECT * FROM invoices WHERE id = $1) and rely on an application-layer if (invoice.owner_id !== user.id) check added separately per handler. Split validation is fragile: engineers frequently miss checks on bulk PATCH routes, background CSV/PDF exports, file downloads, or webhook replay handlers.
Instead, every data-access helper must embed tenant and ownership predicates directly in the query:
SELECT id, order_code, total_amount, status
FROM orders
WHERE id = $1
AND tenant_id = $2
AND (owner_id = $3 OR $4 = true);
When architecting multi-tenant portals and distribution backends for enterprise systems and ERP platforms, ToanKhonTech pairs application query scoping with a database backstop using PostgreSQL Row Level Security (RLS). As documented in PostgreSQL's Row Security Policies reference, enabling ALTER TABLE orders ENABLE ROW LEVEL SECURITY; forces every SELECT, INSERT, UPDATE, and DELETE from standard application roles to satisfy per-row policy expressions (CREATE POLICY ... USING (...) WITH CHECK (...)).
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
CREATE POLICY orders_tenant_isolation ON orders USING (tenant_id = current_setting('app.current_tenant_id', true)::uuid) WITH CHECK (tenant_id = current_setting('app.current_tenant_id', true)::uuid); ```
On each authenticated request, the database layer opens a transaction and binds context via SET LOCAL app.current_tenant_id = '...' (plus app.current_user_id when row access narrows to an individual rep or buyer). Even if a developer omits tenant_id in a complex reporting query, PostgreSQL blocks rows from other organizations. Two PostgreSQL rules are critical in production: table owners bypass RLS unless FORCE ROW LEVEL SECURITY is set, and the app runtime role must never hold SUPERUSER or BYPASSRLS.
3. Defense-in-depth comparison and negative CI tests
API teams should combine four complementary layers across every multi-user workflow:
| Control layer | Primary protection | Failure mode if used alone | Production engineering rule |
|---|
| Opaque IDs (UUID / ULID) | Slows numeric scraping and enumeration | Fails whenever an ID leaks in logs or URLs | Use for external IDs, never as sole security |
| Route-level RBAC middleware | Restricts endpoint groups by user role | Cannot separate user A's record from user B's | Gate functional routes only; add object checks |
| Ownership-scoped queries | Binds tenant_id and owner_id in SQL/ORM | Depends on consistency across every new route | Enforce via shared helpers for reads, writes, exports |
| PostgreSQL Row Level Security | Isolates rows inside the database engine | Breaks if connection-pool session state leaks | Set FORCE ROW LEVEL SECURITY and SET LOCAL |
Every multi-user endpoint also needs automated negative authorization tests in CI. The suite should create at least two isolated tenants and multiple intra-tenant roles, then replay GET, PATCH, DELETE, attachment-download, and export calls using actor A's token against actor B's record IDs. Every cross-user or cross-tenant request must return 403 or 404 (preferring 404 to hide record existence), never 200.
Scope, caveats, and engineering boundaries
The BOLA/IDOR risk framing in this guide comes from the public OWASP API Security Project, and the row-isolation mechanics follow official PostgreSQL documentation on Row Security Policies. The multi-layer pattern, transaction context binding, and negative-test workflow reflect ToanKhonTech's engineering practice rather than an audit of any external vendor system.
When running PostgreSQL RLS behind transaction-mode poolers such as PgBouncer, session variables must be bound with SET LOCAL inside an explicit transaction so tenant context never leaks across pooled requests. Cross-tenant admin reports or reconciliation jobs require a separate, audited service role, and negative test cases must expand alongside every new API route or export format.