contrast-agent 4.6.0 → 4.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -1
  3. data/.gitmodules +1 -1
  4. data/.simplecov +1 -0
  5. data/Rakefile +1 -2
  6. data/ext/build_funchook.rb +3 -3
  7. data/ext/extconf_common.rb +1 -5
  8. data/lib/contrast.rb +24 -14
  9. data/lib/contrast/agent/assess.rb +1 -1
  10. data/lib/contrast/agent/assess/contrast_event.rb +1 -4
  11. data/lib/contrast/agent/assess/contrast_object.rb +2 -2
  12. data/lib/contrast/agent/assess/events/event_factory.rb +2 -1
  13. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -4
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +6 -3
  15. data/lib/contrast/agent/assess/policy/patcher.rb +16 -21
  16. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  17. data/lib/contrast/agent/assess/policy/policy_node.rb +25 -33
  18. data/lib/contrast/agent/assess/policy/policy_scanner.rb +3 -5
  19. data/lib/contrast/agent/assess/policy/preshift.rb +7 -5
  20. data/lib/contrast/agent/assess/policy/propagation_method.rb +10 -19
  21. data/lib/contrast/agent/assess/policy/propagation_node.rb +19 -8
  22. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  23. data/lib/contrast/agent/assess/policy/propagator/center.rb +2 -1
  24. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +3 -6
  25. data/lib/contrast/agent/assess/policy/propagator/insert.rb +3 -1
  26. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +2 -1
  27. data/lib/contrast/agent/assess/policy/propagator/rack_protection.rb +73 -0
  28. data/lib/contrast/agent/assess/policy/propagator/select.rb +2 -12
  29. data/lib/contrast/agent/assess/policy/propagator/split.rb +12 -13
  30. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +3 -10
  31. data/lib/contrast/agent/assess/policy/propagator/trim.rb +3 -15
  32. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +13 -10
  33. data/lib/contrast/agent/assess/policy/source_method.rb +12 -12
  34. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -3
  35. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +5 -1
  36. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -3
  37. data/lib/contrast/agent/assess/policy/trigger_method.rb +8 -18
  38. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -2
  39. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +4 -3
  40. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +1 -2
  41. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -8
  42. data/lib/contrast/agent/assess/property/evented.rb +8 -5
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +11 -5
  44. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +4 -1
  45. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -9
  46. data/lib/contrast/agent/at_exit_hook.rb +3 -3
  47. data/lib/contrast/agent/class_reopener.rb +9 -6
  48. data/lib/contrast/agent/disable_reaction.rb +4 -7
  49. data/lib/contrast/agent/exclusion_matcher.rb +7 -14
  50. data/lib/contrast/agent/inventory/dependencies.rb +2 -0
  51. data/lib/contrast/agent/inventory/dependency_analysis.rb +2 -6
  52. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +3 -5
  53. data/lib/contrast/agent/inventory/policy/datastores.rb +3 -4
  54. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  55. data/lib/contrast/agent/middleware.rb +17 -18
  56. data/lib/contrast/agent/module_data.rb +3 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -3
  58. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +9 -9
  59. data/lib/contrast/agent/patching/policy/method_policy.rb +6 -2
  60. data/lib/contrast/agent/patching/policy/module_policy.rb +14 -7
  61. data/lib/contrast/agent/patching/policy/patch.rb +20 -25
  62. data/lib/contrast/agent/patching/policy/patch_status.rb +6 -7
  63. data/lib/contrast/agent/patching/policy/patcher.rb +21 -18
  64. data/lib/contrast/agent/patching/policy/policy.rb +2 -4
  65. data/lib/contrast/agent/patching/policy/policy_node.rb +16 -7
  66. data/lib/contrast/agent/patching/policy/trigger_node.rb +21 -8
  67. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +1 -1
  68. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +1 -1
  69. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  70. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -3
  71. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +1 -1
  72. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +5 -9
  73. data/lib/contrast/agent/protect/policy/policy.rb +1 -1
  74. data/lib/contrast/agent/protect/policy/rule_applicator.rb +7 -9
  75. data/lib/contrast/agent/protect/rule/base.rb +20 -23
  76. data/lib/contrast/agent/protect/rule/base_service.rb +9 -5
  77. data/lib/contrast/agent/protect/rule/cmd_injection.rb +18 -23
  78. data/lib/contrast/agent/protect/rule/deserialization.rb +6 -13
  79. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +3 -14
  80. data/lib/contrast/agent/protect/rule/no_sqli.rb +6 -2
  81. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -3
  82. data/lib/contrast/agent/protect/rule/path_traversal.rb +6 -10
  83. data/lib/contrast/agent/protect/rule/sqli.rb +1 -1
  84. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  85. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  86. data/lib/contrast/agent/protect/rule/xxe.rb +5 -12
  87. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +1 -2
  88. data/lib/contrast/agent/reaction_processor.rb +13 -13
  89. data/lib/contrast/agent/request.rb +27 -26
  90. data/lib/contrast/agent/request_context.rb +17 -22
  91. data/lib/contrast/agent/request_handler.rb +5 -3
  92. data/lib/contrast/agent/response.rb +2 -3
  93. data/lib/contrast/agent/rewriter.rb +9 -6
  94. data/lib/contrast/agent/rule_set.rb +5 -4
  95. data/lib/contrast/agent/service_heartbeat.rb +4 -6
  96. data/lib/contrast/agent/static_analysis.rb +6 -5
  97. data/lib/contrast/agent/thread.rb +2 -4
  98. data/lib/contrast/agent/thread_watcher.rb +3 -4
  99. data/lib/contrast/agent/tracepoint_hook.rb +5 -5
  100. data/lib/contrast/agent/version.rb +1 -1
  101. data/lib/contrast/api/communication/messaging_queue.rb +4 -5
  102. data/lib/contrast/api/communication/response_processor.rb +11 -13
  103. data/lib/contrast/api/communication/service_lifecycle.rb +9 -6
  104. data/lib/contrast/api/communication/socket_client.rb +22 -31
  105. data/lib/contrast/api/communication/speedracer.rb +8 -13
  106. data/lib/contrast/api/decorators/address.rb +2 -3
  107. data/lib/contrast/api/decorators/agent_startup.rb +7 -9
  108. data/lib/contrast/api/decorators/application_startup.rb +12 -10
  109. data/lib/contrast/api/decorators/application_update.rb +0 -4
  110. data/lib/contrast/api/decorators/http_request.rb +3 -7
  111. data/lib/contrast/api/decorators/instrumentation_mode.rb +3 -5
  112. data/lib/contrast/api/decorators/library.rb +8 -6
  113. data/lib/contrast/api/decorators/message.rb +9 -9
  114. data/lib/contrast/api/decorators/trace_event.rb +3 -1
  115. data/lib/contrast/api/decorators/trace_event_object.rb +3 -6
  116. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +1 -6
  117. data/lib/contrast/components/agent.rb +17 -17
  118. data/lib/contrast/components/app_context.rb +11 -15
  119. data/lib/contrast/components/assess.rb +16 -16
  120. data/lib/contrast/components/base.rb +40 -0
  121. data/lib/contrast/components/config.rb +2 -3
  122. data/lib/contrast/components/contrast_service.rb +12 -18
  123. data/lib/contrast/components/heap_dump.rb +5 -4
  124. data/lib/contrast/components/inventory.rb +2 -7
  125. data/lib/contrast/components/logger.rb +1 -2
  126. data/lib/contrast/components/protect.rb +10 -13
  127. data/lib/contrast/components/sampling.rb +13 -7
  128. data/lib/contrast/components/scope.rb +0 -4
  129. data/lib/contrast/components/settings.rb +5 -7
  130. data/lib/contrast/config/assess_rules_configuration.rb +1 -3
  131. data/lib/contrast/config/base_configuration.rb +4 -5
  132. data/lib/contrast/config/exception_configuration.rb +1 -5
  133. data/lib/contrast/config/heap_dump_configuration.rb +12 -6
  134. data/lib/contrast/config/logger_configuration.rb +1 -5
  135. data/lib/contrast/configuration.rb +6 -18
  136. data/lib/contrast/extension/assess/array.rb +3 -10
  137. data/lib/contrast/extension/assess/erb.rb +1 -7
  138. data/lib/contrast/extension/assess/eval_trigger.rb +4 -9
  139. data/lib/contrast/extension/assess/exec_trigger.rb +3 -9
  140. data/lib/contrast/extension/assess/fiber.rb +8 -17
  141. data/lib/contrast/extension/assess/hash.rb +3 -3
  142. data/lib/contrast/extension/assess/kernel.rb +4 -13
  143. data/lib/contrast/extension/assess/marshal.rb +6 -10
  144. data/lib/contrast/extension/assess/regexp.rb +6 -10
  145. data/lib/contrast/extension/assess/string.rb +8 -6
  146. data/lib/contrast/extension/kernel.rb +2 -2
  147. data/lib/contrast/extension/protect/kernel.rb +0 -5
  148. data/lib/contrast/framework/manager.rb +3 -5
  149. data/lib/contrast/framework/rack/patch/session_cookie.rb +11 -24
  150. data/lib/contrast/framework/rack/patch/support.rb +6 -4
  151. data/lib/contrast/framework/rails/patch/assess_configuration.rb +12 -9
  152. data/lib/contrast/framework/rails/patch/support.rb +41 -35
  153. data/lib/contrast/framework/rails/railtie.rb +34 -0
  154. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +4 -1
  155. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +2 -0
  156. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +5 -4
  157. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +2 -0
  158. data/lib/contrast/framework/rails/support.rb +2 -2
  159. data/lib/contrast/framework/sinatra/support.rb +3 -1
  160. data/lib/contrast/funchook/funchook.rb +5 -8
  161. data/lib/contrast/logger/application.rb +13 -15
  162. data/lib/contrast/logger/format.rb +2 -5
  163. data/lib/contrast/logger/log.rb +26 -9
  164. data/lib/contrast/logger/request.rb +1 -6
  165. data/lib/contrast/security_exception.rb +1 -1
  166. data/lib/contrast/tasks/config.rb +0 -1
  167. data/lib/contrast/tasks/service.rb +6 -7
  168. data/lib/contrast/utils/assess/sampling_util.rb +2 -3
  169. data/lib/contrast/utils/assess/tracking_util.rb +3 -6
  170. data/lib/contrast/utils/class_util.rb +0 -8
  171. data/lib/contrast/utils/hash_digest.rb +2 -5
  172. data/lib/contrast/utils/heap_dump_util.rb +5 -3
  173. data/lib/contrast/utils/invalid_configuration_util.rb +4 -3
  174. data/lib/contrast/utils/inventory_util.rb +2 -3
  175. data/lib/contrast/utils/io_util.rb +3 -5
  176. data/lib/contrast/utils/job_servers_running.rb +13 -7
  177. data/lib/contrast/utils/os.rb +4 -4
  178. data/lib/contrast/utils/ruby_ast_rewriter.rb +2 -1
  179. data/lib/contrast/utils/string_utils.rb +2 -3
  180. data/lib/contrast/utils/tag_util.rb +25 -19
  181. data/resources/assess/policy.json +55 -0
  182. data/ruby-agent.gemspec +17 -16
  183. data/service_executables/VERSION +1 -1
  184. data/service_executables/linux/contrast-service +0 -0
  185. data/service_executables/mac/contrast-service +0 -0
  186. data/sonar-project.properties +9 -0
  187. metadata +61 -46
  188. data/lib/contrast/agent/railtie.rb +0 -31
  189. data/lib/contrast/components/interface.rb +0 -195
  190. data/lib/contrast/delegators/input_analysis.rb +0 -12
