当表为 varchar() 时,如何验证数据免受注入攻击
我公司的很多表格在应该输入的时候却没有输入。例如,当唯一有效值为 int
时,许多表都使用 varchar(##)
作为键控。由于依赖项列表现在已固定,我根本无法翻转所有数据类型。
因此,短期内我想提供某种验证数据的方式,以防止 Bobby Drop Tables 停下来访问。
在这种特殊情况下,我应该运行读取查询。我需要验证 Order
属性:
public class Model
{
public string Order { get; set; } // In DB this is a varchar(20)
public aType Read()
{
var result = from a in table
where a.Column == Order;
select new { ... };
}
}
如何根据我的模型验证用户输入?
A lot of the tables at my company don't type input when they should. For example, many tables are keyed with varchar(##)
when the only valid values are int
. Due to a list of now cemented in dependencies, I simply cannot just go flipping all the data types.
So, in the short term I want to provide some manner of validating data to prevent Bobby Drop Tables from stopping by for a visit.
In this particular case I should be running read queries. I need to validate the Order
property:
public class Model
{
public string Order { get; set; } // In DB this is a varchar(20)
public aType Read()
{
var result = from a in table
where a.Column == Order;
select new { ... };
}
}
How can I validate user input against my Model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Linq2Sql 和 Linq2Entities 都在后台使用 sql 参数来传递查询中的任何变量。您不会受到任何 SQL 注入攻击,但有人可能能够插入不仅仅是数字字符的字符串。
确保只能插入“数字”字符串的一种方法是在数据库中创建约束。这将确保所有使用数据库的应用程序都能正确运行,但不会强制它们更改数据类型。
编辑
如果您使用 Linq2Sql 并且希望在应用程序中进行检查,则可以在 Linq 上下文中的特定类型上实现部分方法
OnValidate
。Both Linq2Sql and Linq2Entities uses sql-parameters behind the scenes to pass any variables in the queries. You won't get any SQL injection attacks, but someone might be able to insert a string that is not just numerical characters.
One way to make sure you can only insert "numerical" strings is to create a constraint in the database. That will make sure all applications that uses the database behaves correctly, but does not force them to change datatypes.
Edit
If you use Linq2Sql and you want to have a check within your application you can implement the partial method
OnValidate
on the specific type in your Linq-context.使用 LINQ,您使用它的方式可以防止 SQL 注入。
只要您使用参数化查询,您就没有问题。
Using LINQ, the way you're using it, prevents SQL injection.
As long as you're using parametrized queries, you're in the clear.