Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is IQueryable SQL injection proof using Entity Framework?

I know that if I use linq to sql, everything will be parametrised and sql injection safe. But how about IQueryable?

For example, I can cast some entity to Iqueryable:

var myquery = mytesttable.AsQueryable();
var qText = "name="+ "\""+DynamicSearchCondition+ "\"";
myquery = myquery.Where(qText);

Then when the query is run, from trace I can see that the DynamicSearchCondition passed in is not parametrised.

Initially I thought this is not sql injection proof, but I then tried some examples, and just can't break this one. Does it mean it is sql injection free then (I think it is now)?

If that is true, will it mean all IQueryable are sql injection safe?

like image 511
daxu Avatar asked Oct 01 '15 11:10

daxu


People also ask

Does Entity Framework protect against SQL injection?

Generally speaking, Entity Framework uses LINQ-to-Entities parametrized queries, and it is not susceptible to traditional SQL Injection attacks. However, Entity Framework does allow for the use of raw SQL queries when working with a relational database, introducing the risk of writing injectable queries.

Does LINQ prevent SQL injection?

Yes, LINQ will help stop SQL injection. LINQ to SQL passes all data to the database via SQL parameters. So, although the SQL query is composed dynamically, the values are substitued server side through parameters safeguarding against the most common cause of SQL injection attacks.


1 Answers

Absolutely it is vulnerable to injection attacks.

For your particular example:

var myquery = mytesttable.AsQueryable();
var qText = "name="+ "\""+DynamicSearchCondition+ "\"";
myquery = myquery.Where(qText);

would fail with this:

var DynamicSearchCondition= "\" or \"\"=\"";
like image 145
Robert McKee Avatar answered Oct 03 '22 06:10

Robert McKee