@@ -34,7 +34,7 @@ module Contrast
34
34
 
35
35
  protected
36
36
 
37
- def name
37
+ def rule_name
38
38
  Contrast::Agent::Protect::Rule::Sqli::NAME
39
39
  end
40
40
 
@@ -49,16 +49,15 @@ module Contrast
49
49
 
50
50
  protected
51
51
 
52
- def name
52
+ def rule_name
53
53
  Contrast::Agent::Protect::Rule::Xxe::NAME
54
54
  end
55
55
 
56
56
  private
57
57
 
58
- DATA_KEY = '@data'.to_sym
58
+ DATA_KEY = :@data
59
59
  def valid_data_input? object
60
- object.instance_variable_defined?(DATA_KEY) &&
61
- object.instance_variable_get(DATA_KEY)
60
+ object.instance_variable_defined?(DATA_KEY) && object.instance_variable_get(DATA_KEY)
62
61
  end
63
62
 
64
63
  NOKOGIRI_MARKER = 'Nokogiri::'
@@ -115,11 +114,8 @@ module Contrast
115
114
  raise e
116
115
  rescue StandardError => e
117
116
  parser ||= Contrast::Utils::ObjectShare::UNKNOWN
118
- logger.error(
119
- 'Error applying xxe',
120
- e,
121
- module: potential_parser.cs__class.cs__name,
122
- method: method, parser: parser)
117
+ logger.error('Error applying xxe', e, module: potential_parser.cs__class.cs__name, method: method,
118
+ parser: parser)
123
119
  end
