Using MS Anti XSS library for sanitizing HTML

霸气de小男生 提交于 2019-12-05 09:15:55

You should be set. Though obviously this won't protect you from anything already in the database.

You could use AntiXSS.GetSafeHtmlFragment() while outputting the page instead of when saving. But doing when saving is probably safer. You would not want to do it both while rendering and saving though.

The whitelist is not editable.

In regards to your question about "black box": yes, it's a black box, and my understanding is that you can't edit it. If you're looking for more granularity, check out the AntiSamy.NET project.

In the latest 4.x Anti-XSS libraries, GetSafeHtml() and SetSafeHtmlFragment() are under the Sanitizer class in Microsoft.Security.Application, which was moved to the HtmlSanitizationLibrary assembly.

[Deprecated Link: http://www.microsoft.com/en-us/download/details.aspx?id=28589 ] Update: This looks like it was moved into a NuGet package: https://www.nuget.org/packages/HtmlSanitizationLibrary/

You're almost there. You need to make sure that you choose the proper encoding. For example, if the user input went into a url, you'd need to use AntiXSS.UrlEncode(), and if it went into JavaScript you'd want to use AntiXSS.JavaScriptEncode(). If you can't guarantee when you save the input what the output format will be, it's better to do the sanitizing at output time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!