← Java / Spring · Contents

JAVA-073 / Advanced

Concatenating string property names in JPA Criteria or QueryDSL

A03:2021 Injection · CWE-89 SQL Injection

Why is this a problem?

Input property names enter sorting or filtering expressions, enabling arbitrary column queries.

How to fix it

java
Map<String, SingularAttribute<?, ?>> ALLOW = Map.of("email", User_.email);
cb.equal(root.get(ALLOW.get(field).getName()), value);   // Metamodel allowlist

Further reading