Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Hibernate's createCriteria() sanitize input?

Came across some code today that uses Hibernate to perform a query. The query uses a value submitted from a form. It made me curious as to whether or not this sort of code "sanitizes" its input.

public List<School> search(String query) {
    Session session = this.getCurrentSession();
    query = "%" + query + "%";
    Criteria criteria = session.createCriteria(getPersistentClass());
    criteria.createAlias("country", "a");
    Criterion nameCriterion = Restrictions.ilike("name", query);
    Criterion cityCriterion = Restrictions.ilike("city", query);
    Criterion countryCriterion = Restrictions.ilike("a.name", query);
    Criterion criterion = Restrictions.or(Restrictions.or(nameCriterion, cityCriterion), countryCriterion);
    criteria.add(criterion);
    return criteria.list();
}

Is this safe?

like image 840
Marvo Avatar asked Apr 05 '13 19:04

Marvo


People also ask

What is createCriteria in Hibernate?

The Hibernate Session interface provides createCriteria() method, which can be used to create a Criteria object that returns instances of the persistence object's class when your application executes a criteria query.

Does Hibernate protect against SQL injection?

Hibernate does not grant immunity to SQL Injection, one can misuse the api as they please. There is nothing special about HQL (Hibernates subset of SQL) that makes it any more or less susceptible.

Is Criteria API deprecated?

The Criteria API allows us to build up a criteria query object programmatically, where we can apply different kinds of filtration rules and logical conditions. Since Hibernate 5.2, the Hibernate Criteria API is deprecated, and new development is focused on the JPA Criteria API.


2 Answers

Hibernate Criteria Queries are quiet safe in terms of Sql Injection since they pass strings as parameter while performing any fetch. Even, Hql is quiet safe unless you build the query via string literal.

For more details, you should take a look at queries getting fired at the database level by switching on hibernate sql logging.

like image 173
Ankit Bansal Avatar answered Sep 21 '22 02:09

Ankit Bansal


If you think to SQL injection attacks, then yes, Hibernate Criteria API is safe.

It will generate the underlying query by first compiling it from the specified query fields and only after apply the query parameters (It should use a classical PreparedStatement). This way the JDBC driver will know which part of the query are fields and which part are parameters. Then the driver will take care to sanitize the parameters.

Tough you should take care with the SQL restrictions applied on the Criteria, if you need to place parameters there. For example

String vulnerable = //parameter from user interface 

criteria.add(
    Restrictions.sqlRestriction("some sql like + vulnerable") //vulnerable

criteria.add(
    Restrictions.sqlRestriction("some sql like ?", 
              vulnerable, Hibernate.STRING)) //safe

In this case the vulnerable parameter could "leak" in to the query fields part and be bypassed by JDBC driver checking as in a normal vulnerable SQL query.

like image 33
dcernahoschi Avatar answered Sep 20 '22 02:09

dcernahoschi