Class: Spektr::Checks::Sqli

Inherits:
Base
  • Object
show all
Defined in:
lib/spektr/checks/sqli.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#app_version_between?, #dupe?, #model_attribute?, #should_run?, #target_affected?, #user_input?, #version_affected, #version_between?, #warn!

Constructor Details

#initialize(app, target) ⇒ Sqli

Returns a new instance of Sqli.



4
5
6
7
8
9
# File 'lib/spektr/checks/sqli.rb', line 4

def initialize(app, target)
  super
  @name = "SQL Injection"
  @name = "SQL Injection"
  @targets = ["Spektr::Targets::Base", "Spektr::Targets::Controller", "Spektr::Targets::Model"]
end

Instance Method Details

#check_argument(argument, method, call) ⇒ Object



44
45
46
47
48
49
# File 'lib/spektr/checks/sqli.rb', line 44

def check_argument(argument, method, call)
  return if argument.nil?
  if user_input?(argument.type, argument.name, argument.ast, argument)
    warn! @target, self, call.location, "Possible SQL Injection at #{method}"
  end
end

#runObject



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
# File 'lib/spektr/checks/sqli.rb', line 11

def run
  return unless super

  [
    :average, :count, :maximum, :minimum, :sum, :exists?,
    :find_by, :find_by!, :find_or_create_by, :find_or_create_by!,
    :find_or_initialize_by, :from, :group, :having, :join, :lock,
    :where, :not, :select, :rewhere, :reselect, :update_all

  ].each do |m|
    @target.find_calls(m).each do |call|
      check_argument(call.arguments.first, m, call)
    end
  end
  [:calculate].each do |m|
    @target.find_calls(m).each do |call|
      check_argument(call.arguments[1], m, call)
    end
  end

  [:delete_by, :destroy_by].each do |m|
    @target.find_calls(m).each do |call|
      if call.arguments.first
        check_argument(call.arguments.first, m, call)
      end
      call.options.values.each do |option|
        check_argument(@target.ast_to_exp(option.key), m, call)
        check_argument(@target.ast_to_exp(option.value), m, call)
      end
    end
  end
end