LINQ to SQL significantly reduces the capabilities of SQL Injection, but does not completely eliminate it. For example, if you use stored procedures and call spExecuteSQL on a concatenated string inside SQL Stored Proc, you are still exposed to SQL Injection. Of course, this applies regardless of data access technology and shows that even stored procs cannot eliminate the SQL Injection feature.
In addition, LINQ to SQL DataContext offers the ability to pass an SQL Pass-through query as a string that can be embedded. For example, the following returns all rows in the Authors table:
string searchName = "Good' OR ''='"; TypedDataContext context = this; string sql = @"Select ID, LastName, FirstName, WebSite, TimeStamp " + "From dbo.Author " + "Where LastName = '" + searchName + "'"; IEnumerable<Author> authors = context.ExecuteQuery<Author>(sql);
LINQ to SQL allows you to use the safe version for injection if you use string parameter holders and pass parameters to the overloaded ExecuteQuery version, which accepts a pararray parameter for objects:
string sql = @"Select ID, LastName, FirstName, WebSite, TimeStamp " + "From dbo.Author " + "Where LastName = {0}"; IEnumerable<Author> authors = context.ExecuteQuery<Author>(sql, searchName);
Fortunately, if you are standard LINQ methods, you can avoid SQL injection with LINQ to SQL. On the other hand, in the Entity Framework, on the other hand, there are some other potential areas that you can use if you are thinking of going down this route.
source share