Skip to content Skip to sidebar Skip to footer

How To Handle Html Inputs In The Textbox

I have a requirement that user can input HTML tags in the ASP.NET TextBox. The value of the textbox will be saved in the database and then we need to show it on some other page wh

Solution 1:

Always always always encode the input from the user and then and only then persist in your database. You can achieve this easily by doing

Server.HtmlEncode(userinput) 

Now, when it come time to display the content to the user decode the user input and put it on the screen:

Server.HtmlDecode(userinput)

Solution 2:

You need to encode all of the input before you output it back to the user and you could consider implementing a whitelist based approach to what kind of HTML you allow a user to submit.

I suggest a whitelist approach because it's much easier to write rules to allow p,br,em,strong,a (for example) rather than to try and identify every kind of malicious input and blacklist them.

Possibly consider using something like MarkDown (as used on StackOverflow) instead of allowing plain HTML?

Solution 3:

You need to escape some characters during generating the HTML: '<' -> &lt;, '>' -> &gt;, '&' -> &amp;. This way you get displayed exactly what the user entered, otherwise the HTML parser would possibly recognize HTML tags and execute them.

Solution 4:

Have you tried using HTMLEncode on all of your inputs? I personally use the Telerik RadEditor that escapes the characters before submitting them... that way the system doesn't barf on exceptions.

Here's an SO question along the same lines.

Solution 5:

You should have a look at the HTML tags you do not want to support because of vulnerabilities as the one you described, such as

  • script

  • img

  • iframe

  • applet

  • object

  • embed

  • form, button, input

and replace the leading "<" by "& lt;".

Also replace < /body> and < /html>

HTML editors such as CKEditor allow you to require well-formed XHTML, and define tags to be excluded from input.

Post a Comment for "How To Handle Html Inputs In The Textbox"