Class: Brakeman::Rails2ConfigProcessor

Inherits:
BasicProcessor
  • Object
show all
Defined in:
lib/brakeman/processors/lib/rails2_config_processor.rb

Overview

Processes configuration. Results are put in tracker.config.

Configuration of Rails via Rails::Initializer are stored in tracker.config.rails. For example:

Rails::Initializer.run |config| config.action_controller.session_store = :cookie_store end

will be stored in

tracker.config[:action_controller][:session_store]

Values for tracker.config.rails will still be Sexps.

Constant Summary collapse

RAILS_CONFIG =

Replace block variable in

Rails::Initializer.run |config|

with this value so we can keep track of it.

Sexp.new(:const, :"!BRAKEMAN_RAILS_CONFIG")

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Rails2ConfigProcessor

Returns a new instance of Rails2ConfigProcessor.



25
26
27
# File 'lib/brakeman/processors/lib/rails2_config_processor.rb', line 25

def initialize *args
  super
end

Instance Method Details

#get_rails_config(exp) ⇒ Object

Returns an array of symbols for each 'level' in the config

config.action_controller.session_store = :cookie

becomes

[:action_controller, :session_store]



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

def get_rails_config exp
  if node_type? exp, :attrasgn
    attribute = exp.method.to_s[0..-2].to_sym
    get_rails_config(exp.target) << attribute
  elsif call? exp
    if exp.target == RAILS_CONFIG
      [exp.method]
    else
      get_rails_config(exp.target) << exp.method
    end
  else
    raise "WHAT"
  end
end

#include_rails_config?(exp) ⇒ Boolean

Check if an expression includes a call to set Rails config

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/brakeman/processors/lib/rails2_config_processor.rb', line 86

def include_rails_config? exp
  target = exp.target
  if call? target
    if target.target == RAILS_CONFIG
      true
    else
      include_rails_config? target
    end
  elsif target == RAILS_CONFIG
    true
  else
    false
  end
end

#process_attrasgn(exp) ⇒ Object

Look for configuration settings



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

def process_attrasgn exp
  if exp.target == RAILS_CONFIG
    #Get rid of '=' at end
    attribute = exp.method.to_s[0..-2].to_sym
    if exp.num_args > 1
      #Multiple arguments?...not sure if this will ever happen
      @tracker.config.rails[attribute] = exp.args
    else
      @tracker.config.rails[attribute] = exp.first_arg
    end
  elsif include_rails_config? exp
    options = get_rails_config exp
    level = @tracker.config.rails
    options[0..-2].each do |o|
      level[o] ||= {}
      level = level[o]
    end

    level[options.last] = exp.first_arg
  end

  exp
end

#process_call(exp) ⇒ Object

Check if config is set to use Erubis but because it's 2026 we're going to use Erubi



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/brakeman/processors/lib/rails2_config_processor.rb', line 38

def process_call exp
  target = exp.target
  target = process target if sexp? target

  if exp.method == :gem and exp.first_arg.value == "erubis"
    Brakeman.debug "[Notice] Using Erubi for ERB templates"
    @tracker.config.erubi = true
  end

  exp
end

#process_cdecl(exp) ⇒ Object

Check for Rails version



76
77
78
79
80
81
82
83
# File 'lib/brakeman/processors/lib/rails2_config_processor.rb', line 76

def process_cdecl exp
  #Set Rails version required
  if exp.lhs == :RAILS_GEM_VERSION
    @tracker.config.set_rails_version exp.rhs.value
  end

  exp
end

#process_config(src, current_file) ⇒ Object

Use this method to process configuration file



30
31
32
33
34
# File 'lib/brakeman/processors/lib/rails2_config_processor.rb', line 30

def process_config src, current_file
  @current_file = current_file
  res = Brakeman::ConfigAliasProcessor.new.process_safely(src, nil, current_file)
  process res
end