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.



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

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

Class Method Details

.constant_as_array(exp) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/brakeman/tracker/constants.rb', line 121

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



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/brakeman/tracker/constants.rb', line 149

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



55
56
57
58
59
60
61
62
63
64
# File 'lib/brakeman/tracker/constants.rb', line 55

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

  if match
    match.value
  else
    nil
  end
end

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



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

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



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

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

#find_constant(exp) ⇒ Object



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

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



105
106
107
108
109
110
111
# File 'lib/brakeman/tracker/constants.rb', line 105

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

#literal?(exp) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/brakeman/tracker/constants.rb', line 101

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

#sizeObject



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

def size
  @constants.length
end