Class: Brakeman::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/brakeman/scanner.rb

Overview

Scans the Rails application.

Direct Known Subclasses

Rescanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, processor = nil) ⇒ Scanner

Pass in path to the root of the Rails application



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/brakeman/scanner.rb', line 21

def initialize options, processor = nil
  @options = options
  @app_tree = Brakeman::AppTree.from_options(options)

  if (!@app_tree.root || !@app_tree.exists?("app")) && !options[:force_scan]
    message = "Please supply the path to a Rails application (looking in #{@app_tree.root}).\n" <<
              "  Use `--force` to run a scan anyway."

    raise Brakeman::NoApplication, message
  end

  @processor = processor || Brakeman::Processor.new(@app_tree, options)
  @show_timing = tracker.options[:debug] || tracker.options[:show_timing]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/brakeman/scanner.rb', line 18

def options
  @options
end

Instance Method Details

#detect_file_typesObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/brakeman/scanner.rb', line 144

def detect_file_types
  @file_list = {
    controllers: [],
    initializers: [],
    libs: [],
    models: [],
    templates: [],
  }

  detector = Brakeman::FileTypeDetector.new

  @parsed_files.each do |file|
    if file.is_a? Brakeman::TemplateParser::TemplateFile
      @file_list[:templates] << file
    else
      type = detector.detect_type(file)
      unless type == :skip
        if @file_list[type].nil?
          raise type.to_s
        else
          @file_list[type] << file
        end
      end
    end
  end
end

#guess_rails_versionObject

Set :rails3/:rails4 option if version was not determined from Gemfile



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/brakeman/scanner.rb', line 249

def guess_rails_version
  unless tracker.options[:rails3] or tracker.options[:rails4]
    if @app_tree.exists?("script/rails")
      tracker.options[:rails3] = true
      Brakeman.notify "[Notice] Detected Rails 3 application"
    elsif @app_tree.exists?("app/channels")
      tracker.options[:rails3] = true
      tracker.options[:rails4] = true
      tracker.options[:rails5] = true
      Brakeman.notify "[Notice] Detected Rails 5 application"
    elsif not @app_tree.exists?("script")
      tracker.options[:rails3] = true
      tracker.options[:rails4] = true
      Brakeman.notify "[Notice] Detected Rails 4 application"
    end
  end
end

#index_call_sitesObject



412
413
414
# File 'lib/brakeman/scanner.rb', line 412

def index_call_sites
  tracker.index_call_sites
end

#parse_filesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/brakeman/scanner.rb', line 127

def parse_files
  fp = Brakeman::FileParser.new(tracker.app_tree, tracker.options[:parser_timeout], tracker.options[:parallel_checks])

  fp.parse_files tracker.app_tree.ruby_file_paths

  template_parser = Brakeman::TemplateParser.new(tracker, fp)

  fp.read_files(@app_tree.template_paths) do |path, contents|
    template_parser.parse_template path, contents
  end

  # Collect errors raised during parsing
  tracker.add_errors(fp.errors)

  @parsed_files = fp.file_list
end

#parse_ruby_file(file) ⇒ Object



416
417
418
419
420
421
422
# File 'lib/brakeman/scanner.rb', line 416

def parse_ruby_file file
  fp = Brakeman::FileParser.new(tracker.app_tree, tracker.options[:parser_timeout])
  fp.parse_ruby(file.read, file)
rescue Exception => e
  tracker.error(e)
  nil
end

#processObject

Process everything in the Rails application



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 70

def process
  process_step 'Processing gems' do
    process_gems
  end

  process_step 'Processing configuration' do
    guess_rails_version
    process_config
  end

  process_step 'Parsing files' do
    parse_files
  end

  process_step 'Detecting file types' do
    detect_file_types
  end

  process_step 'Processing initializers' do
    process_initializers
  end

  process_step 'Processing libs' 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 in templates' 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 in controllers' do
    process_controller_data_flows
  end

  process_step 'Indexing call sites' do
    index_call_sites
  end

  tracker
end

#process_configObject

Process config/environment.rb and config/gems.rb