124
120
  end
125
121
  end
@@ -24,7 +24,7 @@ module Contrast
24
24
  end
25
25
 
26
26
  def disabled_globally?
27
- PROTECT.forcibly_disabled?
27
+ ::Contrast::PROTECT.forcibly_disabled?
28
28
  end
29
29
 
30
30
  def node_type
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2021 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/components/interface'
4
+ require 'contrast/components/logger'
5
5
 
6
6
  module Contrast
7
7
  module Agent
@@ -11,9 +11,7 @@ module Contrast
11
11
  # form of the Applicator, which will override specific implementations
12
12
  # in order to properly invoke its Rule.
13
13
  module RuleApplicator
14
- include Contrast::Components::Interface
15
-
16
- access_component :analysis, :logging
14
+ include Contrast::Components::Logger::InstanceMethods
17
15
 
18
16
  # Calls the actual invocation for this applicator, if required. Will
19
17
  # attempt to transform the data as required prior to invocation and
@@ -42,7 +40,8 @@ module Contrast
42
40
  rescue Contrast::SecurityException => e
43
41
  raise e
44
42
  rescue StandardError => e
45
- logger.error('Error applying protect rule', e, module: object.cs__class.cs__name, method: method, rule: name)
43
+ logger.error('Error applying protect rule', e, module: object.cs__class.cs__name, method: method,
44
+ rule: rule_name)
46
45
  end
