I am using MVC3, .NET4.5, C#, Razor.
I have reused some very useful code, off Codeproject, from Albin, to prevent URL tampering, by adding a hash. Works well in the main.
However....
I now discovered that I am getting 404 error:
The request filtering module is configured to deny a request that contains a double escape sequence.
This I understand to be due to the inclusion of "+" in the url, which in my case is within the hash ie:
http://myServer/Controller/Action/Ivz7P+b2ZPmatG6kZaY_IR0az1s=?mode=xxx
The code for generating the hash is:
//Converting the salt in to a byte array
byte[] saltValueBytes = System.Text.Encoding.ASCII.GetBytes(salt);
//Encrypt the salt bytes with the password
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, saltValueBytes,2);
//get the key bytes from the above process
byte[] secretKey = key.GetBytes(16);
//generate the hash
HMACSHA1 tokenHash = new HMACSHA1(secretKey);
tokenHash.ComputeHash(System.Text.Encoding.ASCII.GetBytes(stringToToken));
//convert the hash to a base64string
token = Convert.ToBase64String(tokenHash.Hash).Replace("/", "_");
It seems that I need a way of generating hashes without "+" or replacing the "+"s, but that would invalidate the hash.
Best approach would be to generate hashes without "+"s.
Thoughts.
EDIT1
It seems if I take the UrlToken out of the routing and therefore have it appended at the end of the URL ie:
http://myServer/Controller/Action?mode=xxx&urltoken=Ivz7P+b2ZPmatG6kZaY_IR0az1s=
It now works. Not sure why. Well the 404 goes away, but the URL tampering mechanism now fails all the time !! May not be related.
EDIT2
I have now got this working by altering the URL tampering code to extract the hash from the non route bit ie after "?". This all works fine, and as I understand it I have not opened any new vulnerabilities as I have kept the default ASP.NET settings.
EDIT3
See: