template_processor.rb 1.6 KB
Newer Older
J
Justin Collins 已提交
1
require 'brakeman/processors/base_processor'
J
Justin 已提交
2 3

#Base Processor for templates/views
J
Justin Collins 已提交
4
class Brakeman::TemplateProcessor < Brakeman::BaseProcessor
J
Justin 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  #Initializes template information.
  def initialize tracker, template_name, called_from = nil, file_name = nil
    super(tracker) 
    @current_template = { :name => template_name,
                          :caller => called_from,
                          :partial => template_name.to_s[0,1] == "_",
                          :outputs => [],
                          :src => nil, #set in Processor
                          :type => nil, #set in Processor
                          :file => file_name } 
    if called_from
      template_name = (template_name.to_s + "." + called_from.to_s).to_sym
    end

    tracker.templates[template_name] = @current_template

    @inside_concat = false
  end

  #Process the template Sexp.
  def process exp
    begin
      super
    rescue Exception => e
      except = e.exception("Error when processing #{@current_template[:name]}: #{e.message}")
      except.set_backtrace(e.backtrace)
      raise except
    end
  end

  #Ignore initial variable assignment
  def process_lasgn exp
J
Justin Collins 已提交
38
    if exp.lhs == :_erbout and exp.rhs.node_type == :str  #ignore
J
Justin 已提交
39
      ignore
J
Justin Collins 已提交
40
    elsif exp.lhs == :_buf and exp.rhs.node_type == :str
J
Justin 已提交
41 42
      ignore
    else
J
Justin Collins 已提交
43
      exp.rhs = process exp.rhs
J
Justin 已提交
44 45 46 47 48 49
      exp
    end
  end

  #Adds output to the list of outputs.
  def process_output exp
50
    exp.value = process exp.value
51
    @current_template[:outputs] << exp unless exp.original_line
J
Justin 已提交
52 53
    exp
  end
54 55 56 57

  def process_escaped_output exp
    process_output exp
  end
J
Justin 已提交
58
end