I have an input, which displays a value (Firstname) in a box on the page. On the server side, the input is escaped by following rule (vb.net):
Public Function UnescapeString(strValue As String) As String
If (String.IsNullOrEmpty(strValue)) Then Return strValue
strValue = Replace$(strValue, """", "")
strValue = Replace$(strValue, "'", "")
strValue = Replace$(strValue, "<", "")
strValue = Replace$(strValue, ">", "")
Return strValue
End Function
The output isn't validated at all.
I can't believe escaping input from <>
could be this safe as it feels for me!?
Are there tricks to inject code or is the most major thing to prevent XSS here, to cut out or escape <>
signs?
Question 2: I am planning a rewrite of this method like this:
strValue = Replace$(strValue, """", """)
strValue = Replace$(strValue, "<", "<")
strValue = Replace$(strValue, ">", ">")
strValue = Replace$(strValue, "&", "&")
strValue = Replace$(strValue, "'", "'")
strValue = Replace$(strValue, "/", "/")
strValue = System.Web.HttpUtility.HtmlEncode(strValue) <- or only this
I'm planning not to cut out, but escape values properly, like in the example above. Or I just use .NET's HtmlEncode
(last line), which even encodes special characters like German umlauts, as well as dangerous signs like <>"'/
.
<
in order to prevent XSS.