views:

182

answers:

3

What is a good method in Ruby to prevent SQL Injection?

+2  A: 

Not just in Ruby - bind your parameters (be it in the database, or in your client code).

davek
+1  A: 

Check out the guide they have up on this: http://guides.rubyonrails.org/security.html#injection

Basically, you want to use bind variables in your models to find data, rather than inline parameters..

Model.find(:first, :conditions => ["login = ? AND password = ?", entered_user_name, entered_password])
Dan McNevin
Yes, this does look good. Pretty much what I am used to in Java, thanks.
Zombies
You should at least mention that you're talking about active record.
sepp2k
I was just about to comment on this.. in my haste, I forgot to read that it was not specifically for Rails/ActiveRecord.. sorry about that!
Dan McNevin
+2  A: 

in straight up ruby? use prepared statements:

require 'mysql'
db = Mysql.new('localhost', 'user', 'password', 'database')
statement = db.prepare "SELECT * FROM table WHERE field = ?"
statement.execute 'value'
statement.fetch
statement.close
Mike Sherov
My problem with this is that it returns an array of results as opposed to a field or something much more manageable.....
Zombies
This Is just an example of how to use prepared statements for a select query. What you do with the results is up to you.
Mike Sherov