Class: Brakeman::Rails3RoutesProcessor

Inherits:
BaseProcessor 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::PARAMETERS, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION

Constants inherited from SexpProcessor

SexpProcessor::VERSION

Instance Attribute Summary collapse

Attributes inherited from BaseProcessor

#ignore

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 BaseProcessor

#find_render_type, #make_render, #make_render_in_view, #process_arglist, #process_attrasgn, #process_block, #process_class, #process_default, #process_dstr, #process_evstr, #process_hash, #process_if, #process_ignore, #process_lasgn, #process_scope

Methods included from Util

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

Methods included from ProcessorHelper

#class_name, #process_all, #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.



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

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.



8
9
10
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 8

def current_controller
  @current_controller
end

#mapObject (readonly)

Returns the value of attribute map.



8
9
10
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 8

def map
  @map
end

#nestedObject (readonly)

Returns the value of attribute nested.



8
9
10
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 8

def nested
  @nested
end

Instance Method Details

#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



266
267
268
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 266

def extract_action str
  str.split "#"
end

#in_controller_blockObject



274
275
276
277
278
279
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 274

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

#in_controller_block?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 270

def in_controller_block?
  @controller_block
end

#process_call(exp) ⇒ Object



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

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



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 254

def process_controller_block exp
  args = exp[1][3]
  self.current_controller = args[1][1]

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

  @current_controller = nil
  exp
end

#process_iter(exp) ⇒ Object



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

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

#process_match(exp) ⇒ Object



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

def process_match exp
  args = exp.args

  #Check if there is an unrestricted action parameter
  action_variable = false

  if string? args.first
    matcher = args.first.value

    if matcher == ':controller(/:action(/:id(.:format)))' or
      matcher.include? ':controller' and matcher.include? ':action' #Default routes
      @tracker.routes[:allow_all_actions] = args.first
      return exp
    elsif matcher.include? ':action'
      action_variable = true
    elsif args[1].nil? and in_controller_block? and not matcher.include? ":"
      add_route matcher
    end
  end

  if hash? args.last
    hash_iterate args.last 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

        action_variable = false
       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

  if action_variable
    @tracker.routes[@current_controller] = :allow_all_actions
  end

  @current_controller = nil unless in_controller_block?
  exp
end

#process_namespace(exp) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 60

def process_namespace exp
  name = exp.block_call.first_arg.value
  block = exp.block

  @prefix << camelize(name)

  process block

  @prefix.pop

  exp
end

#process_resource(exp) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 212

def process_resource exp
  #Does resource even take more than one controller name?
  exp.args.each 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



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

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 196

def process_resources exp
  if exp.args and exp.args.second and exp.args.second.node_type == :hash
    self.current_controller = exp.first_arg.value
    #handle hash
    add_resources_routes
  elsif exp.args.all? { |s| symbol? s }
    exp.args.each 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



228
229
230
231
232
233
234
235
236
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 228

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



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

def process_root exp
  args = exp.args

  if value = hash_access(args.first, :to)
    if string? value
      add_route_from_string value
    end
  end

  exp
end

#process_routes(exp) ⇒ Object



20
21
22
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 20

def process_routes exp
  process exp.dup
end

#process_scope_block(exp) ⇒ Object



248
249
250
251
252
# File 'lib/brakeman/processors/lib/rails3_route_processor.rb', line 248

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

def process_verb exp
  args = exp.args
  first_arg = args.first

  if symbol? first_arg and not hash? args.second
    add_route first_arg
  elsif hash? args.second
    hash_iterate args.second 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
      end
    end
  elsif string? first_arg
    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