How to prevent sql injection when using spring data couchbase with Query annotation?
I am having Controller, Service, Repository kind of Spring web project having enterprise couchbase server as DB, I am using spring-data-couchbase for querying data of couchbase, and I am using @Query Annotation to put custom queries.
Recently I came across a case where, i am having @Param String name as method param. in one of the request, i got name param’s value was having escaped double quotes, which has broken my couchbase query which is complex query but its simple representation is below
@Query(
“”"
#{n1ql.selectEntity} WHERE #{n1ql.filter}
AND name = “#{#name}” AND …
“”")
→ I want to sanitize value having escaped double quotes in “#{#name}”, using standard owasp library, and I do not want to do custom regex things.
Use $name (no quotes) instead of “#{#name}”. Using #{#name} is handled by the Spring SPEL parser and is simply a string replace. $name (or $1) is a n1ql query parameter and there is no possibility for sql injection.
@Query(
"""
#{#n1ql.selectEntity} WHERE #{#n1ql.filter}
AND name = $name
""")
List<Info> getInfoByName(@Param("name") String name);
Thanks m-reiche for answer,
above is loose description of my InfoRepository.java file’s method to extract data from couchbase, can I still use @Param annotation of spring-data-couchbase, in case i am doine $name instead of “#{#name}”