contrast-agent 4.1.0 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -0
  3. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +22 -10
  4. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +4 -3
  5. data/lib/contrast/agent.rb +5 -1
  6. data/lib/contrast/agent/assess.rb +0 -9
  7. data/lib/contrast/agent/assess/contrast_event.rb +49 -132
  8. data/lib/contrast/agent/assess/contrast_object.rb +54 -0
  9. data/lib/contrast/agent/assess/events/source_event.rb +4 -9
  10. data/lib/contrast/agent/assess/finalizers/hash.rb +7 -0
  11. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +17 -3
  12. data/lib/contrast/agent/assess/policy/patcher.rb +4 -3
  13. data/lib/contrast/agent/assess/policy/policy_node.rb +31 -59
  14. data/lib/contrast/agent/assess/policy/preshift.rb +3 -3
  15. data/lib/contrast/agent/assess/policy/propagation_method.rb +41 -32
  16. data/lib/contrast/agent/assess/policy/propagation_node.rb +12 -24
  17. data/lib/contrast/agent/assess/policy/propagator/append.rb +29 -15
  18. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -2
  19. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  20. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +21 -18
  21. data/lib/contrast/agent/assess/policy/propagator/insert.rb +1 -2
  22. data/lib/contrast/agent/assess/policy/propagator/keep.rb +1 -2
  23. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +3 -2
  24. data/lib/contrast/agent/assess/policy/propagator/next.rb +1 -2
  25. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +1 -2
  26. data/lib/contrast/agent/assess/policy/propagator/remove.rb +2 -4
  27. data/lib/contrast/agent/assess/policy/propagator/replace.rb +1 -2
  28. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +1 -2
  29. data/lib/contrast/agent/assess/policy/propagator/select.rb +3 -4
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +25 -17
  31. data/lib/contrast/agent/assess/policy/propagator/split.rb +83 -120
  32. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +41 -25
  33. data/lib/contrast/agent/assess/policy/propagator/trim.rb +3 -7
  34. data/lib/contrast/agent/assess/policy/source_method.rb +2 -14
  35. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +5 -8
  36. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/trigger_method.rb +13 -8
  38. data/lib/contrast/agent/assess/policy/trigger_node.rb +28 -7
  39. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +59 -0
  40. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -3
  41. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +6 -4
  42. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +2 -4
  43. data/lib/contrast/agent/assess/properties.rb +0 -2
  44. data/lib/contrast/agent/assess/property/tagged.rb +56 -32
  45. data/lib/contrast/agent/assess/tracker.rb +16 -18
  46. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +7 -0
  47. data/lib/contrast/agent/middleware.rb +134 -55
  48. data/lib/contrast/agent/patching/policy/method_policy.rb +1 -1
  49. data/lib/contrast/agent/patching/policy/patch.rb +6 -0
  50. data/lib/contrast/agent/patching/policy/patch_status.rb +1 -1
  51. data/lib/contrast/agent/patching/policy/patcher.rb +51 -44
  52. data/lib/contrast/agent/patching/policy/trigger_node.rb +5 -2
  53. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +47 -1
  54. data/lib/contrast/agent/protect/policy/rule_applicator.rb +53 -0
  55. data/lib/contrast/agent/protect/rule/base.rb +63 -14
  56. data/lib/contrast/agent/protect/rule/cmd_injection.rb +12 -28
  57. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -4
  58. data/lib/contrast/agent/protect/rule/deserialization.rb +4 -1
  59. data/lib/contrast/agent/protect/rule/no_sqli.rb +3 -3
  60. data/lib/contrast/agent/protect/rule/sqli.rb +20 -14
  61. data/lib/contrast/agent/protect/rule/xxe.rb +32 -11
  62. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +10 -6
  63. data/lib/contrast/agent/reaction_processor.rb +1 -1
  64. data/lib/contrast/agent/request_context.rb +12 -0
  65. data/lib/contrast/agent/response.rb +5 -5
  66. data/lib/contrast/agent/rewriter.rb +3 -3
  67. data/lib/contrast/agent/scope.rb +81 -55
  68. data/lib/contrast/agent/static_analysis.rb +13 -7
  69. data/lib/contrast/agent/thread.rb +1 -1
  70. data/lib/contrast/agent/thread_watcher.rb +20 -5
  71. data/lib/contrast/agent/version.rb +1 -1
  72. data/lib/contrast/api/communication/messaging_queue.rb +18 -21
  73. data/lib/contrast/api/communication/response_processor.rb +8 -1
  74. data/lib/contrast/api/communication/socket_client.rb +22 -14
  75. data/lib/contrast/api/decorators.rb +2 -0
  76. data/lib/contrast/api/decorators/agent_startup.rb +58 -0
  77. data/lib/contrast/api/decorators/application_startup.rb +51 -0
  78. data/lib/contrast/api/decorators/library.rb +1 -0
  79. data/lib/contrast/api/decorators/library_usage_update.rb +1 -0
  80. data/lib/contrast/api/decorators/route_coverage.rb +15 -5
  81. data/lib/contrast/api/decorators/trace_event.rb +58 -42
  82. data/lib/contrast/api/decorators/trace_event_object.rb +11 -3
  83. data/lib/contrast/api/decorators/trace_event_signature.rb +27 -5
  84. data/lib/contrast/api/decorators/user_input.rb +2 -1
  85. data/lib/contrast/common_agent_configuration.rb +2 -1
  86. data/lib/contrast/components/agent.rb +2 -0
  87. data/lib/contrast/components/app_context.rb +4 -22
  88. data/lib/contrast/components/assess.rb +36 -0
  89. data/lib/contrast/components/interface.rb +5 -3
  90. data/lib/contrast/components/sampling.rb +48 -6
  91. data/lib/contrast/components/scope.rb +72 -6
  92. data/lib/contrast/components/settings.rb +11 -7
  93. data/lib/contrast/config/assess_configuration.rb +2 -1
  94. data/lib/contrast/extension/assess/array.rb +2 -3
  95. data/lib/contrast/extension/assess/erb.rb +1 -3
  96. data/lib/contrast/extension/assess/exec_trigger.rb +1 -4
  97. data/lib/contrast/extension/assess/fiber.rb +2 -3
  98. data/lib/contrast/extension/assess/hash.rb +4 -2
  99. data/lib/contrast/extension/assess/kernel.rb +1 -2
  100. data/lib/contrast/extension/assess/marshal.rb +34 -26
  101. data/lib/contrast/extension/assess/regexp.rb +3 -8
  102. data/lib/contrast/extension/assess/string.rb +1 -2
  103. data/lib/contrast/framework/base_support.rb +51 -53
  104. data/lib/contrast/framework/manager.rb +16 -14
  105. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  106. data/lib/contrast/framework/rack/support.rb +2 -1
  107. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -1
  108. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  109. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +1 -1
  110. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +1 -1
  111. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +1 -1
  112. data/lib/contrast/framework/rails/support.rb +44 -44
  113. data/lib/contrast/framework/sinatra/support.rb +102 -42
  114. data/lib/contrast/logger/application.rb +0 -3
  115. data/lib/contrast/logger/log.rb +31 -15
  116. data/lib/contrast/utils/class_util.rb +3 -1
  117. data/lib/contrast/utils/duck_utils.rb +1 -1
  118. data/lib/contrast/utils/heap_dump_util.rb +103 -87
  119. data/lib/contrast/utils/invalid_configuration_util.rb +21 -12
  120. data/lib/contrast/utils/object_share.rb +3 -3
  121. data/lib/contrast/utils/preflight_util.rb +1 -1
  122. data/lib/contrast/utils/resource_loader.rb +1 -1
  123. data/lib/contrast/utils/sha256_builder.rb +2 -2
  124. data/lib/contrast/utils/string_utils.rb +1 -1
  125. data/lib/contrast/utils/tag_util.rb +9 -13
  126. data/resources/assess/policy.json +12 -18
  127. data/resources/deadzone/policy.json +156 -0
  128. data/resources/protect/policy.json +12 -0
  129. data/ruby-agent.gemspec +61 -19
  130. data/service_executables/VERSION +1 -1
  131. data/service_executables/linux/contrast-service +0 -0
  132. data/service_executables/mac/contrast-service +0 -0
  133. metadata +126 -113
  134. data/lib/contrast/agent/assess/rule.rb +0 -18
  135. data/lib/contrast/agent/assess/rule/base.rb +0 -52
  136. data/lib/contrast/agent/assess/rule/redos.rb +0 -67
  137. data/lib/contrast/framework/sinatra/patch/base.rb +0 -83
  138. data/lib/contrast/framework/sinatra/patch/support.rb +0 -27
  139. data/lib/contrast/utils/prevent_serialization.rb +0 -52
