Ruby on Rails的update_attributes真的能有效防止SQL注入吗?
- 内容介绍
- 文章标签
- 相关推荐
本文共计331个文字,预计阅读时间需要2分钟。
`update_attributes` 方法本身并不能直接防止 SQL 注入。它只是 Rails 中用于更新记录的一个便捷方法。为了防止 SQL 注入,你需要在传递参数给 `update_attributes` 时确保参数是安全的。
例如:
rubyif @user.update_attributes(params[:user]) # 更新成功后的操作else # 更新失败后的操作end
在这个例子中,`params[:user]` 应该是一个安全的参数,通常是通过 Rails 的表单对象或参数过滤来确保的。确保传递给 `update_attributes` 的参数是经过验证和清理的,可以有效地防止 SQL 注入攻击。
update_attributes是否可以防止sql注入?例:
if @user.update_attributes(params[:user]) # updated end
我知道find(),{}和[]做find:conditions,但是没有看到关于这个方法的任何信息.
是的,它确实.在内部,它只是循环遍历所有属性,设置它们的值然后调用save!def update_attributes(attributes) with_transaction_returning_status do self.attributes = attributes save end end def attributes=(new_attributes, guard_protected_attributes = true) ... attributes.each do |k, v| if k.include?("(") multi_parameter_attributes << [ k, v ] elsif respond_to?("#{k}=") send("#{k}=", v) else raise(UnknownAttributeError, "unknown attribute: #{k}") end end end
换句话说,它的作用是什么
m.update_attributes(:attr1 => "foo", :attr2 => "bar") m.attr1 = "foo" m.attr2 = "bar" m.save
本文共计331个文字,预计阅读时间需要2分钟。
`update_attributes` 方法本身并不能直接防止 SQL 注入。它只是 Rails 中用于更新记录的一个便捷方法。为了防止 SQL 注入,你需要在传递参数给 `update_attributes` 时确保参数是安全的。
例如:
rubyif @user.update_attributes(params[:user]) # 更新成功后的操作else # 更新失败后的操作end
在这个例子中,`params[:user]` 应该是一个安全的参数,通常是通过 Rails 的表单对象或参数过滤来确保的。确保传递给 `update_attributes` 的参数是经过验证和清理的,可以有效地防止 SQL 注入攻击。
update_attributes是否可以防止sql注入?例:
if @user.update_attributes(params[:user]) # updated end
我知道find(),{}和[]做find:conditions,但是没有看到关于这个方法的任何信息.
是的,它确实.在内部,它只是循环遍历所有属性,设置它们的值然后调用save!def update_attributes(attributes) with_transaction_returning_status do self.attributes = attributes save end end def attributes=(new_attributes, guard_protected_attributes = true) ... attributes.each do |k, v| if k.include?("(") multi_parameter_attributes << [ k, v ] elsif respond_to?("#{k}=") send("#{k}=", v) else raise(UnknownAttributeError, "unknown attribute: #{k}") end end end
换句话说,它的作用是什么
m.update_attributes(:attr1 => "foo", :attr2 => "bar") m.attr1 = "foo" m.attr2 = "bar" m.save

