rails2_route_processor.rb 7.5 KB
Newer Older
J
Justin Collins 已提交
1 2
require 'brakeman/processors/base_processor'

J
Justin 已提交
3 4 5 6
#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.
7
class Brakeman::Rails2RoutesProcessor < Brakeman::BaseProcessor
J
Justin Collins 已提交
8
  include Brakeman::RouteHelper
9

J
Justin 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  attr_reader :map, :nested, :current_controller

  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
  end

  #Call this with parsed route file information.
  #
  #This method first calls RouteAliasProcessor#process_safely on the +exp+,
  #so it does not modify the +exp+.
  def process_routes exp
J
Justin Collins 已提交
26
    process Brakeman::RouteAliasProcessor.new.process_safely(exp)
J
Justin 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  end

  #Looking for mapping of routes
  def process_call exp
    target = exp[1]

    if target == map or target == nested
      process_map exp

    else
      process_default exp
    end

    exp
  end

  #Process a map.something call
  #based on the method used
  def process_map exp
    args = exp[3][1..-1]

    case exp[2]
    when :resource
      process_resource args
    when :resources
      process_resources args
    when :connect, :root
      process_connect args
    else
      process_named_route args
    end

    exp
  end

  #Look for map calls that take a block.
  #Otherwise, just do the default processing.
  def process_iter exp
    if exp[1][1] == map or exp[1][1] == nested
      method = exp[1][2]
      case method
      when :namespace
        process_namespace exp
      when :resources, :resource
        process_resources exp[1][3][1..-1]
J
Justin 已提交
72
        process_default exp[3] if exp[3]
J
Justin 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      when :with_options
        process_with_options exp
      end
      exp
    else
      super
    end
  end

  #Process
  # map.resources :x, :controller => :y, :member => ...
  #etc.
  def process_resources exp
    controller = check_for_controller_name exp
    if controller
      self.current_controller = controller
      process_resource_options exp[-1]
    else
      exp.each do |argument|
92
        if node_type? argument, :lit
J
Justin 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
          self.current_controller = exp[0][1]
          add_resources_routes
          process_resource_options exp[-1]
        end
      end
    end
  end

  #Process all the options that might be in the hash passed to
  #map.resource, et al.
  def process_resource_options exp
    if exp.nil? and @with_options
      exp = @with_options
    elsif @with_options
      exp = exp.concat @with_options[1..-1]
    end
    return unless exp.node_type == :hash

    hash_iterate(exp) do |option, value|
      case option[1]
      when :controller, :requirements, :singular, :path_prefix, :as,
114
        :path_names, :shallow, :name_prefix, :member_path, :nested_member_path
J
Justin 已提交
115 116 117 118 119
        #should be able to skip
      when :collection, :member, :new
        process_collection value
      when :has_one
        save_controller = current_controller
120
        process_resource value[1..-1] #Verify this is proper behavior
J
Justin 已提交
121 122 123 124 125 126 127 128 129 130
        self.current_controller = save_controller
      when :has_many
        save_controller = current_controller
        process_resources value[1..-1]
        self.current_controller = save_controller
      when :only
        process_option_only value
      when :except
        process_option_except value
      else
J
Justin 已提交
131
        Brakeman.notify "[Notice] Unhandled resource option: #{option}"
J
Justin 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      end
    end
  end

  #Process route option :only => ...
  def process_option_only exp
    routes = @tracker.routes[@current_controller]
    [:index, :new, :create, :show, :edit, :update, :destroy].each do |r|
      routes.delete r
    end

    if exp.node_type == :array
      exp[1..-1].each do |e|
        routes << e[1]
      end
    end
  end

  #Process route option :except => ...
  def process_option_except exp
    return unless exp.node_type == :array
    routes = @tracker.routes[@current_controller]

    exp[1..-1].each do |e|
      routes.delete e[1]
    end
  end

  #  map.resource :x, ..
  def process_resource exp
    controller = check_for_controller_name exp
    if controller
      self.current_controller = controller
      process_resource_options exp[-1]
    else
      exp.each do |argument|
168
        if node_type? argument, :lit
J
Justin 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
          self.current_controller = pluralize(exp[0][1].to_s)
          add_resource_routes
          process_resource_options exp[-1]
        end
      end
    end
  end

  #Process
  # map.connect '/something', :controller => 'blah', :action => 'whatever'
  def process_connect exp
    controller = check_for_controller_name exp
    self.current_controller = controller if controller
    
    #Check for default route
    if string? exp[0]
      if exp[0][1] == ":controller/:action/:id"
        @tracker.routes[:allow_all_actions] = exp[0]
      elsif exp[0][1].include? ":action"
188
        @tracker.routes[@current_controller] = [:allow_all_actions, exp.line]
J
Justin 已提交
189 190 191 192 193 194
        return
      end
    end

    #This -seems- redundant, but people might connect actions
    #to a controller which already allows them all
195 196
    return if @tracker.routes[@current_controller].is_a? Array and @tracker.routes[@current_controller][0] == :allow_all_actions
  
J
Justin 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    exp[-1].each_with_index do |e,i|
      if symbol? e and e[1] == :action
        @tracker.routes[@current_controller] << exp[-1][i + 1][1].to_sym
        return
      end
    end
  end

  # map.with_options :controller => 'something' do |something|
  #   something.resources :blah
  # end
  def process_with_options exp
    @with_options = exp[1][3][-1]
    @nested = Sexp.new(:lvar, exp[2][1])

    self.current_controller = check_for_controller_name exp[1][3]
    
    #process block
    process exp[3] 

    @with_options = nil
    @nested = nil
  end

  # map.namespace :something do |something|
  #   something.resources :blah
  # end
  def process_namespace exp
    call = exp[1]
    formal_args = exp[2]
    block = exp[3]

    @prefix << camelize(call[3][1][1])

    @nested = Sexp.new(:lvar, formal_args[1])

    process block

    @prefix.pop
  end

  # map.something_abnormal '/blah', :controller => 'something', :action => 'wohoo'
  def process_named_route exp
    process_connect exp
  end

  #Process collection option
  # :collection => { :some_action => :http_actions }
  def process_collection exp
    return unless exp.node_type == :hash
    routes = @tracker.routes[@current_controller]

    hash_iterate(exp) do |action, type|
      routes << action[1]
    end
  end

  private

  #Checks an argument list for a hash that has a key :controller.
  #If it does, returns the value.
  #
  #Otherwise, returns nil.
  def check_for_controller_name args
    args.each do |a|
      if hash? a
        hash_iterate(a) do |k, v|
          if k[1] == :controller
            return v[1]
          end 
        end
      end
    end

    nil
  end
end

#This is for a really specific case where a hash is used as arguments
#to one of the map methods.
J
Justin Collins 已提交
277
class Brakeman::RouteAliasProcessor < Brakeman::AliasProcessor
J
Justin 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  
  #This replaces
  # { :some => :hash }.keys
  #with 
  # [:some]
  def process_call exp
    process_default exp
    
    if hash? exp[1] and exp[2] == :keys
        keys = get_keys exp[1] 
      exp.clear
      keys.each_with_index do |e,i|
        exp[i] = e
      end
    end
    exp
  end

  #Returns an array Sexp containing the keys from the hash
  def get_keys hash
    keys = Sexp.new(:array)
    hash_iterate(hash) do |key, value|
      keys << key
    end

    keys
  end
end