← Java / Spring · Contents

JAVA-012 / Beginner

IDOR: looking up by ID without ownership checks

A01:2021 Broken Access Control · CWE-639 Authorization Bypass via User-Controlled Key

Why is this a problem?

Changing /orders/{id} enables access to another user's order.

How to fix it

java
@GetMapping("/orders/{id}") Order get(@PathVariable Long id, @AuthenticationPrincipal Me me) {
    return repo.findByIdAndOwnerId(id, me.getId()).orElseThrow(NotFound::new);
}

Further reading