0

やあみんな!

私はasp.net mvc 3とAntiXssLibrary 4.2を使用しており、テキストを一重引用符または二重引用符でエンコードしようとしていますが、問題は ' "'または"の代わりに得られることであり、ヘブライ語では非常に便利です( øמב"םまたはצのように) 'ק)。このメソッドのヘブライ語とデフォルトのパラメーターに含まれていることを私は知っています:

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

すべてのエンコード方法を試してみましたが、期待どおりの結果は得られませんでした。

編集:

このようなhtml文字列をビューに配置しようとする2番目の問題について

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

レンダリングされたページではなく、すべてのhtml形式を取得します。問題は、MicrosoftがGetSafeHtml()メソッドをHtmlSanitizationLibraryアセンブリに移動することでした-この回答で見つけて、ここからダウンロードします。こんな感じで使えるようになりました

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

その後、もちろん参照を追加しました

using Microsoft.Security.Application;

今、私はそれらのクォートで立ち往生しています。

4

2 に答える 2

1

わかりました。' "レンダリングされた html ページにアクセスすると、二重の html エンコーディングの問題が発生していることに気付きます。

状況を再現するには、 Replication:コードをコピーしてビューの 1 つに貼り付け、問題を自分で確認します。

HtmlStringMvcHtmlStringすでにエンコードされている 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 エンコードされている文字列には Html.Raw を使用します。または、安全でない非 html エンコード文字列については、razor の @ 構文に依存するだけです。


  • 複製:

以下は、シナリオを再現するためのコードです。そして、サンプル出力と画像。ビューの 1 つに以下のコードを挿入します。

@{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>

サンプル出力イメージ

.doubleEncodedHtml

于 2014-03-20T14:38:33.323 に答える