Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

h method, TextHelper, sanitize(), strip_tags()

2006-04-15   [ 1 comments ]

Now that I have my rudimentary commenting system, I thought it best to add some level of security to my site. All I need is some malicious visitor to stop by and insert a load of javascript into my comment system and- bam! my site is a walking timebomb.

So I ventured over to the wonderful world of the Rails API to find a suitable quick fix (as I plan on putting more effort into this later). After scanning the documentation I came across 3 ways to accomplish my task. Enter option 1:

<%=h comment.stuff_to_print %>

I could simply add the h method in every place I output the potentially dangerous contents. Quite easy to remember - but I am not too keen on using it for my situation - besides it would have to process those comments everytime they are pulled out of the database!

That alone drove me to find another, better solution. However, other flaws exist with this option - such as if the way you output changes (or the number of fields outputed changes) you would have to remember to always have that 'h' in the right spot.

Too much maintenance!

Now, enter option 2 and 3, both of which come from ActionView::Helpers::TextHelper and are easily used by adding this to your application.rb (or to a single controller if you so desire):

include ActionView::Helpers::TextHelper

Simple enough- if you run into a RoR error message such as:

undefined method `sanitize' for #<UserController:0x4093ed94>

Then it means you didn't properly include TextHelper.

Now that you have included TextHelper you can use the following 2 options without shame or fear of reprimand:

1. sanitize()
2. strip_tags()

The difference between the 2 functions is simple - strip_tags REMOVES all html tags, while sanitize EXCHANGES '<' with & lt; thus both effectively removing html or javascript threats (others as well).

My decision was to go with the sanitize() method and use it to fix up comment posts before inserting them into the database (that way I can still see who was trying to be not-so-nice, and so can everyone else!).

Well, The post today is simple and short, but I plan on getting more time to work on RoR this week. Happy Easter!

:author => "Charles Abbott"
Converting to Ruby on Rails
 


Charles said:

<script language='javascript1.3' type='text/javascript'>alert('I cant use javascript!')</script>

2006-04-15 13:44:54 UTC

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"