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_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 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, #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_env?, #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(tracker) ⇒ Rails3RoutesProcessor

Returns a new instance of Rails3RoutesProcessor.



12
13
14
15
16
17
18
19
20
21
# 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
  @current_file = "config/routes.rb"
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)


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

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



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

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



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

def extract_action str
  str.split "#"
end

#in_controller_blockObject



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

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

#in_controller_block?Boolean

Returns:

  • (Boolean)


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

def in_controller_block?
  @controller_block
end

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



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

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



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

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



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

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



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

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



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
141
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 90

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



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

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



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

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



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

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



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

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



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

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



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

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



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

def process_routes exp
  process Brakeman::AliasProcessor.new.process_safely(exp, nil, @current_file)
end

#process_scope_block(exp) ⇒ Object



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

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

#process_verb(exp) ⇒ Object



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
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 155

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