47
46
 
48
47
  protected
@@ -68,11 +67,10 @@ module Contrast
68
67
  raise NoMethodError, 'This is abstract, override it.'
69
68
  end
70
69
 
71
- # The name of the rule, as expected by the Contrast Service and
72
- # Contrast UI.
70
+ # The name of the rule, as expected by the Contrast Service and Contrast UI.
73
71
  #
74
72
  # @return [String]
75
- def name
73
+ def rule_name
76
74
  raise NoMethodError, 'This is abstract, override it.'
77
75
  end
78
76
 
@@ -82,7 +80,7 @@ module Contrast
82
80
  #
83
81
  # @return [Contrast::Agent::Protect::Rule::Base]
84
82
  def rule
85
- PROTECT.rule name
83
+ ::Contrast::PROTECT.rule rule_name
86
84
  end
87
85
 
88
86
  # Should we skip analysis for this rule for this method invocation?
@@ -1,7 +1,8 @@
1
1
  # Copyright (c) 2021 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/components/interface'
4
+ require 'contrast/components/logger'
5
+ require 'contrast/components/scope'
5
6
 
6
7
  module Contrast
7
8
  module Agent
@@ -12,9 +13,8 @@ module Contrast
12
13
  #
13
14
  # @abstract Subclass and override {#prefilter}, {#infilter}, {#find_attacker}, {#postfilter} to implement
