goo*_*ate 7 .net c# linq security entity-framework-4
.NET或其中一种语言可以强制清除不受信任的数据......还是防止在错误的位置意外变量使用?
其中一个例子是在SQL事务中使用用户POST的数据和"原始"响应.这可能会导致从客户端脚本漏洞到整个服务器受到攻击的任何问题.
另一个例子是我必须将数据传递给COM对象以进行进一步处理.
作为C#,ASP.net和SQL开发人员,我有哪些选择来确保我的用户的脏位在清理之前不会触及任何内部的东西?我可以利用运行时(或编译器)的功能吗?
如果没有语言实际执行它,也许我只能在我的传入变量中添加_dirty的后缀.这是你推荐的最佳做法吗?
专业人士如何解决这个问题?
更新
这是我要去的概念方向
根据目前给出的答案(特别是SteveCzetty和Erlend),这有点受到启发
例如:
public Interface ICleanForJavascript { bool IsCleanForJavascript(); }
public Interface ICleanForXSS { bool IsCleanForJavascript(); }
public class DirtyData
{
string Name {get; set;}
}
public class CleanData
{
private CleanData() {}
string Name {get; private set;}
// Perhaps use casting to support the conversion from Dirty to Clean data
// Might use this in an option explicit DirtyData CleanData(object o); command
public static CleanData Validate(DirtyData d)
{
CleanData data = new CleanData();
if (ValidateString(d.Name))
{
data.Name = d.Name
}
else
{
throw new ValidationException();
}
return CleanData;
}
}
[RequiresCleanedDataAttribute(ICleanForJavascript )]
public void DoSomething(CleanData data)
{
//...
}
Attribute RequiresCleanedDataAttribute(object arrayOfCleanings[])
{
// do some reflection on the method signature and see if the object supports the required interfaces
}
Run Code Online (Sandbox Code Playgroud)
有了上述,那么:
DoSomething(new DirtyData()); // Compiler Error, or runtime error if Attribute validation fails
DoSomething(CleanData.Validate(new DirtyData())); // Compiles
Run Code Online (Sandbox Code Playgroud)
.NET或其中一种语言可以强制清除不受信任的数据
是的它可以,但不是你问的方式.相反,您可以通过正确参数化来确保干净的sql.例如:
string sql = "SELECT * FROM [table] WHERE [column] = @value";
using (var cn = new SqlConnection("connection string here"))
using (var cmd = new SqlCommand(sql, cn)
{
cmd.Parameters.Add("@value").Value = "'';DROP Table Users;--";
cn.Open();
SomeControl.DataSource = cmd.ExecuteReader();
SomeControl.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
该代码运行起来非常安全,即使有明显的注入尝试.原因是SqlCommand对象的参数集合永远不会直接在查询中替换.数据被发送到服务器并完全独立于代码处理,因此无论用户输入什么内容,一切都是安全的.
你做的其他任何试图"消毒"请求的东西都将是与破解者的军备竞赛.这样,正确的方式,将数据和代码分开.