Class: Brakeman::Collection

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/brakeman/tracker/collection.rb

Direct Known Subclasses

Controller, Library, Model, Template

Constant Summary

Constants included from Util

Util::ALL_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::DIR_CONST, Util::LITERALS, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_COOKIES, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::REQUEST_REQUEST_PARAMETERS, Util::SAFE_LITERAL, Util::SESSION, Util::SESSION_SEXP, Util::SIMPLE_LITERALS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#all_literals?, #array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #dir_glob?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #hash_values, #integer?, #kwsplat?, #literal?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #recurse_check?, #regexp?, #remove_kwsplat, #request_env?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #simple_literal?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #underscore

Constructor Details

#initialize(name, parent, file_name, src, tracker) ⇒ Collection

Returns a new instance of Collection.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/brakeman/tracker/collection.rb', line 10

def initialize name, parent, file_name, src, tracker
  @name = name
  @parent = parent
  @files = []
  @src = {}
  @includes = []
  @methods = { :public => {}, :private => {}, :protected => {} }
  @class_methods = {}
  @simple_methods = { :class => {}, instance: {} }
  @options = {}
  @tracker = tracker

  add_file file_name, src
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



8
9
10
# File 'lib/brakeman/tracker/collection.rb', line 8

def collection
  @collection
end

#filesObject (readonly)

Returns the value of attribute files.



8
9
10
# File 'lib/brakeman/tracker/collection.rb', line 8

def files
  @files
end

#includesObject (readonly)

Returns the value of attribute includes.



8
9
10
# File 'lib/brakeman/tracker/collection.rb', line 8

def includes
  @includes
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/brakeman/tracker/collection.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/brakeman/tracker/collection.rb', line 8

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/brakeman/tracker/collection.rb', line 8

def parent
  @parent
end

#srcObject (readonly)

Returns the value of attribute src.



8
9
10
# File 'lib/brakeman/tracker/collection.rb', line 8

def src
  @src
end

#trackerObject (readonly)

Returns the value of attribute tracker.



8
9
10
# File 'lib/brakeman/tracker/collection.rb', line 8

def tracker
  @tracker
end

Instance Method Details

#add_file(file_name, src) ⇒ Object



37
38
39
40
# File 'lib/brakeman/tracker/collection.rb', line 37

def add_file file_name, src
  @files << file_name unless @files.include? file_name
  @src[file_name] = src
end

#add_include(class_name) ⇒ Object



42
43
44
# File 'lib/brakeman/tracker/collection.rb', line 42

def add_include class_name
  @includes << class_name unless ancestor?(class_name)
end

#add_method(visibility, name, src, file_name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/brakeman/tracker/collection.rb', line 51

def add_method visibility, name, src, file_name
  meth_info = Brakeman::MethodInfo.new(name, src, self, file_name)
  add_simple_method_maybe meth_info

  if src.node_type == :defs
    @class_methods[name] = meth_info

    # TODO fix this weirdness
    name = :"#{src[1]}.#{name}"
  end

  @methods[visibility][name] = meth_info
end

#add_option(name, exp) ⇒ Object



46
47
48
49
# File 'lib/brakeman/tracker/collection.rb', line 46

def add_option name, exp
  @options[name] ||= []
  @options[name] << exp
end

#ancestor?(parent, seen = {}) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/brakeman/tracker/collection.rb', line 25

def ancestor? parent, seen={}
  seen[self.name] = true

  if self.parent == parent or self.name == parent or seen[self.parent]
    true
  elsif parent_model = collection[self.parent]
    parent_model.ancestor? parent, seen
  else
    false
  end
end

#each_methodObject



65
66
67
68
69
70
71
# File 'lib/brakeman/tracker/collection.rb', line 65

def each_method
  @methods.each do |_vis, meths|
    meths.each do |name, info|
      yield name, info
    end
  end
end

#fileObject



98
99
100
# File 'lib/brakeman/tracker/collection.rb', line 98

def file
  @files.first
end

#get_class_method(name) ⇒ Object



94
95
96
# File 'lib/brakeman/tracker/collection.rb', line 94

def get_class_method name
  @class_methods[name]
end

#get_instance_method(name) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/brakeman/tracker/collection.rb', line 84

def get_instance_method name
  @methods.each do |_vis, meths|
    if meths[name]
      return meths[name]
    end
  end

  nil
end

#get_method(name, type = :instance) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/brakeman/tracker/collection.rb', line 73

def get_method name, type = :instance
  case type
  when :class
    get_class_method name
  when :instance
    get_instance_method name
  else
    raise "Unexpected method type: #{type.inspect}"
  end
end

#get_simple_method_return_value(type, name) ⇒ Object



118
119
120
# File 'lib/brakeman/tracker/collection.rb', line 118

def get_simple_method_return_value type, name
  @simple_methods[type][name]
end

#methods_publicObject



114
115
116
# File 'lib/brakeman/tracker/collection.rb', line 114

def methods_public
  @methods[:public]
end

#top_lineObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/brakeman/tracker/collection.rb', line 102

def top_line
  if sexp? @src[file]
    @src[file].line
  else
    @src.each_value do |source|
      if sexp? source
        return source.line
      end
    end
  end
end