rails2_config_processor.rb 3.4 KB
Newer Older
1 2 3 4 5
#Replace block variable in
#
#  Rails::Initializer.run |config|
#
#with this value so we can keep track of it.
6
Brakeman::RAILS_CONFIG = Sexp.new(:const, :"!BRAKEMAN_RAILS_CONFIG") unless defined? Brakeman::RAILS_CONFIG
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

#Processes configuration. Results are put in tracker.config.
#
#Configuration of Rails via Rails::Initializer are stored in tracker.config[:rails].
#For example:
#
#  Rails::Initializer.run |config|
#    config.action_controller.session_store = :cookie_store
#  end
#
#will be stored in
#
#  tracker.config[:rails][:action_controller][:session_store]
#
#Values for tracker.config[:rails] will still be Sexps.
22
class Brakeman::Rails2ConfigProcessor < Brakeman::BaseProcessor
23 24 25 26 27 28 29
  def initialize *args
    super
    @tracker.config[:rails] ||= {}
  end

  #Use this method to process configuration file
  def process_config src
J
Justin Collins 已提交
30
    res = Brakeman::ConfigAliasProcessor.new.process_safely(src)
31 32 33 34 35 36 37 38 39
    process res
  end

  #Check if config is set to use Erubis
  def process_call exp
    target = exp[1]
    target = process target if sexp? target

    if exp[2] == :gem and exp[3][1][1] == "erubis"
40
      Brakeman.notify "[Notice] Using Erubis for ERB templates"
41 42 43 44 45 46 47 48
      @tracker.config[:erubis] = true
    end

    exp
  end

  #Look for configuration settings
  def process_attrasgn exp
J
Justin Collins 已提交
49
    if exp[1] == Brakeman::RAILS_CONFIG
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      #Get rid of '=' at end
      attribute = exp[2].to_s[0..-2].to_sym
      if exp[3].length > 2
        #Multiple arguments?...not sure if this will ever happen
        @tracker.config[:rails][attribute] = exp[3][1..-1]
      else
        @tracker.config[:rails][attribute] = exp[3][1]
      end
    elsif include_rails_config? exp
      options = get_rails_config exp
      level = @tracker.config[:rails]
      options[0..-2].each do |o|
        level[o] ||= {}
        level = level[o]
      end

      level[options.last] = exp[3][1]
    end

    exp
  end

  #Check for Rails version
  def process_cdecl exp
    #Set Rails version required
    if exp[1] == :RAILS_GEM_VERSION
      @tracker.config[:rails_version] = exp[2][1]
    end

    exp
  end

  #Check if an expression includes a call to set Rails config
  def include_rails_config? exp
    target = exp[1]
    if call? target
J
Justin Collins 已提交
86
      if target[1] == Brakeman::RAILS_CONFIG
87 88 89 90
        true
      else
        include_rails_config? target
      end
J
Justin Collins 已提交
91
    elsif target == Brakeman::RAILS_CONFIG
92 93 94 95 96 97 98 99 100 101 102 103 104 105
      true
    else
      false
    end
  end

  #Returns an array of symbols for each 'level' in the config
  #
  #  config.action_controller.session_store = :cookie
  #
  #becomes
  #
  #  [:action_controller, :session_store]
  def get_rails_config exp
106
    if node_type? exp, :attrasgn
107 108 109
      attribute = exp[2].to_s[0..-2].to_sym
      get_rails_config(exp[1]) << attribute
    elsif call? exp
J
Justin Collins 已提交
110
      if exp[1] == Brakeman::RAILS_CONFIG
111 112 113 114 115 116 117 118 119 120 121
        [exp[2]]
      else
        get_rails_config(exp[1]) << exp[2]
      end
    else
      raise "WHAT"
    end
  end
end

#This is necessary to replace block variable so we can track config settings
J
Justin Collins 已提交
122
class Brakeman::ConfigAliasProcessor < Brakeman::AliasProcessor
123 124 125 126 127 128 129 130 131

  RAILS_INIT = Sexp.new(:colon2, Sexp.new(:const, :Rails), :Initializer)

  #Look for a call to 
  #
  #  Rails::Initializer.run do |config|
  #    ...
  #  end
  #
J
Justin Collins 已提交
132
  #and replace config with Brakeman::RAILS_CONFIG
133 134 135 136 137
  def process_iter exp
    target = exp[1][1]
    method = exp[1][2]

    if sexp? target and target == RAILS_INIT and method == :run
J
Justin Collins 已提交
138
      exp[2][2] = Brakeman::RAILS_CONFIG
139 140 141 142 143
    end

    process_default exp
  end
end