.NET 库来清理输入?
是否有经过彻底测试的 .NET 库可以清理脚本/sql 注入等输入?
Are there any thoroughly tested .NET libraries out there to sanitize input from things like script/sql injection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SQL 注入和跨站脚本(又名 XSS 或脚本注入)是不同的问题。
1) SQL 注入非常简单,始终使用参数化查询(SQLParameter),并尽量不要在 T-SQL 存储过程中执行 sp_exec @query。 .Net 参数化查询无法防止这种二阶注入。
2) XSS 更难普遍缓解,因为 HTML 文档中有很多地方可以插入 JavaScript。 使用 AntiXSS 对用户数据进行编码的建议是正确的。 在将用户数据插入输出文档之前使用此库。 不幸的是,如果您使用 ASP.Net 服务器控件对所有数据进行编码,可能会导致双重编码和显示伪像。 发生这种情况是因为某些控件属性对数据进行编码,而另一些则不编码。 请参阅 此表来查找默认编码的属性。 在分配给任何不编码的属性之前,请使用 Anti-XSS。
SQL injection and Cross-Site Scripting (a.k.a. XSS or Script Injection) are different problems.
1) SQL Injection is very easy, always use parametrized queries (SQLParameter) and try really hard to NEVER do sp_exec @query within T-SQL stored procedures. .Net parametrized queries will not protect against this second order injection.
2) XSS is more difficult to universally mitigate since there are so many places that JavaScript can be inserted into HTML documents. The recommendations to use AntiXSS for encoding user data is right on. Use this library before inserting user data into output documents. Unfortunately, if you are using ASP.Net server controls encoding all data may lead to double-encoding and display artifacts. This happens because some control properties encode data while others don't. Refer to this table to find out the properties encoded by default. Use Anti-XSS before assigning to any properties that don't encode.
使用参数化命令来防范 SQL,而不是尝试清理字符串注射。
Use parameterised commands, rather than trying to sanitize strings, to guard against SQL Injection.
AntiXSS 可用于防止 XSS 攻击。
AntiXSS can be used for preventing XSS attacks.
如果您使用的是 ASP.NET 4.5,您现在可以使用附带的 AntiXSS 功能在框架中。
这些是已合并到 ASP.NET 4.5 中的外部 AntiXSS 库的部分:
HtmlEncode
、HtmlFormUrlEncode
和HtmlAttributeEncode
XmlAttributeEncode
和XmlEncode
UrlEncode
和UrlPathEncode
(新)CssEncode
If you are using ASP.NET 4.5 you can now use the AntiXSS features that ship in the framework.
These are the portions of the external AntiXSS Library that have been incorporated into ASP.NET 4.5:
HtmlEncode
,HtmlFormUrlEncode
, andHtmlAttributeEncode
XmlAttributeEncode
andXmlEncode
UrlEncode
andUrlPathEncode
(new)CssEncode
我喜欢使用 Microsoft AntiXSS 库。 它是免费的并且非常易于使用。
对于 SQL 注入,我总是使用参数。 同样,它们很容易使用,而且我不喜欢尝试转义特殊字符。 如果你问我的话,这就是灾难的根源。
I like to use the Microsoft AntiXSS library. It's free and pretty easy to use.
For SQL injection, I always use parameters. Again, they are easy to use and I don't like trying to escape special characters. It's a recipe for disaster if you ask me.