Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the @Query annotation in spring SQL Injection safe?

Do the parameters of a string passed to the @Query annotation, for Spring, get treated as pure data as they would if, for example, you were using the PreparedStatement class or any method meant to prevent SQL injection?

final String MY_QUERY = "SELECT * FROM some_table WHERE some_column = ?1";

@Query(value=MY_QUERY, nativeQuery = true)
List<SomeEntity> findResults(String potentiallyMaliciousUserInput);

Bottom Line: Is the code above susceptible to SQL injection?

like image 973
Usman Mutawakil Avatar asked Jan 20 '15 16:01

Usman Mutawakil


People also ask

What is @query annotation used for?

The @Query annotation can only be used to annotate repository interface methods. The call of the annotated methods will trigger the execution of the statement found in it, and their usage is pretty straightforward. The @Query annotation supports both native SQL and JPQL.

Is Spring data JPA safe from SQL injection?

Spring Data JPA is vulnerable to SQL Injection. spring-data-jpa is vulnerable to SQL injection. When Sort is used together with a String query, it does not check the path for Sort instances that can be created from untrustable sources.

Which is safe from SQL injection?

The only sure way to prevent SQL Injection attacks is input validation and parametrized queries including prepared statements.

What is @query used for spring data?

Spring Data JPA @Query The @Query annotation declares finder queries directly on repository methods. While similar @NamedQuery is used on domain classes, Spring Data JPA @Query annotation is used on Repository interface. This frees the domain classes from persistence specific information, which is a good thing.


1 Answers

It looks like Spring Data's @Query is just a wrapper around JPA

See this SO answer: Are SQL injection attacks possible in JPA?

like image 77
Alex Avatar answered Oct 17 '22 19:10

Alex