How to disable ASP.NET request validation for WCF service? - asp.net

I have a .NET 4 WCF service running as an IIS website with ASP.NET compatibility mode. Once of the service methods accepts a string parameter and when there is a value which contains a '&' the ASP.NET pipeline raises a validation exception. I've assigned the following config settings:
<system.web>
<httpRuntime requestValidationMode="2.0"/>
<pages validateRequest="false"/>
</system.web>
And the error persists. The error occurs regardless of whether the input is encoded. I found a potential solution here which suggests providing a custom implementation of System.Web.Util.RequestValidator, however I was wondering whether there are alternatives that can be done with configuration settings only.
EDIT:
I've also found this, however the proposed solution does not fix the problem.

I've found the solution. I set the following configuration setting:
<system.web>
<httpRuntime requestPathInvalidCharacters=""/>
</system.web>
By default, and ampersand is an invalid path character.

Some links that may help:
summary: MS is stating that you should use %26 if the ampersand appears in the query string.
http://connect.microsoft.com/wcf/feedback/details/527185/wcf-rest-uritemplate-does-not-recognise-or-amp-ampersand-as-same-character
summary: poster is talking about encoding the & in the body of the WCF message using &
XmlReader chopping off whitespace after ampersand entity?
So, if it's in the query string encode it with %26. If it's in the body of the message encode as an html entity: &

Related

ASP.NET customErrors web.config not catching all invalid URLs

It looks like there is a bug in customErrors default redirect in web.config. In my web.config file I have the following customErrors setting
<customErrors defaultRedirect="~/generalerror.html?" mode="On" />
As far as I know this should send all errors to the custom generalerror.html page. It seems to work for some invalid URLS like
http://website.com/?x="<p>"
http://website.com/"<p>"
BUT it is not working when “&” is used in the URL and there is no “?” and there is an HTML tag. So this
http://website.com/&x="<p>"
totally ignores customErrors and you are given the default yellow Runtime Error instead of being sent to the custom generalerror.html page. How do I get this URL to also be redirected to the custom error page ?
If I turn mode="Off" in the web.config I get the following error
A potentially dangerous Request.RawUrl value was detected from the client (="/&x="<p>"").
Since you are passing HTML tags in the URL, it could be an indicative of cross-site scripting attack. Not all HTML tags are dangerous, but when HTML characters are followed by certain characters like '&' in your case, asp.net considers it as a cross-site scripting attack and doesn't allow it by default.
You should consider encoding the URL to get around this. And it is always a best practice. Here is a good explanation about XSS. And here is a link that explains in detail how to get around this issue.
To change this behavior, you can set request validation to false in web.config.
<configuration>
<system.web>
<pages validateRequest="false" />
</system.web>
</configuration>
But in this case, requests need to be validated in the pages.
Breaking changes were made to ASP.NET request validation in .NET 4.0 and this entry is required to revert the behavior to .NET 2.0 where invalid URLs will redirect to custom error page.
<httpRuntime requestValidationMode="2.0" />

IIS 6 not allowing periods in the querystring

I have deployed an asp.net site with a wcf rest service to a virtual directory. It accepts lat/lon in the querystring. IIS is apparently not allowing querystrings that contain a '.'. I have found numerious posts on the topic, but cannot seem to resolve the issue.
I have tried enabling parent paths on both the parent website and the virtual directory:
http://support.microsoft.com/kb/332117
I have already tried the httpRuntime setting for relaxedUrlToFileSystemMapping="true"
http://haacked.com/archive/2010/04/29/allowing-reserved-filenames-in-URLs.aspx
currently I have both parent paths enabled and my httpRuntime settings are:
<httpRuntime requestValidationMode="2.0" requestPathInvalidCharacters="" relaxedUrlToFileSystemMapping="true" />
I noticed that the Urls you were using have a / rather than a ? which causes the elements to be treated as a path rather than a query string.
If that was intended and you are using .NET 4.0, you can try using the <schemeSettings> Element (Uri Settings) under configuration:
<uri>
<schemeSettings>
<add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" />
</schemeSettings>
</uri>
See GenericUriParserOptions Enumeration for the valid values.
If that was not intended update your template to use the ? mark and you should be ok:
[WebGet(UriTemplate = "?username={username}&lat={lat}&lng={lng}")]
Probablly not what you were looking for but I would use UrlEncode:
http://msdn.microsoft.com/en-us/library/zttxte6w.aspx