@@ -1,7 +1,6 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/agent/at_exit_hook'
5
4
  require 'contrast/agent/protect/rule/base_service'
6
5
  require 'contrast/utils/stack_trace_utils'
7
6
  require 'contrast/utils/object_share'
@@ -24,10 +23,10 @@ module Contrast
24
23
  end
25
24
 
26
25
  def infilter context, classname, method, command
27
- return nil unless infilter?(context)
26
+ return unless infilter?(context)
28
27
 
29
28
  ia_results = gather_ia_results(context)
30
- return nil if ia_results.empty?
29
+ return if ia_results.empty?
31
30
 
32
31
  if APP_CONTEXT.in_new_process?
33
32
  logger.trace('Running cmd-injection infilter within new process - creating new context')
@@ -37,7 +36,7 @@ module Contrast
37
36
 
38
37
  result = find_attacker_with_results(context, command, ia_results, **{ classname: classname, method: method })
39
38
  result ||= report_command_execution(context, command, **{ classname: classname, method: method })
40
- return nil unless result
39
+ return unless result
41
40
 
42
41
  append_to_activity(context, result)
43
42
  return unless blocked?
@@ -45,11 +44,6 @@ module Contrast
45
44
  raise Contrast::SecurityException.new(
46
45
  self,
47
46
  "Command Injection rule triggered. Call to #{ classname }.#{ method } blocked.")
