Class: Brakeman::TemplateParser

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/brakeman/parsers/template_parser.rb

Defined Under Namespace

Classes: TemplateFile

Constant Summary collapse

KNOWN_TEMPLATE_EXTENSIONS =
/.*\.(erb|haml|rhtml|slim)$/

Constants included from Util

Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION, Util::SESSION_SEXP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

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

Constructor Details

#initialize(tracker, file_parser) ⇒ TemplateParser

Returns a new instance of TemplateParser.


9
10
11
12
13
# File 'lib/brakeman/parsers/template_parser.rb', line 9

def initialize tracker, file_parser
  @tracker = tracker
  @file_parser = file_parser
  @file_parser.file_list[:templates] ||= []
end

Instance Attribute Details

#trackerObject (readonly)

Returns the value of attribute tracker


4
5
6
# File 'lib/brakeman/parsers/template_parser.rb', line 4

def tracker
  @tracker
end

Instance Method Details

#erubis?Boolean

Returns:

  • (Boolean)

68
69
70
71
# File 'lib/brakeman/parsers/template_parser.rb', line 68

def erubis?
  tracker.config[:escape_html] or
  tracker.config[:erubis]
end

#parse_erb(text) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/brakeman/parsers/template_parser.rb', line 48

def parse_erb text
  if tracker.config[:escape_html]
    if tracker.options[:rails3]
      require 'brakeman/parsers/rails3_erubis'
      Brakeman::Rails3Erubis.new(text).src
    else
      require 'brakeman/parsers/rails2_xss_plugin_erubis'
      Brakeman::Rails2XSSPluginErubis.new(text).src
    end
  elsif tracker.config[:erubis]
    require 'brakeman/parsers/rails2_erubis'
    Brakeman::ScannerErubis.new(text).src
  else
    require 'erb'
    src = ERB.new(text, nil, "-").src
    src.sub!(/^#.*\n/, '') if Brakeman::Scanner::RUBY_1_9
    src
  end
end

#parse_haml(text) ⇒ Object


73
74
75
76
77
78
79
# File 'lib/brakeman/parsers/template_parser.rb', line 73

def parse_haml text
  Brakeman.load_brakeman_dependency 'haml'
  Brakeman.load_brakeman_dependency 'sass'

  Haml::Engine.new(text,
                   :escape_html => !!tracker.config[:escape_html]).precompiled
end

#parse_slim(text) ⇒ Object


81
82
83
84
85
86
# File 'lib/brakeman/parsers/template_parser.rb', line 81

def parse_slim text
  Brakeman.load_brakeman_dependency 'slim'

  Slim::Template.new(:disable_capture => true,
                     :generator => Temple::Generators::RailsOutputBuffer) { text }.precompiled_template
end

#parse_template(path, text) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/brakeman/parsers/template_parser.rb', line 15

def parse_template path, text
  type = path.match(KNOWN_TEMPLATE_EXTENSIONS)[1].to_sym
  type = :erb if type == :rhtml
  name = template_path_to_name path

  begin
    src = case type
          when :erb
            type = :erubis if erubis?
            parse_erb text
          when :haml
            parse_haml text
          when :slim
            parse_slim text
          else
            tracker.error "Unknown template type in #{path}"
            nil
          end

    if src and ast = @file_parser.parse_ruby(src, path)
      @file_parser.file_list[:templates] << TemplateFile.new(path, ast, name, type)
    end
  rescue Racc::ParseError => e
    tracker.error e, "could not parse #{path}"
  rescue Haml::Error => e
    tracker.error e, ["While compiling HAML in #{path}"] << e.backtrace
  rescue StandardError, LoadError => e
    tracker.error e.exception(e.message + "\nWhile processing #{path}"), e.backtrace
  end

  nil
end