← Java / Spring · Contents

JAVA-001 / Beginner

SQL injection through JPQL/HQL string concatenation

A03:2021 Injection · CWE-89 SQL Injection

Why is this a problem?

Input becomes query syntax, allowing conditions to be bypassed and data to be leaked.

How to fix it

java
em.createQuery("select u from User u where u.name = :name", User.class)
  .setParameter("name", name).getResultList();

Further reading