【问题标题】:What's the best way to sanitize destroy_all - Rails清理destroy_all的最佳方法是什么-Rails
【发布时间】:2015-05-14 04:00:49
【问题描述】:

我在Rails 中有以下控制器:

class FooController < ApplicationController
    def delete_foo(bar):
        Foo.destroy_all("foo = '#{@bar}'")

Foo.destroy_all("foo = ?", @bar)

始终有效?

【问题讨论】:

    标签: sql ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    destroy_all 作用于关系。为什么不这样做

     Foo.where(foo: bar).destroy_all
    

    【讨论】:

      【解决方案2】:

      Foo.destroy_all("foo = ?", @bar),这是无效的。

      apidoc,我们会发现:

      destroy_all(conditions = nil) public
      

      destroy_all 方法只接受一个参数,参数可以是字符串、数组或散列。你不能传递两个参数。

      所以你可以这样写:

      Foo.destroy_all("foo = #{@bar}")
      Foo.destroy_all(foo: @bar)
      Foo.where(foo: @bar).destroy_all
      

      【讨论】:

      • 您可能会在那里受到SQL 攻击。
      • 是的,我们应该考虑在 SQL 中使用外部字符串时的安全后果。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 2013-02-03
      • 1970-01-01
      • 2017-05-07
      • 2011-03-25
      • 2010-11-25
      相关资源
      最近更新 更多