Class: Brakeman::GemProcessor

Inherits:
BasicProcessor show all
Defined in:
lib/brakeman/processors/gem_processor.rb

Overview

Processes Gemfile and Gemfile.lock

Constant Summary collapse

GEM_SPEC =
s(:colon2, s(:const, :Gem), :Specification)

Constants included from Util

Util::ALL_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::DIR_CONST, Util::LITERALS, 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, Util::SIMPLE_LITERALS

Constants inherited from SexpProcessor

SexpProcessor::VERSION

Instance Attribute Summary

Attributes inherited from SexpProcessor

#context, #env, #expected

Instance Method Summary collapse

Methods inherited from BasicProcessor

#process_default, #process_if

Methods included from Util

#all_literals?, #array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #dir_glob?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #hash_values, #integer?, #kwsplat?, #literal?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #recurse_check?, #regexp?, #remove_kwsplat, #request_headers?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #simple_literal?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #underscore

Methods included from ProcessorHelper

#current_file, #process_all, #process_all!, #process_call_args, #process_call_defn?, #process_class, #process_module

Methods inherited from SexpProcessor

#in_context, #process, processors, #scope

Constructor Details

#initialize(*args) ⇒ GemProcessor

Returns a new instance of GemProcessor.



6
7
8
9
10
# File 'lib/brakeman/processors/gem_processor.rb', line 6

def initialize *args
  super
  @gem_name_version = /^\s*([-_+.A-Za-z0-9]+) \((\w(\.\w+)*)\)/
  @ruby_version = /^\s+ruby (\d\.\d.\d+)/
end

Instance Method Details

#process_call(exp) ⇒ Object

Known issue: Brakeman does not yet support ‘gem` calls with multiple “version requirements”. Consider the following example from the ruby docs:

gem 'rake', '>= 1.1.a', '< 2'

We are assuming that ‘second_arg` (eg. ’>= 1.1.a’) is the only requirement. Perhaps we should instantiate an array of ‘::Gem::Requirement`s or even a `::Gem::Dependency` and pass that to `Tracker::Config#add_gem`?



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/brakeman/processors/gem_processor.rb', line 41

def process_call exp
  if exp.target == nil
    if exp.method == :gem
      gem_name = exp.first_arg
      return exp unless string? gem_name

      gem_version = exp.second_arg

      version = if string? gem_version
                  gem_version.value
                else
                  nil
                end

      @tracker.config.add_gem gem_name.value, version, @gemfile, exp.line
    elsif exp.method == :ruby
      version = exp.first_arg
      if string? version
        @tracker.config.set_ruby_version version.value, @gemfile, exp.line
      end
    end
  elsif @inside_gemspec and exp.method == :add_dependency
    if string? exp.first_arg and string? exp.second_arg
      @tracker.config.add_gem exp.first_arg.value, exp.second_arg.value, @gemspec, exp.line
    end
  end

  exp
end

#process_gem_lockObject



86
87
88
89
90
91
92
93
# File 'lib/brakeman/processors/gem_processor.rb', line 86

def process_gem_lock
  line_num = 1
  file = @gem_files[:gemlock][:file]
  @gem_files[:gemlock][:src].each_line do |line|
    set_gem_version_and_file line, file, line_num
    line_num += 1
  end
end

#process_gems(gem_files) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/brakeman/processors/gem_processor.rb', line 12

def process_gems gem_files
  @gem_files = gem_files
  @gemfile = gem_files[:gemfile] && gem_files[:gemfile][:file]
  @gemspec = gem_files[:gemspec] && gem_files[:gemspec][:file]


  if @gemspec
    process gem_files[:gemspec][:src]
  end

  if @gemfile
    process gem_files[:gemfile][:src]
  end

  if gem_files[:gemlock]
    process_gem_lock
  end

  @tracker.config.set_rails_version
end

#process_iter(exp) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/brakeman/processors/gem_processor.rb', line 73

def process_iter exp
  if exp.block_call.target == GEM_SPEC and exp.block_call.method == :new
    @inside_gemspec = true
    process exp.block if sexp? exp.block

    exp
  else
    process_default exp
  end
ensure
  @inside_gemspec = false
end

#set_gem_version_and_file(line, file, line_num) ⇒ Object

Supports .rc2 but not ~>, >=, or <=



96
97
98
99
100
101
102
# File 'lib/brakeman/processors/gem_processor.rb', line 96

def set_gem_version_and_file line, file, line_num
  if line =~ @gem_name_version
    @tracker.config.add_gem $1, $2, file, line_num
  elsif line =~ @ruby_version
    @tracker.config.set_ruby_version $1, file, line_num
  end
end