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?
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.