Best way to go about sanitizing user input in rails

Dave picture Dave · Feb 19, 2014 · Viewed 35.5k times · Source

I've read a lot about this and know there are many related questions on here, but I couldn't find a definitive guide for how to go about sanitizing everything. One option is to sanitize on insert, for example I have the following in my model

before_validation :sanitize_content, :on => :create
def sanitize_content
  self.content = ActionController::Base.helpers.sanitize(self.content)
end

Do I need to run this on every field in every model? I'm guessing the :on => :create should be removed too so it runs when updates too?

The other option is to sanitize when data is displayed in views, using simple_format, or .html_safe or sanitize(fieldname). SHould I be sanitizing in all my views for every single field, as well as on insert? Having to do this manually everywhere doesn't seem very railsy

Thanks for any help

Answer

Paul Richter picture Paul Richter · Feb 19, 2014

TL;DR
Regarding user input and queries: Make sure to always use the active record query methods (such as .where), and avoid passing parameters using string interpolation; pass them as hash parameter values, or as parameterized statements.

Regarding rendering potentially unsafe user-generated html / javascript content: As of Rails 3, html/javascript text is automatically properly escaped so that it appears as plain text on the page, rather than interpreted as html/javascript, so you don't need to explicitly sanitize (or use <%= h(potentially_unsafe_user_generated_content)%>

If I understand you correctly, you don't need to worry about sanitizing data in this manner, as long as you use the active record query methods correctly. For example:

Lets say our parameter map looks like this, as a result of a malicious user inputting the following string into the user_name field:

:user_name => "(select user_name from users limit 1)"

The bad way (don't do this):

Users.where("user_name = #{params[:id}") # string interpolation is bad here

The resulting query would look like:

SELECT `users`.* FROM `users` WHERE (user_name = (select user_name from users limit 1))

Direct string interpolation in this manner will place the literal contents of the parameter value with key :user_name into the query without sanitization. As you probably know, the malicious user's input is treated as plain 'ol SQL, and the danger is pretty clear.

The good way (Do this):

Users.where(id: params[:id]) # hash parameters

OR

Users.where("id = ?", params[:id]) # parameterized statement

The resulting query would look like:

SELECT `users`.* FROM `users` WHERE user_name = '(select user_name from users limit 1)'

So as you can see, Rails in fact sanitizes it for you, so long as you pass the parameter in as a hash, or method parameter (depending on which query method you're using).

The case for sanitization of data on creating new model records doesn't really apply, as the new or create methods are expecting a hash of values. Even if you attempt to inject unsafe SQL code into the hash, the values of the hash are treated as plain strings, for example:

User.create(:user_name=>"bobby tables); drop table users;")

Results in the query:

INSERT INTO `users` (`user_name`) VALUES ('bobby tables); drop table users;')

So, same situation as above.

I hope that helps. Let me know if I've missed or misunderstood anything.

Edit Regarding escaping html and javascript, the short version is that ERB "escapes" your string content for you so that it is treated as plain text. You can have it treated like html if you really want, by doing your_string_content.html_safe.

However, simply doing something like <%= your_string_content %> is perfectly safe. The content is treated as a string on the page. In fact, if you examine the DOM using Chrome Developer Tools or Firebug, you should in fact see quotes around that string.