Stores parsed information in tracker.config



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
# File 'lib/brakeman/scanner.rb', line 174

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 options[:rails3] or options[:rails4] or options[:rails5] or options[: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
    options[:rails3] or options[:escape_html]

    tracker.config.escape_html = true
    Brakeman.notify "[Notice] Escaping HTML by default"
  end

  if @app_tree.exists? ".ruby-version"
    if version = @app_tree.file_path(".ruby-version").read[/(\d\.\d.\d+)/]
      tracker.config.set_ruby_version version, @app_tree.file_path(".ruby-version"), 1
    end
  end

  tracker.config.load_rails_defaults
end

#process_controller(astfile) ⇒ Object



347
348
349
350
351
352
353
# File 'lib/brakeman/scanner.rb', line 347

def process_controller astfile
  begin
    @processor.process_controller(astfile.ast, astfile.path)
  rescue => e
    tracker.error e.exception(e.message + "\nWhile processing #{astfile.path}"), e.backtrace
  end
end

#process_controller_data_flowsObject



332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/brakeman/scanner.rb', line 332

def process_controller_data_flows
  controllers = tracker.controllers.sort_by { |name, _| name.to_s }

  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_controllersObject

Process all .rb files in controllers/

Adds processed controllers to tracker.controllers



324
325
326
327
328
329
330
# File 'lib/brakeman/scanner.rb', line 324

def process_controllers
  track_progress @file_list[:controllers] do |controller|
    process_step_file controller.path do
      process_controller controller
    end
  end
end

#process_gemsObject

Process Gemfile



217
218
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
# File 'lib/brakeman/scanner.rb', line 217

def process_gems
  gem_files = {}

  if @app_tree.exists? "Gemfile"
    file = @app_tree.file_path("Gemfile")
    gem_files[:gemfile] = { :src => parse_ruby_file(file), :file => file }
  elsif @app_tree.exists? "gems.rb"
    file = @app_tree.file_path("gems.rb")
    gem_files[:gemfile] = { :src => parse_ruby_file(file), :file => file }
  end

  if @app_tree.exists? "Gemfile.lock"
    file = @app_tree.file_path("Gemfile.lock")
    gem_files[:gemlock] = { :src => file.read, :file => file }
  elsif @app_tree.exists? "gems.locked"
    file = @app_tree.file_path("gems.locked")
    gem_files[:gemlock] = { :src => file.read, :file => file }
  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.notify "[Notice] Error while processing Gemfile."
  tracker.error e.exception(e.message + "\nWhile processing Gemfile"), e.backtrace
end

#process_initializer(init) ⇒ Object

Process an initializer



279
280
281
# File 'lib/brakeman/scanner.rb', line 279

def process_initializer init
  @processor.process_initializer(init.path, init.ast)
end

#process_initializersObject

Process all the .rb files in config/initializers/

Adds parsed information to tracker.initializers



270
271
272
273
274
275
276
# File 'lib/brakeman/scanner.rb', line 270

def process_initializers
  track_progress @file_list[:initializers] do |init|
    process_step_file init[:path] do
      process_initializer init
    end
  end
end

#process_lib(lib) ⇒ Object

Process a library



300
301
302
# File 'lib/brakeman/scanner.rb', line 300

def process_lib lib
  @processor.process_lib lib.ast, lib.path
end

#process_libsObject

Process all .rb in lib/

Adds parsed information to tracker.libs.



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/brakeman/scanner.rb', line 286

def process_libs
  if options[:skip_libs]
    Brakeman.notify '[Skipping]'
    return
  end

  track_progress @file_list[:libs] do |lib|
    process_step_file lib.path do
      process_lib lib
    end
  end
end

#process_model(path, ast) ⇒ Object



393
394
395
# File 'lib/brakeman/scanner.rb', line 393

def process_model path, ast
  @processor.process_model(ast, path)
end

#process_modelsObject

Process all the .rb files in models/

Adds the processed models to tracker.models



385
386
387
388
389
390
391
# File 'lib/brakeman/scanner.rb', line 385

def process_models
  track_progress @file_list[:models] do |model|
    process_step_file model[:path] do
      process_model model[:path], model[:ast]
    end
  end
end

#process_routesObject

Process config/routes.rb

Adds parsed information to tracker.routes



307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/brakeman/scanner.rb', line 307

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.notify "[Notice] Error while processing routes - assuming all public controller methods are actions."
      options[:assume_all_routes] = true
    end
  else
    Brakeman.notify "[Notice] No route information found"
  end
end

#process_step(description) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brakeman/scanner.rb', line 41

def process_step description
  Brakeman.notify "#{description}...".ljust(40)

  if @show_timing
    start_t = Time.now
    yield
    duration = Time.now - start_t

    Brakeman.notify "(#{description}) Duration: #{duration} seconds"
  else
    yield
  end
end

#process_step_file(description) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/brakeman/scanner.rb', line 55

def process_step_file description
  if @show_timing
    Brakeman.notify "Processing #{description}"

    start_t = Time.now
    yield
    duration = Time.now - start_t

    Brakeman.notify "(#{description}) Duration: #{duration} seconds"
  else
    yield
  end
end

#process_template(template) ⇒ Object



368
369
370
# File 'lib/brakeman/scanner.rb', line 368

def process_template template
  @processor.process_template(template.name, template.ast, template.type, nil, template.path)
end

#process_template_data_flowsObject



372
373
374
375
376
377
378
379
380
# File 'lib/brakeman/scanner.rb', line 372

def process_template_data_flows
  templates = tracker.templates.sort_by { |name, _| name.to_s }

  track_progress templates, "templates" do |name, template|
    process_step_file name do
      @processor.process_template_alias template
    end
  end
end

#process_templatesObject

Process all views and partials in views/

Adds processed views to tracker.views



358
359
360
361
362
363
364
365
366
# File 'lib/brakeman/scanner.rb', line 358

def process_templates
  templates = @file_list[:templates].sort_by { |t| t[:path] }

  track_progress templates, "templates" do |template|
    process_step_file template[:path] do
      process_template template
    end
  end
end

#report_progress(current, total, type = "files") ⇒ Object



407
408
409
410
# File 'lib/brakeman/scanner.rb', line 407

def report_progress(current, total, type = "files")
  return unless @options[:report_progress]
  $stderr.print " #{current}/#{total} #{type} processed\r"
end

#track_progress(list, type = "files") ⇒ Object



397
398
399
400
401
402
403
404
405
# File 'lib/brakeman/scanner.rb', line 397

def track_progress list, type = "files"
  total = list.length
  current = 0
  list.each do |item|
    report_progress current, total, type
    current += 1
    yield item
  end
end

#trackerObject

Returns the Tracker generated from the scan



37
38
39
# File 'lib/brakeman/scanner.rb', line 37

def tracker
  @processor.tracked_events
end