Class: Brakeman::Constants

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

Constant Summary collapse

LITERALS =
[:lit, :false, :str, :true, :array, :hash]

Constants included from Util

Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION, Util::SESSION_SEXP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #context_for, #cookies?, #false?, #file_by_name, #file_for, #github_url, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #regexp?, #relative_path, #request_env?, #request_value?, #result?, #set_env_defaults, #sexp?, #string?, #string_interp?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore

Constructor Details

#initializeConstants

Returns a new instance of Constants.



51
52
53
# File 'lib/brakeman/tracker/constants.rb', line 51

def initialize
  @constants = Hash.new { |h, k| h[k] = [] }
end

Class Method Details

.constant_as_array(exp) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/brakeman/tracker/constants.rb', line 130

def self.constant_as_array exp
  res = []
  while exp
    if exp.is_a? Sexp
      case exp.node_type
      when :const
        res << exp.value
        exp = nil
      when :colon3
        res << exp.value << :""
        exp = nil
      when :colon2
        res << exp.last
        exp = exp[1]
      else
        res << exp
        exp = nil
      end
    else
      res << exp
      exp = nil
    end
  end

  res.reverse!
  res
end

.get_constant_base_name(exp) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/brakeman/tracker/constants.rb', line 158

def self.get_constant_base_name exp
  return exp unless exp.is_a? Sexp

  case exp.node_type
  when :const, :colon3
    exp.value
  when :colon2
    exp.last
  else
    exp
  end
end

Instance Method Details

#[](exp) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/brakeman/tracker/constants.rb', line 59

def [] exp
  return unless constant? exp
  match = find_constant exp

  if match
    match.value
  else
    nil
  end
end

#add(name, value, context = nil) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/brakeman/tracker/constants.rb', line 100

def add name, value, context = nil
  if call? value and value.method == :freeze
    value = value.target
  end

  base_name = Constants.get_constant_base_name(name)
  @constants[base_name] << Constant.new(name, value, context)
end

#eachObject



122
123
124
125
126
127
128
# File 'lib/brakeman/tracker/constants.rb', line 122

def each
  @constants.each do |name, values|
    values.each do |constant|
      yield constant
    end
  end
end

#find_all(exp) ⇒ Object



95
96
97
98
# File 'lib/brakeman/tracker/constants.rb', line 95

def find_all exp
  base_name = Constants.get_constant_base_name(exp)
  @constants[base_name]
end

#find_constant(exp) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/brakeman/tracker/constants.rb', line 70

def find_constant exp
  base_name = Constants.get_constant_base_name(exp)

  if @constants.key? base_name
    @constants[base_name].find do |c|
      if c.match? exp
        return c
      end
    end

    name_array = Constants.constant_as_array(exp)

    # Avoid losing info about dynamic constant values
    return unless name_array.all? { |n| constant? n or n.is_a? Symbol }

    @constants[base_name].find do |c|
      if c.match? name_array
        return c
      end
    end
  end

  nil
end

#get_literal(name) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/brakeman/tracker/constants.rb', line 114

def get_literal name
  if x = self[name] and literal? x
    x
  else
    nil
  end
end

#literal?(exp) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/brakeman/tracker/constants.rb', line 110

def literal? exp
  exp.is_a? Sexp and LITERALS.include? exp.node_type
end

#sizeObject



55
56
57
# File 'lib/brakeman/tracker/constants.rb', line 55

def size
  @constants.length
end