0

Rails 3.2アプリでは、Brakeman 1.8.3により、モデル内の次のコードに対して高信頼性SQLインジェクション警告が発生します。

micropost.rb

def self.from_users_followed_by(user)
  followed_user_ids = Relationship.select(:followed_id).
                      where("follower_id = :user_id").
                      to_sql
  where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
        user_id: user.id)
end

ただし、Arel構文を使用しないようにコードを変更しても、警告は発生しません。

def self.from_users_followed_by(user)
  followed_user_ids = "SELECT followed_id FROM relationships
                       WHERE follower_id = :user_id"
  where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
        user_id: user.id)
end

これは誤検知ですか、それともArel構文またはto_sqlメソッドと関係がありますか...?警告を正当化する2つの例で実行される実際のコードの違いが何であるかわかりません。

4

1 に答える 1

3

誤検知です。

この状況では、BrakemanRelationshipはモデルであり、selectそれwhereがクエリメソッドであることを認識しています。Relationship.select(...).where(...).to_sqlしたがって、レコード属性(および潜在的に危険)であると想定します。to_sqlただし、前述のようにクエリのSQLコードを生成するだけなので、そうすべきではありません。これを修正します。

もちろん、2番目のバージョンでは、文字列リテラルを補間しているため、警告は表示されません。

于 2012-11-29T15:09:48.547 に答える