
208
|
Chapter 9: ASP.NET Security
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
HTML encoding
Another technique for filtering input is to HTML encode all input (and/or all
output), and use the String.Replace function to allow specific HTML content by
replacing the encoded value with an unencoded version. This snippet shows how:
Dim InputString As String = Server.HtmlEncode(TextBox1.Text)
InputString = InputString.Replace("<b>", "<b>")
InputString = InputString.Replace("<B>", "<B>")
InputString = InputString.Replace("</b>", "</b>")
InputString = InputString.Replace("</B>", "</B>")
InputString = InputString.Replace("<i>", "<i>")
InputString = InputString.Replace("<I>", "<I>")
InputString = InputString.Replace("</i>", "</i>")
InputString = InputString.Replace("</I>", "</I>")
Like the RegularExpressionValidator code snippet, HTML encoding will allow the
use of the <b> and <i> tags. In this case, all other tags will remain encoded. Note
that extensive string manipulation can be expensive from a performance stand-
point so, where possible, using regular expressions may be more efficient.
Certain HTML tags, such as the <img> tag, allow script in their
attributes. If you allow these tags, you will need to perform addi-
tional filtering to ensure that script is not passed in with these tags.
SQL Injection ...