Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET whitespace or %20 in URL

Good day, everyone.

I found strange behavior within ASP.NET engine when it handles non-existent URL with whitespace.

When we have normal URL like this one: https://stackoverflow.com/questions/tagged1/c%23

we get normal custom 404 page as was intended by developers (if any).

But here's the bug. Just add some white space like this:https://stackoverflow.com/questions/tagged /c%23

and you'll see nasty ASP.NET 404 error page. Whether such a page should ever be displayed is another story. I've already made some heavy googling, made debugging research, and I can say that in this situation all custom handlers are ignored, global application class (Application_Error in global.asax) are just not reached. Actually, I don't see how can this situation be handled by ASP.NET. Any ideas?

Just as a note, this behavior is related to ASP.NET and ASP.NET MVC (as show on StackOverflow.com example). I tried other sites and found that even Microsoft.com falls into this category (see this: http://www.microsoft.com/en/us%20/default.aspx). Also, we can replace whitespace with %20 sequence with no better result.

like image 208
terR0Q Avatar asked Nov 06 '22 18:11

terR0Q


1 Answers

Workaround is found.

HttpException is thrown with 'Error executing child request for pageName.aspx' message.

First of all, ASP.NET can't find requested page or route. Then exception is thrown from System.Web.CachedPathData.GetVirtualPathData (actually, from System.Web.CachedPathData.GetConfigPathData, but GetVirtualPathData makes things clear).

Then if we have any error handler, it provides some actions and tries to do something. Here we usually have Server.Transfer or Request.Redirect that moves user to page 404 or more common error page. But. In current situation HttpException is thrown anyway and we get ASP.NET error page. And in this situation Server.ClearError and Server.RewritePath can help: exception is not thrown this way. BUT! Such error handling crashes normal business logic exceptions thrown by our application.

And to resolve new trouble we have to use if/else depending on whether we receive HttpException (so we use ServerRewrite with ClearError) or some exception from our own code. But again, that's good if you provide your own exception classes, but what if not...

Anyway, I consider this a very strange case, especially that Application_Error handler is ignored whilst other application code is still executed.

Edit

As code-way was not the best selection (we can only guess that we have THIS error when exception Message is empty and InnerException is null) the better solution lies within ISAPI rewriter with rule like (\s+(\.aspx).*)|(\s+/) to custom 404 page. Here we catch all requests like /somePage .aspx and /somePath /. When we have whitespace not preceding .aspx or slash but within page name or path part we don't have mentioned problem.

like image 87
terR0Q Avatar answered Nov 15 '22 06:11

terR0Q