Class: Brakeman::Scanner

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

Overview

Scans the Rails application.

Direct Known Subclasses

Rescanner

Constant Summary

RUBY_1_9 =
!!(RUBY_VERSION =~ /^1\.9/)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Scanner) initialize(options, processor = nil)

Pass in path to the root of the Rails application



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/brakeman/scanner.rb', line 33

def initialize options, processor = nil
  @options = options
  @report_progress = options[:report_progress]
  @path = options[:app_path]
  @app_path = File.join(@path, "app")
  @processor = processor || Brakeman::Processor.new(options)

  if RUBY_1_9
    @ruby_parser = ::Ruby19Parser
  else
    @ruby_parser = ::Ruby18Parser
  end
end

Instance Attribute Details

- (Object) options (readonly)

Returns the value of attribute options



28
29
30
# File 'lib/brakeman/scanner.rb', line 28

def options
  @options
end

Instance Method Details

- (Object) index_call_sites



355
356
357
# File 'lib/brakeman/scanner.rb', line 355

def index_call_sites
  tracker.index_call_sites
end

- (Object) parse_ruby(input)



359
360
361
# File 'lib/brakeman/scanner.rb', line 359

def parse_ruby input
  @ruby_parser.new.parse input
end

- (Object) process

Process everything in the Rails application



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/brakeman/scanner.rb', line 53

def process
  warn "Processing configuration..."
  process_config
  warn "Processing gems..."
  process_gems
  warn "Processing initializers..."
  process_initializers
  warn "Processing libs..."
  process_libs
  warn "Processing routes...        "
  process_routes
  warn "Processing templates...     "
  process_templates
  warn "Processing models...        "
  process_models
  warn "Processing controllers...   "
  process_controllers
  warn "Indexing call sites...      "
  index_call_sites
  tracker
end

- (Object) process_config

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

Stores parsed information in tracker.config



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/brakeman/scanner.rb', line 78

def process_config
  if options[:rails3]
    process_config_file "application.rb"
    process_config_file "environments/production.rb"
  else
    process_config_file "environment.rb"
    process_config_file "gems.rb"
  end

  if File.exists? "#@path/vendor/plugins/rails_xss" or
    options[:rails3] or options[:escape_html] or
    (File.exists? "#@path/Gemfile" and File.read("#@path/Gemfile").include? "rails_xss")

    tracker.config[:escape_html] = true
    warn "[Notice] Escaping HTML by default"
  end
end

- (Object) process_controller(path)



227
228
229
230
231
232
233
234
235
# File 'lib/brakeman/scanner.rb', line 227

def process_controller path
  begin
    @processor.process_controller(parse_ruby(File.read(path)), path)
  rescue Racc::ParseError => e
    tracker.error e, "could not parse #{path}. There is probably a typo in the file. Test it with 'ruby_parse #{path}'"
  rescue Exception => e
    tracker.error e.exception(e.message + "\nWhile processing #{path}"), e.backtrace
  end
end

- (Object) process_controllers

Process all .rb files in controllers/

Adds processed controllers to tracker.controllers



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/brakeman/scanner.rb', line 197

def process_controllers
  controller_files = Dir.glob(@app_path + "/controllers/**/*.rb").sort
  total = controller_files.length * 2
  current = 0

  controller_files.each do |f|
    warn "Processing #{f}" if options[:debug]
    if @report_progress
      $stderr.print " #{current}/#{total} files processed\r"
      current += 1
    end

    process_controller f
  end

  current = 0
  total = tracker.controllers.length

  warn "Processing data flow in controllers..."

  tracker.controllers.each do |name, controller|
    if @report_progress
      $stderr.print " #{current}/#{total} controllers processed\r"
      current += 1
    end

    @processor.process_controller_alias controller[:src]
  end
end

- (Object) process_gems

Process Gemfile



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/brakeman/scanner.rb', line 109

def process_gems
  if File.exists? "#@path/Gemfile"
    if File.exists? "#@path/Gemfile.lock"
      @processor.process_gems(parse_ruby(File.read("#@path/Gemfile")), File.read("#@path/Gemfile.lock"))
    else
      @processor.process_gems(parse_ruby(File.read("#@path/Gemfile")))
    end
  end
rescue Exception => e
  warn "[Notice] Error while processing Gemfile."
  tracker.error e.exception(e.message + "\nWhile processing Gemfile"), e.backtrace
end

- (Object) process_initializer(path)

Process an initializer



132
133
134
135
136
137
138
139
140
# File 'lib/brakeman/scanner.rb', line 132

def process_initializer path
  begin
    @processor.process_initializer(path, parse_ruby(File.read(path)))
  rescue Racc::ParseError => e
    tracker.error e, "could not parse #{path}. There is probably a typo in the file. Test it with 'ruby_parse #{path}'"
  rescue Exception => e
    tracker.error e.exception(e.message + "\nWhile processing #{path}"), e.backtrace
  end
end

- (Object) process_initializers

Process all the .rb files in config/initializers/

Adds parsed information to tracker.initializers



125
126
127
128
129
# File 'lib/brakeman/scanner.rb', line 125

def process_initializers
  Dir.glob(@path + "/config/initializers/**/*.rb").sort.each do |f|
    process_initializer f
  end
