views:

539

answers:

5

Hmm. Instead of "defanging" input or using some kind of regex to remove tags, how safe is it to dump user stuff into a <textarea>?

For example, say there's a PHP page that does the following:

echo '<textarea>';
echo $_GET['whuh_you_say'] ;
echo '</textarea>';

Normally this is vulnerable to xss attacks, but in the textarea, all script tags will just show up as <script> and they won't be executed.

Is this unsafe?

+15  A: 
</textarea>
  <script type="text/javascript">
    alert("this safe...");
    /* load malicious c0dez! */
  </script>
<textarea>
Jonathan Sampson
yea i just thought of that. ho ho ho. thanks
bobobobo
if you were always going to use textarea...you could develop a simple regex pattern to ONLY remove textarea etc from the string to prevent the above attack...and let the textarea itself take care of the rest
davidsleeps
+1 beat me to it - @davidsleeps - while that may be true, the *best* way to deal with this is whitelisting only the tags you need, and making sure you are sanitizing any output generated by a user's input
John Rasch
It wouldn't necessarily be simple. What if I put`</text</textarea>area>`. It still needs to be thought through.
Ian Elliott
You should be using htmlspecialshars() anyway, even if you're putting it inside a <textarea>
Josh
@Josh: Won't the entities show up in the textarea then?
Svish
@Svish — no. `<textarea>` elements do not contain CDATA.
David Dorward
@David: Aha. :)
Svish
+1  A: 

strip_tags(string);

Is wonderful! Honest!

asperous.us
It was broken in some versions of PHP, though. http://www.derkeiler.com/Mailing-Lists/securityfocus/bugtraq/2004-07/0149.html
ceejayoz
+1 for the honesty!
Shoan
Don't use it on text - you'll get invalid entities. Use htmlspecialchars() instead (as a bonus, it won't destroy innocent text like "1<2")
porneL
A: 

Good enough for the basics:

sanitized = str_replace("<", "&lt;", $_GET['whuh_you_say']);
sanitized = str_replace(">", "&gt;", sanitized);
nilamo
Or you could just use htmlentities().
Frank Farmer
+1  A: 

If your users aren't supposed to be using any HTML tags whatsoever (which if you're proposing this textarea solution, that's the case), just run it through htmlspecialchars() or htmlentities() and be done with it. Guaranteed safety.

ceejayoz
A: 

This talks about an XSS hole found in textarea's in google documents (I think the post is a little old - so google have probably secured it by now), but it deomstrates how textareas can be used as an attack vector.

ha.ckers.org discussing google docs textarea exploit

Alex Key