14
15
  class Base
15
- include Contrast::Components::Interface
16
-
17
- access_component :agent, :analysis, :logging, :scope, :settings
16
+ include Contrast::Components::Logger::InstanceMethods
17
+ include Contrast::Components::Scope::InstanceMethods
18
18
 
19
19
  UNKNOWN_USER_INPUT = Contrast::Api::Dtm::UserInput.new.tap do |user_input|
20
20
  user_input.input_type = :UNKNOWN
@@ -37,22 +37,22 @@ module Contrast
37
37
  attr_reader :mode
38
38
 
39
39
  def initialize
40
- PROTECT.rules[name] = self
40
+ ::Contrast::PROTECT.rules[rule_name] = self
41
41
  @mode = mode_from_settings
42
42
  end
43
43
 
44
44
  # Should return the name as it is known to Teamserver; defaults to class
45
- def name
46
- cs__class.name
45
+ def rule_name
46
+ cs__class.cs__name
47
47
  end
48
48
 
49
49
  def enabled?
50
50
  # 1. it is not enabled because protect is not enabled
51
- return false unless AGENT.enabled?
52
- return false unless PROTECT.enabled?
51
+ return false unless ::Contrast::AGENT.enabled?
52
+ return false unless ::Contrast::PROTECT.enabled?
53
53
 
54
54
  # 2. it is not enabled because it is in the list of disabled protect rules
55
- return false if PROTECT.rule_config&.disabled_rules&.include?(name)
55
+ return false if ::Contrast::PROTECT.rule_config&.disabled_rules&.include?(rule_name)
56
56
 
57
57
  # 3. it is enabled so long as its mode is not NO_ACTION
58
58
  @mode != Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION
@@ -60,7 +60,7 @@ module Contrast
60
60
 
61
61
  def excluded? exclusions
62
62
  Array(exclusions).any? do |ex|
63
- ex.protection_rule?(name)
63
+ ex.protection_rule?(rule_name)
64
64
  end
65
65
  end
66
66
 
@@ -176,8 +176,8 @@ module Contrast
176
176
  protected
177
177
 
178
178
  def mode_from_settings
179
- PROTECT.rule_mode(name).tap do |mode|
180
- logger.trace('Retrieving rule mode', rule: name, mode: mode)
179
+ ::Contrast::PROTECT.rule_mode(rule_name).tap do |mode|
180
+ logger.trace('Retrieving rule mode', rule: rule_name, mode: mode)
181
181
  end
182
182
  end
183
183
 
@@ -191,10 +191,10 @@ module Contrast
191
191
  # @return [Boolean] if an exclusion was applicable to this request
192
192
  # for this rule
193
193
  def protect_excluded_by_code?
194
- exclusions = SETTINGS.code_exclusions
194
+ exclusions = ::Contrast::SETTINGS.code_exclusions
195
195
  return false unless exclusions
196
196
 
197
- for_rule = exclusions.select { |ex| ex.protection_rule?(name) }
197
+ for_rule = exclusions.select { |ex| ex.protection_rule?(rule_name) }
198
198
  return false if for_rule.empty?
199
199
 
200
200
  stack = caller_locations
@@ -213,7 +213,7 @@ module Contrast
213
213
  # @param _kwargs [Hash] key-value pairs used by the rule to build a
