簡體   English   中英

Rails Brakeman:Arel 的 SQL 注塑

[英]Rails Brakeman: SQL Injection for Arel

我的user_ransaker.rb文件中有以下代碼:

ransacker :new_donors do
      sql = %{(
              users.id IN (
                #{User.new_donor_sql}
              )
            )}
      Arel.sql(sql)
    end

user.rb model 上:

def self.new_donor_sql
    part_1 = %{(
      SELECT distinct(user_id)
      FROM donations
    }
    part_1
end

對於上述聲明,我收到以下 Brakeman 警告:

Confidence: High
Category: SQL Injection
Check: SQL
Message: Possible SQL injection
Code: Arel.sql("(\n users.id IN (\n #{User.new_donor_sql}\n)\n)")
File: app/models/concerns/user_ransackers.rb

這是一個有效的錯誤嗎? 如果我使用 ActiveRecord 來編寫 SQL 語句,我可以使用? 占位符,如果我需要插入值。 我不確定如何解決此警告。 如果這是一個有效的警告,我該如何補救?

如果你要 Arel 然后做一些關系代數:

class User < ApplicationRecord
  def self.new_donor_sql
    arel_table.project(arel_table[:user_id]).distinct
  end
end
ransacker :new_donors do
  User.arel_table.then do |users|
    users.where(users[:id].in(User.new_donor_sql)).where_sql
  end
end

您也可以放棄 class 方法:

ransacker :new_donors do
  User.arel_table.then do |users|
    subquery = users.project(users[:user_id]).distinct
    users.where(users[:id].in(subquery)).where_sql
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM