2014-03-18 63 views
0

Heyy all!如何在HtmlEncode中将引号输入“白名单”?

我使用的是asp.net mvc 3和AntiXssLibrary 4.2,我尝试使用单引号或双引号对一些文本进行编码,问题是我得到' "而不是“或”,并且在希伯来文中它们非常有用רמב"ם或צ'ק)。我知道,有包括在这种方法希伯来语和默认参数:

UnicodeCharacterEncoder.MarkAsSafe(
     LowerCodeCharts.Default | LowerCodeCharts.Hebrew, 
     LowerMidCodeCharts.None, 
     MidCodeCharts.None, 
     UpperMidCodeCharts.None, 
     UpperCodeCharts.None); 

我尝试所有没有预期结果的编码方法。

编辑:

我的第二个问题,我尽量把我的观点一个HTML字符串这样

return new HtmlString(Encoder.HtmlEncode(resFile)); 

和我得到的所有的HTML格式,而不是呈现的页面,这个问题是微软将GetSafeHtml()方法移动到HtmlSanitizationLibrary程序集 - 我找到on this answer,我从here下载它。现在我可以这样使用它

return new HtmlString(Sanitizer.GetSafeHtml(questionsAnswerString)); 

之后,当然我说的参考

using Microsoft.Security.Application; 

现在,我只能和那些qoutes'任何帮助吗?

+1

可能是你可以编辑你的问题说你的预期输出是什么?如果您将html标记传递给Encoder.HtmlEncode(),那么它应该作为html写在网页上,并且不会被浏览器解析和呈现。请尝试阅读[Html编码和MvcHtmlString.Create,Html.Raw和@](http://renouncedthoughts.wordpress.com/2013/01/10/html-encoding-and-mvchtmlstring-create-html-raw-and /),看看它是否有帮助。 – gmaran23

+0

你是对的!我试图得到safeHtml像antixss – oCcSking

回答

0

对于麻烦,我很抱歉,但不可能把这些字符列入白名单。 我们可以在微软参考资源MarkAsSafe上看到hare。 他打电话ApplyHtmlSpecificValues()有我们可以看到

private static void ApplyHtmlSpecificValues() { 
     characterValues['<'] = "lt".ToCharArray(); 
     characterValues['>'] = "gt".ToCharArray(); 
     characterValues['&'] = "amp".ToCharArray(); 
     characterValues['"'] = "quot".ToCharArray(); 
     characterValues['\''] = "#39".ToCharArray(); 
    } 

反正他们保留这些字符,所以你不能编码后得到他们。

所以我认为应该调用这个函数唯一的办法就是总是从一个地方执行后只是改变了字符后面:(

return Encoder.HtmlEncode(input).Replace("&quot;", "\"").Replace("&#39;", "'"); 

10倍;)

1

好的,如果您在呈现的html页面上获得&#039; &quot;,那么发生在我身上的是您正在运行双HTML编码问题。

要复制您的情况,请在您的某个视图中复制并粘贴复制:代码,然后亲自查看该问题。

HtmlString and MvcHtmlString不应该编码已编码的html字符串。所以你的情况要么

return new HtmlString(Encoder.HtmlEncode(resFile)); 

Sanitizer.GetSafeHtml(questionsAnswerString) 

返回一个字符串,它是HTML编码,并且之后在视图中你实际上编码一个更多的时间。

这可能在你看来这实际上是使你的内容,因为发生,你正在使用的剃刀

@alreadyHtmlEncodedString 
// razor's @ syntax html encodes the given string 
//(irrespective of the fact that the given string is not html encoded 
//or the given string is html encoded already or whatever. 
//it just encodes the given string) 

或ASPX

<%:alreadyHtmlEncodedString%> 
// aspx's <%: %> html encodes the given string 
//(irrespective of the fact that the given string is not html encoded 
//or the given string is html encoded already or whatever. 
//it just encodes the given string) 

所以,如果是这样的话。使用Html.Raw作为已经被html编码的字符串。或者只是依赖剃刀的@语法来处理不安全的非HTML编码字符串,无论哪种方式。


  • 复制:

下面是复制您的情况,如果它可以帮助一些代码。还有一个示例输出以及一个图像。将下面的代码放在您的某个视图中。

@{string quotes = @"'"""; 
    string quotesHtmlEncoded = Html.Encode(@"'"""); 
    string hebrew = @"like רמב""ם or צ'ק"; 
    string hebrewHtmlEncoded = Html.Encode(@"like רמב""ם or צ'ק"); 
    string sampleXss = "<script>alert('1')</script>"; 
    string sampleXssHtmlEncoded = Html.Encode("<script>alert('1')</script>"); 
} 

<table border="1"> 
    <thead> 
     <tr> 
      <th></th> 
      <th>razor @@ 
      </th> 
      <th>Raw 
      </th> 
      <th>MvcHtmlString.Create 
      </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>quotes 
      </td> 
      <td> 
       @quotes 
      </td> 
      <td> 
       @Html.Raw(quotes) 
      </td> 
      <td> 
       @MvcHtmlString.Create(quotes) 
      </td> 
     </tr> 
     <tr> 
      <td>quotesHtmlEncoded 
      </td> 
      <td> 
       @quotesHtmlEncoded 
      </td> 
      <td> 
       @Html.Raw(quotesHtmlEncoded) 
      </td> 
      <td> 
       @MvcHtmlString.Create(quotesHtmlEncoded) 
      </td> 
     </tr> 
     <tr> 
      <td>hebrew 
      </td> 
      <td> 
       @hebrew 
      </td> 
      <td> 
       @Html.Raw(hebrew) 
      </td> 
      <td> 
       @MvcHtmlString.Create(hebrew) 
      </td> 
     </tr> 
     <tr> 
      <td>hebrewHtmlEncoded 
      </td> 
      <td> 
       @hebrewHtmlEncoded 
      </td> 
      <td> 
       @Html.Raw(hebrewHtmlEncoded) 
      </td> 
      <td> 
       @MvcHtmlString.Create(hebrewHtmlEncoded) 
      </td> 
     </tr> 
     <tr> 
      <td>sampleXss 
      </td> 
      <td> 
       @sampleXss 
      </td> 
      <td> 
       @Html.Raw(sampleXss) 
      </td> 
      <td> 
       @MvcHtmlString.Create(sampleXss) 
      </td> 
     </tr> 
     <tr> 
      <td>sampleXssHtmlEncoded 
      </td> 
      <td> 
       @sampleXssHtmlEncoded 
      </td> 
      <td> 
       @Html.Raw(sampleXssHtmlEncoded) 
      </td> 
      <td> 
       @MvcHtmlString.Create(sampleXssHtmlEncoded) 
      </td> 
     </tr> 
    </tbody> 
</table> 

sample output image

doubleEncodedHtml

+0

的老版本..真的非常感谢您对投资的回应.. 在任何情况下,问题是我们得到的视图.... 在控制器上我发现报价取代编码文本。 – oCcSking

相关问题