zoukankan      html  css  js  c++  java
  • A potentially dangerous Request.Form value was detected from the client

    System.Web.HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (Name="<script>alert(1)</sc...").
    at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)
    at System.Web.HttpValueCollection.EnsureKeyValidated(String key)
    at System.Web.HttpValueCollection.GetValues(String name)
    at System.Web.Mvc.NameValueCollectionValueProvider.ValueProviderResultPlaceholder.GetResultFromCollection(String key, NameValueCollection collection, CultureInfo culture)
    at System.Web.Mvc.NameValueCollectionValueProvider.GetValue(String key, Boolean skipValidation)
    at System.Web.Mvc.ValueProviderCollection.GetValue(String key, Boolean skipValidation)
    at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    at System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
    at System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
    at System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext)
    at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model)
    at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
    at UKERecognition.Admin.ParameterValidationAsyncActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) in

    A potentially dangerous Request.Form value was detected from the client

    Every time a user posts something containing < or > in a page in my web application, I get this exception thrown.

    I don't want to go into the discussion about the smartness of throwing an exception or crashing an entire web application because somebody entered a character in a text box, but I am looking for an elegant way to handle this.

    Trapping the exception and showing

    An error has occurred please go back and re-type your entire form again, but this time please do not use <

    doesn't seem professional enough to me.

    Disabling post validation (validateRequest="false") will definitely avoid this error, but it will leave the page vulnerable to a number of attacks.

    Ideally: When a post back occurs containing HTML restricted characters, that posted value in the Form collection will be automatically HTML encoded. So the .Text property of my text-box will be something & lt; html & gt;

    Is there a way I can do this from a handler?

    回答

    I think you are attacking it from the wrong angle by trying to encode all posted data.

    Note that a "<" could also come from other outside sources, like a database field, a configuration, a file, a feed and so on.

    Furthermore, "<" is not inherently dangerous. It's only dangerous in a specific context: when writing strings that haven't been encoded to HTML output (because of XSS).

    In other contexts different sub-strings are dangerous, for example, if you write an user-provided URL into a link, the sub-string "javascript:" may be dangerous. The single quote character on the other hand is dangerous when interpolating strings in SQL queries, but perfectly safe if it is a part of a name submitted from a form or read from a database field.

    The bottom line is: you can't filter random input for dangerous characters, because any character may be dangerous under the right circumstances. You should encode at the point where some specific characters may become dangerous because they cross into a different sub-language where they have special meaning. When you write a string to HTML, you should encode characters that have special meaning in HTML, using Server.HtmlEncode. If you pass a string to a dynamic SQL statement, you should encode different characters (or better, let the framework do it for you by using prepared statements or the like)..

    When you are sure you HTML-encode everywhere you pass strings to HTML, then set ValidateRequest="false" in the <%@ Page ... %> directive in your .aspx file(s).

    In .NET 4 you may need to do a little more. Sometimes it's necessary to also add <httpRuntime requestValidationMode="2.0" /> to web.config (reference).

  • 相关阅读:
    每日英语:Here's Why You Won't Finish This Article
    每日英语:'Still Out Of Work?' How To Handle Holiday Small Talk
    每日英语:How to Spend Christmas in Asia
    每日英语:Losing It at the Movies: Silly Beats Sober in China's Box Office
    每日英语:How Do iPhone Photos Impact Our Experience?
    一种使用CSS固定表头和列的方法
    一个asp.net中使用的flash展示控件
    gridview中cell的选择和编辑
    asp.net 2.0中的profile对象简介
    gridview中使用方向键标记选中行
  • 原文地址:https://www.cnblogs.com/chucklu/p/14480272.html
Copyright © 2011-2022 走看看