contrast-agent 3.9.0 → 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (271) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +5 -2
  3. data/ext/build_funchook.rb +14 -9
  4. data/ext/cs__assess_active_record_named/cs__active_record_named.c +5 -12
  5. data/ext/cs__assess_active_record_named/extconf.rb +3 -0
  6. data/ext/cs__assess_array/cs__assess_array.c +3 -5
  7. data/ext/cs__assess_array/extconf.rb +3 -0
  8. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +10 -4
  9. data/ext/cs__assess_basic_object/extconf.rb +3 -0
  10. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +4 -3
  11. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +3 -3
  12. data/ext/cs__assess_fiber_track/extconf.rb +3 -0
  13. data/ext/cs__assess_hash/cs__assess_hash.c +40 -17
  14. data/ext/cs__assess_hash/cs__assess_hash.h +4 -6
  15. data/ext/cs__assess_hash/extconf.rb +3 -0
  16. data/ext/cs__assess_kernel/cs__assess_kernel.c +10 -8
  17. data/ext/cs__assess_kernel/cs__assess_kernel.h +1 -0
  18. data/ext/cs__assess_kernel/extconf.rb +3 -0
  19. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +3 -6
  20. data/ext/cs__assess_marshal_module/extconf.rb +3 -0
  21. data/ext/cs__assess_module/cs__assess_module.c +13 -9
  22. data/ext/cs__assess_module/extconf.rb +3 -0
  23. data/ext/cs__assess_regexp/cs__assess_regexp.c +13 -9
  24. data/ext/cs__assess_regexp/cs__assess_regexp.h +1 -0
  25. data/ext/cs__assess_regexp/extconf.rb +3 -0
  26. data/ext/cs__assess_string/cs__assess_string.c +5 -8
  27. data/ext/cs__assess_string/cs__assess_string.h +2 -1
  28. data/ext/cs__assess_string/extconf.rb +3 -0
  29. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +2 -2
  30. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.h +3 -3
  31. data/ext/cs__assess_string_interpolation26/extconf.rb +3 -0
  32. data/ext/cs__assess_yield_track/cs__assess_yield_track.h +1 -1
  33. data/ext/cs__assess_yield_track/extconf.rb +3 -0
  34. data/ext/cs__common/cs__common.c +79 -0
  35. data/ext/cs__common/cs__common.h +34 -0
  36. data/ext/cs__common/extconf.rb +9 -8
  37. data/ext/cs__contrast_patch/cs__contrast_patch.h +1 -6
  38. data/ext/cs__contrast_patch/extconf.rb +3 -0
  39. data/ext/cs__protect_kernel/cs__protect_kernel.c +20 -11
  40. data/ext/cs__protect_kernel/extconf.rb +3 -0
  41. data/ext/extconf_common.rb +10 -8
  42. data/funchook/autom4te.cache/output.0 +1 -13
  43. data/funchook/autom4te.cache/requests +44 -45
  44. data/funchook/autom4te.cache/traces.0 +0 -3
  45. data/funchook/config.log +378 -217
  46. data/funchook/config.status +23 -24
  47. data/funchook/configure +1 -13
  48. data/funchook/src/Makefile +7 -7
  49. data/funchook/src/config.h +2 -2
  50. data/funchook/src/decoder.o +0 -0
  51. data/funchook/src/distorm.o +0 -0
  52. data/funchook/src/funchook.o +0 -0
  53. data/funchook/src/funchook_io.o +0 -0
  54. data/funchook/src/funchook_syscall.o +0 -0
  55. data/funchook/src/funchook_unix.o +0 -0
  56. data/funchook/src/funchook_x86.o +0 -0
  57. data/funchook/src/instructions.o +0 -0
  58. data/funchook/src/insts.o +0 -0
  59. data/funchook/src/libfunchook.dylib +0 -0
  60. data/funchook/src/mnemonics.o +0 -0
  61. data/funchook/src/operands.o +0 -0
  62. data/funchook/src/os_func.o +0 -0
  63. data/funchook/src/os_func_unix.o +0 -0
  64. data/funchook/src/prefix.o +0 -0
  65. data/funchook/src/printf_base.o +0 -0
  66. data/funchook/src/textdefs.o +0 -0
  67. data/funchook/src/wstring.o +0 -0
  68. data/funchook/test/Makefile +2 -2
  69. data/funchook/test/funchook_test +0 -0
  70. data/funchook/test/libfunchook_test.so +0 -0
  71. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +20 -0
  72. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  73. data/funchook/test/test_main.o +0 -0
  74. data/funchook/test/x86_64_test.o +0 -0
  75. data/lib/contrast.rb +1 -0
  76. data/lib/contrast/agent.rb +21 -15
  77. data/lib/contrast/agent/assess.rb +1 -2
  78. data/lib/contrast/agent/assess/adjusted_span.rb +3 -1
  79. data/lib/contrast/agent/assess/contrast_event.rb +20 -68
  80. data/lib/contrast/agent/assess/events/event_factory.rb +25 -0
  81. data/lib/contrast/agent/assess/events/source_event.rb +83 -0
  82. data/lib/contrast/agent/assess/insulator.rb +0 -4
  83. data/lib/contrast/agent/assess/policy/patcher.rb +5 -2
  84. data/lib/contrast/agent/assess/policy/policy_node.rb +0 -7
  85. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  86. data/lib/contrast/agent/assess/policy/preshift.rb +1 -1
  87. data/lib/contrast/agent/assess/policy/propagation_method.rb +65 -33
  88. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -1
  89. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  90. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +80 -0
  91. data/lib/contrast/agent/assess/policy/propagator/select.rb +35 -22
  92. data/lib/contrast/agent/assess/policy/propagator/split.rb +26 -6
  93. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -0
  94. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +40 -27
  95. data/lib/contrast/agent/assess/policy/source_method.rb +20 -20
  96. data/lib/contrast/agent/assess/policy/source_node.rb +0 -15
  97. data/lib/contrast/agent/assess/policy/trigger_method.rb +29 -40
  98. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -6
  99. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -31
  100. data/lib/contrast/agent/assess/properties.rb +5 -3
  101. data/lib/contrast/agent/assess/rule/base.rb +1 -5
  102. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +2 -22
  103. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +5 -1
  104. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +2 -2
  105. data/lib/contrast/agent/assess/rule/redos.rb +4 -4
  106. data/lib/contrast/agent/assess/tag.rb +24 -14
  107. data/lib/contrast/agent/at_exit_hook.rb +16 -13
  108. data/lib/contrast/agent/class_reopener.rb +15 -6
  109. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  110. data/lib/contrast/agent/disable_reaction.rb +3 -4
  111. data/lib/contrast/agent/exclusion_matcher.rb +8 -48
  112. data/lib/contrast/agent/feature_state.rb +45 -78
  113. data/lib/contrast/agent/logger.rb +173 -0
  114. data/lib/contrast/agent/middleware.rb +87 -250
  115. data/lib/contrast/agent/module_data.rb +2 -1
  116. data/lib/contrast/agent/patching/policy/after_load_patch.rb +2 -1
  117. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +21 -4
  118. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -3
  119. data/lib/contrast/agent/patching/policy/module_policy.rb +0 -25
  120. data/lib/contrast/agent/patching/policy/patch.rb +96 -23
  121. data/lib/contrast/agent/patching/policy/patcher.rb +19 -19
  122. data/lib/contrast/agent/patching/policy/policy.rb +7 -7
  123. data/lib/contrast/agent/patching/policy/policy_node.rb +2 -10
  124. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -4
  125. data/lib/contrast/agent/protect/rule/base.rb +14 -33
  126. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  127. data/lib/contrast/agent/protect/rule/cmd_injection.rb +10 -13
  128. data/lib/contrast/agent/protect/rule/csrf.rb +2 -1
  129. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +11 -14
  130. data/lib/contrast/agent/protect/rule/default_scanner.rb +0 -13
  131. data/lib/contrast/agent/protect/rule/deserialization.rb +2 -0
  132. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +2 -2
  133. data/lib/contrast/agent/protect/rule/no_sqli.rb +4 -4
  134. data/lib/contrast/agent/protect/rule/path_traversal.rb +5 -4
  135. data/lib/contrast/agent/protect/rule/sqli.rb +1 -0
  136. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +2 -0
  137. data/lib/contrast/agent/protect/rule/xss.rb +2 -0
  138. data/lib/contrast/agent/protect/rule/xxe.rb +10 -4
  139. data/lib/contrast/agent/railtie.rb +2 -8
  140. data/lib/contrast/agent/reaction_processor.rb +5 -5
  141. data/lib/contrast/agent/request.rb +9 -12
  142. data/lib/contrast/agent/request_context.rb +12 -14
  143. data/lib/contrast/agent/request_handler.rb +35 -0
  144. data/lib/contrast/agent/response.rb +52 -30
  145. data/lib/contrast/agent/rewriter.rb +25 -11
  146. data/lib/contrast/agent/rule_set.rb +49 -0
  147. data/lib/contrast/agent/scope.rb +4 -12
  148. data/lib/contrast/agent/service_heartbeat.rb +1 -2
  149. data/lib/contrast/agent/settings_state.rb +10 -74
  150. data/lib/contrast/agent/socket_client.rb +17 -11
  151. data/lib/contrast/agent/static_analysis.rb +42 -0
  152. data/lib/contrast/agent/thread.rb +1 -1
  153. data/lib/contrast/agent/tracepoint_hook.rb +1 -5
  154. data/lib/contrast/agent/version.rb +1 -1
  155. data/lib/contrast/api.rb +1 -1
  156. data/lib/contrast/api/decorators.rb +14 -0
  157. data/lib/contrast/api/decorators/application_settings.rb +37 -0
  158. data/lib/contrast/api/decorators/application_update.rb +66 -0
  159. data/lib/contrast/api/decorators/exclusion.rb +20 -0
  160. data/lib/contrast/api/decorators/input_analysis.rb +17 -0
  161. data/lib/contrast/api/decorators/server_features.rb +24 -0
  162. data/lib/contrast/api/speedracer.rb +31 -29
  163. data/lib/contrast/api/tcp_socket.rb +0 -2
  164. data/lib/contrast/components/agent.rb +27 -22
  165. data/lib/contrast/components/app_context.rb +18 -43
  166. data/lib/contrast/components/config.rb +7 -5
  167. data/lib/contrast/components/contrast_service.rb +0 -4
  168. data/lib/contrast/components/heap_dump.rb +12 -8
  169. data/lib/contrast/components/interface.rb +11 -10
  170. data/lib/contrast/components/logger.rb +3 -68
  171. data/lib/contrast/components/sampling.rb +22 -11
  172. data/lib/contrast/components/settings.rb +18 -5
  173. data/lib/contrast/config/base_configuration.rb +1 -0
  174. data/lib/contrast/config/default_value.rb +1 -0
  175. data/lib/contrast/config/protect_rule_configuration.rb +0 -14
  176. data/lib/contrast/configuration.rb +3 -5
  177. data/lib/contrast/extensions/framework/rails/action_controller_inheritance.rb +39 -0
  178. data/lib/contrast/extensions/framework/rails/active_record_named.rb +13 -8
  179. data/lib/contrast/extensions/ruby_core/assess.rb +1 -2
  180. data/lib/contrast/extensions/ruby_core/assess/assess_extension.rb +27 -22
  181. data/lib/contrast/extensions/ruby_core/assess/erb.rb +0 -8
  182. data/lib/contrast/extensions/ruby_core/assess/exec_trigger.rb +6 -8
  183. data/lib/contrast/extensions/ruby_core/assess/fiber.rb +88 -100
  184. data/lib/contrast/extensions/ruby_core/assess/hash.rb +32 -15
  185. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +28 -27
  186. data/lib/contrast/extensions/ruby_core/assess/regexp.rb +74 -196
  187. data/lib/contrast/extensions/ruby_core/assess/string.rb +15 -7
  188. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +29 -24
  189. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +2 -2
  190. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -1
  191. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +2 -2
  192. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +9 -20
  193. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +9 -19
  194. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +10 -27
  195. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +13 -21
  196. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +11 -23
  197. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +62 -78
  198. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +50 -0
  199. data/lib/contrast/framework/base_support.rb +10 -0
  200. data/lib/contrast/framework/manager.rb +28 -2
  201. data/lib/contrast/framework/platform_version.rb +1 -0
  202. data/lib/contrast/framework/rails_support.rb +16 -0
  203. data/lib/contrast/framework/sinatra_support.rb +12 -2
  204. data/lib/contrast/framework/view_technologies_descriptor.rb +1 -0
  205. data/lib/contrast/tasks/service.rb +2 -8
  206. data/lib/contrast/utils/assess/sampling_util.rb +4 -9
  207. data/lib/contrast/utils/assess/tracking_util.rb +7 -1
  208. data/lib/contrast/utils/boolean_util.rb +2 -5
  209. data/lib/contrast/utils/cache.rb +0 -11
  210. data/lib/contrast/utils/class_util.rb +20 -1
  211. data/lib/contrast/utils/gemfile_reader.rb +5 -3
  212. data/lib/contrast/utils/hash_digest.rb +0 -4
  213. data/lib/contrast/utils/heap_dump_util.rb +12 -11
  214. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  215. data/lib/contrast/utils/inventory_util.rb +2 -2
  216. data/lib/contrast/utils/io_util.rb +1 -11
  217. data/lib/contrast/utils/job_servers_running.rb +2 -2
  218. data/lib/contrast/utils/object_share.rb +1 -37
  219. data/lib/contrast/utils/os.rb +1 -25
  220. data/lib/contrast/utils/rack_assess_session_cookie.rb +3 -3
  221. data/lib/contrast/utils/rails_assess_configuration.rb +3 -3
  222. data/lib/contrast/utils/ruby_ast_rewriter.rb +5 -1
  223. data/lib/contrast/utils/service_response_util.rb +27 -53
  224. data/lib/contrast/utils/service_sender_util.rb +9 -5
  225. data/lib/contrast/utils/sinatra_helper.rb +0 -6
  226. data/lib/contrast/utils/stack_trace_utils.rb +86 -182
  227. data/lib/contrast/utils/string_utils.rb +18 -2
  228. data/lib/contrast/utils/tag_util.rb +11 -1
  229. data/lib/contrast/utils/thread_tracker.rb +2 -2
  230. data/lib/contrast/utils/timer.rb +0 -40
  231. data/resources/assess/policy.json +33 -21
  232. data/resources/protect/policy.json +9 -9
  233. data/ruby-agent.gemspec +7 -4
  234. data/service_executables/VERSION +1 -1
  235. data/service_executables/linux/contrast-service +0 -0
  236. data/service_executables/mac/contrast-service +0 -0
  237. metadata +79 -54
  238. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.c +0 -63
  239. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.h +0 -29
  240. data/ext/cs__assess_regexp_track/extconf.rb +0 -2
  241. data/funchook/src/libfunchook.so +0 -0
  242. data/lib/contrast/agent/assess/frozen_properties.rb +0 -41
  243. data/lib/contrast/agent/logger_manager.rb +0 -116
  244. data/lib/contrast/delegators.rb +0 -9
  245. data/lib/contrast/delegators/application_update.rb +0 -32
  246. data/lib/contrast/utils/comment_range.rb +0 -19
  247. data/lib/contrast/utils/environment_util.rb +0 -82
  248. data/lib/contrast/utils/performs_logging.rb +0 -152
  249. data/resources/factory-bot-spec/spec_helper.rb +0 -30
  250. data/resources/rubocops/kernel/catch_cop.rb +0 -37
  251. data/resources/rubocops/kernel/require_cop.rb +0 -37
  252. data/resources/rubocops/kernel/require_relative_cop.rb +0 -33
  253. data/resources/rubocops/module/autoload_cop.rb +0 -37
  254. data/resources/rubocops/module/const_defined_cop.rb +0 -37
  255. data/resources/rubocops/module/const_get_cop.rb +0 -37
  256. data/resources/rubocops/module/const_set_cop.rb +0 -37
  257. data/resources/rubocops/module/constants_cop.rb +0 -37
  258. data/resources/rubocops/module/name_cop.rb +0 -37
  259. data/resources/rubocops/object/class_cop.rb +0 -37
  260. data/resources/rubocops/object/freeze_cop.rb +0 -37
  261. data/resources/rubocops/object/frozen_cop.rb +0 -37
  262. data/resources/rubocops/object/is_a_cop.rb +0 -37
  263. data/resources/rubocops/object/method_cop.rb +0 -37
  264. data/resources/rubocops/object/respond_to_cop.rb +0 -37
  265. data/resources/rubocops/object/singleton_class_cop.rb +0 -37
  266. data/resources/rubocops/regexp/spelling_cop.rb +0 -44
  267. data/resources/rubocops/thread/new_cop.rb +0 -39
  268. data/resources/ruby-spec/ancestors_spec.rb +0 -70
  269. data/resources/ruby-spec/modulo_spec.rb +0 -831
  270. data/resources/ruby-spec/parameters_spec.rb +0 -261
  271. data/resources/ruby-spec/ruby_spec_spec_helper.rb +0 -35
