← Java / Spring · Contents

JAVA-006 / Intermediate

JPA @Query(nativeQuery=true) string concatenation

A03:2021 Injection · CWE-89 SQL Injection

Why is this a problem?

Concatenating parameters while constructing native queries leads to injection.

How to fix it

java
@Query(value = "SELECT * FROM users WHERE name = :name", nativeQuery = true)
List<User> findNative(@Param("name") String name);

Further reading