Class: Brakeman::CheckJSONParsing

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

Constant Summary

Constants inherited from BaseCheck

BaseCheck::CONFIDENCE

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

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_call, #process_cookies, #process_default, #process_if, #process_params, #process_string_interp

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

Methods included from ProcessorHelper

#class_name, #process_all, #process_all!, #process_call_args, #process_class, #process_module

Methods inherited from SexpProcessor

#error_handler, #in_context, #initialize, #process, #process_dummy, #scope

Constructor Details

This class inherits a constructor from Brakeman::BaseCheck

Instance Method Details

#check_cve_2013_0269Object


61
62
63
64
65
66
# File 'lib/brakeman/checks/check_json_parsing.rb', line 61

def check_cve_2013_0269
  [:json, :json_pure].each do |name|
    gem_hash = tracker.config[:gems][name] if tracker.config[:gems]
    check_json_version name, gem_hash[:version] if gem_hash and gem_hash[:version]
  end
end

#check_cve_2013_0333Object


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
# File 'lib/brakeman/checks/check_json_parsing.rb', line 13

def check_cve_2013_0333
  return unless version_between? "0.0.0", "2.3.15" or version_between? "3.0.0", "3.0.19"

  unless uses_yajl? or uses_gem_backend?
    new_version = if version_between? "0.0.0", "2.3.14"
                    "2.3.16"
                  elsif version_between? "3.0.0", "3.0.19"
                    "3.0.20"
                  end

    message = "Rails #{tracker.config[:rails_version]} has a serious JSON parsing vulnerability: upgrade to #{new_version} or patch"
    if uses_yajl?
      gem_info = gemfile_or_environment(:yajl)
    else
      gem_info = gemfile_or_environment
    end

    warn :warning_type => "Remote Code Execution",
      :warning_code => :CVE_2013_0333,
      :message => message,
      :confidence => CONFIDENCE[:high],
      :gem_info => gem_info,
      :link_path => "https://groups.google.com/d/topic/rubyonrails-security/1h2DR63ViGo/discussion"
  end
end

#check_json_version(name, version) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/brakeman/checks/check_json_parsing.rb', line 68

def check_json_version name, version
  return if version >= "1.7.7" or
            (version >= "1.6.8" and version < "1.7.0") or
            (version >= "1.5.5" and version < "1.6.0")

  warning_type = "Denial of Service"
  confidence = CONFIDENCE[:med]
  message = "#{name} gem version #{version} has a symbol creation vulnerablity: upgrade to "

  if version >= "1.7.0"
    confidence = CONFIDENCE[:high]
    warning_type = "Remote Code Execution"
    message = "#{name} gem version #{version} has a remote code vulnerablity: upgrade to 1.7.7"
  elsif version >= "1.6.0"
    message << "1.6.8"
  elsif version >= "1.5.0"
    message << "1.5.5"
  else
    confidence = CONFIDENCE[:low]
    message << "1.5.5"
  end

  if confidence == CONFIDENCE[:med] and uses_json_parse?
    confidence = CONFIDENCE[:high]
  end

  warn :warning_type => warning_type,
    :warning_code => :CVE_2013_0269,
    :message => message,
    :confidence => confidence,
    :gem_info => gemfile_or_environment(name),
    :link => "https://groups.google.com/d/topic/rubyonrails-security/4_YvCpLzL58/discussion"
end

#run_checkObject


8
9
10
11
# File 'lib/brakeman/checks/check_json_parsing.rb', line 8

def run_check
  check_cve_2013_0333
  check_cve_2013_0269
end

#uses_gem_backend?Boolean

Check for `ActiveSupport::JSON.backend = “JSONGem”`

Returns:

  • (Boolean)

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/brakeman/checks/check_json_parsing.rb', line 45

def uses_gem_backend?
  matches = tracker.check_initializers(:'ActiveSupport::JSON', :backend=)

  unless matches.empty?
    json_gem = s(:str, "JSONGem")

    matches.each do |result|
      if result.call.first_arg == json_gem
        return true
      end
    end
  end

  false
end

#uses_json_parse?Boolean

Returns:

  • (Boolean)

102
103
104
105
106
# File 'lib/brakeman/checks/check_json_parsing.rb', line 102

def uses_json_parse?
  return @uses_json_parse unless @uses_json_parse.nil?

  not tracker.find_call(:target => :JSON, :method => :parse).empty?
end

#uses_yajl?Boolean

Check if `yajl` is included in Gemfile

Returns:

  • (Boolean)

40
41
42
# File 'lib/brakeman/checks/check_json_parsing.rb', line 40

def uses_yajl?
  tracker.config[:gems][:yajl]
end