Topic: Question about ActiveRecord and sanitizing inputs
I've been reading through the ActiveRecord::Base API, and came across this:
Conditions
Conditions can either be specified as a string, array, or hash representing the WHERE-part of an SQL statement. The array form is to be used when the condition input is tainted and requires sanitization. The string form can be used for statements that don’t involve tainted data. The hash form works much like the array form, except only equality and range is possible. Examples:
class User < ActiveRecord::Base def self.authenticate_unsafely(user_name, password) where("user_name = '#{user_name}' AND password = '#{password}'").first end def self.authenticate_safely(user_name, password) where("user_name = ? AND password = ?", user_name, password).first end def self.authenticate_safely_simply(user_name, password) where(:user_name => user_name, :password => password).first end endThe authenticate_unsafely method inserts the parameters directly into the query and is thus susceptible to SQL-injection attacks if the user_name and password parameters come directly from an HTTP request. The authenticate_safely and authenticate_safely_simply both will sanitize the user_name and password before inserting them in the query, which will ensure that an attacker can’t escape the query and fake the login (or worse).
My question is about the bolded statement towards the end:
The authenticate_safely and authenticate_safely_simply both will sanitize the user_name and password before inserting them in the query
So does this mean that Rails automatically sanitizes the inputs for me, or is this just saying that the latter two formats allow the inputs to be sanitized using code that's not shown?
Thanks.