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_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::DIR_CONST, Util::LITERALS, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_COOKIES, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::REQUEST_REQUEST_PARAMETERS, Util::SAFE_LITERAL, Util::SESSION, Util::SESSION_SEXP, Util::SIMPLE_LITERALS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#all_literals?, #array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #dir_glob?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #hash_values, #integer?, #kwsplat?, #literal?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #recurse_check?, #regexp?, #remove_kwsplat, #request_headers?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #simple_literal?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #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
  @slim_smart = nil # Load slim/smart ?
end

Instance Attribute Details

#tracker ⇒ Object (readonly)

Returns the value of attribute tracker.



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

def tracker
  @tracker
end

Class Method Details

.parse_inline_erb(tracker, text) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/brakeman/parsers/template_parser.rb', line 146

def self.parse_inline_erb tracker, text
  fp = Brakeman::FileParser.new(tracker.app_tree, tracker.options[:parser_timeout])
  tp = self.new(tracker, fp)
  src = tp.parse_erb '_inline_', text
  type = tp.erubi? ? :erubi : :erb

  return type, fp.parse_ruby(src, "_inline_")
end

Instance Method Details

#erubi? ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/brakeman/parsers/template_parser.rb', line 71

def erubi?
  tracker.config.escape_html? or
    tracker.config.erubi?
end

#haml6? ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/brakeman/parsers/template_parser.rb', line 101

def haml6?
  return @haml6 unless @haml6.nil?

  Brakeman.load_brakeman_dependency 'haml'
  major_version = Haml::VERSION.split('.').first.to_i

  if major_version >= 6
    @haml6 = true
  else
    @haml6 = false
  end
end

#load_slim_smart? ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/brakeman/parsers/template_parser.rb', line 131

def load_slim_smart?
  return !@slim_smart unless @slim_smart.nil?

  # Terrible hack to find
  #   gem "slim", "~> 3.0.1", require: ["slim", "slim/smart"]
  if tracker.app_tree.exists? 'Gemfile'
    gemfile_contents = tracker.app_tree.file_path('Gemfile').read
    if gemfile_contents.include? 'slim/smart'
      return true
    end
  end

  false
end

#parse_erb(path, text) ⇒ Object



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

def parse_erb path, text
  if erubi?
    require 'brakeman/parsers/rails_erubi'
    Brakeman::Erubi.new(text, :filename => path).src
  else
    require 'erb'
    src = if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
      ERB.new(text, trim_mode: '-').src
    else
      ERB.new(text, nil, '-').src
    end

    # Encoding comment at the beginning throws off line numbers
    # but the string is frozen in ERB 6.0.2+, so we don't want to
    # unnecessarily duplicate the string
    if src.start_with? '#'
      src.sub(/^#.*\n/, '')
    else
      src
    end
  end
end

#parse_haml(path, text) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/brakeman/parsers/template_parser.rb', line 76

def parse_haml path, text
  if haml6?
    require_relative 'haml6_embedded'

    Haml::Template.new(filename: path.relative,
                       :escape_html => tracker.config.escape_html?,
                       generator: Temple::Generators::RailsOutputBuffer,
                       use_html_safe: true,
                       buffer_class: 'ActionView::OutputBuffer',
                       disable_capture: true,
                      ) { text }.precompiled_template
  else
    require_relative 'haml_embedded'

    Haml::Engine.new(text,
                     :filename => path,
                     :escape_html => tracker.config.escape_html?,
                     :escape_filter_interpolations => tracker.config.escape_filter_interpolations?
                    ).precompiled.gsub(/([^\\])\\n/, '\1')
  end
rescue Haml::Error => e
  tracker.error e, ["While compiling HAML in #{path}"] << e.backtrace
  nil
end

#parse_slim(path, text) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/brakeman/parsers/template_parser.rb', line 114

def parse_slim path, text
  Brakeman.load_brakeman_dependency 'slim'

  if @slim_smart.nil? and load_slim_smart?
    @slim_smart = true
    Brakeman.load_brakeman_dependency 'slim/smart'
  else
    @slim_smart = false
  end

  require_relative 'slim_embedded'

  Slim::Template.new(path,
                     :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.relative.match(KNOWN_TEMPLATE_EXTENSIONS)[1].to_sym
  type = :erb if type == :rhtml
  name = template_path_to_name path
  Brakeman.debug "Parsing #{path}"

  begin
    src = case type
          when :erb
            type = :erubi if erubi?
            parse_erb path, text
          when :haml
            type = :haml6 if haml6?
            parse_haml path, text
          when :slim
            parse_slim path, 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 << TemplateFile.new(path, ast, name, type)
    end
  rescue Racc::ParseError => e
    tracker.error e, "Could not parse #{path}"
  rescue StandardError, LoadError => e
    tracker.error e.exception(e.message + "\nWhile processing #{path}"), e.backtrace
  end

  nil
end