2014-03-18 62 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

相關問題