SQL注入prevention为创造轨控制方法 [英] sql injection prevention for create method in rails controller

查看:119
本文介绍了SQL注入prevention为创造轨控制方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

中所见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屋!

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