Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how much safe from SQL-Injection if using hibernate

Does Hibernate guard against SQL injection attack? If i am using hibernate then am i completely safe from SQL injection attack? I heard that Using Hibernate to execute a dynamic SQL statement built with user input can allow an attacker to modify the statement's meaning or to execute arbitrary SQL commands.

like image 566
harry Avatar asked Mar 07 '12 05:03

harry


People also ask

Is hibernate safe from 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.

How hibernate prevent SQL injection in Java?

We should always try to use stored Procedures in general to prevent SQLInjection.. If stored procedures are not possible; we should try for Prepared Statements. I agree, it is technically better, because of speed. however, no ORM framework I know of in any language generates stored procedure s.

What protects against SQL injection?

Developers can prevent SQL Injection vulnerabilities in web applications by utilizing parameterized database queries with bound, typed parameters and careful use of parameterized stored procedures in the database. This can be accomplished in a variety of programming languages including Java, . NET, PHP, and more.


1 Answers

Does Hibernate guard against SQL injection attack?

No, it doesn't guard the wrongly written ones, So you need to be careful when you write the queries. Always use the prepared statement style, for example consider the below HQL queries,

String query1 = "select * from MyBean where id = "+ id;
String query2 = "select * from MyBean where id = :id";

query1 ** is still vulnerable to **SQL Injection where as query2 is not.

So In short hibernate provides you many ways that you should use to guard yourself from the SQL Injection attacks.

like image 199
ManuPK Avatar answered Oct 26 '22 12:10

ManuPK