SQL注入prevention为创造轨控制方法 [英] sql injection prevention for create method in rails controller
问题描述
中所见comment_controller.rb:
As seen in comment_controller.rb:
def create
@comment = Comment.new(params[:comment])
@comment.save
end
林假设这是SQL注入不安全的。但是,什么是做这件事的正确方法是什么?..所有在网上处理发现的例子。
Im assuming that this is SQL injection-unsafe. But what is the correct way of doing it?.. All the examples on the net deal with finds.
推荐答案
这code 是从SQL注入攻击安全。逃逸是通过ActiveRecord的完成,因此任何时候你调用一个模型的查找
,创建
,新
/ 保存
,或者,做数据库交互的任何其他方法,你真行。如果你使用原始的SQL的选项之一,唯一的例外是,例如:
That code is safe from SQL injection attacks. The escaping is done by ActiveRecord, so any time you call a model's find
, create
, new
/save
, or any other method that does database interaction, you're OK. The only exception is if you use raw SQL for one of the options, for example:
Comment.find(:all, :conditions => "user_id = #{params[:user_id]}")
在preferred格式为:
the preferred form is:
Comment.find(:all, :conditions => {:user_id => params[:user_id]})
将被自动保护,防止SQL注入攻击。
which will be automatically protected against SQL injection.
这篇关于SQL注入prevention为创造轨控制方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!