← Java / Spring · Contents

JAVA-099 / Intermediate

Privilege escalation: missing administrator checks on role-change APIs

A01:2021 Broken Access Control · CWE-269 Improper Privilege Management

Why is this a problem?

Ordinary users can change their own or others' privileges through /users/{id}/role.

How to fix it

java
@PreAuthorize("hasRole('ADMIN')")
@PostMapping("/users/{id}/role") void setRole(@PathVariable Long id, @RequestParam Role r) { ... }

Further reading