48
- ensure
49
- # Kernel#exec replaces the current process and does not go through
50
- # at_exit hooks. Kernel#` runs as a subshell - messages appended
51
- # here do not seem to be present in the original process.
52
- Contrast::Agent::AtExitHook.on_exit if %i[exec `].include?(method.to_sym)
53
47
  end
54
48
 
55
49
  def build_attack_with_match context, input_analysis_result, result, candidate_string, **kwargs
@@ -104,37 +98,27 @@ module Contrast
104
98
 
105
99
  def report_command_execution context, command, **kwargs
106
100
  return unless report_any_command_execution?
107
- return nil if protect_excluded_by_code?
101
+ return if protect_excluded_by_code?
108
102
 
109
103
  build_attack_with_match(context, nil, nil, command, **kwargs)
110
104
  end
111
105
 
112
106
  def find_probable_attacker context, potential_attack_string, ia_results, **kwargs
113
- result = nil
114
- if chained_command?(potential_attack_string) # this is probably an attack
115
- most_likely = nil
116
- ia_results.each do |input_analysis_result|
117
- next unless chained_command?(input_analysis_result.value)
118
-
119
- most_likely = input_analysis_result
120
- break
121
- end
122
- end
123
- return result unless most_likely
107
+ return unless chained_command?(potential_attack_string)
108
+
109
+ likely_attacker = ia_results.find { |input_analysis_result| chained_command?(input_analysis_result.value) }
110
+ return unless likely_attacker
124
111
 
125
- result ||= build_attack_with_match(
112
+ build_attack_with_match(
126
113
  context,
127
- most_likely,
128
- result,
114
+ likely_attacker,
115
+ nil,
129
116
  potential_attack_string,
130
117
  **kwargs)
131
- result
132
118
  end
133
119
 
134
120
  def chained_command? command
135
- return true if CHAINED_COMMAND_CHARS.match(command)
136
-
137
- false
121
+ CHAINED_COMMAND_CHARS.match(command)
138
122
  end
139
123
 
140
124
  # Part of the Hardening for Command Injection detection is the
@@ -123,10 +123,7 @@ class Contrast::Agent::Protect::Rule::DefaultScanner # rubocop:disable Style/Cla
123
123
  elsif start_block_comment?(char, index, query)
124
124
  boundaries << index
125
125
  :STATE_INSIDE_BLOCK_COMMENT
126
- elsif operator?(char)
127
- boundaries << index
128
- :STATE_EXPECTING_TOKEN
129
- elsif char.match?(Contrast::Utils::ObjectShare::WHITE_SPACE_REGEXP)
126
+ elsif operator?(char) || char.match?(Contrast::Utils::ObjectShare::WHITE_SPACE_REGEXP)
130
127
  boundaries << index
131
128
  :STATE_EXPECTING_TOKEN
132
129
  else
@@ -17,19 +17,22 @@ module Contrast
17
17
  BLOCK_MESSAGE = 'Untrusted Deserialization rule triggered. Deserialization blocked.'
18
18
 
19
19
  # Gadgets that map to ERB modules
20
- ERB_GADGETS = %w[
20
+ ERB_GADGETS = %W[
21
21
  object:ERB
22
+ o:\bERB
22
23
  ].cs__freeze
23
24
 
24
25
  # Gadgets that map to ActionDispatch modules
25
26
  ACTION_DISPATCH_GADGETS = %w[
26
27
  object:ActionDispatch::Routing::RouteSet::NamedRouteCollection
28
+ o:\bActionDispatch::Routing::RouteSet::NamedRouteCollection
27
29
  ].cs__freeze
28
30
 
29
31
  # Gadgets that map to Arel Modules
30
32
  AREL_GADGETS = %w[
31
33
  string:Arel::Nodes::SqlLiteral
32
34
  object:Arel::Nodes
35
+ o:\bArel::Nodes
33
36
  ].cs__freeze
34
37
 
35
38
  # Used to indicate to TeamServer the gadget is an ERB module
@@ -21,10 +21,10 @@ module Contrast
21
21
  end
22
22
 
23
23
  def infilter context, database, query_string
24
- return nil unless infilter?(context)
24
+ return unless infilter?(context)
25
25
 
26
26
  result = find_attacker(context, query_string, database: database)
27
- return nil unless result
27
+ return unless result
28
28
 
29
29
  append_to_activity(context, result)
30
30
 
@@ -36,7 +36,7 @@ module Contrast
36
36
 
37
37
  attack_string = input_analysis_result.value
38
38
  regexp = Regexp.new(Regexp.escape(attack_string), Regexp::IGNORECASE)
39
- return nil unless query_string.match?(regexp)
39
+ return unless query_string.match?(regexp)
40
40
 
41
41
  scanner = select_scanner
42
42
  ss = StringScanner.new(query_string)
@@ -22,10 +22,10 @@ module Contrast
22
22
  end
23
23
 
24
24
  def infilter context, database, query_string
25
- return nil unless infilter?(context)
25
+ return unless infilter?(context)
26
26
 
27
27
  result = find_attacker(context, query_string, database: database)
28
- return nil unless result
28
+ return unless result
29
29
 
30
30
  append_to_activity(context, result)
31
31
 
@@ -36,7 +36,7 @@ module Contrast
36
36
  attack_string = input_analysis_result.value
37
37
  regexp = Regexp.new(Regexp.escape(attack_string), Regexp::IGNORECASE)
38
38
 
39
- return nil unless query_string.match?(regexp)
39
+ return unless query_string.match?(regexp)
40
40
 
41
41
  database = kwargs[:database]
42
42
  scanner = select_scanner(database)
@@ -50,16 +50,9 @@ module Contrast
50
50
  last_boundary, boundary = scanner.crosses_boundary(query_string, idx, input_analysis_result.value)
51
51
  next unless last_boundary && boundary
52
52
 
53
- input_analysis_result.attack_count = input_analysis_result.attack_count + 1
54
-
55
- kwargs[:start_idx] = idx
56
- kwargs[:end_idx] = idx + length
57
- kwargs[:boundary_overrun_idx] = boundary
58
- kwargs[:input_boundary_idx] = last_boundary
59
-
60
53
  result ||= build_attack_result(context)
61
- update_successful_attack_response(context, input_analysis_result, result, query_string)
62
- append_sample(context, input_analysis_result, result, query_string, **kwargs)
54
+ record_match(idx, length, boundary, last_boundary, kwargs)
55
+ append_match(context, input_analysis_result, result, query_string, kwargs)
63
56
  end
64
57
 
65
58
  result
@@ -75,13 +68,26 @@ module Contrast
75
68
  sample.sqli.query = Contrast::Utils::StringUtils.protobuf_safe_string(candidate_string)
76
69
  sample.sqli.start_idx = sample.sqli.query.index(input).to_i
77
70
  sample.sqli.end_idx = sample.sqli.start_idx + input.length
78
- sample.sqli.boundary_overrun_idx = kwargs[:boundary].to_i
79
- sample.sqli.input_boundary_idx = kwargs[:last_boundary].to_i
71
+ sample.sqli.boundary_overrun_idx = kwargs[:boundary_overrun_idx].to_i
72
+ sample.sqli.input_boundary_idx = kwargs[:input_boundary_idx].to_i
80
73
  sample
81
74
  end
82
75
 
83
76
  private
84
77
 
78
+ def record_match idx, length, boundary, last_boundary, kwargs
79
+ kwargs[:start_idx] = idx
80
+ kwargs[:end_idx] = idx + length
81
+ kwargs[:boundary_overrun_idx] = boundary
82
+ kwargs[:input_boundary_idx] = last_boundary
83
+ end
84
+
85
+ def append_match context, input_analysis_result, result, query_string, **kwargs
86
+ input_analysis_result.attack_count = input_analysis_result.attack_count + 1
87
+ update_successful_attack_response(context, input_analysis_result, result, query_string)
88
+ append_sample(context, input_analysis_result, result, query_string, **kwargs)
89
+ end
90
+
85
91
  def select_scanner database
86
92
  @sql_scanners ||= {
87
93
  Contrast::Agent::Protect::Policy::AppliesSqliRule::DATABASE_MYSQL =>
@@ -19,7 +19,9 @@ module Contrast
19
19
  NAME
20
20
  end
21
21
 
22
- # Given an xml, evaluate it for an XXE attack.
22
+ # Given an xml, evaluate it for an XXE attack. There's no return here
23
+ # as this method handles appending the evaluation to the request
24
+ # context, connecting it to the reporting mechanism at request end.
23
25
  #
24
26
  # @param context [Contrast::Agent::RequestContext] the context of the
25
27
  # request in which this input is evaluated.
@@ -29,7 +31,7 @@ module Contrast
29
31
  # attack is found and the rule is in block mode.
30
32
  def infilter context, framework, xml
31
33
  result = find_attacker(context, xml, framework: framework)
32
- return nil unless result
34
+ return unless result
33
35
 
34
36
  append_to_activity(context, result)
35
37
  return unless blocked?
@@ -39,16 +41,23 @@ module Contrast
39
41
 
40
42
  protected
41
43
 
44
+ # Given an XML, find any externally resolved entities and create an
45
+ # Attack Result for them
46
+ #
47
+ # @param context [Contrast::Agent::RequestContext] the context of the
48
+ # request in which this input is evaluated.
49
+ # @param xml [String] the literal value of the XML being checked for
50
+ # external entity resolution
51
+ # @param _kwargs [Hash]
52
+ # @return [Contrast::Api::Dtm::AttackResult, nil] the determination
53
+ # as to whether or not this XML has an XXE attack in it.
42
54
  def find_attacker context, xml, **_kwargs
43
- return nil unless xml
44
- return nil if protect_excluded_by_code?
55
+ return unless xml
56
+ return if protect_excluded_by_code?
45
57
 
46
- xxe_details, last_idx = build_details(xml)
47
- return nil unless xxe_details
58
+ xxe_details = build_details(xml)
59
+ return unless xxe_details
48
60
 
49
- # For our definition, the prolog goes from the start of the XML
50
- # string to the end of the last entity declaration.
51
- xxe_details.xml = Contrast::Utils::StringUtils.protobuf_safe_string(xml[0, last_idx])
52
61
  ia_result = build_evaluation(xxe_details.xml)
53
62
  build_attack_with_match(
54
63
  context,
@@ -58,7 +67,15 @@ module Contrast
58
67
  details: xxe_details)
59
68
  end
60
69
 
61
- def build_details xml, _evaluation = nil
70
+ # Given an XML determined to be unsafe, build out the details of the
71
+ # attack. The details will include a substring of the given XML up to
72
+ # the end of the prolog, where the external entities are declared.
73
+ #
74
+ # @param xml [String] the literal value of the XML being checked for
75
+ # external entity resolution
76
+ # @return [Contrast::Api::Dtm::XxeDetails] The details of
77
+ # the XXE attack and the index of the last entity discovered
78
+ def build_details xml
62
79
  last_idx = 0
63
80
  ss = StringScanner.new(xml)
64
81
  while ss.scan_until(EXTERNAL_ENTITY_PATTERN)
@@ -70,7 +87,11 @@ module Contrast
70
87
  xxe_details.declared_entities << build_match(ss)
71
88
  xxe_details.entities_resolved << build_wrapper(entity_wrapper)
72
89
  end
73
- [xxe_details, last_idx]
90
+ # For our definition, the prolog goes from the start of the XML
91
+ # string to the end of the last entity declaration.
92
+ xxe_details.xml = Contrast::Utils::StringUtils.protobuf_safe_string(xml[0, last_idx]) if xxe_details
93
+
94
+ xxe_details
74
95
  end
75
96
 
76
97
  def build_sample context, ia_result, _url, **kwargs
@@ -18,12 +18,16 @@ module Contrast
18
18
  end
19
19
 
20
20
  def external_entity?
21
- @_external_entity ||= begin
22
- return external_id?(@system_id) if @system_id
23
- return external_id?(@public_id) if @public_id
24
-
25
- false
21
+ if @_external_entity.nil?
22
+ @_external_entity ||= if @system_id
23
+ external_id?(@system_id)
24
+ elsif @public_id
25
+ external_id?(@public_id)
26
+ else
27
+ false
28
+ end
26
29
  end
30
+ @_external_entity
27
31
  end
28
32
 
29
33
  # <!ENTITY name SYSTEM "URI">
@@ -48,7 +52,7 @@ module Contrast
48
52
  UP_DIR_LINUX = '../'
49
53
  UP_DIR_WIN = '..\\'
50
54
  # we only use this against lowercase strings, removed A-Z for speed
51
- FILE_PATTERN_WINDOWS = /^[\\\\]*[a-z]{1,3}:.*/.cs__freeze
55
+ FILE_PATTERN_WINDOWS = /^\\*[a-z]{1,3}:.*/.cs__freeze
52
56
  def external_id? entity_id
53
57
  return false unless entity_id
54
58
 
@@ -22,7 +22,7 @@ module Contrast
22
22
  # @param application_settings [Contrast::Api::Settings::ApplicationSettings]
23
23
  # those settings which the Service has relayed from TeamServer.
24
24
  def self.process application_settings
25
- return nil unless application_settings&.reactions&.any?
25
+ return unless application_settings&.reactions&.any?
26
26
 
27
27
  application_settings.reactions.each do |reaction|
28
28
  # the enums are all uppercase, we need to downcase them before attempting to log
@@ -12,6 +12,18 @@ module Contrast
12
12
  # This class acts to encapsulate information about the currently executed
13
13
  # request, making it available to the Agent for the duration of the request
14
14
  # in a standardized and normalized format which the Agent understands.
15
+ #
16
+ # @attr_reader timer [Contrast::Utils::Timer] when the context was created
17
+ # @attr_reader logging_hash [Hash] context used to log the request
18
+ # @attr_reader speedracer_input_analysis [Contrast::Api::Settings::InputAnalysis] the protect input analysis of
19
+ # sources on this request
20
+ # @attr_reader request [Contrast::Agent::Request] our wrapper around the Rack::Request for this context
21
+ # @attr_reader response [Contrast::Agent::Response] our wrapper aroudn the Rack::Response or Array for this context,
22
+ # only available after the application has finished its processing
23
+ # @attr_reader activity [Contrast::Api::Dtm::Activity] the application activity found in this request
24
+ # @attr_reader server_activity [Contrast::Api::Dtm::ServerActivity] the server activity found in this request
25
+ # @attr_reader route [Contrast::Api::Dtm::RouteCoverage] the route, used for findings, of this request
26
+ # @attr_reader observed_route [Contrast::Api::Dtm::ObservedRoute] the route, used for coverage, of this request
15
27
  class RequestContext
16
28
  include Contrast::Components::Interface
17
29
  access_component :agent, :analysis, :logging, :scope
@@ -118,16 +118,16 @@ module Contrast
118
118
  # holds, wraps, or is the body of the Response
119
119
  # @return [nil, String] the content of the body
120
120
  def extract_body body
121
- return nil unless body
121
+ return unless body
122
122
 
123
123
  if defined?(Rack::File) && body.is_a?(Rack::File)
124
124
  # not sure what to do in this situation, so don't do anything.
125
125
  nil
126
126
  elsif body.is_a?(Rack::BodyProxy)
127
127
  handle_rack_body_proxy(body)
128
- elsif defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)
129
- extract_body(body.body)
130
- elsif body.is_a?(Rack::Response)
128
+ elsif (defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)) ||
129
+ body.is_a?(Rack::Response)
130
+
131
131
  extract_body(body.body)
132
132
  elsif Contrast::Utils::DuckUtils.quacks_to?(body, :each)
133
133
  acc = []
@@ -152,7 +152,7 @@ module Contrast
152
152
  end
153
153
 
154
154
  def read_or_string obj
155
- return nil unless obj
155
+ return unless obj
156
156
 
157
157
  if Contrast::Utils::DuckUtils.quacks_to?(obj, :read)
158
158
  tmp = obj.read
@@ -96,11 +96,11 @@ module Contrast
96
96
  return unless method_exists?(clazz, method_name, type)
97
97
 
98
98
  method_instance = method_instance(clazz, method_name, type)
99
- return nil if method_instance.nil?
99
+ return if method_instance.nil?
100
100
 
101
101
  location = method_instance.source_location
102
- return nil unless location_available?(location)
103
- return nil if opener.written_from_location?(location)
102
+ return unless location_available?(location)
103
+ return if opener.written_from_location?(location)
104
104
 
105
105
  opener.written_from_location!(location)
106
106
  opener.source_code(location, method_name)
@@ -15,67 +15,93 @@ module Contrast
15
15
  # Instead, we should say "If I'm already doing Contrast things, don't track
16
16
  # this"
17
17
  class Scope
18
- # The following %i[] list is the authoritative list
19
- # of scopes. If you define a new symbol here, you'll
20
- # get scope methods:
21
- # %i[monkey] ->
22
- # enter_monkey_scope!
23
- # exit_monkey_scope!
24
- # in_monkey_scope?
25
- # with_monkey_scope { special_monkey_function }
26
- SCOPE_LIST = %i[contrast deserialization].cs__freeze
27
-
28
- iv_list = SCOPE_LIST.map { |name| :"@#{ name }_scope" }
29
- define_method 'initialize' do
30
- iv_list.each do |iv_sym|
31
- instance_variable_set(iv_sym, 0)
32
- end
18
+ SCOPE_LIST = %i[contrast deserialization split].cs__freeze
19
+
20
+ def initialize
21
+ @contrast_scope = 0
22
+ @deserialization_scope = 0
23
+ @split_scope = 0
33
24
  end
34
25
 
35
- SCOPE_LIST.each do |name|
36
- iv_sym = :"@#{ name }_scope"
26
+ def in_contrast_scope?
27
+ @contrast_scope.positive?
28
+ end
37
29
 
38
- define_method "in_#{ name }_scope?" do
39
- instance_variable_get(iv_sym).positive?
40
- end
30
+ def in_deserialization_scope?
31
+ @deserialization_scope.positive?
32
+ end
41
33
 
42
- enter_method_sym = :"enter_#{ name }_scope!"
43
- define_method enter_method_sym do
44
- level = instance_variable_get(iv_sym)
45
- instance_variable_set(iv_sym, level + 1)
46
- end
34
+ def in_split_scope?
35
+ @split_scope.positive?
36
+ end
47
37
 
48
- exit_method_sym = :"exit_#{ name }_scope!"
49
- define_method exit_method_sym do
50
- # by design, can go below zero.
51
- # every exit/enter pair (regardless of series)
52
- # should cancel each other out.
53
- #
54
- # so we prefer this sequence:
55
- # scope = 0
56
- # exit = -1
57
- # enter = 0
58
- # enter = 1
59
- # exit = 0
60
- # scope = 0
61
- #
62
- # over this sequence:
63
- # scope = 0
64
- # exit = 0
65
- # enter = 1
66
- # enter = 2
67
- # exit = 1
68
- # scope = 1
69
- level = instance_variable_get(iv_sym)
70
- instance_variable_set(iv_sym, level - 1)
71
- end
38
+ def enter_contrast_scope!
39
+ @contrast_scope += 1
40
+ end
72
41
 
73
- define_method "with_#{ name }_scope" do |*_args, &block|
74
- send enter_method_sym
75
- block.call
76
- ensure
77
- send exit_method_sym
78
- end
42
+ def enter_deserialization_scope!
43
+ @deserialization_scope += 1
44
+ end
45
+
46
+ def enter_split_scope!
47
+ @split_scope += 1
48
+ end
49
+
50
+ def split_scope_depth
51
+ @split_scope
52
+ end
53
+
54
+ # Scope Exits...
55
+ # by design, can go below zero.
56
+ # every exit/enter pair (regardless of series)
57
+ # should cancel each other out.
58
+ #
59
+ # so we prefer this sequence:
60
+ # scope = 0
61
+ # exit = -1
62
+ # enter = 0
63
+ # enter = 1
64
+ # exit = 0
65
+ # scope = 0
66
+ #
67
+ # over this sequence:
68
+ # scope = 0
69
+ # exit = 0
70
+ # enter = 1
71
+ # enter = 2
72
+ # exit = 1
73
+ # scope = 1
74
+ def exit_contrast_scope!
75
+ @contrast_scope -= 1
76
+ end
77
+
78
+ def exit_deserialization_scope!
79
+ @deserialization_scope -= 1
80
+ end
81
+
82
+ def exit_split_scope!
83
+ @split_scope -= 1
84
+ end
85
+
86
+ def with_contrast_scope
87
+ enter_contrast_scope!
88
+ yield
89
+ ensure
90
+ exit_contrast_scope!
91
+ end
92
+
93
+ def with_deserialization_scope
94
+ enter_deserialization_scope!
95
+ yield
96
+ ensure
97
+ exit_deserialization_scope!
98
+ end
99
+
100
+ def with_split_scope
101
+ enter_split_scope!
102
+ yield
103
+ ensure
104
+ exit_split_scope!
79
105
  end
80
106
 
81
107
  # Dynamic versions of the above.