Fix BOLA (Broken Object Level Authorization) in Micronaut
BOLA (Broken Object Level Authorization), formerly known as IDOR, is the top threat in the OWASP API Security Top 10. In Micronaut, this occurs when an endpoint exposes an object identifier and fails to validate that the authenticated user actually owns or has permission to access that specific resource. Attackers simply iterate through IDs to exfiltrate data. To kill BOLA, you must move beyond simple authentication and implement granular, resource-level ownership checks in your service layer or controllers.
The Vulnerable Pattern
@Controller("/api/v1/invoices") @Secured(SecurityRule.IS_AUTHENTICATED) public class InvoiceController {@Inject InvoiceRepository repository; @Get("/{id}") public Invoice getInvoice(Long id) { // VULNERABILITY: Trusting the user-provided ID without checking ownership. // Any authenticated user can access any invoice by changing the ID in the URL. return repository.findById(id).orElse(null); }
}
The Secure Implementation
The fix involves three pillars of defense. First, we inject the 'Authentication' object to retrieve the verified identity of the requester. Second, we fetch the object from the database and perform a server-side ownership comparison ('invoice.getOwnerId().equals(currentUserId)'). Third, we return a 403 Forbidden (or a 404 to hide the resource's existence) if the ownership check fails. For complex scenarios, consider using Micronaut's 'SecurityRule' or a dedicated 'AccessControlService' to centralize these authorization predicates.
@Controller("/api/v1/invoices") @Secured(SecurityRule.IS_AUTHENTICATED) public class InvoiceController {@Inject InvoiceRepository repository; @Get("/{id}") public HttpResponse<Invoice> getInvoice(Long id, Authentication authentication) { String currentUserId = authentication.getName(); return repository.findById(id) .map(invoice -> { // SECURE: Explicitly verify that the resource belongs to the requester if (invoice.getOwnerId().equals(currentUserId)) { return HttpResponse.ok(invoice); } else { // Return 403 Forbidden or 404 Not Found to prevent resource enumeration return HttpResponse.<Invoice>status(HttpStatus.FORBIDDEN); } }) .orElse(HttpResponse.notFound()); }
}
About this page
Framework notes in /guides are generated sketches kept for URL stability. They are not human pentest reports and they are not GuardAPI scan output. The product is a GET-only BOLA merge gate. Maintained by GuardAPI. Questions: support@guard-api.com