@@ -15,8 +15,8 @@ module Contrast
15
15
  include Contrast::Components::Interface
16
16
  access_component :scope
17
17
 
18
- attr_accessor :class_name, :instance_method, :method_name, :method_scope, :method_visibility
19
- attr_reader :properties
18
+ attr_accessor :class_name, :instance_method, :method_name, :method_visibility
19
+ attr_reader :properties, :method_scope
20
20
 
21
21
  def node_class
22
22
  raise NotImplementedError, 'specify the type of the feature for which this node patches'
@@ -60,14 +60,6 @@ module Contrast
60
60
  instance_method
61
61
  end
62
62
 
63
- def private?
64
- @method_visibility == :private
65
- end
66
-
67
- def public?
68
- @method_visibility == :public
69
- end
70
-
71
63
  private
72
64
 
73
65
  # Convert strings to symbols here, once, to avoid doing so on every
@@ -15,16 +15,13 @@ module Contrast
15
15
  # input attempted to or did do damage).
16
16
  class TriggerNode < PolicyNode
17
17
  JSON_NAME = 'name'
18
- JSON_NODES = 'nodes'
19
18
  JSON_APPLICATOR = 'applicator'
20
19
  JSON_APPLICATOR_METHOD = 'applicator_method'
21
20
  JSON_REQUIRED_PROPS = 'required_properties'
