针对 .NET DataView RowFilter 的注入攻击
因此,我正在编写一个处理程序,使用 DataView RowFilter 属性根据 AppRelativeCurrentExecutionFilePath 过滤缓存的 DataTable。 对输入进行编码以防止注入攻击的最佳方法是什么?
以下内容是否足够? 有更好/更优雅的方法吗?
dataView.RowFilter = String.Format("Name LIKE '{0}%'", EncodeString(query));
private string EncodeString(string s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (c == '*' || c == '%' || c == '[' || c == ']')
sb.Append("[").Append(c).Append("]");
else if (c == '\'')
sb.Append("''");
else
sb.Append(c);
}
return sb.ToString();
}
So I'm writing a handler that filters a cached DataTable based on the AppRelativeCurrentExecutionFilePath using the DataView RowFilter property. What's the best way to encode the input to prevent an injection attack?
Is the following sufficient? Is there a better/more-elegant way?
dataView.RowFilter = String.Format("Name LIKE '{0}%'", EncodeString(query));
private string EncodeString(string s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (c == '*' || c == '%' || c == '[' || c == ']')
sb.Append("[").Append(c).Append("]");
else if (c == '\'')
sb.Append("''");
else
sb.Append(c);
}
return sb.ToString();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在 RowFilter 中注入 sql。
编辑:正如所指出的,可以通过注入获取表中的所有行,可以像以下工作一样:
You cannot inject sql in a RowFilter.
Edit: As pointed out, it is possible to get all rows in the table by injection, could something like the following work: