views:

66

answers:

2

If I were to create a site where users could put arbitrary html into their 'profile' or something similar, how might I prevent JavaScript embedded in that html from running?

Could I put an infinite loop for(;;); somewhere? If so, where would I put it?

What other security concerns are associated with this approach?

+9  A: 

Umm, the infinite loop would just hang the browser.

For PHP, I would recommend HTML Purifier to keep evil HTML out by only letting in the good stuff :)

Python HTML Sanitizer seems like a good option for Python, as well, though I haven't had the chance to try it. This StackOverflow question offers some simple HTML sanitizer solutions using BeautifulSoup, but be sure to be careful with that first answer - it looks like it doesn't have an attribute whitelist, which is vital for safe code, as well.

Matchu
Seems like a great library. What are some equivalents in other languages (like python)?
colinmarc
@colinmarc - Ya know, once I started looking up Python alternatives a few minutes ago, I found [my own answer](http://stackoverflow.com/questions/2357750/making-user-made-html-templates-safe/2357799#2357799) from a few months ago xD Link added in just a sec.
Matchu
+5  A: 

You should use a white-list approach. When you display the profile you should first HTML-encode everything, and from there you can decode the HTML elements that you allow. That way only the specific elements and attributes that you have specified are usable, and there is no way to sneak any code around it by using something that you didn't think of.

Guffa
... I think this is correct, and note that doing this is not at all an easy thing. Witness the YouTube debacle of a few days ago as illustration of why you should prevent cross-site scripting attacks!!
Pointy
Are `<style ...>` tags safe? How do I prevent people from adding something like `<div onclick="malicous()">`?
colinmarc
Whitelists should include not just safe tags, but safe attributes, as well. HTML Purifier purges unsafe attributes, and I think it may be able to scrub down unsafe CSS, as well (CSS can be used to execute Javascript sometimes), but that requires configuration.
Matchu
@colinmarc: You can put Javascript expressions in style tags, which will be executed by Internet Explorer (except in version 8), so you can't just allow any content in a style tag. To prevent use of events in tags you just specify what attributes to allow in each tag.
Guffa