22
21
  JSON_OPTIONAL_PROPS = 'optional_properties'
23
- JSON_SCOPE = 'scope'
24
22
  JSON_ON_EXCEPTION = 'on_exception'
25
23
 
26
- attr_reader :rule_id
27
- attr_accessor :applicator_method, :applicator, :on_exception, :required_properties, :optional_properties
24
+ attr_reader :applicator, :applicator_method, :on_exception, :optional_properties, :required_properties, :rule_id
28
25
 
29
26
  def initialize trigger_hash = {}, rule_hash = {}
30
27
  super(trigger_hash)
@@ -14,14 +14,13 @@ module Contrast
14
14
  class Base
15
15
  include Contrast::Components::Interface
16
16
 
17
- access_component :logging, :analysis, :settings, :scope
17
+ access_component :analysis, :logging, :scope, :settings
18
18
 
19
19
  UNKNOWN_USER_INPUT = Contrast::Api::Dtm::UserInput.new.tap do |user_input|
20
20
  user_input.input_type = :UNKNOWN
21
21
  end.cs__freeze
22
22
 
23
23
  BLOCKING_MODES = Set.new(%i[BLOCK BLOCK_AT_PERIMETER]).cs__freeze
24
- PREFILTER_MODES = Set.new(%i[BLOCK_AT_PERIMETER]).cs__freeze
25
24
  POSTFILTER_MODES = Set.new(%i[BLOCK PERMIT MONITOR]).cs__freeze
