Class: Brakeman::Rails3ConfigProcessor

Inherits:
BasicProcessor
  • Object
show all
Defined in:
lib/brakeman/processors/lib/rails3_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:

MyApp::Application.configure do config.active_record.whitelist_attributes = true end

will be stored in

tracker.config.rails[:whitelist_attributes]

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

Direct Known Subclasses

Rails4ConfigProcessor

Constant Summary collapse

RAILS_CONFIG =
Sexp.new(:call, nil, :config)
RAILS_APPLICATION =
Sexp.new(:colon2, s(:const, :Rails), :Application)

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Rails3ConfigProcessor

Returns a new instance of Rails3ConfigProcessor.



22
23
24
25
# File 'lib/brakeman/processors/lib/rails3_config_processor.rb', line 22

def initialize *args
  super
  @inside_config = false
end

Instance Method Details

#application_class?(exp) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'lib/brakeman/processors/lib/rails3_config_processor.rb', line 61

def application_class? exp
  return unless node_type? exp, :class

  exp.class_name == :Application or
  (node_type? exp.class_name, :colon2 and exp.class_name.rhs == :Application) or
  (exp.parent_name == RAILS_APPLICATION)
end

#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]



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

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)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/brakeman/processors/lib/rails3_config_processor.rb', line 105

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/brakeman/processors/lib/rails3_config_processor.rb', line 84

def process_attrasgn exp
  return exp unless @inside_config

  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_path = get_rails_config exp
    @tracker.config.set_rails_config(value: exp.first_arg, path: options_path, overwrite: true)
  end

  exp
end

#process_call(exp) ⇒ Object

Look for configuration settings that are just a call like

config.load_defaults 5.2



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

def process_call exp
  return exp unless @inside_config

  if exp.target == RAILS_CONFIG and exp.first_arg
    @tracker.config.rails[exp.method] = exp.first_arg
  end

  exp
end

#process_class(exp) ⇒ Object

Look for class Application < Rails::Application



51
52
53
54
55
56
57
58
59
# File 'lib/brakeman/processors/lib/rails3_config_processor.rb', line 51

def process_class exp
  if application_class? exp
    @inside_config = true
    process_all exp.body if sexp? exp.body
    @inside_config = false
  end

  exp
end

#process_config(src, current_file) ⇒ Object

Use this method to process configuration file



28
29
30
31
32
# File 'lib/brakeman/processors/lib/rails3_config_processor.rb', line 28

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

#process_iter(exp) ⇒ Object

Look for MyApp::Application.configure do ... end



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

def process_iter exp
  call = exp.block_call

  if node_type?(call.target, :colon2) and
    call.target.rhs == :Application and
    call.method == :configure

    @inside_config = true
    process exp.block if sexp? exp.block
    @inside_config = false
  end

  exp
end