R – Sanitize output in Rails

ruby-on-railsSecurityxss

What is the best solution to sanitize output HTML in Rails (to avoid XSS attacks)?

I have two options: white_list plugin or sanitize method from Sanitize Helper http://api.rubyonrails.com/classes/ActionView/Helpers/SanitizeHelper.html . For me until today the white_list plugin worked better and in the past, Sanitize was very buggy, but as part of the Core, probably it will be under development and be supported for a while.

Best Answer

I think the h helper method will work here:

<%= h @user.profile %>

This will escape angle brackets and therefore neutralize any embedded JavaScript. Of course this will also eliminate any formatting your users might use.

If you want formatting, maybe look at markdown.