Rails SQL injection?

与世无争的帅哥 提交于 2019-11-26 19:43:06

问题


In Rails, when I want to find by a user given value and avoid SQL injection (escape apostrophes and the like) I can do something like this:

Post.all(:conditions => ['title = ?', params[:title]])

I know that an unsafe way of doing this (possible SQL injection) is this:

Post.all(:conditions => "title = #{params[:title]}")

My question is, does the following method prevent SQL injection or not?

Post.all(:conditions => {:title => params[:title]})

回答1:


Yes, it does. Only the second one is dangerous.




回答2:


One good reference from the RoR Guides.




回答3:


+1 @fphilipe and @yuval Check this 5 min video from railscast and this one from rails guide



来源:https://stackoverflow.com/questions/2962263/rails-sql-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!