2012-11-18 8 views
10

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

答えて

4

自動的な方法はありません。一番近いのは、AntiXss Nugetパッケージを入手することです。次に、あなたのコントローラで、以下のようにそれを使用することができます:

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

OR

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

を使用する場合は、

Server.HtmlDecode("HtmlEncodedString"); 

を使用して、それをデコードすることができ、この情報がお役に立てば幸いです。

10

まず、afaik、何もそれのために組み込まれていません。 しかし、MVCは、あなたの

public class CustomAntiXssAttribute : Attribute { } 

を定義し、それをあなたの特性を飾る(および必要に応じてさえAllowHtmlAttributeから継承する)ことができ、カスタムModelBindersを経由して簡単にそのようなことを行うことができます。次に、モデルとあなたが特定の抗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ベースのフィルタリングを適用することができます。

P.S. 、(私は忘れてしまった:))

+3

YOUは "ITはシールクラスの" ALLOWHTMLATTRIBUTEを継承することはできません – VJAI

0

未テストコードのApplication_StartにModelBinders.Binders.DefaultBinder = new CutstomModelBinder();を追加することを忘れないでください

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(); 
1

私はRegularExpressionデータアノテーション検証と属性AllowHtmlそれらを置き換えるために行くだろう。利点は、この方法でエラーをトラップし、前者がエラーをグローバルレベルで発生している間に何がうまくいかなかったかをユーザーに示すことができます。

例:

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

参考:RegularExpression validator encoding regex < & > symbols as &lt; &gt;, causing jQuery validation to fail

関連する問題