class Brakeman::FindCall

Finds method calls matching the given target(s).

#-- This should be deprecated --#
#--  Do not use for new code  --#

Targets/methods can be:

- nil: matches anything, including nothing
- Empty array: matches nothing
- Symbol: matches single target/method exactly
- Array of symbols: matches against any of the symbols
- Regular expression: matches the expression
- Array of regular expressions: matches any of the expressions

If a target is also the name of a class, methods called on instances of that class will also be matched, in a very limited way. (Any methods called on Klass.new, basically. More useful when used in conjunction with AliasProcessor.)

Examples:

#To find any uses of this class:
FindCall.new :FindCall, nil

#Find system calls without a target
FindCall.new [], [:system, :exec, :syscall]

#Find all calls to length(), no matter the target
FindCall.new nil, :length

#Find all calls to sub, sub!, gsub, or gsub!
FindCall.new nil, /^g?sub!?$/

Public Class Methods

new(targets, methods, tracker) click to toggle source
Calls superclass method Brakeman::BasicProcessor::new
# File lib/brakeman/processors/lib/find_call.rb, line 36
def initialize targets, methods, tracker
  super tracker
  @calls = []
  @find_targets = targets
  @find_methods = methods
  @current_class = nil
  @current_method = nil
end

Public Instance Methods

matches() click to toggle source

Returns a list of results.

A result looks like:

s(:result, :ClassName, :method_name, s(:call, ...))
# File lib/brakeman/processors/lib/find_call.rb, line 50
def matches
  @calls
end
process_attrasgn(exp) click to toggle source

Process an assignment like a call

# File lib/brakeman/processors/lib/find_call.rb, line 84
def process_attrasgn exp
  process_call exp
end
process_call(exp) click to toggle source

Look for matching calls and add them to results

# File lib/brakeman/processors/lib/find_call.rb, line 70
def process_call exp
  target = get_target exp.target
  method = exp.method

  process_call_args exp

  if match(@find_targets, target) and match(@find_methods, method)
    @calls << Sexp.new(:result, @current_module, @current_class, @current_method, exp).line(exp.line)
  end
  
  exp
end
process_defn(exp) click to toggle source

Process body of method

# File lib/brakeman/processors/lib/find_call.rb, line 63
def process_defn exp
  process_all exp.body
end
Also aliased as: process_defs
process_defs(exp)
Alias for: process_defn
process_source(exp) click to toggle source

Process the given source. Provide either class and method being searched or the template. These names are used when reporting results.

Use FindCall#matches to retrieve results.

# File lib/brakeman/processors/lib/find_call.rb, line 58
def process_source exp
  process exp
end

Private Instance Methods

get_target(exp) click to toggle source

Gets the target of a call as a Symbol if possible

# File lib/brakeman/processors/lib/find_call.rb, line 92
def get_target exp
  if sexp? exp
    case exp.node_type
    when :ivar, :lvar, :const, :lit
      exp.value
    when :colon2
      class_name exp
    else
      exp
    end
  else
    exp
  end
end
match(search_terms, item) click to toggle source

Checks if the search terms match the given item

# File lib/brakeman/processors/lib/find_call.rb, line 108
def match search_terms, item
  case search_terms
  when Symbol
    if search_terms == item
      true
    else
      false
    end
  when Enumerable
    if search_terms.empty?
      item == nil
    end
  end
end