Is ActiveRecord's “order” method vulnerable to SQL injection?

后端 未结 5 2212
我寻月下人不归
我寻月下人不归 2020-12-17 10:32

I know it\'s not safe to use interpolated strings when calling .where.

e.g. this:

Client.where(\"orders_count = #{params[:orders]}\")

5条回答
  •  余生分开走
    2020-12-17 11:18

    Yes, ActiveRecord's “order” method is vulnerable to SQL injection.

    No, it is not safe to use interpolated strings when calling .order.

    The above answers to my question have been confirmed by Aaron Patterson, who pointed me to http://rails-sqli.org/#order . From that page:

    Taking advantage of SQL injection in ORDER BY clauses is tricky, but a CASE statement can be used to test other fields, switching the sort column for true or false. While it can take many queries, an attacker can determine the value of the field.

    Therefore it's important to manually check anything going to order is safe; perhaps by using methods similar to @dmcnally's suggestions.

    Thanks all.

提交回复
热议问题