← Java / Spring · Contents

JAVA-060 / Beginner

Using @RequestBody without input validation (missing Bean Validation)

A03:2021 Injection · CWE-20 Improper Input Validation

Why is this a problem?

Length, range and format are not checked before forwarding data to the DB or external APIs.

How to fix it

java
@PostMapping void create(@Valid @RequestBody ProductDto dto) { ... }
record ProductDto(@NotBlank @Size(max=100) String name, @PositiveOrZero int price) {}

Further reading