26
25
  STACK_COLLECTION_RESULTS = Set.new(%i[BLOCKED MONITORED]).cs__freeze
27
26
 
@@ -59,7 +58,6 @@ module Contrast
59
58
  @mode != :NO_ACTION
60
59
  end
61
60
 
62
- # this rule is excluded if any of the given exclusions have a protection rule that matches this rule name
63
61
  def excluded? exclusions
64
62
  Array(exclusions).any? do |ex|
65
63
  ex.protection_rule?(name)
@@ -141,7 +139,7 @@ module Contrast
141
139
 
142
140
  def mode_from_settings
143
141
  SETTINGS.protect_rule_mode(name).tap do |mode|
144
- logger.debug("rule #{ name } mode = #{ mode }")
142
+ logger.trace('Retrieving rule mode', rule: name, mode: mode)
145
143
  end
146
144
  end
147
145
 
@@ -215,7 +213,7 @@ module Contrast
215
213
  return unless sample
216
214
  return unless STACK_COLLECTION_RESULTS.include?(result&.response)
217
215
 
218
- stack = Contrast::Utils::StackTraceUtils.build(ignore: true, class_lookup: true, depth: 50)
216
+ stack = Contrast::Utils::StackTraceUtils.build_protect_stack_array
219
217
  return unless stack
