views:

1665

answers:

2

I have an ASP.NET MVC application with a route that allows searching for stuff via /search/<searchterm>.

When I supply "search/abc" it works well, but when I supply "/search/a+b+c" (correctly url encoded) then IIS7 rejects the request with HTTP Error 404.11 (The request filtering module is configured to deny a request that contains a double escape sequence). FIrst of all, why does it do this? It only seems to throw the error if it is part of the URL, but not as part of a query string ( /transmit?q=a+b+c works fine).

Now I could enable double escape requests in the security section of my web.config but I'm hesitant to do so as I don't understand the implications, and neither why the server would reject the request "a+b+c" as part of the URL but accept as part of a query string.

Can someone explain and give some advice what to do?

+8  A: 

Edit: Added emphasis to relevant sections.

Basically: IIS is being excessively paranoid. You can safely disable this check if you're not doing anything particularly unwise with the uri decoded data (such as generating local filesystem URI's via string concatenation).

To disable the check do the following (from here or here): (see my comment below for what double escaping entails).

<system.webServer>
    <security>
        <requestFiltering allowDoubleEscaping="True"/>
    </security>
</system.webServer>

If the plus symbol is a valid character in a search input, you will need to enable "allowDoubleEscaping" to permit IIS to process such input from the URI's path.

Finally, a very simple, if limited workaround is simply to avoid '+' and use '%20' instead. In any case, using the '+' symbol to encode a space is not valid url encoding, but specific to a limited set of protocols and probably widely supported for backwards-compatibility reasons. If only for canonicalization purposes, you're better off encoding spaces as '%20' anyhow; and this nicely sidesteps the IIS7 issue (which can still crop up for other sequences, such as %25ab.)

Eamon Nerbonne
OK, but what should I do?
Alex
I'd disable the check. It's a hassle and doesn't provide extra security to most apps.
Eamon Nerbonne
Do you have a link/reference that it's pretty much safe to disable double escaping? Also, what exactly does this security measure prevent from happening?
Alex
If a uri is double-escaped, then the unescaped uri components may themnselves contain reserved characters and thus (parts of) the unescaped uri may itself be a valid uri. In short, if you use the unescaped uri string to construct new uri's - in particular filesystem paths - and you fail to correctly escape the new path, you may allow path injection. Path injection could allow an attacker to trick your program into processing data it shouldn't, or into confusing it into thinking two uri's are different when they are actually identical but simply encoded differently.
Eamon Nerbonne
A: 

Have you thought about having the search URL like '/search/a/b/c'?

You'd need to setup a route like

search/{*path}

And then extract the search values from your path string in the action.

HTHs
Charles

Charlino
The problem is that other URL encoded characters (including '/' itself) could be part of the search.
Alex
Could you not encode all the '/' that are actually part of the search to '%2F'?
Charlino