ASP.NET避免跨站脚本攻击(XSS)


可以采用微软的AntiXSS,最新版本是4.2.1 

下载地址:

http://www.microsoft.com/en-us/download/details.aspx?id=28589

http://wpl.codeplex.com/

 

原来的代码是:

string safeHtml = AntiXss.GetSafeHtmlFragment(html);

Console.WriteLine(safeHtml);

 

但现在,你会发现会提示:

 

Microsoft.Security.Application.Antixss已过时,

This class has been deprecated. Please use Microsoft.Security.Application.Encoder instead.

 

4.0以后的版本,已经将AntiXss中的HtmlSanitizationLibrary独立出来,使用时需要引用HtmlSanitizationLibrary.dll,

using Microsoft.Security.Application;

string safeHtml = Sanitizer.GetSafeHtmlFragment(html);

 

GetSafeHtml 方法:过滤完整的HTML页面。 

GetSafeHtmlFragment 方法:将传入的HTML 视为一个HTML 片段进行过滤。

 

存在的问题:

AntiXSS会替换除<p><span>等之外的所有HTML标记,无法实现仅过滤script等标记。

如果采用正则表达式来过滤,内容较多时效率较低。黑名单方式的过滤很容易有漏网之鱼,还是采用白名单方式更安全些!

 

 

更多XSS方面的内容,可以阅读:

https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29

http://ha.ckers.org/xss.html

 

一篇清理HTML的文章也很值得参考:

http://refactormycode.com/codes/333-sanitize-html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM