Understanding Request Validation in ASP.NET MVC 3

Introduction:

A fact that you must always remember "never ever trust user inputs". An application that trusts user inputs may be easily vulnerable to XSS, XSRF, SQL Injection, etc attacks. XSS and XSRF are very dangerous attacks. So to mitigate these attacks ASP.NET introduced request validation in ASP.NET 1.1. During request validation, ASP.NET will throw HttpRequestValidationException:'A potentially dangerous XXX value was detected from the client', if he found, < followed by an exclamation(like <!) or < followed by the letters a through z(like <s) or & followed by a pound sign(like &#123) as a part of query string, posted form and cookie collection. In ASP.NET 4.0, request validation becomes extensible. This means that you can extend request validation. Also in ASP.NET 4.0, by default request validation is enabled before the BeginRequest phase of an HTTP request. ASP.NET MVC 3 moves one step further by making request validation granular. This allows you to disable request validation for some properties of a model while maintaining request validation for all other cases. In this article I will show you the use of request validation in ASP.NET MVC 3. Then I will briefly explain the internal working of granular request validation. 

Description:

  First of all create a new ASP.NET MVC 3 application. Then create a simple model class called MyModel,  

public class MyModel  
{
   public string Prop1 { get;  set; }  
   public string Prop2 { get; set; }  
} 


 Then just update the index action method as follows,

public ActionResult Index(MyModel p)  
{
  return View();  
}


Now just run this application. You will find that everything works just fine. Now just append this query string?Prop1=<s to the url of this application, you will get the HttpRequestValidationException exception.

Now just decorate the Index action method with [ValidateInputAttribute(false)],

 

[ValidateInput(false)]  
public ActionResult Index(MyModel p)  
{  
    return View();  
} 


Run this application again with same query string. You will find that your application run without any unhandled exception.

          Up to now, there is nothing new in ASP.NET MVC 3 because ValidateInputAttribute was present in the previous versions of ASP.NET MVC. Any problem with this approach? Yes there is a problem with this approach. The problem is that now users can send html for both Prop1 and Prop2 properties and a lot of developers are not aware of it. This means that now everyone can send html with both parameters(e.g,?Prop1=<s&Prop2=<s). So ValidateInput attribute does not gives you the guarantee that your application is safe to XSS or XSRF. This is the reason why ASP.NET MVC team introduced granular request validation in ASP.NET MVC 3. Let's see this feature.


Remove [ValidateInputAttribute(false)] on Index action and update MyModel class as follows,

public class MyModel  
{  
    [AllowHtml]  
    public string Prop1 { get;  set; }  
    public string Prop2 { get; set; }  
} 


 Note that AllowHtml attribute is only decorated on Prop1 property. Run this application again with?Prop1=<s query string. You will find that your application run just fine. Run this application again with?Prop1=<s&Prop2=<s query string, you will get HttpRequestValidationException exception. This shows that the granular request validation in ASP.NET MVC 3 only allows users to send html for properties decorated with AllowHtml attribute. 

Sometimes you may need to access Request.QueryString or Request.Form directly. You may change your code as follows,

[ValidateInput(false)]  
public ActionResult Index()  
{  
    var prop1 = Request.QueryString["Prop1"];  
    return View();  
} 


Run this application again, you will get the HttpRequestValidationException exception again even you have [ValidateInput(false)] on your Index action. The reason is that Request flags are still not set to unvalidate. I will explain this later. For making this work you need to use Unvalidated extension method,

public ActionResult Index()  
{  
    var q = Request.Unvalidated().QueryString;  
    var prop1 = q["Prop1"];  
    return View();  
} 


Unvalidated extension method is defined in System.Web.Helpers namespace. So you need to addusing System.Web.Helpers; in this class file. Run this application again, your application run just fine

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值