Saturday, June 3, 2017

ValidateInputIfRequiredByConfig being called after Page_Load and Page_PreRender

Leave a Comment

I have a routing setup which is doing this Rewrite:

<rule name="some Rule" patternSyntax="Wildcard">     <match url="*" />     <conditions>         <add input="{PATH_INFO}" pattern="/folder/*" />         <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />     </conditions>     <action type="Rewrite" url="/folder/details.aspx?url={PATH_INFO}" appendQueryString="true" /> </rule> 

And I also have this:

<httpRuntime targetFramework="4.6.1" maxUrlLength="256" /> 

I have a bot calling to an invalid url, something like:

http://localhost:53649/route/Somefolder-Heres/W-ithsom-tet-Text-(An%C3%83%C6%92%C3%86%E2%80%99%C3%83%E2%80%A0%C3%A2%E2%82%AC%E2%84%A2%C3%83%C6%92%C3%A2%E2%82%AC%20%C3%83/MoreTExt-(%C2%A2%C3%A2%E2%80%9A%C2%AC%C3%A2%E2%80%9E%C2%A2%C3%83%C6%92%C3%86%E2%80%99%C3%83%C2%A2%C3%A2%E2%80%9A%C2%AC%C3%82%20%C3%83%C6%92%C3%82%C2%A2%C3%83%C2%A2%C3%A2%E2%82%AC%C5%A1%C3%82%C2%AC%C3%83%C2%A2%C3%A2%E2%82%AC%C5%BE%C3%82%C2%A2%C3%83%C6%92%C3%86%E2%80%99%C3%83%E2%80%A0%C3%A2%E2%82%AC%E2%84%A2%C3%83%C6%92%C3%82%C2%A2%C3%83%C2%A2%C3%A2%E2%82%AC%C5%A1%C3%82%C2%AC%C3%83%E2%80%A6%C3%82%C2%A1%C3%83%C6%92%C3%86%E2%80%99%C3%83%C2%A2%C3%A2%E2%80%9A%C2%AC%C3%85%C2%A1%C3%83%C6%92%C3%A2%E2%82%AC%C5%A1%C3%83%E2%80%9A%C3%82%C2%A1stasis)/someUrl(An%C3%83%C6%92%C3%86%E2%80%99%C3%83%E2%80%A0%C3%A2%E2%82%AC%E2%84%A2%C3%83%C6%92%C3%A2%E2%82%AC%20%C3%83%C2%A2%C3%A2%E2%80%9A%C2%AC%C3%A2%E2%80%9E%C2%A2%C3%83%C6%92%C3%86%E2%80%99%C3%83%C2%A2%C3%A2%E2%80%9A%C2%AC%C3%82%20%C3%83%C6%92%C3%82%C2%A2%C3%83%C2%A2%C3%A2%E2%82%AC%C5%A1%C3%82%C2%AC%C3%83%C2%A2%C3%A2%E2%82%AC%C5%BE%C3%82%C2%A2%C3%83%C6%92%C3%86%E2%80%99%C3%83%E2%80%A0%C3%A2%E2%82%AC%E2%84%A2%C3%83%C6%92%C3%82%C2%A2%C3%83%C2%A2%C3%A2%E2%82%AC%C5%A1%C3%82%C2%AC%C3%83%E2%80%A6%C3%82%C2%A1%C3%83%C6%92%C3%86%E2%80%99%C3%83%C2%A2%C3%A2%E2%80%9A%C2%AC%C3%85%C2%A1%C3%83%C6%92%C3%A2%E2%82%AC%C5%A1%C3%83%E2%80%9A%C3%82%C2%A1stasis)/

So ASP.NET throws this exception

The length of the URL for this request exceeds the configured maxUrlLength value.

at System.Web.HttpRequest.ValidateInputIfRequiredByConfig()

at System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext context)

The problem I have is that the ValidateInputIfRequiredByConfig from ASP.NET is being called after my Page_Load and Page_PreRender. I could explain a little more about what I'm doing here but my question is:

Why ASP.NET is validating the URL after my page is executed? it makes no sense, why it wastes time processing the page, shouldn't make more sense to do that before? Is there any way to change this behavior?

I uploaded a demo on Github: So you can download this solution https://github.com/kblok/StackOverflowExamples/tree/master/AspNetDemoProject

And break In the page load and then in the Error module. You'll see that the PageLoad is being hit first.

1 Answers

Answers 1

Based on the points that you have mentioned in the comments what I understand is

  1. Your page details.aspx is getting accessed with query string param url=<path with unicode characters>.
  2. In the Page_Load your code checks the request, finds out that its invalid and redirects user to new URL.
  3. And THIS NEW URL is longer than the max allowed length so you get The length of the URL for this request exceeds the configured maxUrlLength value.

The lengh of the original request URL is within limits so there is no error before Page_Load.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment