brakeman-min 0.5.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +529 -0
- data/README.md +74 -28
- data/bin/brakeman +60 -266
- data/lib/brakeman.rb +422 -0
- data/lib/brakeman/app_tree.rb +101 -0
- data/lib/brakeman/brakeman.rake +10 -0
- data/lib/brakeman/call_index.rb +215 -0
- data/lib/brakeman/checks.rb +180 -0
- data/lib/brakeman/checks/base_check.rb +538 -0
- data/lib/brakeman/checks/check_basic_auth.rb +89 -0
- data/lib/brakeman/checks/check_content_tag.rb +162 -0
- data/lib/brakeman/checks/check_cross_site_scripting.rb +334 -0
- data/lib/{checks → brakeman/checks}/check_default_routes.rb +13 -6
- data/lib/brakeman/checks/check_deserialize.rb +57 -0
- data/lib/brakeman/checks/check_digest_dos.rb +38 -0
- data/lib/brakeman/checks/check_escape_function.rb +21 -0
- data/lib/brakeman/checks/check_evaluation.rb +33 -0
- data/lib/brakeman/checks/check_execute.rb +98 -0
- data/lib/brakeman/checks/check_file_access.rb +62 -0
- data/lib/brakeman/checks/check_filter_skipping.rb +31 -0
- data/lib/brakeman/checks/check_forgery_setting.rb +54 -0
- data/lib/brakeman/checks/check_jruby_xml.rb +38 -0
- data/lib/brakeman/checks/check_json_parsing.rb +102 -0
- data/lib/brakeman/checks/check_link_to.rb +132 -0
- data/lib/brakeman/checks/check_link_to_href.rb +92 -0
- data/lib/{checks → brakeman/checks}/check_mail_to.rb +14 -13
- data/lib/brakeman/checks/check_mass_assignment.rb +143 -0
- data/lib/brakeman/checks/check_model_attr_accessible.rb +48 -0
- data/lib/brakeman/checks/check_model_attributes.rb +118 -0
- data/lib/brakeman/checks/check_model_serialize.rb +66 -0
- data/lib/{checks → brakeman/checks}/check_nested_attributes.rb +10 -6
- data/lib/brakeman/checks/check_quote_table_name.rb +40 -0
- data/lib/brakeman/checks/check_redirect.rb +177 -0
- data/lib/brakeman/checks/check_render.rb +62 -0
- data/lib/brakeman/checks/check_response_splitting.rb +21 -0
- data/lib/brakeman/checks/check_safe_buffer_manipulation.rb +31 -0
- data/lib/brakeman/checks/check_sanitize_methods.rb +54 -0
- data/lib/brakeman/checks/check_select_tag.rb +60 -0
- data/lib/brakeman/checks/check_select_vulnerability.rb +58 -0
- data/lib/brakeman/checks/check_send.rb +35 -0
- data/lib/brakeman/checks/check_send_file.rb +19 -0
- data/lib/brakeman/checks/check_session_settings.rb +145 -0
- data/lib/brakeman/checks/check_single_quotes.rb +101 -0
- data/lib/brakeman/checks/check_skip_before_filter.rb +62 -0
- data/lib/brakeman/checks/check_sql.rb +577 -0
- data/lib/brakeman/checks/check_strip_tags.rb +64 -0
- data/lib/brakeman/checks/check_symbol_dos.rb +67 -0
- data/lib/brakeman/checks/check_translate_bug.rb +45 -0
- data/lib/brakeman/checks/check_unsafe_reflection.rb +51 -0
- data/lib/brakeman/checks/check_validation_regex.rb +88 -0
- data/lib/brakeman/checks/check_without_protection.rb +64 -0
- data/lib/brakeman/checks/check_yaml_parsing.rb +121 -0
- data/lib/brakeman/differ.rb +66 -0
- data/lib/{format → brakeman/format}/style.css +28 -0
- data/lib/brakeman/options.rb +256 -0
- data/lib/brakeman/parsers/rails2_erubis.rb +6 -0
- data/lib/brakeman/parsers/rails2_xss_plugin_erubis.rb +48 -0
- data/lib/{scanner_erubis.rb → brakeman/parsers/rails3_erubis.rb} +8 -21
- data/lib/brakeman/processor.rb +102 -0
- data/lib/brakeman/processors/alias_processor.rb +780 -0
- data/lib/{processors → brakeman/processors}/base_processor.rb +90 -74
- data/lib/brakeman/processors/config_processor.rb +14 -0
- data/lib/brakeman/processors/controller_alias_processor.rb +334 -0
- data/lib/brakeman/processors/controller_processor.rb +265 -0
- data/lib/{processors → brakeman/processors}/erb_template_processor.rb +21 -19
- data/lib/brakeman/processors/erubis_template_processor.rb +96 -0
- data/lib/brakeman/processors/gem_processor.rb +59 -0
- data/lib/{processors → brakeman/processors}/haml_template_processor.rb +26 -21
- data/lib/brakeman/processors/lib/find_all_calls.rb +185 -0
- data/lib/{processors → brakeman/processors}/lib/find_call.rb +23 -28
- data/lib/brakeman/processors/lib/find_return_value.rb +134 -0
- data/lib/brakeman/processors/lib/processor_helper.rb +82 -0
- data/lib/{processors/config_processor.rb → brakeman/processors/lib/rails2_config_processor.rb} +32 -35
- data/lib/{processors → brakeman/processors}/lib/rails2_route_processor.rb +60 -52
- data/lib/brakeman/processors/lib/rails3_config_processor.rb +129 -0
- data/lib/brakeman/processors/lib/rails3_route_processor.rb +282 -0
- data/lib/{processors → brakeman/processors}/lib/render_helper.rb +54 -20
- data/lib/brakeman/processors/lib/route_helper.rb +62 -0
- data/lib/{processors → brakeman/processors}/library_processor.rb +24 -17
- data/lib/{processors → brakeman/processors}/model_processor.rb +46 -22
- data/lib/{processors → brakeman/processors}/output_processor.rb +34 -40
- data/lib/brakeman/processors/route_processor.rb +17 -0
- data/lib/brakeman/processors/slim_template_processor.rb +113 -0
- data/lib/brakeman/processors/template_alias_processor.rb +120 -0
- data/lib/{processors → brakeman/processors}/template_processor.rb +10 -7
- data/lib/brakeman/report.rb +68 -0
- data/lib/brakeman/report/ignore/config.rb +130 -0
- data/lib/brakeman/report/ignore/interactive.rb +311 -0
- data/lib/brakeman/report/initializers/faster_csv.rb +7 -0
- data/lib/brakeman/report/initializers/multi_json.rb +29 -0
- data/lib/brakeman/report/renderer.rb +24 -0
- data/lib/brakeman/report/report_base.rb +279 -0
- data/lib/brakeman/report/report_csv.rb +56 -0
- data/lib/brakeman/report/report_hash.rb +22 -0
- data/lib/brakeman/report/report_html.rb +203 -0
- data/lib/brakeman/report/report_json.rb +46 -0
- data/lib/brakeman/report/report_table.rb +109 -0
- data/lib/brakeman/report/report_tabs.rb +17 -0
- data/lib/brakeman/report/templates/controller_overview.html.erb +18 -0
- data/lib/brakeman/report/templates/controller_warnings.html.erb +17 -0
- data/lib/brakeman/report/templates/error_overview.html.erb +25 -0
- data/lib/brakeman/report/templates/header.html.erb +44 -0
- data/lib/brakeman/report/templates/ignored_warnings.html.erb +21 -0
- data/lib/brakeman/report/templates/model_warnings.html.erb +17 -0
- data/lib/brakeman/report/templates/overview.html.erb +34 -0
- data/lib/brakeman/report/templates/security_warnings.html.erb +19 -0
- data/lib/brakeman/report/templates/template_overview.html.erb +17 -0
- data/lib/brakeman/report/templates/view_warnings.html.erb +30 -0
- data/lib/brakeman/report/templates/warning_overview.html.erb +13 -0
- data/lib/brakeman/rescanner.rb +446 -0
- data/lib/brakeman/scanner.rb +362 -0
- data/lib/brakeman/tracker.rb +296 -0
- data/lib/brakeman/util.rb +413 -0
- data/lib/brakeman/version.rb +3 -0
- data/lib/brakeman/warning.rb +217 -0
- data/lib/brakeman/warning_codes.rb +68 -0
- data/lib/ruby_parser/bm_sexp.rb +562 -0
- data/lib/ruby_parser/bm_sexp_processor.rb +230 -0
- metadata +152 -66
- data/lib/checks.rb +0 -71
- data/lib/checks/base_check.rb +0 -357
- data/lib/checks/check_cross_site_scripting.rb +0 -336
- data/lib/checks/check_evaluation.rb +0 -27
- data/lib/checks/check_execute.rb +0 -110
- data/lib/checks/check_file_access.rb +0 -46
- data/lib/checks/check_forgery_setting.rb +0 -42
- data/lib/checks/check_mass_assignment.rb +0 -74
- data/lib/checks/check_model_attributes.rb +0 -36
- data/lib/checks/check_redirect.rb +0 -98
- data/lib/checks/check_render.rb +0 -65
- data/lib/checks/check_send_file.rb +0 -15
- data/lib/checks/check_session_settings.rb +0 -79
- data/lib/checks/check_sql.rb +0 -146
- data/lib/checks/check_validation_regex.rb +0 -60
- data/lib/processor.rb +0 -86
- data/lib/processors/alias_processor.rb +0 -384
- data/lib/processors/controller_alias_processor.rb +0 -237
- data/lib/processors/controller_processor.rb +0 -202
- data/lib/processors/erubis_template_processor.rb +0 -85
- data/lib/processors/lib/find_model_call.rb +0 -39
- data/lib/processors/lib/processor_helper.rb +0 -36
- data/lib/processors/lib/rails3_route_processor.rb +0 -184
- data/lib/processors/lib/route_helper.rb +0 -34
- data/lib/processors/params_processor.rb +0 -77
- data/lib/processors/route_processor.rb +0 -11
- data/lib/processors/template_alias_processor.rb +0 -86
- data/lib/report.rb +0 -680
- data/lib/scanner.rb +0 -227
- data/lib/tracker.rb +0 -144
- data/lib/util.rb +0 -141
- data/lib/version.rb +0 -1
- data/lib/warning.rb +0 -99
@@ -0,0 +1,10 @@
|
|
1
|
+
namespace :brakeman do
|
2
|
+
|
3
|
+
desc "Run Brakeman"
|
4
|
+
task :run, :output_files do |t, args|
|
5
|
+
require 'brakeman'
|
6
|
+
|
7
|
+
files = args[:output_files].split(' ') if args[:output_files]
|
8
|
+
Brakeman.run :app_path => ".", :output_files => files, :print_report => true
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,215 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
#Stores call sites to look up later.
|
4
|
+
class Brakeman::CallIndex
|
5
|
+
|
6
|
+
#Initialize index with calls from FindAllCalls
|
7
|
+
def initialize calls
|
8
|
+
@calls_by_method = Hash.new
|
9
|
+
@calls_by_target = Hash.new
|
10
|
+
|
11
|
+
index_calls calls
|
12
|
+
end
|
13
|
+
|
14
|
+
#Find calls matching specified option hash.
|
15
|
+
#
|
16
|
+
#Options:
|
17
|
+
#
|
18
|
+
# * :target - symbol, array of symbols, or regular expression to match target(s)
|
19
|
+
# * :method - symbol, array of symbols, or regular expression to match method(s)
|
20
|
+
# * :chained - boolean, whether or not to match against a whole method chain (false by default)
|
21
|
+
# * :nested - boolean, whether or not to match against a method call that is a target itself (false by default)
|
22
|
+
def find_calls options
|
23
|
+
target = options[:target] || options[:targets]
|
24
|
+
method = options[:method] || options[:methods]
|
25
|
+
nested = options[:nested]
|
26
|
+
|
27
|
+
if options[:chained]
|
28
|
+
return find_chain options
|
29
|
+
#Find by narrowest category
|
30
|
+
elsif target and method and target.is_a? Array and method.is_a? Array
|
31
|
+
if target.length > method.length
|
32
|
+
calls = filter_by_target calls_by_methods(method), target
|
33
|
+
else
|
34
|
+
calls = calls_by_targets(target)
|
35
|
+
calls = filter_by_method calls, method
|
36
|
+
end
|
37
|
+
|
38
|
+
#Find by target, then by methods, if provided
|
39
|
+
elsif target
|
40
|
+
calls = calls_by_target target
|
41
|
+
|
42
|
+
if calls and method
|
43
|
+
calls = filter_by_method calls, method
|
44
|
+
end
|
45
|
+
|
46
|
+
#Find calls with no explicit target
|
47
|
+
#with either :target => nil or :target => false
|
48
|
+
elsif options.key? :target and not target and method
|
49
|
+
calls = calls_by_method method
|
50
|
+
calls = filter_by_target calls, nil
|
51
|
+
|
52
|
+
#Find calls by method
|
53
|
+
elsif method
|
54
|
+
calls = calls_by_method method
|
55
|
+
else
|
56
|
+
notify "Invalid arguments to CallCache#find_calls: #{options.inspect}"
|
57
|
+
end
|
58
|
+
|
59
|
+
return [] if calls.nil?
|
60
|
+
|
61
|
+
#Remove calls that are actually targets of other calls
|
62
|
+
#Unless those are explicitly desired
|
63
|
+
calls = filter_nested calls unless nested
|
64
|
+
|
65
|
+
calls
|
66
|
+
end
|
67
|
+
|
68
|
+
def remove_template_indexes template_name = nil
|
69
|
+
@calls_by_method.each do |name, calls|
|
70
|
+
calls.delete_if do |call|
|
71
|
+
from_template call, template_name
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
@calls_by_target.each do |name, calls|
|
76
|
+
calls.delete_if do |call|
|
77
|
+
from_template call, template_name
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def remove_indexes_by_class classes
|
83
|
+
@calls_by_method.each do |name, calls|
|
84
|
+
calls.delete_if do |call|
|
85
|
+
call[:location][:type] == :class and classes.include? call[:location][:class]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
@calls_by_target.each do |name, calls|
|
90
|
+
calls.delete_if do |call|
|
91
|
+
call[:location][:type] == :class and classes.include? call[:location][:class]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def index_calls calls
|
97
|
+
calls.each do |call|
|
98
|
+
@calls_by_method[call[:method]] ||= []
|
99
|
+
@calls_by_method[call[:method]] << call
|
100
|
+
|
101
|
+
unless call[:target].is_a? Sexp
|
102
|
+
@calls_by_target[call[:target]] ||= []
|
103
|
+
@calls_by_target[call[:target]] << call
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
def find_chain options
|
111
|
+
target = options[:target] || options[:targets]
|
112
|
+
method = options[:method] || options[:methods]
|
113
|
+
|
114
|
+
calls = calls_by_method method
|
115
|
+
|
116
|
+
return [] if calls.nil?
|
117
|
+
|
118
|
+
calls = filter_by_chain calls, target
|
119
|
+
end
|
120
|
+
|
121
|
+
def calls_by_target target
|
122
|
+
if target.is_a? Array
|
123
|
+
calls_by_targets target
|
124
|
+
else
|
125
|
+
@calls_by_target[target]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def calls_by_targets targets
|
130
|
+
calls = []
|
131
|
+
|
132
|
+
targets.each do |target|
|
133
|
+
calls.concat @calls_by_target[target] if @calls_by_target.key? target
|
134
|
+
end
|
135
|
+
|
136
|
+
calls
|
137
|
+
end
|
138
|
+
|
139
|
+
def calls_by_method method
|
140
|
+
if method.is_a? Array
|
141
|
+
calls_by_methods method
|
142
|
+
else
|
143
|
+
@calls_by_method[method.to_sym] || []
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def calls_by_methods methods
|
148
|
+
methods = methods.map { |m| m.to_sym }
|
149
|
+
calls = []
|
150
|
+
|
151
|
+
methods.each do |method|
|
152
|
+
calls.concat @calls_by_method[method] if @calls_by_method.key? method
|
153
|
+
end
|
154
|
+
|
155
|
+
calls
|
156
|
+
end
|
157
|
+
|
158
|
+
def calls_with_no_target
|
159
|
+
@calls_by_target[nil] || []
|
160
|
+
end
|
161
|
+
|
162
|
+
def filter calls, key, value
|
163
|
+
if value.is_a? Array
|
164
|
+
values = Set.new value
|
165
|
+
|
166
|
+
calls.select do |call|
|
167
|
+
values.include? call[key]
|
168
|
+
end
|
169
|
+
elsif value.is_a? Regexp
|
170
|
+
calls.select do |call|
|
171
|
+
call[key].to_s.match value
|
172
|
+
end
|
173
|
+
else
|
174
|
+
calls.select do |call|
|
175
|
+
call[key] == value
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def filter_by_method calls, method
|
181
|
+
filter calls, :method, method
|
182
|
+
end
|
183
|
+
|
184
|
+
def filter_by_target calls, target
|
185
|
+
filter calls, :target, target
|
186
|
+
end
|
187
|
+
|
188
|
+
def filter_nested calls
|
189
|
+
filter calls, :nested, false
|
190
|
+
end
|
191
|
+
|
192
|
+
def filter_by_chain calls, target
|
193
|
+
if target.is_a? Array
|
194
|
+
targets = Set.new target
|
195
|
+
|
196
|
+
calls.select do |call|
|
197
|
+
targets.include? call[:chain].first
|
198
|
+
end
|
199
|
+
elsif target.is_a? Regexp
|
200
|
+
calls.select do |call|
|
201
|
+
call[:chain].first.to_s.match target
|
202
|
+
end
|
203
|
+
else
|
204
|
+
calls.select do |call|
|
205
|
+
call[:chain].first == target
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def from_template call, template_name
|
211
|
+
return false unless call[:location][:type] == :template
|
212
|
+
return true if template_name.nil?
|
213
|
+
call[:location][:template] == template_name
|
214
|
+
end
|
215
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'brakeman/differ'
|
3
|
+
|
4
|
+
#Collects up results from running different checks.
|
5
|
+
#
|
6
|
+
#Checks can be added with +Check.add(check_class)+
|
7
|
+
#
|
8
|
+
#All .rb files in checks/ will be loaded.
|
9
|
+
class Brakeman::Checks
|
10
|
+
@checks = []
|
11
|
+
|
12
|
+
attr_reader :warnings, :controller_warnings, :model_warnings, :template_warnings, :checks_run
|
13
|
+
|
14
|
+
#Add a check. This will call +_klass_.new+ when running tests
|
15
|
+
def self.add klass
|
16
|
+
@checks << klass
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.checks
|
20
|
+
@checks
|
21
|
+
end
|
22
|
+
|
23
|
+
#No need to use this directly.
|
24
|
+
def initialize options = { }
|
25
|
+
if options[:min_confidence]
|
26
|
+
@min_confidence = options[:min_confidence]
|
27
|
+
else
|
28
|
+
@min_confidence = Brakeman.get_defaults[:min_confidence]
|
29
|
+
end
|
30
|
+
|
31
|
+
@warnings = []
|
32
|
+
@template_warnings = []
|
33
|
+
@model_warnings = []
|
34
|
+
@controller_warnings = []
|
35
|
+
@checks_run = []
|
36
|
+
end
|
37
|
+
|
38
|
+
#Add Warning to list of warnings to report.
|
39
|
+
#Warnings are split into four different arrays
|
40
|
+
#for template, controller, model, and generic warnings.
|
41
|
+
#
|
42
|
+
#Will not add warnings which are below the minimum confidence level.
|
43
|
+
def add_warning warning
|
44
|
+
unless warning.confidence > @min_confidence
|
45
|
+
case warning.warning_set
|
46
|
+
when :template
|
47
|
+
@template_warnings << warning
|
48
|
+
when :warning
|
49
|
+
@warnings << warning
|
50
|
+
when :controller
|
51
|
+
@controller_warnings << warning
|
52
|
+
when :model
|
53
|
+
@model_warnings << warning
|
54
|
+
else
|
55
|
+
raise "Unknown warning: #{warning.warning_set}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
#Return a hash of arrays of new and fixed warnings
|
61
|
+
#
|
62
|
+
# diff = checks.diff old_checks
|
63
|
+
# diff[:fixed] # [...]
|
64
|
+
# diff[:new] # [...]
|
65
|
+
def diff other_checks
|
66
|
+
my_warnings = self.all_warnings
|
67
|
+
other_warnings = other_checks.all_warnings
|
68
|
+
Brakeman::Differ.new(my_warnings, other_warnings).diff
|
69
|
+
end
|
70
|
+
|
71
|
+
#Return an array of all warnings found.
|
72
|
+
def all_warnings
|
73
|
+
@warnings + @template_warnings + @controller_warnings + @model_warnings
|
74
|
+
end
|
75
|
+
|
76
|
+
#Run all the checks on the given Tracker.
|
77
|
+
#Returns a new instance of Checks with the results.
|
78
|
+
def self.run_checks(app_tree, tracker)
|
79
|
+
if tracker.options[:parallel_checks]
|
80
|
+
self.run_checks_parallel(app_tree, tracker)
|
81
|
+
else
|
82
|
+
self.run_checks_sequential(app_tree, tracker)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
#Run checks sequentially
|
87
|
+
def self.run_checks_sequential(app_tree, tracker)
|
88
|
+
check_runner = self.new :min_confidence => tracker.options[:min_confidence]
|
89
|
+
|
90
|
+
@checks.each do |c|
|
91
|
+
check_name = get_check_name c
|
92
|
+
|
93
|
+
#Run or don't run check based on options
|
94
|
+
unless tracker.options[:skip_checks].include? check_name or
|
95
|
+
(tracker.options[:run_checks] and not tracker.options[:run_checks].include? check_name)
|
96
|
+
|
97
|
+
Brakeman.notify " - #{check_name}"
|
98
|
+
|
99
|
+
check = c.new(app_tree, tracker)
|
100
|
+
|
101
|
+
begin
|
102
|
+
check.run_check
|
103
|
+
rescue Exception => e
|
104
|
+
tracker.error e
|
105
|
+
end
|
106
|
+
|
107
|
+
check.warnings.each do |w|
|
108
|
+
check_runner.add_warning w
|
109
|
+
end
|
110
|
+
|
111
|
+
#Maintain list of which checks were run
|
112
|
+
#mainly for reporting purposes
|
113
|
+
check_runner.checks_run << check_name[5..-1]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
check_runner
|
118
|
+
end
|
119
|
+
|
120
|
+
#Run checks in parallel threads
|
121
|
+
def self.run_checks_parallel(app_tree, tracker)
|
122
|
+
threads = []
|
123
|
+
error_mutex = Mutex.new
|
124
|
+
|
125
|
+
check_runner = self.new :min_confidence => tracker.options[:min_confidence]
|
126
|
+
|
127
|
+
@checks.each do |c|
|
128
|
+
check_name = get_check_name c
|
129
|
+
|
130
|
+
#Run or don't run check based on options
|
131
|
+
unless tracker.options[:skip_checks].include? check_name or
|
132
|
+
(tracker.options[:run_checks] and not tracker.options[:run_checks].include? check_name)
|
133
|
+
|
134
|
+
Brakeman.notify " - #{check_name}"
|
135
|
+
|
136
|
+
threads << Thread.new do
|
137
|
+
check = c.new(app_tree, tracker)
|
138
|
+
|
139
|
+
begin
|
140
|
+
check.run_check
|
141
|
+
rescue Exception => e
|
142
|
+
error_mutex.synchronize do
|
143
|
+
tracker.error e
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
check.warnings
|
148
|
+
end
|
149
|
+
|
150
|
+
#Maintain list of which checks were run
|
151
|
+
#mainly for reporting purposes
|
152
|
+
check_runner.checks_run << check_name[5..-1]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
threads.each { |t| t.join }
|
157
|
+
|
158
|
+
Brakeman.notify "Checks finished, collecting results..."
|
159
|
+
|
160
|
+
#Collect results
|
161
|
+
threads.each do |thread|
|
162
|
+
thread.value.each do |warning|
|
163
|
+
check_runner.add_warning warning
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
check_runner
|
168
|
+
end
|
169
|
+
|
170
|
+
private
|
171
|
+
|
172
|
+
def self.get_check_name check_class
|
173
|
+
check_class.to_s.split("::").last
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
#Load all files in checks/ directory
|
178
|
+
Dir.glob("#{File.expand_path(File.dirname(__FILE__))}/checks/*.rb").sort.each do |f|
|
179
|
+
require f.match(/(brakeman\/checks\/.*)\.rb$/)[0]
|
180
|
+
end
|
@@ -0,0 +1,538 @@
|
|
1
|
+
require 'brakeman/processors/output_processor'
|
2
|
+
require 'brakeman/processors/lib/processor_helper'
|
3
|
+
require 'brakeman/warning'
|
4
|
+
require 'brakeman/util'
|
5
|
+
|
6
|
+
#Basis of vulnerability checks.
|
7
|
+
class Brakeman::BaseCheck < Brakeman::SexpProcessor
|
8
|
+
include Brakeman::ProcessorHelper
|
9
|
+
include Brakeman::Util
|
10
|
+
attr_reader :tracker, :warnings
|
11
|
+
|
12
|
+
CONFIDENCE = { :high => 0, :med => 1, :low => 2 }
|
13
|
+
|
14
|
+
Match = Struct.new(:type, :match)
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_accessor :name
|
18
|
+
|
19
|
+
def inherited(subclass)
|
20
|
+
subclass.name = subclass.to_s.match(/^Brakeman::(.*)$/)[1]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
#Initialize Check with Checks.
|
25
|
+
def initialize(app_tree, tracker)
|
26
|
+
super()
|
27
|
+
@app_tree = app_tree
|
28
|
+
@results = [] #only to check for duplicates
|
29
|
+
@warnings = []
|
30
|
+
@tracker = tracker
|
31
|
+
@string_interp = false
|
32
|
+
@current_set = nil
|
33
|
+
@current_template = @current_module = @current_class = @current_method = nil
|
34
|
+
@active_record_models = nil
|
35
|
+
@mass_assign_disabled = nil
|
36
|
+
@has_user_input = nil
|
37
|
+
@safe_input_attributes = Set[:to_i, :to_f, :arel_table, :id]
|
38
|
+
end
|
39
|
+
|
40
|
+
#Add result to result list, which is used to check for duplicates
|
41
|
+
def add_result result, location = nil
|
42
|
+
location ||= (@current_template && @current_template[:name]) || @current_class || @current_module || @current_set || result[:location][:class] || result[:location][:template]
|
43
|
+
location = location[:name] if location.is_a? Hash
|
44
|
+
location = location.to_sym
|
45
|
+
|
46
|
+
if result.is_a? Hash
|
47
|
+
line = result[:call].original_line || result[:call].line
|
48
|
+
elsif sexp? result
|
49
|
+
line = result.original_line || result.line
|
50
|
+
else
|
51
|
+
raise ArgumentError
|
52
|
+
end
|
53
|
+
|
54
|
+
@results << [line, location, result]
|
55
|
+
end
|
56
|
+
|
57
|
+
#Default Sexp processing. Iterates over each value in the Sexp
|
58
|
+
#and processes them if they are also Sexps.
|
59
|
+
def process_default exp
|
60
|
+
exp.each_with_index do |e, i|
|
61
|
+
if sexp? e
|
62
|
+
process e
|
63
|
+
else
|
64
|
+
e
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
exp
|
69
|
+
end
|
70
|
+
|
71
|
+
#Process calls and check if they include user input
|
72
|
+
def process_call exp
|
73
|
+
process exp.target if sexp? exp.target
|
74
|
+
process_call_args exp
|
75
|
+
|
76
|
+
target = exp.target
|
77
|
+
|
78
|
+
unless @safe_input_attributes.include? exp.method
|
79
|
+
if params? target
|
80
|
+
@has_user_input = Match.new(:params, exp)
|
81
|
+
elsif cookies? target
|
82
|
+
@has_user_input = Match.new(:cookies, exp)
|
83
|
+
elsif request_env? target
|
84
|
+
@has_user_input = Match.new(:request, exp)
|
85
|
+
elsif sexp? target and model_name? target[1] #TODO: Can this be target.target?
|
86
|
+
@has_user_input = Match.new(:model, exp)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
exp
|
91
|
+
end
|
92
|
+
|
93
|
+
def process_if exp
|
94
|
+
#This is to ignore user input in condition
|
95
|
+
current_user_input = @has_user_input
|
96
|
+
process exp.condition
|
97
|
+
@has_user_input = current_user_input
|
98
|
+
|
99
|
+
process exp.then_clause if sexp? exp.then_clause
|
100
|
+
process exp.else_clause if sexp? exp.else_clause
|
101
|
+
|
102
|
+
exp
|
103
|
+
end
|
104
|
+
|
105
|
+
#Note that params are included in current expression
|
106
|
+
def process_params exp
|
107
|
+
@has_user_input = Match.new(:params, exp)
|
108
|
+
exp
|
109
|
+
end
|
110
|
+
|
111
|
+
#Note that cookies are included in current expression
|
112
|
+
def process_cookies exp
|
113
|
+
@has_user_input = Match.new(:cookies, exp)
|
114
|
+
exp
|
115
|
+
end
|
116
|
+
|
117
|
+
#Does not actually process string interpolation, but notes that it occurred.
|
118
|
+
def process_string_interp exp
|
119
|
+
@string_interp = Match.new(:interp, exp)
|
120
|
+
process_default exp
|
121
|
+
end
|
122
|
+
|
123
|
+
private
|
124
|
+
|
125
|
+
#Report a warning
|
126
|
+
def warn options
|
127
|
+
extra_opts = { :check => self.class.to_s }
|
128
|
+
|
129
|
+
warning = Brakeman::Warning.new(options.merge(extra_opts))
|
130
|
+
warning.file = file_for warning
|
131
|
+
warning.relative_path = relative_path(warning.file)
|
132
|
+
|
133
|
+
@warnings << warning
|
134
|
+
end
|
135
|
+
|
136
|
+
#Run _exp_ through OutputProcessor to get a nice String.
|
137
|
+
def format_output exp
|
138
|
+
Brakeman::OutputProcessor.new.format(exp).gsub(/\r|\n/, "")
|
139
|
+
end
|
140
|
+
|
141
|
+
#Checks if the model inherits from parent,
|
142
|
+
def ancestor? model, parent
|
143
|
+
if model == nil
|
144
|
+
false
|
145
|
+
elsif model[:parent] == parent
|
146
|
+
true
|
147
|
+
elsif model[:parent]
|
148
|
+
ancestor? tracker.models[model[:parent]], parent
|
149
|
+
else
|
150
|
+
false
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def unprotected_model? model
|
155
|
+
model[:attr_accessible].nil? and !parent_classes_protected?(model) and ancestor?(model, :"ActiveRecord::Base")
|
156
|
+
end
|
157
|
+
|
158
|
+
# go up the chain of parent classes to see if any have attr_accessible
|
159
|
+
def parent_classes_protected? model
|
160
|
+
if model[:attr_accessible] or model[:includes].include? :"ActiveModel::ForbiddenAttributesProtection"
|
161
|
+
true
|
162
|
+
elsif parent = tracker.models[model[:parent]]
|
163
|
+
parent_classes_protected? parent
|
164
|
+
else
|
165
|
+
false
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
#Checks if mass assignment is disabled globally in an initializer.
|
170
|
+
def mass_assign_disabled?
|
171
|
+
return @mass_assign_disabled unless @mass_assign_disabled.nil?
|
172
|
+
|
173
|
+
@mass_assign_disabled = false
|
174
|
+
|
175
|
+
if version_between?("3.1.0", "3.9.9") and
|
176
|
+
tracker.config[:rails][:active_record] and
|
177
|
+
tracker.config[:rails][:active_record][:whitelist_attributes] == Sexp.new(:true)
|
178
|
+
|
179
|
+
@mass_assign_disabled = true
|
180
|
+
elsif version_between?("4.0.0", "4.9.9")
|
181
|
+
#May need to revisit dependng on what Rails 4 actually does/has
|
182
|
+
@mass_assign_disabled = true
|
183
|
+
else
|
184
|
+
matches = tracker.check_initializers(:"ActiveRecord::Base", :send)
|
185
|
+
|
186
|
+
if matches.empty?
|
187
|
+
#Check for
|
188
|
+
# class ActiveRecord::Base
|
189
|
+
# attr_accessible nil
|
190
|
+
# end
|
191
|
+
matches = tracker.check_initializers([], :attr_accessible)
|
192
|
+
|
193
|
+
matches.each do |result|
|
194
|
+
if result.module == "ActiveRecord" and result.result_class == :Base
|
195
|
+
arg = result.call.first_arg
|
196
|
+
|
197
|
+
if arg.nil? or node_type? arg, :nil
|
198
|
+
@mass_assign_disabled = true
|
199
|
+
break
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
else
|
204
|
+
#Check for ActiveRecord::Base.send(:attr_accessible, nil)
|
205
|
+
matches.each do |result|
|
206
|
+
call = result.call
|
207
|
+
if call? call
|
208
|
+
if call.first_arg == Sexp.new(:lit, :attr_accessible) and call.second_arg == Sexp.new(:nil)
|
209
|
+
@mass_assign_disabled = true
|
210
|
+
break
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
#There is a chance someone is using Rails 3.x and the `strong_parameters`
|
218
|
+
#gem and still using hack above, so this is a separate check for
|
219
|
+
#including ActiveModel::ForbiddenAttributesProtection in
|
220
|
+
#ActiveRecord::Base in an initializer.
|
221
|
+
if not @mass_assign_disabled and version_between?("3.1.0", "3.9.9") and tracker.config[:gems][:strong_parameters]
|
222
|
+
matches = tracker.check_initializers([], :include)
|
223
|
+
forbidden_protection = Sexp.new(:colon2, Sexp.new(:const, :ActiveModel), :ForbiddenAttributesProtection)
|
224
|
+
|
225
|
+
matches.each do |result|
|
226
|
+
if call? result.call and result.call.first_arg == forbidden_protection
|
227
|
+
@mass_assign_disabled = true
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
unless @mass_assign_disabled
|
232
|
+
matches = tracker.check_initializers(:"ActiveRecord::Base", :send)
|
233
|
+
|
234
|
+
matches.each do |result|
|
235
|
+
if call? result.call and result.call.second_arg == forbidden_protection
|
236
|
+
@mass_assign_disabled = true
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
@mass_assign_disabled
|
243
|
+
end
|
244
|
+
|
245
|
+
#This is to avoid reporting duplicates. Checks if the result has been
|
246
|
+
#reported already from the same line number.
|
247
|
+
def duplicate? result, location = nil
|
248
|
+
if result.is_a? Hash
|
249
|
+
line = result[:call].original_line || result[:call].line
|
250
|
+
elsif sexp? result
|
251
|
+
line = result.original_line || result.line
|
252
|
+
else
|
253
|
+
raise ArgumentError
|
254
|
+
end
|
255
|
+
|
256
|
+
location ||= (@current_template && @current_template[:name]) || @current_class || @current_module || @current_set || result[:location][:class] || result[:location][:template]
|
257
|
+
|
258
|
+
location = location[:name] if location.is_a? Hash
|
259
|
+
location = location.to_sym
|
260
|
+
|
261
|
+
@results.each do |r|
|
262
|
+
if r[0] == line and r[1] == location
|
263
|
+
if tracker.options[:combine_locations]
|
264
|
+
return true
|
265
|
+
elsif r[2] == result
|
266
|
+
return true
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
false
|
272
|
+
end
|
273
|
+
|
274
|
+
#Checks if an expression contains string interpolation.
|
275
|
+
#
|
276
|
+
#Returns Match with :interp type if found.
|
277
|
+
def include_interp? exp
|
278
|
+
@string_interp = false
|
279
|
+
process exp
|
280
|
+
@string_interp
|
281
|
+
end
|
282
|
+
|
283
|
+
#Checks if _exp_ includes user input in the form of cookies, parameters,
|
284
|
+
#request environment, or model attributes.
|
285
|
+
#
|
286
|
+
#If found, returns a struct containing a type (:cookies, :params, :request, :model) and
|
287
|
+
#the matching expression (Match#type and Match#match).
|
288
|
+
#
|
289
|
+
#Returns false otherwise.
|
290
|
+
def include_user_input? exp
|
291
|
+
@has_user_input = false
|
292
|
+
process exp
|
293
|
+
@has_user_input
|
294
|
+
end
|
295
|
+
|
296
|
+
#This is used to check for user input being used directly.
|
297
|
+
#
|
298
|
+
##If found, returns a struct containing a type (:cookies, :params, :request) and
|
299
|
+
#the matching expression (Match#type and Match#match).
|
300
|
+
#
|
301
|
+
#Returns false otherwise.
|
302
|
+
def has_immediate_user_input? exp
|
303
|
+
if exp.nil?
|
304
|
+
false
|
305
|
+
elsif call? exp and not @safe_input_attributes.include? exp.method
|
306
|
+
if params? exp
|
307
|
+
return Match.new(:params, exp)
|
308
|
+
elsif cookies? exp
|
309
|
+
return Match.new(:cookies, exp)
|
310
|
+
elsif request_env? exp
|
311
|
+
return Match.new(:request, exp)
|
312
|
+
else
|
313
|
+
has_immediate_user_input? exp.target
|
314
|
+
end
|
315
|
+
elsif sexp? exp
|
316
|
+
case exp.node_type
|
317
|
+
when :string_interp
|
318
|
+
exp.each do |e|
|
319
|
+
if sexp? e
|
320
|
+
match = has_immediate_user_input?(e)
|
321
|
+
return match if match
|
322
|
+
end
|
323
|
+
end
|
324
|
+
false
|
325
|
+
when :string_eval
|
326
|
+
if sexp? exp.value
|
327
|
+
if exp.value.node_type == :rlist
|
328
|
+
exp.value.each_sexp do |e|
|
329
|
+
match = has_immediate_user_input?(e)
|
330
|
+
return match if match
|
331
|
+
end
|
332
|
+
false
|
333
|
+
else
|
334
|
+
has_immediate_user_input? exp.value
|
335
|
+
end
|
336
|
+
end
|
337
|
+
when :format
|
338
|
+
has_immediate_user_input? exp.value
|
339
|
+
when :if
|
340
|
+
(sexp? exp.then_clause and has_immediate_user_input? exp.then_clause) or
|
341
|
+
(sexp? exp.else_clause and has_immediate_user_input? exp.else_clause)
|
342
|
+
when :or
|
343
|
+
has_immediate_user_input? exp.lhs or
|
344
|
+
has_immediate_user_input? exp.rhs
|
345
|
+
else
|
346
|
+
false
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
#Checks for a model attribute at the top level of the
|
352
|
+
#expression.
|
353
|
+
def has_immediate_model? exp, out = nil
|
354
|
+
out = exp if out.nil?
|
355
|
+
|
356
|
+
if sexp? exp and exp.node_type == :output
|
357
|
+
exp = exp.value
|
358
|
+
end
|
359
|
+
|
360
|
+
if call? exp
|
361
|
+
target = exp.target
|
362
|
+
method = exp.method
|
363
|
+
|
364
|
+
if @safe_input_attributes.include? method
|
365
|
+
false
|
366
|
+
elsif call? target and not method.to_s[-1,1] == "?"
|
367
|
+
has_immediate_model? target, out
|
368
|
+
elsif model_name? target
|
369
|
+
exp
|
370
|
+
else
|
371
|
+
false
|
372
|
+
end
|
373
|
+
elsif sexp? exp
|
374
|
+
case exp.node_type
|
375
|
+
when :string_interp
|
376
|
+
exp.each do |e|
|
377
|
+
if sexp? e and match = has_immediate_model?(e, out)
|
378
|
+
return match
|
379
|
+
end
|
380
|
+
end
|
381
|
+
false
|
382
|
+
when :string_eval
|
383
|
+
if sexp? exp.value
|
384
|
+
if exp.value.node_type == :rlist
|
385
|
+
exp.value.each_sexp do |e|
|
386
|
+
if match = has_immediate_model?(e, out)
|
387
|
+
return match
|
388
|
+
end
|
389
|
+
end
|
390
|
+
false
|
391
|
+
else
|
392
|
+
has_immediate_model? exp.value, out
|
393
|
+
end
|
394
|
+
end
|
395
|
+
when :format
|
396
|
+
has_immediate_model? exp.value, out
|
397
|
+
when :if
|
398
|
+
((sexp? exp.then_clause and has_immediate_model? exp.then_clause, out) or
|
399
|
+
(sexp? exp.else_clause and has_immediate_model? exp.else_clause, out))
|
400
|
+
when :or
|
401
|
+
has_immediate_model? exp.lhs or
|
402
|
+
has_immediate_model? exp.rhs
|
403
|
+
else
|
404
|
+
false
|
405
|
+
end
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
409
|
+
#Checks if +exp+ is a model name.
|
410
|
+
#
|
411
|
+
#Prior to using this method, either @tracker must be set to
|
412
|
+
#the current tracker, or else @models should contain an array of the model
|
413
|
+
#names, which is available via tracker.models.keys
|
414
|
+
def model_name? exp
|
415
|
+
@models ||= @tracker.models.keys
|
416
|
+
|
417
|
+
if exp.is_a? Symbol
|
418
|
+
@models.include? exp
|
419
|
+
elsif sexp? exp
|
420
|
+
klass = nil
|
421
|
+
begin
|
422
|
+
klass = class_name exp
|
423
|
+
rescue StandardError
|
424
|
+
end
|
425
|
+
|
426
|
+
klass and @models.include? klass
|
427
|
+
else
|
428
|
+
false
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
#Finds entire method call chain where +target+ is a target in the chain
|
433
|
+
def find_chain exp, target
|
434
|
+
return unless sexp? exp
|
435
|
+
|
436
|
+
case exp.node_type
|
437
|
+
when :output, :format
|
438
|
+
find_chain exp.value, target
|
439
|
+
when :call
|
440
|
+
if exp == target or include_target? exp, target
|
441
|
+
return exp
|
442
|
+
end
|
443
|
+
else
|
444
|
+
exp.each do |e|
|
445
|
+
if sexp? e
|
446
|
+
res = find_chain e, target
|
447
|
+
return res if res
|
448
|
+
end
|
449
|
+
end
|
450
|
+
nil
|
451
|
+
end
|
452
|
+
end
|
453
|
+
|
454
|
+
#Returns true if +target+ is in +exp+
|
455
|
+
def include_target? exp, target
|
456
|
+
return false unless call? exp
|
457
|
+
|
458
|
+
exp.each do |e|
|
459
|
+
return true if e == target or include_target? e, target
|
460
|
+
end
|
461
|
+
|
462
|
+
false
|
463
|
+
end
|
464
|
+
|
465
|
+
#Returns true if low_version <= RAILS_VERSION <= high_version
|
466
|
+
#
|
467
|
+
#If the Rails version is unknown, returns false.
|
468
|
+
def version_between? low_version, high_version
|
469
|
+
return false unless tracker.config[:rails_version]
|
470
|
+
|
471
|
+
version = tracker.config[:rails_version].split(".").map! { |n| n.to_i }
|
472
|
+
low_version = low_version.split(".").map! { |n| n.to_i }
|
473
|
+
high_version = high_version.split(".").map! { |n| n.to_i }
|
474
|
+
|
475
|
+
version.each_with_index do |v, i|
|
476
|
+
if v < low_version.fetch(i, 0)
|
477
|
+
return false
|
478
|
+
elsif v > low_version.fetch(i, 0)
|
479
|
+
break
|
480
|
+
end
|
481
|
+
end
|
482
|
+
|
483
|
+
version.each_with_index do |v, i|
|
484
|
+
if v > high_version.fetch(i, 0)
|
485
|
+
return false
|
486
|
+
elsif v < high_version.fetch(i, 0)
|
487
|
+
break
|
488
|
+
end
|
489
|
+
end
|
490
|
+
|
491
|
+
true
|
492
|
+
end
|
493
|
+
|
494
|
+
def gemfile_or_environment
|
495
|
+
if @app_tree.exists?("Gemfile")
|
496
|
+
"Gemfile"
|
497
|
+
else
|
498
|
+
"config/environment.rb"
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
def self.description
|
503
|
+
@description
|
504
|
+
end
|
505
|
+
|
506
|
+
def active_record_models
|
507
|
+
return @active_record_models if @active_record_models
|
508
|
+
|
509
|
+
@active_record_models = {}
|
510
|
+
|
511
|
+
tracker.models.each do |name, model|
|
512
|
+
if ancestor? model, :"ActiveRecord::Base"
|
513
|
+
@active_record_models[name] = model
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
@active_record_models
|
518
|
+
end
|
519
|
+
|
520
|
+
def friendly_type_of input_type
|
521
|
+
if input_type.is_a? Match
|
522
|
+
input_type = input_type.type
|
523
|
+
end
|
524
|
+
|
525
|
+
case input_type
|
526
|
+
when :params
|
527
|
+
"parameter value"
|
528
|
+
when :cookies
|
529
|
+
"cookie value"
|
530
|
+
when :request
|
531
|
+
"request value"
|
532
|
+
when :model
|
533
|
+
"model attribute"
|
534
|
+
else
|
535
|
+
"user input"
|
536
|
+
end
|
537
|
+
end
|
538
|
+
end
|