Class: Brakeman::Constants
- Inherits:
-
Object
- Object
- Brakeman::Constants
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_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, 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
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Util
#array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #kwsplat?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #regexp?, #remove_kwsplat, #request_env?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #underscore
Constructor Details
Returns a new instance of Constants.
51
52
53
|
# File 'lib/brakeman/tracker/constants.rb', line 51
def initialize
@constants = {}
end
|
Class Method Details
.constant_as_array(exp) ⇒ Object
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
157
|
# File 'lib/brakeman/tracker/constants.rb', line 131
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
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/brakeman/tracker/constants.rb', line 159
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
108
|
# 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] ||= []
@constants[base_name] << Constant.new(name, value, context)
end
|
#each ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/brakeman/tracker/constants.rb', line 123
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)
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
115
116
117
118
119
120
121
|
# File 'lib/brakeman/tracker/constants.rb', line 115
def get_literal name
if x = self[name] and literal? x
x
else
nil
end
end
|
#literal?(exp) ⇒ Boolean
111
112
113
|
# File 'lib/brakeman/tracker/constants.rb', line 111
def literal? exp
exp.is_a? Sexp and LITERALS.include? exp.node_type
end
|
#size ⇒ Object
55
56
57
|
# File 'lib/brakeman/tracker/constants.rb', line 55
def size
@constants.length
end
|