Fix BOLA (Broken Object Level Authorization) in Spring Boot
BOLA (Broken Object Level Authorization), formerly known as IDOR, is the most critical vulnerability in modern API security. In Spring Boot, it manifests when a controller blindly trusts a client-provided ID to fetch a resource without verifying if the authenticated principal owns that specific object. If you're just calling repository.findById(id) without checking the owner, you're leaking data. Period.
The Vulnerable Pattern
@GetMapping("/api/v1/invoices/{id}")
public Invoice getInvoice(@PathVariable Long id) {
// VULNERABILITY: Any authenticated user can access any invoice ID
return invoiceRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
The Secure Implementation
To kill BOLA, you must enforce ownership at the logic or data layer. The secure example uses two patterns: 1. A custom SecurityService with @PreAuthorize to check resource ownership before the method executes. 2. Scoped Queries, which is the gold standard. Instead of fetching by ID alone, your JPA repository should fetch by ID AND the User ID extracted from the SecurityContext. If a user tries to access an ID they don't own, the query returns null, effectively treating unauthorized access as a '404 Not Found', which prevents resource enumeration.
@GetMapping("/api/v1/invoices/{id}") @PreAuthorize("@securityService.isInvoiceOwner(authentication, #id)") public Invoice getInvoice(@PathVariable Long id) { return invoiceRepository.findById(id) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); }
// Alternative: Scoped Query (Defense in Depth) @Query(“SELECT i FROM Invoice i WHERE i.id = :id AND i.user.username = :username”) OptionalfindByIdAndOwner(Long id, String username);
Your Spring Boot API
might be exposed to BOLA (Broken Object Level Authorization)
74% of Spring Boot apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.
Free Tier • No Credit Card • Instant Report
Verified by Ghost Labs Security Team
This content is continuously validated by our automated security engine and reviewed by our research team. Ghost Labs analyzes over 500+ vulnerability patterns across 40+ frameworks to provide up-to-date remediation strategies.