ASP.NET special characters in URL (colon) not working even with requestPathInvalidCharacters=""

I need to pass a colon within an URL in ASP.NET, like "http://mywebapp.com/Portal:Main/". Wikipedia does this a lot, and colons are valid URL characters according to the RFC.
I've found this SO question and read through this blog post which covers the invalid characters filter in ASP.NET.
Using VS2010 Ultimate and trying with a new ASP.NET WebForms and a new ASP.NET MVC 2 project I always added this to my web.config:
<httpRuntime requestPathInvalidCharacters="" />
as well as this
<httpRuntime requestValidationMode="2.0"
requestPathInvalidCharacters=""
relaxedUrlToFileSystemMapping="true"
/>
But still, I always get a Error 400: Bad Gateway when accessing anything with a "special character" like http://localhost:2021/1%3As.aspx or http://localhost:2021/1:s.aspx
The projects are definitely using .NET 4 runtime. Whats going wrong here?
I'll answer the question myself as I found out in the meantime:
You have to flip a registry switch for it to work:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET
DWord Value Name: VerificationCompatibility
Value Data: 1
See the according MS KB Article for more details.
For mono users: the registry value is honored on windows systems. On non-windows systems you can change the behaviour by adding a monoSettings section to your web.config:
<monoSettings verificationCompatibility="1" />
<httpRuntime requestValidationMode="2.0"
requestPathInvalidCharacters=""
relaxedUrlToFileSystemMapping="true"
/>

"Potentially Dangerous Request.Form" Exception in a generic handler

I've seen this error before but cannot seem to get around it. In this case, I have an ASHX page spitting out a simple HTML form with a textbox into which XML may be posted. When I try to read the form, I receive the "A potentially dangerous Request.Form value...".
Since it's a generic handler the "ValidateRequest" attribute isn't available. However I already had this defined in web.config:
<location path="xml/MyGenericHandler.ashx">
<system.web>
<pages validateRequest="false" />
</system.web>
</location>
This snippet predates a move from .NET 3.5 to 4.0 so I'm guessing that's where the breakage originated.
Any idea how to get around this error for ASHX pages?
The 3.5-4.0 change that clipped you was some stepped up runtime security features for ASP.NET 4.0. The quick fix is to apply the following attribute:
<httpRuntime requestValidationMode="2.0" />
Unfortunately, that opens all pages up to 2.0 request validation, so I'd only do this if you've got a relatively small attack surface.
While not a direct answer to your question, I would say to read this previous post. it does give you a way to ensure that the error is not thrown. It's a risky way in one sense, because it means turning off a basic protection. However, the answer is well-reasoned, and the it clearly states that you should only implement it when you're absolutely sure you're encoding all output.
A potentially dangerous Request.Form value was detected from the client
As a side note, I would also recommend using the Microsoft Anti-Xss Library rather than the built in Server.HtmlEncode functions.
However, if you can modify the ashx, a simpler solution would be to just modify the error code and add an "if" statement to not log errors if the error message contains the string you want to filter.
You'd better disable validation for you handler page only:
<location path="MyGenericHandler.ashx">
<system.web>
<!-- requestValidationMode is to avoid HTML-validation of data posted to the handler -->
<httpRuntime requestValidationMode="2.0"/>
</system.web>
</location>
Or use this property from within your handler to avoid triggering the exception:
context.Request.Unvalidated.Form

Freetextbox and validating requests

I am using freetextbox and have added to the web.config of my app but I still get the following error when submitting text with html:
A potentially dangerous Request.Form value was detected from the client (ctl00_MainContent_FreeTextBox1="
I know this is not the preferred way to set up an app but why am I getting these errors even though I have turned off request validation in my app?
The short answer is you shouldn't be getting such an error if you turned off Request Validation.
Did you do one of these two things correctly?
Disable on the page by inserting this at the top of the ASPX
Add the below section to your web.config.
<configuration>
<system.web>
<pages validateRequest="false" />
</system.web>
</configuration>
If that doesn't work then check the machine.config and see if the pages validaterequest value is set to true in there as that would override the web.config.
I had the same problem, and it was actually my fault. Maybe you have done the same mistake: I placed <httpRuntime requestValidationMode="2.0"/> inside
<configuration><location><system.web> instead of <configuration><system.web>.
Ensure that you haven't enabled request validation for this page. I would keep validation running for your site - but turn it off on pages where you need this control.
Be sure to sanitize anything that gets posted and be prudent about security.

Resources