Class: Brakeman::Scanner
- Inherits:
-
Object
- Object
- Brakeman::Scanner
- Defined in:
- lib/brakeman/scanner.rb
Overview
Scans the Rails application.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #detect_file_types(astfiles) ⇒ Object
- #file_cache ⇒ Object
-
#guess_rails_version ⇒ Object
Set :rails3/:rails4 option if version was not determined from Gemfile.
- #index_call_sites ⇒ Object
-
#initialize(options, processor = nil) ⇒ Scanner
constructor
Pass in path to the root of the Rails application.
- #parse_files(ruby_paths:, template_paths:) ⇒ Object
- #parse_ruby_file(file) ⇒ Object
-
#process(ruby_paths: nil, template_paths: nil) ⇒ Object
Process everything in the Rails application.
-
#process_config ⇒ Object
Process config/environment.rb and config/gems.rb.
- #process_controller(astfile) ⇒ Object
- #process_controller_data_flows ⇒ Object
-
#process_controllers ⇒ Object
Process all .rb files in controllers/.
-
#process_gems ⇒ Object
Process Gemfile.
-
#process_initializer(init) ⇒ Object
Process an initializer.
-
#process_initializers ⇒ Object
Process all the .rb files in config/initializers/.
-
#process_lib(lib) ⇒ Object
Process a library.
-
#process_libs ⇒ Object
Adds parsed information to tracker.libs.
- #process_model(astfile) ⇒ Object
-
#process_models ⇒ Object
Process all the .rb files in models/.
-
#process_routes ⇒ Object
Process config/routes.rb.
- #process_step(description) ⇒ Object
- #process_step_file(description) ⇒ Object
- #process_template(template) ⇒ Object
- #process_template_data_flows ⇒ Object
-
#process_templates ⇒ Object
Process all views and partials in views/.
- #report_progress(current, total) ⇒ Object
- #support_rescanning? ⇒ Boolean
- #track_progress(list, type = "files") ⇒ Object
-
#tracker ⇒ Object
Returns the Tracker generated from the scan.
Constructor Details
#initialize(options, processor = nil) ⇒ Scanner
Pass in path to the root of the Rails application
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/brakeman/scanner.rb', line 22 def initialize , processor = nil = @app_tree = Brakeman::AppTree.() if (!@app_tree.root || !@app_tree.exists?("app")) && ![:force_scan] = "Please supply the path to a Rails application (looking in #{@app_tree.root}).\n" << " Use `--force` to run a scan anyway." raise Brakeman::NoApplication, end @processor = processor || Brakeman::Processor.new(@app_tree, ) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/brakeman/scanner.rb', line 19 def end |
Instance Method Details
#detect_file_types(astfiles) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/brakeman/scanner.rb', line 145 def detect_file_types(astfiles) detector = Brakeman::FileTypeDetector.new astfiles.each do |file| Brakeman.logger.spin if file.is_a? Brakeman::TemplateParser::TemplateFile file_cache.add_file file, :template else type = detector.detect_type(file) unless type == :skip if file_cache.valid_type? type file_cache.add_file(file, type) else raise "Unexpected file type: #{type.inspect}" end end end end end |
#file_cache ⇒ Object
41 42 43 |
# File 'lib/brakeman/scanner.rb', line 41 def file_cache tracker.file_cache end |
#guess_rails_version ⇒ Object
Set :rails3/:rails4 option if version was not determined from Gemfile
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/brakeman/scanner.rb', line 259 def guess_rails_version unless tracker.[:rails3] or tracker.[:rails4] if @app_tree.exists?("script/rails") tracker.[:rails3] = true Brakeman.debug 'Detected Rails 3 application' elsif @app_tree.exists?("app/channels") tracker.[:rails3] = true tracker.[:rails4] = true tracker.[:rails5] = true Brakeman.debug 'Detected Rails 5 application' elsif not @app_tree.exists?("script") tracker.[:rails3] = true tracker.[:rails4] = true Brakeman.debug 'Detected Rails 4 application' end end end |
#index_call_sites ⇒ Object
423 424 425 |
# File 'lib/brakeman/scanner.rb', line 423 def index_call_sites tracker.index_call_sites end |
#parse_files(ruby_paths:, template_paths:) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/brakeman/scanner.rb', line 127 def parse_files(ruby_paths:, template_paths:) fp = Brakeman::FileParser.new(tracker.app_tree, tracker.[:parser_timeout], tracker.[:parallel_checks], tracker.[:use_prism]) fp.parse_files ruby_paths template_parser = Brakeman::TemplateParser.new(tracker, fp) fp.read_files(template_paths) do |path, contents| Brakeman.logger.spin template_parser.parse_template(path, contents) end # Collect errors raised during parsing tracker.add_errors(fp.errors) fp.file_list end |
#parse_ruby_file(file) ⇒ Object
427 428 429 430 431 432 433 |
# File 'lib/brakeman/scanner.rb', line 427 def parse_ruby_file file fp = Brakeman::FileParser.new(tracker.app_tree, tracker.[:parser_timeout], false, tracker.[:use_prism]) fp.parse_ruby(file.read, file) rescue Exception => e tracker.error(e) nil end |
#process(ruby_paths: nil, template_paths: nil) ⇒ Object
Process everything in the Rails application
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/brakeman/scanner.rb', line 54 def process(ruby_paths: nil, template_paths: nil) process_step 'Processing gems' do process_gems end process_step 'Processing configuration' do guess_rails_version process_config end # - # If ruby_paths or template_paths are set, # only parse those files. The rest will be fetched # from the file cache. # # Otherwise, parse everything normally. # astfiles = nil process_step 'Finding files' do ruby_paths ||= tracker.app_tree.ruby_file_paths template_paths ||= tracker.app_tree.template_paths end process_step 'Parsing files' do astfiles = parse_files(ruby_paths: ruby_paths, template_paths: template_paths) end process_step 'Detecting file types' do detect_file_types(astfiles) end tracker.save_file_cache! if support_rescanning? # - process_step 'Processing initializers' do process_initializers end process_step 'Processing libraries' do process_libs end process_step 'Processing routes' do process_routes end process_step 'Processing templates' do process_templates end process_step 'Processing data flow' do process_template_data_flows end process_step 'Processing models' do process_models end process_step 'Processing controllers' do process_controllers end process_step 'Processing data flow' do process_controller_data_flows end process_step 'Indexing method calls' do index_call_sites end tracker end |
#process_config ⇒ Object
Process config/environment.rb and config/gems.rb
Stores parsed information in tracker.config
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/brakeman/scanner.rb', line 170 def process_config # Sometimes folks like to put constants in environment.rb # so let's always process it even for newer Rails versions process_config_file "environment.rb" if [:rails3] or [:rails4] or [:rails5] or [:rails6] process_config_file "application.rb" process_config_file "environments/production.rb" else process_config_file "gems.rb" end if @app_tree.exists?("vendor/plugins/rails_xss") or [:rails3] or [:escape_html] tracker.config.escape_html = true Brakeman.debug 'Escaping HTML by default' end if @app_tree.exists? ".ruby-version" contents = @app_tree.file_path(".ruby-version").read # Skip alternative Ruby implementations — the EOL dates Brakeman knows # about are MRI's, so a `.ruby-version` of "jruby-10.0.2.0" should not # be parsed as MRI 0.0.2 / 10.0.2. unless contents =~ /\A\s*(jruby|truffleruby|rbx|rubinius|mruby)\b/i if version = contents[/(\d+\.\d+\.\d+)/] tracker.config.set_ruby_version version, @app_tree.file_path(".ruby-version"), 1 end end end tracker.config.load_rails_defaults end |
#process_controller(astfile) ⇒ Object
356 357 358 359 360 361 362 |
# File 'lib/brakeman/scanner.rb', line 356 def process_controller astfile begin @processor.process_controller(astfile.ast, astfile.path) rescue => e tracker.error e.exception(e. + "\nWhile processing #{astfile.path}"), e.backtrace end end |
#process_controller_data_flows ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/brakeman/scanner.rb', line 341 def process_controller_data_flows controllers = tracker.controllers.sort_by { |name, _| name } track_progress controllers, "controllers" do |name, controller| process_step_file name do controller.src.each do |file, src| @processor.process_controller_alias name, src, nil, file end end end #No longer need these processed filter methods tracker.filter_cache.clear end |
#process_controllers ⇒ Object
Process all .rb files in controllers/
Adds processed controllers to tracker.controllers
331 332 333 334 335 336 337 338 339 |
# File 'lib/brakeman/scanner.rb', line 331 def process_controllers controllers = file_cache.controllers.sort_by { |path, _| path } track_progress controllers do |path, controller| process_step_file path do process_controller controller end end end |
#process_gems ⇒ Object
Process Gemfile
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/brakeman/scanner.rb', line 219 def process_gems gem_files = {} gem_file_names = ['Gemfile', 'gems.rb'] lock_file_names = ['Gemfile.lock', 'gems.locked'] if tracker.[:gemfile] name = tracker.[:gemfile] gem_file_names.unshift name lock_file_names.unshift "#{name}.lock" end gem_file_names.each do |name| if @app_tree.exists? name file = @app_tree.file_path(name) gem_files[:gemfile] = { :src => parse_ruby_file(file), :file => file } break end end lock_file_names.each do |name| if @app_tree.exists? name file = @app_tree.file_path(name) gem_files[:gemlock] = { :src => file.read, :file => file } break end end if @app_tree.gemspec gem_files[:gemspec] = { :src => parse_ruby_file(@app_tree.gemspec), :file => @app_tree.gemspec } end if not gem_files.empty? @processor.process_gems gem_files end rescue => e Brakeman.alert 'Error while processing Gemfile' tracker.error e.exception(e. + "\nWhile processing Gemfile"), e.backtrace end |
#process_initializer(init) ⇒ Object
Process an initializer
289 290 291 |
# File 'lib/brakeman/scanner.rb', line 289 def process_initializer init @processor.process_initializer(init.path, init.ast) end |
#process_initializers ⇒ Object
Process all the .rb files in config/initializers/
Adds parsed information to tracker.initializers
280 281 282 283 284 285 286 |
# File 'lib/brakeman/scanner.rb', line 280 def process_initializers track_progress file_cache.initializers do |path, init| process_step_file path do process_initializer init end end end |
#process_lib(lib) ⇒ Object
Process a library
307 308 309 |
# File 'lib/brakeman/scanner.rb', line 307 def process_lib lib @processor.process_lib lib.ast, lib.path end |
#process_libs ⇒ Object
Adds parsed information to tracker.libs. This is a catch-all for any Ruby files that weren't determined to be a specific type of file (like a controller).
296 297 298 299 300 301 302 303 304 |
# File 'lib/brakeman/scanner.rb', line 296 def process_libs libs = file_cache.libs.sort_by { |path, _| path } track_progress libs do |path, lib| process_step_file path do process_lib lib end end end |
#process_model(astfile) ⇒ Object
404 405 406 |
# File 'lib/brakeman/scanner.rb', line 404 def process_model astfile @processor.process_model(astfile.ast, astfile.path) end |
#process_models ⇒ Object
Process all the .rb files in models/
Adds the processed models to tracker.models
394 395 396 397 398 399 400 401 402 |
# File 'lib/brakeman/scanner.rb', line 394 def process_models models = file_cache.models.sort_by { |path, _| path } track_progress models do |path, model| process_step_file path do process_model model end end end |
#process_routes ⇒ Object
Process config/routes.rb
Adds parsed information to tracker.routes
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/brakeman/scanner.rb', line 314 def process_routes if @app_tree.exists?("config/routes.rb") file = @app_tree.file_path("config/routes.rb") if routes_sexp = parse_ruby_file(file) @processor.process_routes routes_sexp else Brakeman.alert 'Error while processing routes - assuming all public controller methods are actions.' [:assume_all_routes] = true end else Brakeman.alert 'No route information found' end end |
#process_step(description) ⇒ Object
45 46 47 |
# File 'lib/brakeman/scanner.rb', line 45 def process_step(description, &) Brakeman.process_step(description, &) end |
#process_step_file(description) ⇒ Object
49 50 51 |
# File 'lib/brakeman/scanner.rb', line 49 def process_step_file(description, &) Brakeman.logger.single_context(description, &) end |
#process_template(template) ⇒ Object
377 378 379 |
# File 'lib/brakeman/scanner.rb', line 377 def process_template template @processor.process_template(template.name, template.ast, template.type, nil, template.path) end |
#process_template_data_flows ⇒ Object
381 382 383 384 385 386 387 388 389 |
# File 'lib/brakeman/scanner.rb', line 381 def process_template_data_flows templates = tracker.templates.sort_by { |name, _| name } track_progress templates, "templates" do |name, template| process_step_file name do @processor.process_template_alias template end end end |
#process_templates ⇒ Object
Process all views and partials in views/
Adds processed views to tracker.views
367 368 369 370 371 372 373 374 375 |
# File 'lib/brakeman/scanner.rb', line 367 def process_templates templates = file_cache.templates.sort_by { |path, _| path } track_progress templates, "templates" do |path, template| process_step_file path do process_template template end end end |
#report_progress(current, total) ⇒ Object
418 419 420 421 |
# File 'lib/brakeman/scanner.rb', line 418 def report_progress(current, total) return unless [:report_progress] Brakeman.logger.update_progress(current, total) end |
#support_rescanning? ⇒ Boolean
435 436 437 |
# File 'lib/brakeman/scanner.rb', line 435 def support_rescanning? tracker.[:support_rescanning] end |
#track_progress(list, type = "files") ⇒ Object
408 409 410 411 412 413 414 415 416 |
# File 'lib/brakeman/scanner.rb', line 408 def track_progress list, type = "files" total = list.length current = 0 list.each do |item| report_progress current, total current += 1 yield item end end |
#tracker ⇒ Object
Returns the Tracker generated from the scan
37 38 39 |
# File 'lib/brakeman/scanner.rb', line 37 def tracker @processor.tracked_events end |