MVC Anti-XSS方案

 http://blog.csdn.net/cassaba/article/details/21094011

XSS攻击是用户提交到服务器的数据包含恶意JavaScript脚本,如果这种数据在存储或显示的时候不加处理,那么其它用户访问页面的时候,这些脚本可能被执行,轻则导致页面无法正常使用,重则导致重要信息泄露。

    开发Web应用程序,需要从全局考虑这个问题,采取一致的处理方式,在整个开发过程中严格执行,避免产生XSS漏洞。下面分别就Form和Json两种数据提交模式,所采取的方案做一下介绍。


1. Form提交模式

在使用Form提交的时候,MVC框架提供了一个默认的机制。如果数据中含有恶意字符,则会自动转向出错界面。如下图:




2. Ajax + JSON提交模式

     由于我所开发的项目,前端和后端交互主要通过Ajax + Json来进行。而MVC框架并未提供对于Json数据的anti-XSS支持, 所以必须自行实现。

基础思路是在MVC进行Model Binder的时候,检查request中的MIME类型,如果是application/json, 则接管系统默认的Model绑定和验证。


    Step 1: 定义一个Attribute [AllowHtml], 用于标记Model中的属性. 如果有这个标记,则说明该属性允许Html, 不需要验证

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2. /// 用于标记某个属性是否允许html字符  
  3. /// </summary>  
  4. [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]  
  5. public sealed class AllowHtmlAttribute : Attribute  
  6. {  
  7.   
  8. }  

   Step 2:  元数据解析的时候,动态设定标记为AllowHtml的属性不激发验证

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider  
  2. {  
  3.   
  4.     public CustomModelMetadataProvider()  
  5.     {  
  6.     }  
  7.   
  8.   
  9.     protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType,  
  10.                                                     Func<object> modelAccessor, Type modelType, string propertyName)  
  11.     {  
  12.         var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);  
  13.         if (containerType == null || propertyName == null)  
  14.             return metadata;  
  15.   
  16.         foreach (Attribute attr in attributes)  
  17.         {  
  18.             if (attr is AllowHtmlAttribute)  
  19.             {  
  20.                 metadata.RequestValidationEnabled = false;  
  21.                 break;  
  22.             }  
  23.         }  
  24.     }  
  25. }  

   Step 3:  实现自定义ModerBinder, 拦截所有json数据,进行anti-xss验证

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2.     /// 检测Json数据中含有恶意字符,抛出HttpRequestValidationException  
  3.     /// </summary>  
  4.     public class AntiXssModelBinder : DefaultModelBinder  
  5.     {  
  6.         protected override bool OnPropertyValidating(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)  
  7.         {  
  8.             if (controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))  
  9.             {  
  10.                 int index;  
  11.   
  12.                 if (controllerContext.Controller.ValidateRequest  
  13.                     && bindingContext.PropertyMetadata[propertyDescriptor.Name].RequestValidationEnabled)  
  14.                 {  
  15.                     if (value is string)  
  16.                     {  
  17.                         if (AntiXssStringHelper.IsDangerousString(value.ToString(), out index))  
  18.                         {  
  19.                             throw new HttpRequestValidationException("Dangerous Input Detected");  
  20.                         }  
  21.                     }  
  22.                     else if (value is IEnumerable)  
  23.                     {  
  24.                         // 字符串数组或者集合,或者Dictionary<string, string>  
  25.                         // Dictionary的Key, Value会ToString后一起验证                          
  26.                         foreach (object obj in value as IEnumerable)  
  27.                         {  
  28.                             if (obj != null)  
  29.                             {  
  30.                                 if (AntiXssStringHelper.IsDangerousString(obj.ToString(), out index))  
  31.                                 {  
  32.                                     throw new HttpRequestValidationException("Dangerous Input Detected");  
  33.                                 }  
  34.                             }  
  35.                         }  
  36.                     }  
  37.                 }  
  38.             }  
  39.   
  40.             return base.OnPropertyValidating(controllerContext, bindingContext, propertyDescriptor, value);  
  41.         }  
  42.     }  
  43.   
  44.     /// <summary>  
  45.     /// 检测绑定的单值字符串是否包含恶意字符  
  46.     /// </summary>  
  47.     public class AntiXssRawModelBinder : StringTrimModelBinder  
  48.     {  
  49.         public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)  
  50.         {  
  51.             var value = base.BindModel(controllerContext, bindingContext);  
  52.             if (value is string)  
  53.             {  
  54.                 var result = (value as string).Trim();  
  55.                 int index;  
  56.                 if (AntiXssStringHelper.IsDangerousString(value.ToString(), out index))  
  57.                 {  
  58.                     throw new HttpRequestValidationException("Dangerous Input Detected");  
  59.                 }  
  60.             }  
  61.   
  62.             return value;  
  63.         }  
  64.     }  
  65.   
  66. }  


Step 4: 在Web站点启动的时候,配置MVC框架

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. RouteTableRegister.RegisterRoutes(_routes);  
  2.   
  3. BundleConfig.RegisterBundles(BundleTable.Bundles);  
  4.   
  5.   
  6. ModelMetadataProviders.Current = new CustomModelMetadataProvider();  
  7. ModelBinders.Binders.DefaultBinder = new AntiXssModelBinder();  

以下面的Model为例子。

public class LoginModel

    {

       [Required]

       [Display(Name="用户名")]

       [AllowHtml]

       publicstringUserName { get;set; }

 

       [Required]

       [DataType(DataType.Password)]

       [Display(Name="密码")]

       publicstringPassword { get;set; }

    }


Password属性未被标记为[AllowHtml], 如果Password传递进来的数据是:<script> alert(4)</script>qwe@1232432<b>粗体</b>

则系统会抛出异常,反之,如果UserName 传递这样的数据则不会报告异常。


如果Action参数通过简单类型传递,并且要验证XSS,则需要使用Model绑定标记, 比如下面的returnUrl.

当然实际开发的时候,最好是参数封装为对象。

        publicActionResultLogin(LoginModelmodel[ModelBinder(typeof(AntiXssRawModelBinder))]stringreturnUrl)

       {

         ….

       }

AntiXssRawModelBinder 代码:

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2.     /// 检测绑定的单值字符串是否包含恶意字符  
  3.     /// </summary>  
  4.     public class AntiXssRawModelBinder : StringTrimModelBinder  
  5.     {  
  6.         public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)  
  7.         {  
  8.             var value = base.BindModel(controllerContext, bindingContext);  
  9.             if (value is string)  
  10.             {  
  11.                 var result = (value as string).Trim();  
  12.                 int index;  
  13.                 if (AntiXssStringHelper.IsDangerousString(value.ToString(), out index))  
  14.                 {  
  15.                     throw new HttpRequestValidationException("Dangerous Input Detected");  
  16.                 }  
  17.             }  
  18.   
  19.             return value;  
  20.         }  
  21.     }  


如果某些参数需要支持部分HTML代码, 可以采取的方法是先将该参数设置为 [AllowHtml], 进到Action后,再自行过滤或者使用AntiXSS库编码。


其它:

微软提供了一个对数据进行编码解码的库,名字是AntiXSS,可以通过Nuget引入。主要工具函数如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值