10

一部のモデル プロパティは、AllowHtml 属性でマークされています。これらのフィールドに AntiXss 保護を自動的に適用する (つまり、許可されたタグのみをフィルタリングする) 方法はありますか?

4

4 に答える 4

10

まず、私の知る限り、そのために組み込まれているものは何もありません。しかし、MVC では、カスタム ModelBinders を介してそのようなことを簡単に行うことができます。

public class CustomAntiXssAttribute : Attribute { }

それを使用してプロパティを装飾します(必要に応じて継承することもAllowHtmlAttributeできます)。次に、モデル バインダーを使用して、特定のアンチ xss 保護を追加できます。

    public class CutstomModelBinder : DefaultModelBinder
    {
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.Attributes.OfType<CustomAntiXssAttribute>().Any())
            {
                var valueResult = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
                var filteredValue = SOME_CUSTOM_FILTER_FUNCTION_HERE(valueResult.AttemptedValue);
                propertyDescriptor.SetValue(bindingContext.Model, filteredValue);
            }
            else // revert to the default behavior.
            {
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }
        }
    }

その中で、SOME_CUSTOM_FILTER_FUNCTION_HERE@ Yogiraj が提案したものを使用したり、正規表現を使用したり、HtmlAgilityPack ベースのフィルタリングを適用したりすることもできます。

PS Application_Start に追加することを忘れないでくださいModelBinders.Binders.DefaultBinder = new CutstomModelBinder();(忘れていました:))

于 2012-11-24T08:59:47.583 に答える
4

自動的な方法はありません。最も近い方法は、AntiXssNugetパッケージを入手することです。次に、コントローラーで以下のように使用できます。

  Microsoft.Security.Application.Sanitizer.GetSafeHtml("YourHtml");

また

  Microsoft.Security.Application.Encoder.HtmlEncode("YourHtml");

使用する場合は、を使用してデコードできます

  Server.HtmlDecode("HtmlEncodedString");

お役に立てれば。

于 2012-11-18T16:49:26.043 に答える
1

AllowHtmlこれらの属性をRegularExpressionデータ注釈の検証に置き換えます。このようにして、エラーをトラップし、前者がグローバルレベルでエラーを発生させている間に、何が問題になったかをユーザーに示すことができるという利点があります。

例:

public class MyViewModel
{
    [DataType(DataType.MultilineText)]
    [RegularExpression(@"^[^\<\>]*$", ErrorMessage = "May not contain <,>")]
    public string Text { get; set; }
}

参照:正規表現<&>シンボルを&lt;としてエンコードするRegularExpressionバリデーター &gt;、jQueryの検証が失敗する原因

于 2012-11-26T12:16:11.887 に答える
0

テストされていないコード、

public class ADefaultModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelMetadata.RequestValidationEnabled)
        {
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
            value = value.Replace("&", "");// replace existing & from the value
            var encodedValue = Microsoft.Security.Application.Encoder.HtmlEncode(value);
            bindingContext.ModelMetadata.RequestValidationEnabled = encodedValue.Contains("&"); // Whether AntiXss encoded a char to &..
        }
        return base.BindModel(controllerContext, bindingContext);
    }
}
public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ModelBinders.Binders.DefaultBinder = new ADefaultModelBinder();
于 2012-11-26T06:56:53.953 に答える