Brakeman 提出的 Rails 中的安全问题

Security Issues in Rails raised by Brakeman

在我的项目中,在使用 Brakeman gem 时,出现了以下安全问题:

1) 在下面的语句中,Unescaped model attribute 引发了错误

CashTransaction.find(session[:transaction_id]).customer.address_1

我知道 Rails 使用基于 cookie 的会话存储。但是,Rails 4 使用 cookie 相对安全,因为您需要 Rails secret token 才能破坏它。

那么,这是误报吗?如果不是,我该如何消除此漏洞?

2)其次,我有一个场景需要检查是否存在具有典型属性的记录。为此,我有以下代码

  def check_email
    render json: ( is_available('email', params[:user][:email]) )
  end

  def is_email_available
    is_email_taken = is_available('email', params[:user][:email])
    render json: !is_email_taken
  end

  def is_username_available
    is_username_taken = is_available('username', params[:user][:username])
    render json: !is_username_taken
  end

  def is_available(type, value)
    User.where("#{type}=?", value).exists?
  end

并且 Brakeman 提出以下警告

Possible SQL injection. User.where("#{(local type)}=?", (local value))

我怎样才能消除这个漏洞,同时让我的代码变干?

第二部分:

如果type不是用户输入,你可以

User.where(type.to_sym => value)

如果是用户输入,您应该这样做。

User.where("%s =  %s" % [type, "'#{value}'"])