Class: Brakeman::CheckYAMLParsing

Inherits:
BaseCheck show all
Defined in:
lib/brakeman/checks/check_yaml_parsing.rb

Constant Summary

Constants inherited from BaseCheck

BaseCheck::CONFIDENCE

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

Constants inherited from SexpProcessor

SexpProcessor::VERSION

Instance Attribute Summary

Attributes inherited from BaseCheck

#tracker, #warnings

Attributes inherited from SexpProcessor

#context, #env, #expected

Instance Method Summary collapse

Methods inherited from BaseCheck

#add_result, inherited, #initialize, #process_array, #process_call, #process_cookies, #process_default, #process_dstr, #process_if, #process_params

Methods included from Messages

#msg, #msg_code, #msg_cve, #msg_file, #msg_input, #msg_lit, #msg_plain, #msg_version

Methods included from Util

#array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #dir_glob?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #kwsplat?, #literal?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #regexp?, #remove_kwsplat, #request_env?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #underscore

Methods included from ProcessorHelper

#current_file, #process_all, #process_all!, #process_call_args, #process_call_defn?, #process_class, #process_module

Methods inherited from SexpProcessor

#in_context, #initialize, #process, processors, #scope

Constructor Details

This class inherits a constructor from Brakeman::BaseCheck

Instance Method Details

#disabled_xml_dangerous_types?Boolean

Returns:

  • (Boolean)

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/brakeman/checks/check_yaml_parsing.rb', line 89

def disabled_xml_dangerous_types?
  if version_between? "0.0.0", "2.3.14"
    matches = tracker.find_call(target: :"ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING", method: :delete)
  else
    matches = tracker.find_call(target: :"ActiveSupport::XmlMini::PARSING", method: :delete)
  end

  symbols_off = false
  yaml_off = false

  matches.each do |result|
    arg = result[:call].first_arg

    if string? arg
      if arg.value == "yaml"
        yaml_off = true
      elsif arg.value == "symbol"
        symbols_off = true
      end
    end
  end

  symbols_off and yaml_off
end

#disabled_xml_parser?Boolean

Returns:

  • (Boolean)

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

def disabled_xml_parser?
  if version_between? "0.0.0", "2.3.14"
    #Look for ActionController::Base.param_parsers.delete(Mime::XML)
    matches = tracker.find_call(target: :"ActionController::Base.param_parsers", method: :delete)
  else
    #Look for ActionDispatch::ParamsParser::DEFAULT_PARSERS.delete(Mime::XML)
    matches = tracker.find_call(target: :"ActionDispatch::ParamsParser::DEFAULT_PARSERS", method: :delete)
  end

  unless matches.empty?
    mime_xml = s(:colon2, s(:const, :Mime), :XML)

    matches.each do |result|
      if result[:call].first_arg == mime_xml
        return true
      end
    end
  end

  false
end

#enabled_yaml_parser?Boolean

Look for ActionController::Base.param_parsers = :yaml in Rails 2.x apps

Returns:

  • (Boolean)

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/brakeman/checks/check_yaml_parsing.rb', line 72

def enabled_yaml_parser?
  matches = tracker.find_call(target: :'ActionController::Base.param_parsers', method: :[]=)

  mime_yaml = s(:colon2, s(:const, :Mime), :YAML)

  matches.each do |result|
    if result[:call].first_arg == mime_yaml and
      symbol? result[:call].second_arg and
      result[:call].second_arg.value == :yaml

      return true
    end
  end

  false
end

#run_checkObject


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
38
39
40
41
42
43
44
45
46
# File 'lib/brakeman/checks/check_yaml_parsing.rb', line 8

def run_check
  return unless version_between? "0.0.0", "2.3.14" or
                version_between? "3.0.0", "3.0.18" or
                version_between? "3.1.0", "3.1.9" or
                version_between? "3.2.0", "3.2.10"

  unless disabled_xml_parser? or disabled_xml_dangerous_types?
    new_version = if version_between? "0.0.0", "2.3.14"
                    "2.3.15"
                  elsif version_between? "3.0.0", "3.0.18"
                    "3.0.19"
                  elsif version_between? "3.1.0", "3.1.9"
                    "3.1.10"
                  elsif version_between? "3.2.0", "3.2.10"
                    "3.2.11"
                  end

    message = msg(msg_version(rails_version), " has a remote code execution vulnerability. Upgrade to ", msg_version(new_version), " or disable XML parsing")

    warn :warning_type => "Remote Code Execution",
      :warning_code => :CVE_2013_0156,
      :message => message,
      :confidence => :high,
      :gem_info => gemfile_or_environment,
      :link_path => "https://groups.google.com/d/topic/rubyonrails-security/61bkgvnSGTQ/discussion"
  end

  #Warn if app accepts YAML
  if version_between?("0.0.0", "2.3.14") and enabled_yaml_parser?
    message = "Parsing YAML request parameters enables remote code execution: disable YAML parser"

    warn :warning_type => "Remote Code Execution",
      :warning_code => :CVE_2013_0156,
      :message => message,
      :confidence => :high,
      :gem_info => gemfile_or_environment,
      :link_path => "https://groups.google.com/d/topic/rubyonrails-security/61bkgvnSGTQ/discussion"
  end
end