Scott on Writing

Musings on technical writing...

Question: Disabling Request Validation from the Page

One of the new features in ASP.NET 1.1 was Request Validation, an addition that, by default, disallowed users from submitting form fields that contained potential markup.  In the classes I teach I find that most developers are unaware of Request Validation's existence, and don't realize that it's there, checking for HTML markup, until they build their first application where they want to let the user submit HTML markup.  And then they get the forboding:

Turning off Request Validation is pretty easy.  As the Request Validation FAQ explains, you can turn it off on a page-by-page basis, via the @Page directive:

<%@Page ... ValidateRequest=“False“ %>

Or you can set it for the entire Web application through Web.config, or for your entire Web server through machine.config.  Today, though, I found an interesting question on the microsoft.public.dotnet.framework.aspnet.webcontrols newsgroup from Umut Tezduyar, who asks:

I want to disable RequestValidation that consumers of my custom control. Is
there a way for this?  ...  By using the control that i am developing, my users can write html from the web site. But framework 1.1 is disables it by default. ... I want to disable it or allow only from some of my controls can post html text.

The question boils down to, Is there some property a custom, compiled control could set that would disable Request Validation for the page is exists on (or, perhaps, turn it on)?  After a bit of poking around with Reflector, it appears that the answer is, No.  In fact, to my knowledge you can't programmatically muck with any of the page-level directive values through a page's source code portion.

As I discussed in Understanding ASP.NET View State (see “The ASP.NET Page Lifecycle” section), when an ASP.NET page is visited for the very first time, or for the first time after the HTML portion has been modified, the ASP.NET engine creates an autogenerated class whose primary responsibility is to construct the control hierarchy programmatically, based on the page's HTML markup and Web controls.  During this compilation, the page's directives are inspected, and their values affect the resulting autogenerated class.  For example, if you have @Register directive, the compiler adds assembly dependencies; in your @Page directive, if you have Debug=”True,” the compiler ensures that you have sufficient rights.

In any event, during this processing of the directives, which can be found in the ProcessDirective() method of the TemplateParser, TemplateControlParser, and PageParser classes, the PageParser class checks to see if the ValidateRequest attribute has been added to the @Page directive, and assigns a Boolean member variable the result of this attribute (or True, if the attribute it omitted).  When the compilation actually occurs via the PageCompiler, TemplateControlCompiler, and BaseCompiler classes, the PageCompiler's BuildFrameworkInitializeMethodContents() method injects a call in the autogenerated class to Request.ValidateInput(), which does the request validation check and throws the HttpRequestValidationException, if needed. 

Since this line of code is literally added to the autogenerated ASP.NET page prior to the page even running, I don't see a way on how to programmatically suspend Request Validation on a given page.  Does anyone else see any workarounds?  Initially I thought it might be possible to somehow HTML encode the received input in, say, the Initialization stage of the page lifecycle, hoping beyond hope that the call to Request.ValidateInput() didn't come until after Initialization, but, alas, the call comes in the Instantiation stage, which preceeds Initialization.  One possible option might be to have the custom control emit a bit of client-side JavaScript that HTML encodes the control's input prior to the Web Form being submitted.  Any other ideas?

posted on Friday, November 12, 2004 6:47 PM

Feedback

# re: Question: Disabling Request Validation from the Page 11/13/2004 9:23 AM Phil Weber

Is it possible to override HttpRequest.ValidateInput?

# re: Question: Disabling Request Validation from the Page 11/13/2004 2:59 PM Scott Mitchell

Phil, while HttpRequest.ValidateInput() is public, it is not virtual, so it can't be overridden. Also, even if you were able to extend HttpRequest and override ValidateInput(), I don't see how you would be able to have the ASP.NET engine start using your extended version vs. the HttpRequest version already embedded in the framework.

# OTC Links for Nov 14th 11/14/2004 7:33 PM OdeToCode Link Blog

# re: Question: Disabling Request Validation from the Page 11/14/2004 11:36 PM Umut Tezduyar

Thanks Scott. I haven't still found an answer to it. It seems, there is a property in the .Net Frmawork 2.0, but not in 1.1.

# The most useless feature, ever 11/15/2004 12:31 AM Thomas Eyde

As long as ASP.NET does not provide any tools to handle the input, this is a useless feature. What does developers do? They waste a lot of time to figure out how to disable it, then disable it and forget about it.

A better approach would be to provide some tools to validate and/or encode the values which also told ASP.NET that the potential danger is handled.

Just providing a means of turning the check off doesn't do any good, as too many developers don't know what to do, or are ignorant to the problem.

# re: Question: Disabling Request Validation from the Page 11/15/2004 1:07 PM Jeffrey Palermo

Scott, I haven't seen a way to detect invalid input and react/handle/encode it. I am one of the many developers who have disabled this feature. I don't think that encoding it with Javascript is an ideal solution because then you have to synchronize it with a Decode in the server code, but doing the client-side encode is probably the only way to keep this "feature" from breaking the page. In open text-fields that can accept any input (which is rare anyway), I allow storing any input in the database. In some cases <!-- may be valid, and I don't want to store encoded data. I do take care, however to HTMLEncode EVERY string that is displayed on my page from the database. This ensures that the above string doesn't comment out a section of my page, and it ensures that malicious data (if one of my authenticated users is being STUPID- he'll be fired shortly) like a <script> element doesn't execute but instead just prints on the page.

# re: Question: Disabling Request Validation from the Page 11/17/2004 10:20 AM Milan Negovan

I disabled this feature on my pages, but wrote a similar validator of my own and added it to the main template. This way I control request validation programmatically.

# re: Question: Disabling Request Validation from the Page 11/22/2004 7:51 AM Peter Blum

Just so people are aware, I wrote a replacement to validateRequest that allows field level control over checking for cross-site scripting and SQL Injection attacks. Details about "Visual Input Security" are at http://www.peterblum.com/vise/home.aspx. (It is a commercial solution.)

# re: Question: Disabling Request Validation from the Page 7/27/2005 2:05 AM Roberto

is there anything we can do to programatically disable requestvalidation in only one control in .NET 2.0?? How?

# http://www.construction-tools.info 4/5/2006 10:47 PM construction-tools@takoe.com

Good site. Me very much has liked.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles