Class: Brakeman::Rails3RoutesProcessor

Inherits:
BasicProcessor show all
Includes:
RouteHelper
Defined in:
lib/brakeman/processors/lib/rails3_route_processor.rb

Overview

Processes the Sexp from routes.rb. Stores results in tracker.routes.

Note that it is only interested in determining what methods on which controllers are used as routes, not the generated URLs for routes.

Constant Summary

Constants included from Util

Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION, Util::SESSION_SEXP

Constants inherited from SexpProcessor

SexpProcessor::VERSION

Instance Attribute Summary collapse

Attributes inherited from SexpProcessor

#context, #env, #expected

Instance Method Summary collapse

Methods included from RouteHelper

#add_resource_routes, #add_resources_routes, #add_route, #current_controller=, #prefix

Methods inherited from BasicProcessor

#process_default

Methods included from Util

#array?, #block?, #call?, #camelize, #contains_class?, #context_for, #cookies?, #false?, #file_by_name, #file_for, #github_url, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #make_call, #node_type?, #number?, #params?, #pluralize, #regexp?, #relative_path, #request_env?, #request_value?, #result?, #set_env_defaults, #sexp?, #string?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore

Methods included from ProcessorHelper

#class_name, #process_all, #process_all!, #process_call_args, #process_class, #process_module

Methods inherited from SexpProcessor

#error_handler, #in_context, #process, #process_dummy, #scope

Constructor Details

#initialize(tracker) ⇒ Rails3RoutesProcessor

Returns a new instance of Rails3RoutesProcessor.


12
13
14
15
16
17
18
19
20
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 12

def initialize tracker
  super
  @map = Sexp.new(:lvar, :map)
  @nested = nil  #used for identifying nested targets
  @prefix = [] #Controller name prefix (a module name, usually)
  @current_controller = nil
  @with_options = nil #For use inside map.with_options
  @controller_block = false
end

Instance Attribute Details

#current_controllerObject (readonly)

Returns the value of attribute current_controller


10
11
12
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 10

def current_controller
  @current_controller
end

#mapObject (readonly)

Returns the value of attribute map


10
11
12
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 10

def map
  @map
end

#nestedObject (readonly)

Returns the value of attribute nested


10
11
12
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 10

def nested
  @nested
end

Instance Method Details

#action_route?(arg) ⇒ Boolean

Returns:

  • (Boolean)

295
296
297
298
299
300
301
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 295

def action_route? arg
  if string? arg
    arg = arg.value
  end

  arg.is_a? String and (arg.include? ":action" or arg.include? "*action")
end

#add_route_from_string(value) ⇒ Object


142
143
144
145
146
147
148
149
150
151
152
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 142

def add_route_from_string value
  value = value[1] if string? value

  controller, action = extract_action value

  if action
    add_route action, controller
  elsif in_controller_block?
    add_route value
  end
end

#extract_action(str) ⇒ Object


280
281
282
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 280

def extract_action str
  str.split "#"
end

#in_controller_blockObject


288
289
290
291
292
293
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 288

def in_controller_block
  prev_block = @controller_block
  @controller_block = true
  yield
  @controller_block = prev_block
end

#in_controller_block?Boolean

Returns:

  • (Boolean)

284
285
286
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 284

def in_controller_block?
  @controller_block
end

#loose_action(controller_name, verb = "any") ⇒ Object


303
304
305
306
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 303

def loose_action controller_name, verb = "any"
  self.current_controller = controller_name.value
  @tracker.routes[@current_controller] = [:allow_all_actions, {:allow_verb => verb}]
end

#process_call(exp) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 26

def process_call exp
  case exp.method
  when :resources
    process_resources exp
  when :resource
    process_resource exp
  when :root
    process_root exp
  when :member
    process_default exp
  when :get, :put, :post, :delete
    process_verb exp
  when :match
    process_match exp
  else
    exp
  end
end

#process_controller_block(exp) ⇒ Object


266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 266

def process_controller_block exp
  if string? exp or symbol? exp
    self.current_controller = exp.block_call.first_arg.value

    in_controller_block do
      process exp[-1] if exp[-1]
    end

    @current_controller = nil
  end

  exp
end

#process_iter(exp) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 45

def process_iter exp
  case exp.block_call.method
  when :namespace
    process_namespace exp
  when :resource
    process_resource_block exp
  when :resources
    process_resources_block exp
  when :scope
    process_scope_block exp
  when :controller
    process_controller_block exp
  else
    process_default exp
  end
end

#process_match(exp) ⇒ Object


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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 89

def process_match exp
  first_arg = exp.first_arg
  second_arg = exp.second_arg
  last_arg = exp.last_arg

  if string? first_arg

    matcher = first_arg.value
    if matcher == ':controller(/:action(/:id(.:format)))' or
      matcher.include? ':controller' and action_route?(matcher)  #Default routes
      @tracker.routes[:allow_all_actions] = first_arg
      return exp
    elsif action_route?(first_arg)
        if hash? second_arg and controller_name = hash_access(second_arg, :controller)
          loose_action(controller_name, "matched") #TODO: Parse verbs
        end
    elsif second_arg.nil? and in_controller_block? and not matcher.include? ":"
      add_route matcher
    end
  end

  if hash? last_arg
    hash_iterate last_arg do |k, v|
      if string? k
        if string? v
          add_route_from_string v
        elsif in_controller_block? and symbol? v
          add_route v
        end
      elsif symbol? k
       case k.value
       when :action
        if string? v
          add_route_from_string v
        else
          add_route v
        end

       when :to
         if string? v
           add_route_from_string v[1]
         elsif in_controller_block? and symbol? v
           add_route v
         end
       end
      end
    end
  end

  @current_controller = nil unless in_controller_block?
  exp
end

#process_namespace(exp) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 62

def process_namespace exp
  arg = exp.block_call.first_arg
  return exp unless symbol? arg or string? arg 

  name = arg.value
  block = exp.block

  @prefix << camelize(name)

  process block

  @prefix.pop

  exp
end

#process_resource(exp) ⇒ Object


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 224

def process_resource exp
  #Does resource even take more than one controller name?
  exp.each_arg do |s|
    if symbol? s
      self.current_controller = pluralize(s.value.to_s)
      add_resource_routes
    else
      #handle something else, like options
      #or something?
    end
  end

  @current_controller = nil unless in_controller_block?
  exp
end

#process_resource_block(exp) ⇒ Object


250
251
252
253
254
255
256
257
258
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 250

def process_resource_block exp
  in_controller_block do
    process_resource exp.block_call
    process exp.block
  end

  @current_controller = nil
  exp
end

#process_resources(exp) ⇒ Object


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 203

def process_resources exp
  first_arg = exp.first_arg
  second_arg = exp.second_arg

  return exp unless symbol? first_arg or string? first_arg

  if second_arg and second_arg.node_type == :hash
    self.current_controller = first_arg.value
    #handle hash
    add_resources_routes
  elsif exp.args.all? { |s| symbol? s }
    exp.each_arg do |s|
      self.current_controller = s.value
      add_resources_routes
    end
  end

  @current_controller = nil unless in_controller_block?
  exp
end

#process_resources_block(exp) ⇒ Object


240
241
242
243
244
245
246
247
248
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 240

def process_resources_block exp
  in_controller_block do
    process_resources exp.block_call
    process exp.block
  end

  @current_controller = nil
  exp
end

#process_root(exp) ⇒ Object

TODO: Need test for this


79
80
81
82
83
84
85
86
87
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 79

def process_root exp
  if value = hash_access(exp.first_arg, :to)
    if string? value
      add_route_from_string value
    end
  end

  exp
end

#process_routes(exp) ⇒ Object


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

def process_routes exp
  process exp.dup
end

#process_scope_block(exp) ⇒ Object


260
261
262
263
264
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 260

def process_scope_block exp
  #How to deal with options?
  process exp.block
  exp
end

#process_verb(exp) ⇒ Object


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
201
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 154

def process_verb exp
  first_arg = exp.first_arg
  second_arg = exp.second_arg

  if symbol? first_arg and not hash? second_arg
    add_route first_arg
  elsif hash? second_arg
    hash_iterate second_arg do |k, v|
      if symbol? k and k.value == :to
        if string? v
          add_route_from_string v
        elsif in_controller_block? and symbol? v
          add_route v
        end
      elsif action_route?(first_arg)
        if hash? second_arg and controller_name = hash_access(second_arg, :controller)
          loose_action(controller_name, exp.method)
        end
      end
    end
  elsif string? first_arg
    if first_arg.value.include? ':controller' and action_route?(first_arg) #Default routes
      @tracker.routes[:allow_all_actions] = first_arg
    end

    route = first_arg.value.split "/"
    if route.length != 2
      add_route route[0]
    else
      add_route route[1], route[0]
    end
  elsif in_controller_block? and symbol? first_arg
    add_route first_arg
  else hash? first_arg
    hash_iterate first_arg do |k, v|
      if string? k
        if string? v
          add_route_from_string v
        elsif in_controller_block?
          add_route v
        end
      end
    end
  end

  @current_controller = nil unless in_controller_block?
  exp
end