Ruby on rails Rails 3-通过来自params的数据安全排序字段
我有这样的方法:Ruby on rails Rails 3-通过来自params的数据安全排序字段,ruby-on-rails,ruby,ruby-on-rails-3,sql-injection,Ruby On Rails,Ruby,Ruby On Rails 3,Sql Injection,我有这样的方法: def index state = state_filter() if state ... else @clients = Client.paginate(:page => params[:page], :per_page => 30).order(sort_column + ' ' + sort_direction) end end ... def sort_direction %
def index
state = state_filter()
if state
...
else
@clients = Client.paginate(:page => params[:page], :per_page => 30).order(sort_column + ' ' + sort_direction)
end
end
...
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
def sort_column
Client.column_names.include?(params[:sort]) ? params[:sort] : "title"
end
我通过扫描了我的应用程序,它发现我在排序的索引方法中可能有SQL注入。我试图解决这个问题,重写我的方法如下:
def sort_direction
case params[:direction]
when "asc" then "asc"
when "desc" then "desc"
else "asc"
end
end
def sort_column
case params[:sort]
when "title" then "title"
when "state" then "state"
when "created_at" then "created_at"
else "title"
end
end
但gem仍然认为我有弱点。解决此问题的正确方法是什么?我真的需要处理这个问题吗?您能给我们展示一个可能的SQL注入示例吗?我认为Rails 3会在默认情况下清理ARel查询,因此不允许这种情况发生。这与类似,甚至使用相同的方法名,在控制器中使用helper方法来处理我认为属于模型的东西。无论如何,请看我的答案。