LINQ的ExecuteCommand是否提供针对SQL注入攻击的保护?
时间:2020-03-06 14:58:44 来源:igfitidea点击:
我有一种情况,我需要使用LINQ的ExecuteCommand方法来运行插入。
类似于(出于此问题的目的而简化):
object[] oParams = { Guid.NewGuid(), rec.WebMethodID }; TransLogDataContext.ExecuteCommand ( "INSERT INTO dbo.Transaction_Log (ID, WebMethodID) VALUES ({0}, {1})", oParams);
问题是,这是否以与参数化查询相同的方式证明了SQL注入能力?
解决方案
做了一些研究,我发现了这一点:
In my simple testing, it looks like the parameters passed in the ExecuteQuery and ExecuteCommand methods are automatically SQL encoded based on the value being supplied. So if you pass in a string with a ' character, it will automatically SQL escape it to ''. I believe a similar policy is used for other data types like DateTimes, Decimals, etc.
http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx
(我们可以向下滚动找到它)
对我来说,这似乎有点奇怪,大多数其他.Net工具都比" SQL转义"任何东西都更好。他们使用真实的查询参数来代替。
LINQ to SQL使用带有参数的exec_sql,这比将其串联到临时查询字符串中要安全得多。 SQL注入应该与使用SqlCommand及其Paramaters集合一样安全(实际上,这可能是LINQ to SQL在内部使用的)。再说一遍,那有多安全?