end

- (Object) process_lib(path)

Process a library



167
168
169
170
171
172
173
174
175
# File 'lib/brakeman/scanner.rb', line 167

def process_lib path
  begin
    @processor.process_lib parse_ruby(File.read(path)), path
  rescue Racc::ParseError => e
    tracker.error e, "could not parse #{path}. There is probably a typo in the file. Test it with 'ruby_parse #{path}'"
  rescue Exception => e
    tracker.error e.exception(e.message + "\nWhile processing #{path}"), e.backtrace
  end
end

- (Object) process_libs

Process all .rb in lib/

Adds parsed information to tracker.libs.



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

def process_libs
  if options[:skip_libs]
    warn '[Skipping]'
    return
  end

  lib_files = Dir.glob(@path + "/lib/**/*.rb").sort
  total = lib_files.length
  current = 0

  lib_files.each do |f|
    warn "Processing #{f}" if options[:debug]
    if @report_progress
      $stderr.print " #{current}/#{total} files processed\r"
      current += 1
    end

    process_lib f
  end
end

- (Object) process_model(path)



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

def process_model path
  begin
    @processor.process_model(parse_ruby(File.read(path)), path)
  rescue Racc::ParseError => e
    tracker.error e, "could not parse #{path}"
  rescue Exception => e
    tracker.error e.exception(e.message + "\nWhile processing #{path}"), e.backtrace
  end
end

- (Object) process_models

Process all the .rb files in models/

Adds the processed models to tracker.models



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/brakeman/scanner.rb', line 328

def process_models
  model_files = Dir.glob(@app_path + "/models/*.rb").sort

  total = model_files.length
  current = 0

  model_files.each do |f|
    if @report_progress
      $stderr.print " #{current}/#{total} files processed\r"
      current += 1
    end

    process_model f

  end
end

- (Object) process_routes

Process config/routes.rb

Adds parsed information to tracker.routes



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/brakeman/scanner.rb', line 180

def process_routes
  if File.exists? "#@path/config/routes.rb"
    begin
      @processor.process_routes parse_ruby(File.read("#@path/config/routes.rb"))
    rescue Exception => e
      tracker.error e.exception(e.message + "\nWhile processing routes.rb"), e.backtrace
      warn "[Notice] Error while processing routes - assuming all public controller methods are actions."
      options[:assume_all_routes] = true
    end
  else
    warn "[Notice] No route information found"
  end
end

- (Object) process_template(path)



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/brakeman/scanner.rb', line 273

def process_template path
  type = path.match(/.*\.(erb|haml|rhtml)$/)[1].to_sym
  type = :erb if type == :rhtml
  name = template_path_to_name path
  text = File.read path

  begin
    if type == :erb
      if tracker.config[:escape_html]
        type = :erubis
        if options[:rails3]
          src = Brakeman::RailsXSSErubis.new(text).src
        else
          src = Brakeman::ErubisEscape.new(text).src
        end
      elsif tracker.config[:erubis]
        type = :erubis
        src = Brakeman::ScannerErubis.new(text).src
      else
        src = ERB.new(text, nil, "-").src
        src.sub!(/^#.*\n/, '') if RUBY_1_9
      end

      parsed = parse_ruby src
    elsif type == :haml
      src = Haml::Engine.new(text,
                             :escape_html => !!tracker.config[:escape_html]).precompiled
      parsed = parse_ruby src
    else
      tracker.error "Unkown template type in #{path}"
    end

    @processor.process_template(name, parsed, type, nil, path)

  rescue Racc::ParseError => e
    tracker.error e, "could not parse #{path}"
  rescue Haml::Error => e
    tracker.error e, ["While compiling HAML in #{path}"] << e.backtrace
  rescue Exception => e
    tracker.error e.exception(e.message + "\nWhile processing #{path}"), e.backtrace
  end
end

- (Object) process_templates

Process all views and partials in views/

Adds processed views to tracker.views



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/brakeman/scanner.rb', line 240

def process_templates

  views_path = @app_path + "/views/**/*.{html.erb,html.haml,rhtml,js.erb}"
  $stdout.sync = true
  count = 0

  template_files = Dir.glob(views_path).sort
  total = template_files.length

  template_files.each do |path|
    if @report_progress
      $stderr.print " #{count}/#{total} files processed\r"
      count += 1
    end

    process_template path
  end

  total = tracker.templates.length
  count = 0

  warn "Processing data flow in templates..."

  tracker.templates.keys.dup.each do |name|
    if @report_progress
      count += 1
      $stderr.print " #{count}/#{total} templates processed\r"
    end

    @processor.process_template_alias tracker.templates[name]
  end
end

- (Object) template_path_to_name(path)

Convert path/filename to view name

views/test/something.html.erb -> test/something



319
320
321
322
323
# File 'lib/brakeman/scanner.rb', line 319

def template_path_to_name path
  names = path.split("/")
  names.last.gsub!(/(\.(html|js)\..*|\.rhtml)$/, '')
  names[(names.index("views") + 1)..-1].join("/").to_sym
end

- (Object) tracker

Returns the Tracker generated from the scan



48
49
50
# File 'lib/brakeman/scanner.rb', line 48

def tracker
  @processor.tracked_events
end