220
218
 
221
219
  sample.stack_trace_elements += stack
@@ -259,39 +257,22 @@ module Contrast
259
257
  end
260
258
 
261
259
  def log_rule_matched _context, ia_result, response, _matched_string = nil
262
- return unless ia_result
263
-
264
- # Log to agent logger
265
- message = if ia_result
266
- log_msg(ia_result, true)
267
- else
268
- "An effective attack was detected against #{ name }."
269
- end
270
-
271
- logger.debug([response, message].join)
260
+ logger.debug('A successful attack was detected',
261
+ rule: name,
262
+ type: ia_result&.input_type,
263
+ name: ia_result&.key,
264
+ input: ia_result&.value,
265
+ result: response)
272
266
  end
273
267
 
274
268
  private
275
269
 
276
270
  def log_rule_probed _context, ia_result
277
- message = if ia_result
278
- log_msg(ia_result, false)
279
- else
280
- "An unsuccessful attack was detected against #{ name }"
281
- end
282
-
283
- logger.debug(message)
284
- end
285
-
286
- def log_msg ia_result, matched
287
- key = ia_result.key ? ia_result.key.to_s + ' ' : ''
288
- val = ia_result.value.to_s
289
- typ = ia_result.input_type.to_s
290
- if matched
291
- "The #{ typ } #{ key } had a value that successfully exploited #{ name } - #{ val }."
292
- else
293
- "The #{ typ } #{ key } had a value that matched a signature for, but did not successfully exploit, #{ name } - #{ val }."
294
- end
271
+ logger.debug('An unsuccessful attack was detected',
272
+ rule: name,
273
+ type: ia_result&.input_type,
274
+ name: ia_result&.key,
275
+ input: ia_result&.value)
295
276
  end
296
277
  end
297
278
  end
