Class: TaintedLove::Replacer::ReplaceActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/tainted_love/replacer/replace_active_record.rb

Instance Method Summary collapse

Methods inherited from Base

replacers

Instance Method Details

#replace!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tainted_love/replacer/replace_active_record.rb', line 10

def replace!
  require 'active_record/relation'

  TaintedLove.proxy_method('ActiveRecord::QueryMethods', :where) do |_, *args|
    unless args.empty?
      f = args.first
      if f.is_a?(String) && f.tainted?
        TaintedLove.report(:ReplaceActiveRecord, f, [:sqli], 'Model.where using tainted string')
      end
    end
  end

  TaintedLove.proxy_method('ActiveRecord::QueryMethods', :select) do |_, *args|
    unless args.empty?
      f = args.first
      if f.is_a?(String) && f.tainted?
        TaintedLove.report(:ReplaceActiveRecord, f, [:sqli], 'Model#select using tainted string')
      end
    end
  end

  mod = Module.new do
    [:find_by_sql, :count_by_sql].each do |method|
      define_method(method) do |*args|
        if args.first.tainted?
          TaintedLove.report(:ReplaceActiveRecord, args.first, [:sqli], "Model##{method} using tainted string")
        end

        super(*args)
      end
    end
  end

  ActiveRecord::Base.extend(mod)
end

#should_replace?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/tainted_love/replacer/replace_active_record.rb', line 6

def should_replace?
  Object.const_defined?('ActiveRecord')
end