Rails是这个查询开放SQL注入? [英] Rails is this query open to sql injection?

查看:160
本文介绍了Rails是这个查询开放SQL注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习如何编写使用ActiveRecord的很好的查询。我很好奇,如果该查询是受到SQL注入的方式,因为我使用的查询日期字段。

有人可以指出任何明显错误或更好的方法来写这个查询?

  @arrangements_for_month =
  Arrangement.joins(:时隙)。
              其中,(timeslots.timeslot BETWEEN'#{月}和{#} month.end_of_month',则params [:ID])。
              为了('LOCATION_ID)
 

解决方案

您应该只使用其中的参数是安全的preferred方式。请查看本指南

  

建立自己的条件,纯弦可以让你容易受到SQL注入攻击。例如, Client.where(FIRST_NAME LIKE'%#{PA​​RAMS [:FIRST_NAME]}%')是不是安全。请参阅下一节为preferred方式使用数组来处理的条件。

尝试:

  @arrangements_for_month = Arrangement.joins(:时隙)
  。凡(timeslots.timeslot之间?和?,月,month.end_of_month)
  .order('LOCATION_ID)
 

和刚抬起头,如果你喜欢,还有就是定义一样,使用红宝石的范围的范围条件的另一种方式,如链接引导的那一段描述:

  Client.where(:created_at =>(Time.now.midnight  -  1.day).. Time.now.midnight)
 

所以,不知道什么对你的code,你也许可以做这样的事情:

  @arrangements_for_month = Arrangement.joins(:时隙)
  。凡(timeslots.timeslot=>当月.. month.end_of_month)
  .order('LOCATION_ID)
 

I'm still learning how to write good queries using ActiveRecord. I'm curious if this query is subject to sql injection because of the way i'm using the date field in the query.

Can someone please point out any obvious mistakes or any better ways to write this query?

@arrangements_for_month =
  Arrangement.joins(:timeslot).
              where("timeslots.timeslot BETWEEN '#{month}' AND '#{month.end_of_month}'", params[:id]).
              order('location_id')

解决方案

You should just use the preferred way of including parameters to be safe. Check out this guide:

Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, Client.where("first_name LIKE '%#{params[:first_name]}%'") is not safe. See the next section for the preferred way to handle conditions using an array.

Try:

@arrangements_for_month = Arrangement.joins(:timeslot)
  .where("timeslots.timeslot BETWEEN ? AND ?", month, month.end_of_month)
  .order('location_id')

And just a heads up, if you like, there is an alternative way to define a range condition like that using ruby ranges, as described in that section of the linked guide:

Client.where(:created_at => (Time.now.midnight - 1.day)..Time.now.midnight)

So, without knowing anything else about your code, you can probably do something like this:

@arrangements_for_month = Arrangement.joins(:timeslot)
  .where("timeslots.timeslot" => month .. month.end_of_month)
  .order('location_id')

这篇关于Rails是这个查询开放SQL注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