@@ -1,6 +1,8 @@
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
+ cs__scoped_require 'contrast/agent/protect/rule/base'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Protect
@@ -56,7 +58,7 @@ module Contrast
56
58
 
57
59
  # Allows for the InputAnalysis from service to be extracted early
58
60
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
59
- logger.debug("checking: #{ name } vectors=#{ ia_results.length } in '#{ potential_attack_string }'")
61
+ logger.trace('Checking vectors for attacks', rule: name, input: potential_attack_string)
60
62
 
61
63
  result = nil
62
64
  ia_results.each do |ia_result|
@@ -1,6 +1,8 @@
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
+ cs__scoped_require 'contrast/agent/at_exit_hook'
5
+ cs__scoped_require 'contrast/agent/protect/rule/base_service'
4
6
  cs__scoped_require 'contrast/utils/stack_trace_utils'
5
7
  cs__scoped_require 'contrast/utils/object_share'
6
8
  cs__scoped_require 'contrast/components/interface'
@@ -12,7 +14,7 @@ module Contrast
12
14
  # The Ruby implementation of the Protect Command Injection rule.
13
15
  class CmdInjection < Contrast::Agent::Protect::Rule::BaseService
14
16
  include Contrast::Components::Interface
15
- access_component :logging, :contrast_service
17
+ access_component :logging
16
18
 
17
19
  NAME = 'cmd-injection'
18
20
  CHAINED_COMMAND_CHARS = /[;&|<>]/.cs__freeze
@@ -28,7 +30,7 @@ module Contrast
28
30
  return nil if ia_results.empty?
29
31
 
30
32
  if Contrast::Agent::FeatureState.instance.in_new_process?
31
- logger.debug('Running cmd-injection infilter within new process - creating new context')
33
+ logger.trace('Running cmd-injection infilter within new process - creating new context')
32
34
  context = Contrast::Agent::RequestContext.new(context.request.rack_request)
33
35
  Contrast::Agent::REQUEST_TRACKER.update_current_context(context)
34
36
  end
@@ -38,18 +40,16 @@ module Contrast
38
40
  return nil unless result
39
41
 
40
42
  append_to_activity(context, result)
