2013-07-10 2 views

답변

4

당신이 사용할 수있는 C#을위한 내장 된 살균제가 없다고 생각하지만 여기에는 같은 문제가있을 때했던 것이 있습니다. AjaxControlToolkit과 함께 제공되는 HtmlAgilityPackSanitizerProvider를 사용했습니다. 코드는 다음과 같습니다.

private static AjaxControlToolkit.Sanitizer.HtmlAgilityPackSanitizerProvider sanitizer = new AjaxControlToolkit.Sanitizer.HtmlAgilityPackSanitizerProvider(); 

private static Dictionary<string, string[]> elementWhitelist = new Dictionary<string, string[]> 
{ 
    {"b"   , new string[] { "style" }}, 
    {"strong"  , new string[] { "style" }}, 
    {"i"   , new string[] { "style" }}, 
    {"em"   , new string[] { "style" }}, 
    {"u"   , new string[] { "style" }}, 
    {"strike"  , new string[] { "style" }}, 
    {"sub"   , new string[] { "style" }}, 
    {"sup"   , new string[] { "style" }}, 
    {"p"   , new string[] { "align" }}, 
    {"div"   , new string[] { "style", "align" }}, 
    {"ol"   , new string[] { }}, 
    {"li"   , new string[] { }}, 
    {"ul"   , new string[] { }}, 
    {"a"   , new string[] { "href" }}, 
    {"font"   , new string[] { "style", "face", "size", "color" }}, 
    {"span"   , new string[] { "style" }}, 
    {"blockquote" , new string[] { "style", "dir" }}, 
    {"hr"   , new string[] { "size", "width", "id" }}, 
    {"img"   , new string[] { "src" }}, 
    {"h1"   , new string[] { "style" }}, 
    {"h2"   , new string[] { "style" }}, 
    {"h3"   , new string[] { "style" }}, 
    {"h4"   , new string[] { "style" }}, 
    {"h5"   , new string[] { "style" }}, 
    {"h6"   , new string[] { "style" }} 
}; 

private static Dictionary<string, string[]> attributeWhitelist = new Dictionary<string, string[]> 
{ 
    {"style" , new string[] {}}, 
    {"align" , new string[] {}}, 
    {"href"  , new string[] {}}, 
    {"face"  , new string[] {}}, 
    {"size"  , new string[] {}}, 
    {"color" , new string[] {}}, 
    {"dir"  , new string[] {}}, 
    {"width" , new string[] {}}, 
    {"id"  , new string[] {}}, 
    {"src"  , new string[] {}} 
}; 

public string SanitizeHtmlInput(string unsafeStr) 
{ 
    return sanitizer.GetSafeHtmlFragment(unsafeStr, elementWhitelist, attributeWhitelist); 
} 

희망이 있습니다.

+0

속성 Whitelist가 실제로 사용되지 않는 코드를 확인했다고 확신하지 않습니다. http://ajaxcontroltoolkit.codeplex.com/SourceControl/latest#Server/AjaxControlToolkit/Sanitizer/HtmlAgilityPackSanitizerProvider.cs –

1

HTML 문서를 살균하는 것은 많은 까다로운 작업이 필요합니다. 이 패키지는 도움이 될 것입니다 : https://github.com/mganss/HtmlSanitizer 나는 내 자신의 프로젝트에 사용합니다.

관련 문제