214
214
  # report.
215
215
  def find_attacker _context, _potential_attack_string, **_kwargs
216
- raise NoMethodError, "Rule #{ name } did not implement find_attack"
216
+ raise NoMethodError, "Rule #{ rule_name } did not implement find_attack"
217
217
  end
218
218
 
219
219
  def update_successful_attack_response context, ia_result, result, attack_string = nil
@@ -249,7 +249,7 @@ module Contrast
249
249
  # @return [Contrast::Api::Dtm::AttackResult]
250
250
  def build_attack_result _context
251
251
  result = Contrast::Api::Dtm::AttackResult.new
252
- result.rule_id = name
252
+ result.rule_id = rule_name
253
253
  result
254
254
  end
255
255
 
@@ -286,7 +286,7 @@ module Contrast
286
286
 
287
287
  def log_rule_matched _context, ia_result, response, _matched_string = nil
288
288
  logger.debug('A successful attack was detected',
289
- rule: name,
289
+ rule: rule_name,
290
290
  type: ia_result&.input_type,
291
291
  name: ia_result&.key,
292
292
  input: ia_result&.value,
@@ -296,11 +296,8 @@ module Contrast
296
296
  private
297
297
 
298
298
  def log_rule_probed _context, ia_result
299
- logger.debug('An unsuccessful attack was detected',
300
- rule: name,
301
- type: ia_result&.input_type,
302
- name: ia_result&.key,
303
- input: ia_result&.value)
299
+ logger.debug('An unsuccessful attack was detected', rule: rule_name, type: ia_result&.input_type,
300
+ name: ia_result&.key, input: ia_result&.value)
304
301
  end
305
302
  end
306
303
  end
@@ -10,7 +10,7 @@ module Contrast
10
10
  # Encapsulate common code for protect rules that do their
11
11
  # input analysis on Speedracer rather in ruby code
12
12
  class BaseService < Contrast::Agent::Protect::Rule::Base
13
- def name
13
+ def rule_name
14
14
  'base-service'
15
15
  end
16
16
 
@@ -32,7 +32,11 @@ module Contrast
32
32
  # streamed responses will break
33
33
  def postfilter context
34
34
  return unless enabled? && POSTFILTER_MODES.include?(mode)
35
- return if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION || mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
35
+ if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
36
+ mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
37
+
38
+ return
39
+ end
36
40
 
37
41
  result = find_postfilter_attacker(context, nil)
38
42
  return unless result&.samples&.any?
@@ -40,14 +44,14 @@ module Contrast
40
44
  append_to_activity(context, result)
41
45
  return unless result.response == :BLOCKED
42
46
 
43
- raise Contrast::SecurityException.new(self, "#{ name } triggered in postfilter. Response blocked.")
47
+ raise Contrast::SecurityException.new(self, "#{ rule_name } triggered in postfilter. Response blocked.")
44
48
  end
45
49
 
46
50
  protected
47
51
 
48
52
  def gather_ia_results context
49
53
  context.speedracer_input_analysis.results.select do |ia_result|
50
- ia_result.rule_id == name
54
+ ia_result.rule_id == rule_name
51
55
  end
52
56
  end
53
57
 
@@ -58,7 +62,7 @@ module Contrast
58
62
 
59
63
  # Allows for the InputAnalysis from service to be extracted early
60
64
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
61
- logger.trace('Checking vectors for attacks', rule: name, input: potential_attack_string)
65
+ logger.trace('Checking vectors for attacks', rule: rule_name, input: potential_attack_string)
62
66
 
63
67
  result = nil
64
68
  ia_results.each do |ia_result|
@@ -4,7 +4,7 @@
4
4
  require 'contrast/agent/protect/rule/base_service'
5
5
  require 'contrast/utils/stack_trace_utils'
6
6
  require 'contrast/utils/object_share'
7
- require 'contrast/components/interface'
7
+ require 'contrast/components/logger'
8
8
 
9
9
  module Contrast
10
10
  module Agent
@@ -12,13 +12,12 @@ module Contrast
12
12
  module Rule
13
13
  # The Ruby implementation of the Protect Command Injection rule.
14
14
  class CmdInjection < Contrast::Agent::Protect::Rule::BaseService
15
- include Contrast::Components::Interface
16
- access_component :app_context, :logging
15
+ include Contrast::Components::Logger::InstanceMethods
17
16
 
18
17
  NAME = 'cmd-injection'
19
18
  CHAINED_COMMAND_CHARS = /[;&|<>]/.cs__freeze
20
19
 
21
- def name
20
+ def rule_name
22
21
  NAME
23
22
  end
24
23
 
@@ -28,26 +27,31 @@ module Contrast
28
27
  ia_results = gather_ia_results(context)
29
28
  return if ia_results.empty?
30
29
 
31
- if APP_CONTEXT.in_new_process?
30
+ if ::Contrast::APP_CONTEXT.in_new_process?
32
31
  logger.trace('Running cmd-injection infilter within new process - creating new context')
33
32
  context = Contrast::Agent::RequestContext.new(context.request.rack_request)
34
33
  Contrast::Agent::REQUEST_TRACKER.update_current_context(context)
35
34
  end
36
35
 
37
- result = find_attacker_with_results(context, command, ia_results, **{ classname: classname, method: method })
36
+ result = find_attacker_with_results(context, command, ia_results,
37
+ **{ classname: classname, method: method })
38
38
  result ||= report_command_execution(context, command, **{ classname: classname, method: method })
39
39
  return unless result
40
40
 
41
41
  append_to_activity(context, result)
42
42
  return unless blocked?
43
43
 
44
- raise Contrast::SecurityException.new(
45
- self,
46
- "Command Injection rule triggered. Call to #{ classname }.#{ method } blocked.")
44
+ raise Contrast::SecurityException.new(self,
45
+ 'Command Injection rule triggered. '\
46
+ "Call to #{ classname }.#{ method } blocked.")
47
47
  end
48
48
 
49
49
  def build_attack_with_match context, input_analysis_result, result, candidate_string, **kwargs
50
- return result if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION || mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
50
+ if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
51
+ mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
52
+
53
+ return result
54
+ end
51
55
 
52
56
  result ||= build_attack_result(context)
53
57
  update_successful_attack_response(context, input_analysis_result, result, candidate_string)
@@ -60,14 +64,10 @@ module Contrast
60
64
  # Because results are not necessarily on the context across
61
65
  # processes; extract early and pass into the method
62
66
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
63
- logger.trace('Checking vectors for attacks', rule: name, input: potential_attack_string)
67
+ logger.trace('Checking vectors for attacks', rule: rule_name, input: potential_attack_string)
64
68
  result = super(context, potential_attack_string, ia_results, **kwargs)
65
69
  if result.nil? && potential_attack_string
66
- result = find_probable_attacker(
67
- context,
68
- potential_attack_string,
69
- ia_results,
70
- **kwargs)
70
+ result = find_probable_attacker(context, potential_attack_string, ia_results, **kwargs)
71
71
  end
72
72
  result
73
73
  end
@@ -109,12 +109,7 @@ module Contrast
109
109
  likely_attacker = ia_results.find { |input_analysis_result| chained_command?(input_analysis_result.value) }
110
110
  return unless likely_attacker
111
111
 
112
- build_attack_with_match(
113
- context,
114
- likely_attacker,
115
- nil,
116
- potential_attack_string,
117
- **kwargs)
112
+ build_attack_with_match(context, likely_attacker, nil, potential_attack_string, **kwargs)
118
113
  end
119
114
 
120
115
  def chained_command? command
@@ -128,7 +123,7 @@ module Contrast
128
123
  # @return [Boolean] if the agent should report all command
129
124
  # executions.
130
125
  def report_any_command_execution?
131
- PROTECT.report_any_command_execution?
126
+ ::Contrast::PROTECT.report_any_command_execution?
132
127
  end
133
128
  end
134
129
  end
@@ -17,10 +17,7 @@ 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[
21
- object:ERB
22
- o:\bERB
23
- ].cs__freeze
20
+ ERB_GADGETS = %W[object:ERB o:\bERB].cs__freeze
24
21
 
25
22
  # Gadgets that map to ActionDispatch modules
26
23
  ACTION_DISPATCH_GADGETS = %w[
@@ -29,11 +26,7 @@ module Contrast
29
26
  ].cs__freeze
30
27
 
31
28
  # Gadgets that map to Arel Modules
32
- AREL_GADGETS = %w[
33
- string:Arel::Nodes::SqlLiteral
34
- object:Arel::Nodes
35
- o:\bArel::Nodes
36
- ].cs__freeze
29
+ AREL_GADGETS = %w[string:Arel::Nodes::SqlLiteral object:Arel::Nodes o:\bArel::Nodes].cs__freeze
37
30
 
38
31
  # Used to indicate to TeamServer the gadget is an ERB module
39
32
  ERB = 'ERB'
@@ -45,7 +38,7 @@ module Contrast
45
38
 
46
39
  # Return the TeamServer understood id / name of this rule.
47
40
  # @return [String] the TeamServer understood id / name of this rule.
48
- def name
41
+ def rule_name
49
42
  NAME
50
43
  end
51
44
 
@@ -124,8 +117,8 @@ module Contrast
124
117
  sample = build_base_sample(context, input_analysis_result)
125
118
  sample.untrusted_deserialization = Contrast::Api::Dtm::UntrustedDeserializationDetails.new
126
119
 
127
- deserializer = kwargs[:GADGET_TYPE]
128
- sample.untrusted_deserialization.deserializer = Contrast::Utils::StringUtils.protobuf_safe_string(deserializer)
120
+ deserializer = Contrast::Utils::StringUtils.protobuf_safe_string(kwargs[:GADGET_TYPE])
121
+ sample.untrusted_deserialization.deserializer = deserializer
129
122
 
130
123
  command = !!kwargs[:COMMAND_SCOPE]
131
124
  sample.untrusted_deserialization.command = command
@@ -148,7 +141,7 @@ module Contrast
148
141
  # of the analysis done by this rule.
149
142
  def build_evaluation gadget_string
150
143
  ia_result = Contrast::Api::Settings::InputAnalysisResult.new
151
- ia_result.rule_id = name
144
+ ia_result.rule_id = rule_name
152
145
  ia_result.input_type = :UNKNOWN
153
146
  ia_result.key = INPUT_NAME
154
147
  ia_result.value = Contrast::Utils::StringUtils.protobuf_safe_string(gadget_string)
@@ -12,7 +12,7 @@ module Contrast
12
12
  NAME = 'method-tampering'
13
13
  STANDARD_METHODS = %w[GET HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATCH].cs__freeze
14
14
 
15
- def name
15
+ def rule_name
16
16
  NAME
17
17
  end
18
18
 
@@ -30,20 +30,9 @@ module Contrast
30
30
 
31
31
  method = ia_results.first.value
32
32
  result = if response_code.to_s.start_with?('4', '5')
33
- build_attack_without_match(
34
- context,
35
- nil,
36
- nil,
37
- method: method,
38
- response_code: response_code)
33
+ build_attack_without_match(context, nil, nil, method: method, response_code: response_code)
39
34
  else
40
- build_attack_with_match(
41
- context,
42
- nil,
43
- nil,
44
- nil,
45
- method: method,
46
- response_code: response_code)
35
+ build_attack_with_match(context, nil, nil, nil, method: method, response_code: response_code)
47
36
  end
48
37
  append_to_activity(context, result) if result
49
38
  end