Dynamic SQL statements are generated without the required data validation and without using parameterized statements or stored procedures.
Inject SQL statements, with the possibility of obtaining information about the database, as well as extract information from it.
Perform queries to the database through sentences or parameterized procedures.
Authenticated attacker from the Internet.
⌚ 15 minutes.
Default score using CVSS 3.1. It may change depending on the context of the src.
Default score using CVSS 4.0. It may change depending on the context of the src.
In Spring Data JPA framework
you can create SQL queries in many ways:
public interface UserRepository extends JpaRepository {
// Using Java Persistence Query Language (JPQL)
@Query("select u from User u where u.emailAddress = ?1")
List findByEmailAddress1(String emailAddress);
// Using Spring Expression Language (SpEL)
@Query("select u from User u where u.emailAddress = :#{[0]}")
List findByEmailAddress2(@Param("emailAddress") String emailAddress);
}
@Entity
// Using named queries
@NamedQuery(
name = "User.findByEmailAddress3",
query = "select u from User u where u.emailAddress = ?1"
)
public class User { /* ... */ }
JPQL
engine will escape
the following user-supplied input parametersJPQL
Positional Parameters:
?1
JPQL
Named Parameters:
:emailAddress
emailAddress
will be interpreted
by the SQL engine as a string literal,
with possible special characters
in a SQL context escaped.JPQL
query like this one:
@Query("select u from User u where u.emailAddress like %?1")
User findByEmailAddress(String emailAddress);
%
we added
in front of the JPQL positional parameter
?1
.emailAddress
equal to
a
will fetch all email addresses from the database
that end with the letter
a
.
The resulting query will be:
SELECT u FROM User u WHERE u.emailAddress LIKE '%a'
LIKE
conditions (or similar in its kind)%
in the
JPQL
statement.SpEL
Expressions are not escaped.
This happens because
SpEL
is designed
as an expression language,
not a SQL language.SpEL Expressions Bindings
like
:#{[0]}
or
?#{[0]}
will just copy the value of
[0]
into the SQL operation
to be executed by the database.SpEL
query like this one:
@Query("select u from User u where u.emailAddress = :#{[0]}")
List findByEmailAddress2(@Param("emailAddress") String emailAddress);
emailAddress
equal to
%
,
will fetch all email addresses from the database.
The evaluated query will be
SELECT u FROM User u WHERE u.emailAddress LIKE '%'
escape
function
from
SpEL
context as follow:
{3-4}
@Query(
"select u from User u " +
"where u.emailAddress like ?#{escape([0])} " +
"escape ?#{escapeCharacter()}"
)
List findByEmailAddress2(@Param("emailAddress") String emailAddress);
SELECT u FROM User u WHERE u.emailAddress LIKE '\%' ESCAPE '\'