Invalid URI: The hostname could not be parsed via our custom rewrite IHttpModule

1.2k Views Asked by At

This is a strange error we're seeing sporadically on our website.

The strange thing about this error is it's being thrown in our URL rewritting IHttpModule. The line in question is:

var host = context.Request.Url.Host;

If I record context.Request.Url when this exception is thrown, it is an empty string.

This rewriting method is only called via our global.asax file:

void Context_AuthorizeRequest(object sender, EventArgs e)
     => URLRewriting.Process();

What might cause Request.Url.Host to be empty?

As a side note, we're seeing a lot of other errors being caught on the site that suggest someone is scanning our website with automated tools to discover weaknesses and exploits. I have a feeling this might somehow be related, although I'm unusure as to why Request.Url.Host would ever be empty.

Second side note, the users IP making this last request is a University network (not unusual for our website).

2

There are 2 best solutions below

1
Grzegorz Smulko On

I would focus on the fact that the context.Request.Url is empty.

Do you have any other IHttpModules that does something with the HttpContext earlier in the pipeline?

Is it possible that there is a relative HTTP request, as described here?

Do you know anything more about the request when the host and URL are empty?

0
TZU On

Could it be an invalid URL that your rewrite module was unable to map? According to this, if the rewrite map does not have a rule to map a request, you will get an empty string by default.

If you have captured the original requests that have caused the error, you might be able to replicate it and step through your code to verify this theory.