CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Protecting an ASP.NET page against malicious input with ValidateRequest (A potentially dangerous Request.Form value was detected)

By default ASP.NET jumps through some hoops to protect your asp.net applications against malicious user input. It does this by scanning the data post back on tags which might contain unintended markup or even script. Take a page where the users enters something like

Now on postback asp.net will raise an exception

It suspects the <SCRIPT> piece of text. It will also suspect something like <B>

To prevent this you have to set the page directive validateRequest to false

Now all user input is accepted.

But you can still scan the user input for malicious input by using the ValidateInput() method of the Request. This methods validates three parts of the input

  • Form variables
  • QueryString
  • Cookies

It does work in a somewhat strange matter. At first sight nothing happens. But the moment you touch one of the parts it is validated. In case it does contain suspected input an HttpRequestValidationException exception is thrown. This snippet of code demonstrates how to work with ValidateInput.

        [Flags]
        public enum RequestValid
        {
            AllInValid = 0,
            FormValid = 1,
            QueryStringValid = 2,
            CookiesValid = 4
        }


        private RequestValid validateRequest()
        {
            RequestValid isValid = RequestValid.AllInValid;
            Request.ValidateInput();
            try
            {
                object touchForm = Request.Form;
                isValid = isValid | RequestValid.FormValid;
            }
            catch(HttpRequestValidationException)
            {
                // Take action
            }
            try
            {
                object touchQueryString = Request.QueryString;
                isValid = isValid | RequestValid.QueryStringValid;
            }
            catch(HttpRequestValidationException)
            {
                // Take action
            }
            try
            {
                object touchCookies = Request.Cookies;
                isValid = isValid | RequestValid.CookiesValid;
            }
            catch(HttpRequestValidationException)
            {
                // Take action
            }
            return isValid;
        }

 

You cannot influence what ValidateInput will scan for, that's hard coded. But it does issue a warning and you know what part of the input needs a closer investigation.


Published Oct 21 2005, 03:02 PM by pvanooijen
Filed under:

Comments

Thomas Williams said:

Cross-site scripting (XSS) is a problem that ASP.NET helps you deal with by not allowing any &quot;malicious&quot;...
# December 7, 2005 9:02 PM

Joel Cable said:

Very good. Where should this be this implemented? On Page Load? I suppose it could be done as soon as the request object is available.
# January 23, 2006 11:16 AM

pvanooijen said:

That's PageLoad. Before that (in the init event) the request is not available yet.
# January 24, 2006 6:09 AM

gerard dsouza said:

Hi,

i have a screen which captures data and saves it in a database. I use the Request.ValidateInput() function to validate data. In case there is an error i redirect it to a custom error screen. all this works fine. my problem arrises when i click the "Back" button on the error screen. The earlier screen is loaded again and the malicious data get saved even though i call the Request.ValidateInput() function on page_load as well as the save button click event. I do not want to dissable the back button in the browser. How do I fix this problem ??

# March 2, 2007 5:57 AM

pvanooijen said:

You cannot disable the back button. Evenif you do so there is always the context menu back, the mouse button back or the page in the history. That's all client side behavior which is beyond your control.

But when the Back button brings you back to the page in error you still cannot post to that page. That will raise the exception again.

# March 2, 2007 2:21 PM

Thomas Williams Tech Blog said:

Regular Expression to Prevent Users Entering Malicious (HTML) Form Data

# June 29, 2008 7:17 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News