← Java / Spring · Contents

JAVA-007 / Intermediate

Unvalidated Spring Data Sort input (JPA sorting injection)

A03:2021 Injection · CWE-89 SQL Injection

Why is this a problem?

Arbitrary properties or expressions in Sort.by(userParam) may expose errors and enable sorting-based SQL manipulation.

How to fix it

java
Set<String> ALLOW = Set.of("createdAt", "title");
String sort = ALLOW.contains(p) ? p : "createdAt";
repo.findAll(PageRequest.of(0, 20, Sort.by(sort)));

Further reading