string message = CommonFunctions.SanitiseInput(context.Request.QueryString["msg"]);
And the function is defined as:
// Sanitise input
public static string SanitiseInput(string inputText)
{
string cleanedString = inputText;
cleanedString.Replace("<","<"); // No code
cleanedString.Replace(">", ">");
cleanedString.Replace("&", "&"); // No query string breaks
return cleanedString;
}
Given input "<b>rg</b>" this returns the same, and not "<b>rg</b>"
The Replace function in C# does not modify the string itself - it returns a modified version of the string.
Try this:
public static string SanitiseInput(string inputText)
{
string cleanedString = inputText;
cleanedString = cleanedString.Replace("<","<"); // No code
cleanedString = cleanedString.Replace(">", ">");
cleanedString = cleanedString.Replace("&", "&"); // No query string breaks
return cleanedString;
}
For "<b>rg</b>" this will give you "&lt;b&gt;rg&lt;/b&gt;". To fix up the unnecessary conversions to "&", move the third replacement to before the other two, which will give you the result you are expecting.
You should use HttpUtility.HtmlEncode(): http://msdn.microsoft.com/en-us/library/system.web.httputility.htmlencode.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With