Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the .order method parameters in ActiveRecord sanitized by default?

I'm trying to pass a string into the .order method, such as

Item.order(orderBy)

I was wondering if orderBy gets sanitized by default and if not, what would be the best way to sanitize it.

like image 739
andreimarinescu Avatar asked Feb 15 '13 14:02

andreimarinescu


1 Answers

The order does not get sanitized. This query will actually drop the Users table:

Post.order("title; drop table users;")

You'll want to check the orderBy variable before running the query if there's any way orderBy could be tainted from user input. Something like this could work:

items = Item.scoped
if Item.column_names.include?(orderBy)
  items = items.order(orderBy)
end
like image 173
Dylan Markow Avatar answered Oct 14 '22 13:10

Dylan Markow