41
- if %I[exec `].include?(method)
42
- # TODO: RUBY-737
43
- # Kernel#exec replaces the current process and does not go through at_exit hooks
44
- # Kernel#` runs as a subshell - messages appended here do not seem to be present in the original process?
45
- CONTRAST_SERVICE.send_message(context.activity)
46
- end
47
-
48
43
  return unless blocked?
49
44
 
50
45
  raise Contrast::SecurityException.new(
51
46
  self,
52
47
  "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
53
  end
54
54
 
55
55
  def build_attack_with_match context, input_analysis_result, result, candidate_string, **kwargs
@@ -66,7 +66,7 @@ module Contrast
66
66
  # Because results are not necessarily on the context across
67
67
  # processes; extract early and pass into the method
68
68
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
69
- logger.debug("checking: #{ name } in '#{ potential_attack_string }'")
69
+ logger.trace('Checking vectors for attacks', rule: name, input: potential_attack_string)
70
70
  result = super(context, potential_attack_string, ia_results, **kwargs)
71
71
  if result.nil? && potential_attack_string
72
72
  result = find_probable_attacker(
@@ -128,9 +128,6 @@ module Contrast
128
128
  result,
129
129
  potential_attack_string,
130
130
  **kwargs)
131
- return nil if result.nil?
132
-
133
- log_rule_matched(context, most_likely, mode, potential_attack_string)
134
131
  result
135
132
  end
136
133
 
@@ -2,9 +2,10 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  cs__scoped_require 'rack'
5
- cs__scoped_require 'contrast/utils/object_share'
5
+ cs__scoped_require 'contrast/agent/protect/rule/base'
6
6
  cs__scoped_require 'contrast/agent/request'
7
7
  cs__scoped_require 'contrast/agent/response'
8
+ cs__scoped_require 'contrast/utils/object_share'
8
9
  cs__scoped_require 'contrast/utils/stack_trace_utils'
9
10
  cs__scoped_require 'contrast/utils/timer'
10
11
 
@@ -13,31 +13,34 @@ class Contrast::Agent::Protect::Rule::Csrf::CsrfEvaluator # rubocop:disable Styl
13
13
 
14
14
  def can_ignore_check? request
15
15
  if !form_submittable_method?(request)
16
- logger.debug("Ignoring method #{ request.request_method } for URI #{ request.uri }")
16
+ logger.trace('Ignoring method', method: request.request_method, uri: request.uri)
17
17
  true
18
18
  elsif ajax_request?(request)
19
- logger.debug("Ignoring Ajax request for URI #{ request.uri }")
19
+ logger.trace('Ignoring Ajax request', uri: request.uri)
20
20
  true
21
21
  elsif empty_post?(request)
22
- logger.debug("Ignoring empty POST request for URI #{ request.uri }")
22
+ logger.trace('Ignoring empty POST', uri: request.uri)
23
23
  true
24
24
  elsif !form_content_type?(request)
25
- logger.debug("Ignoring POST request with Content-Type #{ request.content_type } for URI #{ request.uri }")
25
+ logger.trace(
26
+ 'Ignoring POST request because of Content-Type',
27
+ content_type: request.content_type,
28
+ uri: request.uri)
26
29
  true
27
30
  elsif request.static_request?
28
- logger.debug("Ignoring static request for URI #{ request.uri }")
31
+ logger.trace('Ignoring static request', uri: request.uri)
29
32
  true
30
33
  elsif origin_is_referer?(request)
31
- logger.debug("Ignoring equivalent origin-referer for URI #{ request.uri }")
34
+ logger.trace('Ignoring equivalent origin-referer', uri: request.uri)
32
35
  true
33
36
  elsif login_page?(request)
34
- logger.debug("Ignoring possible login page for URI #{ request.uri }")
37
+ logger.trace('Ignoring possible login page', uri: request.uri)
35
38
  true
36
39
  else
37
40
  false
38
41
  end
39
42
  rescue StandardError => e
40
- logger.warn(e, 'Unable to determine if CSRF can be ignored')
43
+ logger.debug('Unable to determine if CSRF can be ignored', e)
41
44
  end
42
45
 
43
46
  POST_METHOD = 'POST'
@@ -51,12 +54,6 @@ class Contrast::Agent::Protect::Rule::Csrf::CsrfEvaluator # rubocop:disable Styl
51
54
  !requested_with.nil? && !requested_with.empty?
52
55
  end
53
56
 
54
- USER_AGENT = 'USER-AGENT'
55
- def empty_user_agent? request
56
- agent = request.header_value(USER_AGENT)
57
- agent.nil? || agent.empty?
58
- end
59
-
60
57
  def empty_post? request
61
58
  request.request_body_str.empty? && (request.query_string.nil? || request.query_string.empty?)
62
59
  end
@@ -35,7 +35,6 @@ class Contrast::Agent::Protect::Rule::DefaultScanner # rubocop:disable Style/Cla
35
35
  @_token_boundaries ||= scan_token_boundaries(query)
36
36
  end
37
37
 
38
- CANARY = "select * from tbl where bar='bar';#' and pwd='sec3r3t'; # CANARY"
39
38
  def scan_token_boundaries query
40
39
  boundaries = []
41
40
  return boundaries unless query && !query.empty?
@@ -268,24 +267,12 @@ class Contrast::Agent::Protect::Rule::DefaultScanner # rubocop:disable Style/Cla
268
267
  true
269
268
  end
270
269
 
271
- # Does this language let the user redefine the escape character
272
- # We assume no by default
273
- def redefines_escape_char?
274
- false
275
- end
276
-
277
270
  # Is the character provided an escape character?
278
271
  # By default, we'll assume
279
272
  def escape_char? char
280
273
  char == Contrast::Utils::ObjectShare::BACK_SLASH
281
274
  end
282
275
 
283
- # Does this language support string escape sequences?
284
- # We assume no by default
285
- def support_string_escape_sequence?
286
- false
287
- end
288
-
289
276
  # Is this the start of a string escape sequence?
290
277
  # Since escape sequences aren't supported, the answer is always false
291
278
  def escape_sequence_start? _char
@@ -1,6 +1,8 @@
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
+ cs__scoped_require 'contrast/agent/protect/rule/base'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Protect
@@ -1,6 +1,8 @@
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
+ cs__scoped_require 'contrast/agent/protect/rule/base_service'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Protect
@@ -16,8 +18,6 @@ module Contrast
16
18
 
17
19
  def postfilter context
18
20
  return unless enabled? && POSTFILTER_MODES.include?(mode)
19
-
20
- logger.debug("#{ name } postfilter...")
21
21
  return if normal_request?(context)
22
22
 
23
23
  # The only way to be here in postfilter with a result is if the rule mode was MONITOR
@@ -1,9 +1,8 @@
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
- # This could be useful for making patterns maybe?
5
- # https://github.com/cr0hn/nosqlinjection_wordlists
6
- # https://www.owasp.org/index.php/Testing_for_NoSQL_injection
4
+ cs__scoped_require 'contrast/agent/protect/rule/base_service'
5
+
7
6
  module Contrast
8
7
  module Agent
9
8
  module Protect
@@ -70,7 +69,8 @@ module Contrast
70
69
  begin
71
70
  potential_attack_string = JSON.generate(potential_attack_string).to_s
72
71
  rescue JSON::GeneratorError
73
- logger.debug("Error in JSON::generate from input #{ potential_attack_string }")
72
+ logger.trace('Error in JSON::generate', input: potential_attack_string)
73
+ nil
74
74
  end
75
75
  end
76
76
  super(context, potential_attack_string, **kwargs)
@@ -1,8 +1,9 @@
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
- cs__scoped_require 'contrast/utils/stack_trace_utils'
4
+ cs__scoped_require 'contrast/agent/protect/rule/base_service'
5
5
  cs__scoped_require 'contrast/components/interface'
6
+ cs__scoped_require 'contrast/utils/stack_trace_utils'
6
7
 
7
8
  module Contrast
8
9
  module Agent
@@ -12,7 +13,7 @@ module Contrast
12
13
  # Protect rule.
13
14
  class PathTraversal < Contrast::Agent::Protect::Rule::BaseService
14
15
  include Contrast::Components::Interface
15
- access_component :logging, :agent
16
+ access_component :agent
16
17
 
17
18
  NAME = 'path-traversal'
18
19
  SYSTEM_PATHS = %w[
@@ -28,8 +29,6 @@ module Contrast
28
29
  /windows/repair/
29
30
  ].cs__freeze
30
31
 
31
- KNOWN_SECURITY_BYPASS_MARKERS = ['::$DATA', '::$Index', '', '\x00'].cs__freeze
32
-
33
32
  def name
34
33
  NAME
35
34
  end
@@ -121,6 +120,8 @@ module Contrast
121
120
  false
122
121
  end
123
122
 
123
+ # TODO: RUBY-318
124
+ # KNOWN_SECURITY_BYPASS_MARKERS = ['::$DATA', '::$Index', '', '\x00'].cs__freeze
124
125
  def contains_known_attack_signatures? input
125
126
  utf8 = Contrast::Utils::StringUtils.force_utf8(input)
126
127
  _ = CGI.unescape(utf8)
@@ -1,6 +1,7 @@
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
+ cs__scoped_require 'contrast/agent/protect/rule/base_service'
4
5
  cs__scoped_require 'contrast/extensions/ruby_core/protect/applies_sqli_rule'
5
6
 
6
7
  module Contrast
@@ -1,6 +1,8 @@
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
+ cs__scoped_require 'contrast/agent/protect/rule/base_service'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Protect
@@ -1,6 +1,8 @@
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
+ cs__scoped_require 'contrast/agent/protect/rule/base_service'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Protect
@@ -1,6 +1,7 @@
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
+ cs__scoped_require 'contrast/agent/protect/rule/base'
4
5
  cs__scoped_require 'contrast/utils/timer'
5
6
 
6
7
  module Contrast
@@ -11,7 +12,6 @@ module Contrast
11
12
  # of unsafe external entity resolution.
12
13
  class Xxe < Contrast::Agent::Protect::Rule::Base
13
14
  NAME = 'xxe'
14
- EXPLOIT_CHARACTERS = Contrast::Utils::ObjectShare::EMPTY_ARRAY
15
15
  BLOCK_MESSAGE = 'XXE rule triggered. Response blocked.'
16
16
  EXTERNAL_ENTITY_PATTERN = /<!ENTITY\s+[a-zA-Z0-f]+\s+(?:SYSTEM|PUBLIC)\s+(.*?)>/.cs__freeze
17
17
 
@@ -19,6 +19,14 @@ module Contrast
19
19
  NAME
20
20
  end
21
21
 
22
+ # Given an xml, evaluate it for an XXE attack.
23
+ #
24
+ # @param context [Contrast::Agent::RequestContext] the context of the
25
+ # request in which this input is evaluated.
26
+ # @param framework [Object] the name of the Parser being used.
27
+ # @param xml [Object] the container of the XML to be checked.
28
+ # @raise [Contrast::SecurityException] Security exception if an XXE
29
+ # attack is found and the rule is in block mode.
22
30
  def infilter context, framework, xml
23
31
  result = find_attacker(context, xml, framework: framework)
24
32
  return nil unless result
@@ -31,12 +39,10 @@ module Contrast
31
39
 
32
40
  protected
33
41
 
34
- def find_attacker context, xml, **kwargs
42
+ def find_attacker context, xml, **_kwargs
35
43
  return nil unless xml
36
44
  return nil if protect_excluded_by_code?
37
45
 
38
- logger.debug("checking: #{ name } in '#{ kwargs[:framework] }'")
39
-
40
46
  xxe_details, last_idx = build_details(xml)
41
47
  return nil unless xxe_details
42
48