contrast-agent 5.2.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  3. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +19 -5
  4. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  5. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -2
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  9. data/ext/cs__assess_regexp/cs__assess_regexp.c +15 -2
  10. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  11. data/ext/cs__assess_string/cs__assess_string.c +21 -1
  12. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  13. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  14. data/ext/cs__assess_test/extconf.rb +5 -0
  15. data/ext/cs__common/cs__common.c +113 -11
  16. data/ext/cs__common/cs__common.h +29 -5
  17. data/ext/cs__contrast_patch/cs__contrast_patch.c +55 -44
  18. data/ext/cs__os_information/cs__os_information.c +13 -10
  19. data/ext/cs__scope/cs__scope.c +146 -97
  20. data/ext/cs__tests/cs__tests.c +12 -0
  21. data/ext/cs__tests/cs__tests.h +3 -0
  22. data/ext/cs__tests/extconf.rb +5 -0
  23. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  24. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  25. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  26. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  27. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  28. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  29. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  30. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  31. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  32. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  33. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  34. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  35. data/lib/contrast/agent/assess/policy/propagator/split.rb +15 -19
  36. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  38. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  39. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  40. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  41. data/lib/contrast/agent/assess/policy/trigger_method.rb +4 -10
  42. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -2
  44. data/lib/contrast/agent/assess/rule/response/auto_complete_rule.rb +1 -1
  45. data/lib/contrast/agent/assess/rule/response/base_rule.rb +11 -3
  46. data/lib/contrast/agent/assess/rule/response/body_rule.rb +3 -3
  47. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +60 -36
  48. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +3 -3
  49. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +1 -1
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +1 -2
  51. data/lib/contrast/agent/at_exit_hook.rb +1 -1
  52. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  53. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  54. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  55. data/lib/contrast/agent/inventory/database_config.rb +10 -3
  56. data/lib/contrast/agent/middleware.rb +5 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +0 -2
  58. data/lib/contrast/agent/patching/policy/patch.rb +13 -12
  59. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  60. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  61. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  62. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +66 -9
  63. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  64. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/rule/base.rb +37 -5
  66. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  67. data/lib/contrast/agent/protect/rule/cmd_injection.rb +13 -0
  68. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  69. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  71. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +8 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  73. data/lib/contrast/agent/protect/rule/no_sqli.rb +27 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +18 -54
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -4
  76. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  79. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  80. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  81. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  82. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  83. data/lib/contrast/agent/reporting/masker/masker.rb +243 -0
  84. data/lib/contrast/agent/reporting/masker/masker_utils.rb +62 -0
  85. data/lib/contrast/agent/reporting/report.rb +2 -0
  86. data/lib/contrast/agent/reporting/reporter.rb +29 -22
  87. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  88. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +34 -0
  89. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +53 -0
  90. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +48 -0
  91. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +64 -0
  92. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +70 -0
  93. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +57 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +56 -0
  95. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +12 -4
  96. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +58 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +50 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  101. data/lib/contrast/agent/reporting/reporting_events/finding.rb +10 -4
  102. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  104. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  105. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  106. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +16 -12
  107. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  108. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  109. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +8 -11
  110. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  112. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  113. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  114. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  115. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  116. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  117. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +23 -7
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +64 -76
  120. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  121. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +100 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +75 -13
  124. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  125. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +154 -113
  126. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  127. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  128. data/lib/contrast/agent/reporting/settings/protect.rb +1 -1
  129. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +2 -2
  130. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  131. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  132. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  133. data/lib/contrast/agent/request.rb +3 -3
  134. data/lib/contrast/agent/request_context.rb +1 -1
  135. data/lib/contrast/agent/request_context_extend.rb +2 -2
  136. data/lib/contrast/agent/request_handler.rb +7 -3
  137. data/lib/contrast/agent/response.rb +2 -0
  138. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  139. data/lib/contrast/agent/static_analysis.rb +1 -1
  140. data/lib/contrast/agent/telemetry/base.rb +151 -0
  141. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  142. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +59 -0
  143. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +44 -0
  144. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +115 -0
  145. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +83 -0
  146. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +64 -0
  147. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  148. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +32 -0
  149. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  150. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  151. data/lib/contrast/agent/thread_watcher.rb +52 -68
  152. data/lib/contrast/agent/version.rb +1 -1
  153. data/lib/contrast/agent/worker_thread.rb +8 -0
  154. data/lib/contrast/agent.rb +4 -3
  155. data/lib/contrast/api/communication/messaging_queue.rb +28 -11
  156. data/lib/contrast/api/communication/response_processor.rb +7 -10
  157. data/lib/contrast/api/communication/speedracer.rb +1 -1
  158. data/lib/contrast/api/decorators/activity.rb +33 -0
  159. data/lib/contrast/api/decorators/address.rb +1 -1
  160. data/lib/contrast/api/decorators/http_request.rb +1 -1
  161. data/lib/contrast/api/decorators/response_type.rb +30 -0
  162. data/lib/contrast/api/decorators.rb +1 -0
  163. data/lib/contrast/components/app_context.rb +0 -4
  164. data/lib/contrast/components/assess.rb +14 -0
  165. data/lib/contrast/components/config.rb +13 -22
  166. data/lib/contrast/components/contrast_service.rb +9 -0
  167. data/lib/contrast/components/protect.rb +2 -2
  168. data/lib/contrast/components/sampling.rb +7 -11
  169. data/lib/contrast/components/settings.rb +116 -8
  170. data/lib/contrast/config/agent_configuration.rb +34 -41
  171. data/lib/contrast/config/api_configuration.rb +16 -75
  172. data/lib/contrast/config/api_proxy_configuration.rb +9 -48
  173. data/lib/contrast/config/application_configuration.rb +24 -95
  174. data/lib/contrast/config/assess_configuration.rb +21 -76
  175. data/lib/contrast/config/assess_rules_configuration.rb +13 -38
  176. data/lib/contrast/config/base_configuration.rb +11 -76
  177. data/lib/contrast/config/certification_configuration.rb +15 -68
  178. data/lib/contrast/config/exception_configuration.rb +15 -59
  179. data/lib/contrast/config/heap_dump_configuration.rb +19 -73
  180. data/lib/contrast/config/inventory_configuration.rb +11 -55
  181. data/lib/contrast/config/logger_configuration.rb +8 -41
  182. data/lib/contrast/config/protect_configuration.rb +23 -10
  183. data/lib/contrast/config/protect_rule_configuration.rb +23 -37
  184. data/lib/contrast/config/protect_rules_configuration.rb +39 -43
  185. data/lib/contrast/config/request_audit_configuration.rb +16 -55
  186. data/lib/contrast/config/root_configuration.rb +70 -13
  187. data/lib/contrast/config/ruby_configuration.rb +14 -47
  188. data/lib/contrast/config/sampling_configuration.rb +12 -65
  189. data/lib/contrast/config/server_configuration.rb +13 -45
  190. data/lib/contrast/config/service_configuration.rb +36 -17
  191. data/lib/contrast/configuration.rb +21 -13
  192. data/lib/contrast/extension/assess/string.rb +20 -1
  193. data/lib/contrast/extension/module.rb +0 -1
  194. data/lib/contrast/framework/manager.rb +2 -2
  195. data/lib/contrast/framework/rails/patch/support.rb +13 -45
  196. data/lib/contrast/logger/aliased_logging.rb +87 -0
  197. data/lib/contrast/logger/application.rb +1 -5
  198. data/lib/contrast/logger/cef_log.rb +1 -1
  199. data/lib/contrast/tasks/config.rb +100 -4
  200. data/lib/contrast/utils/assess/object_store.rb +36 -0
  201. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  202. data/lib/contrast/utils/class_util.rb +5 -18
  203. data/lib/contrast/utils/input_classification.rb +73 -0
  204. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  205. data/lib/contrast/utils/log_utils.rb +3 -1
  206. data/lib/contrast/utils/middleware_utils.rb +10 -9
  207. data/lib/contrast/utils/net_http_base.rb +1 -1
  208. data/lib/contrast/utils/object_share.rb +2 -1
  209. data/lib/contrast/utils/os.rb +0 -5
  210. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -5
  211. data/lib/contrast/utils/response_utils.rb +18 -33
  212. data/lib/contrast/utils/telemetry.rb +20 -2
  213. data/lib/contrast/utils/telemetry_client.rb +23 -11
  214. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  215. data/lib/contrast/utils/telemetry_identifier.rb +16 -1
  216. data/lib/contrast.rb +9 -0
  217. data/resources/assess/policy.json +98 -0
  218. data/resources/deadzone/policy.json +0 -86
  219. data/ruby-agent.gemspec +10 -9
  220. data/service_executables/VERSION +1 -1
  221. data/service_executables/linux/contrast-service +0 -0
  222. data/service_executables/mac/contrast-service +0 -0
  223. metadata +98 -30
  224. data/lib/contrast/agent/metric_telemetry_event.rb +0 -26
  225. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +0 -121
  226. data/lib/contrast/agent/telemetry.rb +0 -137
  227. data/lib/contrast/agent/telemetry_event.rb +0 -33
  228. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -2,10 +2,12 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'yaml'
5
+ require 'contrast/configuration'
6
+ require 'contrast/agent/reporting/reporter'
5
7
 
6
8
  module Contrast
7
9
  # A Rake task to generate a contrast_security.yaml file with some basic settings
8
- module Config
10
+ module Config # rubocop:disable Metrics/ModuleLength
9
11
  extend Rake::DSL
10
12
  DEFAULT_CONFIG = {
11
13
  'api' => {
@@ -29,6 +31,9 @@ module Contrast
29
31
  }
30
32
  }.cs__freeze
31
33
 
34
+ SKIP_LOG = %w[service_key api_key].cs__freeze
35
+ REQUIRED = %i[url api_key service_key user_name].cs__freeze
36
+
32
37
  namespace :contrast do
33
38
  namespace :config do
34
39
  desc 'Create a contrast_security.yaml in the applications root directory'
@@ -38,9 +43,7 @@ module Contrast
38
43
  if File.exist?(target_path)
39
44
  puts 'WARNING: contrast_security.yaml already exists'
40
45
  else
41
- File.open(target_path, 'w') do |f|
42
- f.write(YAML.dump(DEFAULT_CONFIG))
43
- end
46
+ File.write(target_path, YAML.dump(DEFAULT_CONFIG))
44
47
 
45
48
  puts "Created contrast_security.yaml at #{ target_path }"
46
49
  puts 'Open the file and enter your Contrast Security api keys or set them via environment variables'
@@ -49,5 +52,98 @@ module Contrast
49
52
  end
50
53
  end
51
54
  end
55
+
56
+ namespace :contrast do
57
+ namespace :config do
58
+ desc 'Validate the provided Contrast configuration and confirm connectivity'
59
+ task validate: :environment do
60
+ puts 'Validating Agent Configuration...'
61
+ Contrast::Config.validate_config
62
+ puts '...done!'
63
+ puts 'Validating Contrast Reporter Headers...'
64
+ reporter = Contrast::Config.validate_headers
65
+ puts '...done!'
66
+ puts 'Testing Reporter Client Connection...'
67
+ Contrast::Config.test_connection(reporter) if reporter
68
+ puts '...done!'
69
+ end
70
+ end
71
+
72
+ def self.validate_config
73
+ config = Contrast::Configuration.new
74
+ abort('Unable to Build Config') unless config
75
+ missing = []
76
+ api_hash = config.root.api.to_hash
77
+ api_hash.each_key do |key|
78
+ value = mask_keys api_hash, key
79
+ if value.is_a?(Contrast::Config::ApiProxyConfiguration)
80
+ Contrast::Config.validate_proxy(value)
81
+ elsif value.is_a?(Contrast::Config::CertificationConfiguration)
82
+ Contrast::Config.validate_cert(value)
83
+ next
84
+ elsif value.is_a?(Contrast::Config::RequestAuditConfiguration)
85
+ Contrast::Config.validate_audit(value)
86
+ next
87
+ elsif value.nil? && REQUIRED.includes?(key.to_sym)
88
+ missing << key
89
+ end
90
+ end
91
+ abort("Missing required API configuration values: #{ missing.join(', ') }") unless missing.empty?
92
+ end
93
+
94
+ def self.validate_proxy config
95
+ puts "Proxy Enabled: #{ config.enable }"
96
+ return unless config.enable
97
+
98
+ puts "Proxy URL: #{ config.url }"
99
+ abort('Proxy Enabled but no Proxy URL given') unless config.url
100
+ end
101
+
102
+ def self.validate_cert config
103
+ puts "Certification Enabled: #{ config.enable }"
104
+ return unless config.enable
105
+
106
+ puts "CA File: #{ config.ca_file }"
107
+ abort('CA file path not provided') unless config.ca_file
108
+ puts "Cert File: #{ config.cert_file }"
109
+ abort('Cert file path not provided') unless config.cert_file
110
+ puts "Key File: #{ config.key_file }"
111
+ abort('Key file path not provided') unless config.key_file
112
+ end
113
+
114
+ def self.validate_audit config
115
+ puts "Request Audit Enabled: #{ config.enable }"
116
+ return unless config.enable
117
+
118
+ config.each do |k, v|
119
+ puts "#{ k }::#{ v }"
120
+ end
121
+ end
122
+
123
+ def self.validate_headers
124
+ missing = []
125
+ reporter = Contrast::Agent::Reporter.new
126
+ reporter_headers = reporter.client.headers.to_hash
127
+ reporter_headers.each_key do |key|
128
+ value = mask_keys reporter_headers, key
129
+ missing << key if value.nil?
130
+ end
131
+ abort("Missing required header values: #{ missing.join(', ') }") unless missing.empty?
132
+ reporter
133
+ end
134
+
135
+ def self.test_connection reporter
136
+ connection = reporter.connection
137
+ abort('Failed to Initialize Connection please check error logs for details') unless connection
138
+ abort('Failed to Start Client please check error logs for details') unless reporter.client.startup! connection
139
+ end
140
+
141
+ def self.mask_keys hash, key
142
+ value = hash[key]
143
+ redacted_value = Contrast::Configuration::REDACTED if SKIP_LOG.include?(key.to_s)
144
+ puts "#{ key }::#{ redacted_value || value }" unless value.is_a?(Contrast::Config::BaseConfiguration)
145
+ value
146
+ end
147
+ end
52
148
  end
53
149
  end
@@ -0,0 +1,36 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Utils
6
+ module Assess
7
+ # keep track of object properties and events data
8
+ class ObjectStore
9
+ def initialize capacity = 500
10
+ @capacity = capacity
11
+ @cache = {}
12
+ end
13
+
14
+ def get
15
+ @cache
16
+ end
17
+
18
+ def [] key
19
+ @cache[key]
20
+ end
21
+
22
+ def delete key
23
+ @cache.delete(key)
24
+ end
25
+
26
+ # We would keep object ids with capacity.
27
+ # If a reference is kept to an object only by it's id,
28
+ # It would be CG-ted safely.
29
+ def []= key, value
30
+ @cache[key] = value
31
+ @cache.shift if @cache.length > @capacity
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -10,6 +10,7 @@ module Contrast
10
10
  APPEND_ACTION = 'APPEND'
11
11
  CENTER_ACTION = 'CENTER'
12
12
  INSERT_ACTION = 'INSERT'
13
+ BUFFER_ACTION = 'BUFFER'
13
14
  KEEP_ACTION = 'KEEP'
14
15
  NEXT_ACTION = 'NEXT'
15
16
  NOOP_ACTION = 'NOOP'
@@ -30,6 +31,7 @@ module Contrast
30
31
  INSERT_ACTION => Contrast::Agent::Assess::Policy::Propagator::Insert,
31
32
  KEEP_ACTION => Contrast::Agent::Assess::Policy::Propagator::Keep,
32
33
  NEXT_ACTION => Contrast::Agent::Assess::Policy::Propagator::Next,
34
+ BUFFER_ACTION => Contrast::Agent::Assess::Policy::Propagator::Buffer,
33
35
  NOOP_ACTION => nil,
34
36
  PREPEND_ACTION => Contrast::Agent::Assess::Policy::Propagator::Prepend,
35
37
  REPLACE_ACTION => Contrast::Agent::Assess::Policy::Propagator::Replace,
@@ -94,6 +96,10 @@ module Contrast
94
96
  def can_propagate? propagation_node, preshift, target
95
97
  return false unless appropriate_target?(propagation_node, target)
96
98
  return true if Contrast::Utils::Assess::TrackingUtil.tracked?(target)
99
+ if propagation_node.use_original_object?
100
+ # return true since we don't have preshift while using the original object.
101
+ return true
102
+ end
97
103
  return false unless preshift
98
104
 
99
105
  propagation_node.sources.each do |source|
@@ -50,8 +50,8 @@ module Contrast
50
50
  # After implementing the LRU Cache, we firstly need to check if already had that object cached and if we have
51
51
  # it - we can return it directly; otherwise we'll calculate and store the result before returning.
52
52
  #
53
- # TODO: RUBY-1327
54
- # Once we move to 2.7+, we can combine the caches using ID b/c the memory location stops being the id
53
+ # Combining of the caches have close performance, but keeping the two with current implementation has
54
+ # a slight advantage in performance. For now we can keep the things the way they are.
55
55
  #
56
56
  # @param object [Object, nil] the entity to convert to a String
57
57
  # @return [String, Object] the human readable form of the String, as defined by
@@ -86,28 +86,15 @@ module Contrast
86
86
  end
87
87
  end
88
88
 
89
- # The method const_defined? can cause autoload, which is bad for us. The method autoload? doesn't traverse
90
- # namespaces. This method lets us provide a constant, as a String, and parse it to determine if it has been
91
- # truly truly defined, meaning it existed before this method was invoked, not as a result of it.
89
+ # The method Module.const_defined? can raise an exception if the constant is poorly named. As such, we need to
90
+ # handle the case where that exception is raised.
92
91
  #
93
- # TODO: RUBY-1326
94
- # This is required to handle a bug in Ruby prior to 2.7.0. When we drop support for 2.6.X, we should remove
95
- # this code. https://bugs.ruby-lang.org/issues/10741
96
92
  # @param name [String] the name of the constant to look up
97
93
  # @return [Boolean]
98
94
  def truly_defined? name
99
95
  return false unless name
100
96
 
101
- segments = name.split(Contrast::Utils::ObjectShare::DOUBLE_COLON)
102
- previous_module = Module
103
- segments.each do |segment|
104
- return false if previous_module.cs__autoload?(segment)
105
- return false unless previous_module.cs__const_defined?(segment)
106
-
107
- previous_module = previous_module.cs__const_get(segment)
108
- end
109
-
110
- true
97
+ Module.cs__const_defined?(name)
111
98
  rescue NameError # account for nonsense / poorly formatted constants
112
99
  false
113
100
  end
@@ -0,0 +1,73 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/utils/object_share'
5
+ require 'contrast/agent/reporting/input_analysis/input_type'
6
+ require 'contrast/agent/reporting/input_analysis/score_level'
7
+ require 'contrast/agent/protect/input_analyzer/input_analyzer'
8
+
9
+ module Contrast
10
+ module Agent
11
+ module Protect
12
+ module Rule
13
+ # This module will include all the similar information for all input classifications
14
+ # between different rules
15
+ module InputClassificationBase
16
+ include Contrast::Agent::Reporting::InputType
17
+ include Contrast::Agent::Reporting::ScoreLevel
18
+
19
+ # Input Classification stage is done to determine if an user input is
20
+ # WORTHWATCHING or to be ignored.
21
+ #
22
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
23
+ # @param value [String, Array<String>] the value of the input.
24
+ # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the
25
+ # agent analysis from the current
26
+ # Request.
27
+ # @return ia [Contrast::Agent::Reporting::InputAnalysis] with updated results.
28
+ def classify input_type, value, input_analysis # rubocop:disable Lint/UnusedMethodArgument
29
+ return false unless input_analysis.request
30
+
31
+ true
32
+ end
33
+
34
+ # Creates new isntance of InputAnalysisResult with basic info.
35
+ #
36
+ # @param rule_id [String] The name of the Protect Rule.
37
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
38
+ # @param value [String, Array<String>] the value of the input.
39
+ # @param path [String] the path of the current request context.
40
+ #
41
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
42
+ def new_ia_result rule_id, input_type, path, value = nil
43
+ res = Contrast::Agent::Reporting::InputAnalysisResult.new
44
+ res.rule_id = rule_id
45
+ res.input_type = input_type
46
+ res.path = path
47
+ res.value = value
48
+ res
49
+ end
50
+
51
+ # This methods checks if input is value that matches a key in the input.
52
+ #
53
+ # @param request [Contrast::Agent::Request] the current request context.
54
+ # @param result [Contrast::Agent::Reporting::InputAnalysisResult] result to be updated.
55
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
56
+ # @param value [String, Array<String>] the value of the input.
57
+ #
58
+ # @return result [Contrast::Agent::Reporting::InputAnalysisResult] updated with key result.
59
+ def add_needed_key request, result, input_type, value
60
+ case input_type
61
+ when COOKIE_VALUE
62
+ result.key = request.cookies.key(value)
63
+ when PARAMETER_VALUE
64
+ result.key = request.parameters.key(value)
65
+ else
66
+ result.key
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -53,7 +53,7 @@ module Contrast
53
53
  ruby_finding = Contrast::Agent::Reporting::Finding.new rule_id
54
54
  ruby_finding.hash_code = hash_code
55
55
  set_new_finding_properties(ruby_finding, user_provided_options, call_location)
56
- Contrast::Agent.reporter&.send_event_immediately(new_preflight)
56
+ Contrast::Agent.reporter&.send_event(new_preflight)
57
57
  Contrast::Agent::Reporting::ReportingStorage[hash_code] = ruby_finding
58
58
  end
59
59
 
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'socket'
5
5
  require 'contrast/agent/version'
6
+ require 'contrast/logger/aliased_logging'
6
7
 
7
8
  module Contrast
8
9
  module Utils
@@ -37,6 +38,7 @@ module Contrast
37
38
  logger.extend(Contrast::Logger::Application)
38
39
  logger.extend(Contrast::Logger::Request)
39
40
  logger.extend(Contrast::Logger::Time)
41
+ logger.extend(Contrast::Logger::AliasedLogging) if Contrast::Utils::Telemetry.exceptions_enabled?
40
42
  end
41
43
 
42
44
  # Determine the valid path to which to log, given the precedence of config > settings > default.
@@ -160,7 +162,7 @@ module Contrast
160
162
  message << msg[0]
161
163
  message << severity
162
164
  message << extract_metadata(msg[1], msg[2])
163
- message.join('|') + "\n"
165
+ "#{ message.join('|') }\n"
164
166
  end
165
167
  end
166
168
 
@@ -8,6 +8,13 @@ module Contrast
8
8
  module MiddlewareUtils
9
9
  private
10
10
 
11
+ # TODO: RUBY-1609 update this part of the method for Ruby Version and Year
12
+ LANGUAGE_DEPRECATION_VERSION = '2.7'
13
+ LANGUAGE_DEPRECATION_YEAR = '2023'
14
+ LANGUAGE_DEPRECATION_WARNING =
15
+ "[Contrast Security] [DEPRECATION] Support for Ruby #{ LANGUAGE_DEPRECATION_VERSION } will be removed in "\
16
+ "April #{ LANGUAGE_DEPRECATION_YEAR }. Please contact Customer Support prior if you require continued support."
17
+
11
18
  def setup_agent
12
19
  ::Contrast::SETTINGS.reset_state
13
20
 
@@ -51,21 +58,15 @@ module Contrast
51
58
  # deprecated and soon to be removed functionality. This method handles doing that by leveraging the standard
52
59
  # Kernel#warn approach
53
60
  def inform_deprecations
54
- # Ruby 2.6 will be dropped in april of 2022. We'll begin the deprecation warnings
55
- # now so that customers have time to reach out if they'll be impacted.
56
- # TODO: RUBY-1188 remove this part of the method, leaving it empty if there are no other deprecations, when we
57
- # drop 2.6 support.
58
- return unless RUBY_VERSION < '2.7.0'
59
-
60
- Kernel.warn('[Contrast Security] [DEPRECATION] Support for Ruby 2.6 will be removed in April 2022. '\
61
- 'Please contact Customer Support prior if you require continued support.')
61
+ # Warn customers that they're on a version that's losing support in the next year.
62
+ Kernel.warn(LANGUAGE_DEPRECATION_WARNING) if RUBY_VERSION.start_with?(LANGUAGE_DEPRECATION_VERSION)
62
63
  end
63
64
 
64
65
  # Displays Telemetry disclaimer if Telemetry is enabled.
65
66
  # if .telemetry file doesn't exist we create one and then show the disclaimer.
66
67
  # if the file already exists we do nothing.
67
68
  def telemetry_disclaimer
68
- return unless Contrast::Agent::Telemetry.enabled?
69
+ return unless Contrast::Agent::Telemetry::Base.enabled?
69
70
  return unless Contrast::Utils::Telemetry.create_telemetry_file
70
71
 
71
72
  logger.info Contrast::Utils::Telemetry.disclaimer
@@ -22,7 +22,7 @@ module Contrast
22
22
  # @param use_custom_cert [Boolean] flag used to indicate whether the client is to use
23
23
  # self signed certificates provided by config [default = false]
24
24
  # @return [Net::HTTP, nil] Return open connection or nil
25
- def initialize_connection service_name, url, use_proxy = false, use_custom_cert = false
25
+ def initialize_connection service_name, url, use_proxy: false, use_custom_cert: false
26
26
  return unless url
27
27
 
28
28
  addr = URI(url)
@@ -23,6 +23,7 @@ module Contrast
23
23
  HTTPS_START = 'https:'
24
24
  NEW_LINE = "\n"
25
25
  NIL_STRING = 'nil'
26
+ NIL_64_STRING = 'bmls'
26
27
  PERIOD = '.'
27
28
  POUND_SIGN = '#'
28
29
  QUESTION_MARK = '?'
@@ -38,6 +39,7 @@ module Contrast
38
39
  COLON_SLASH_SLASH = '://'
39
40
  DOLLAR_SIGN = '$'
40
41
  CARROT = '^'
42
+ BANG = '!'
41
43
 
42
44
  WRITE_FLAG = 'w'
43
45
 
@@ -47,7 +49,6 @@ module Contrast
47
49
  CACHE = 'cache'
48
50
 
49
51
  CONTRAST_PATCHED_METHOD_START = 'cs__patched_'
50
- DOUBLE_COLON = '::'
51
52
 
52
53
  EMPTY_ARRAY = [].freeze
53
54
  EMPTY_HASH = {}.freeze
@@ -53,11 +53,6 @@ module Contrast
53
53
  def linux?
54
54
  (unix? and !mac?)
55
55
  end
56
-
57
- def jruby?
58
- @_jruby = RUBY_ENGINE == 'jruby' if @_jruby.nil?
59
- @_jruby
60
- end
61
56
  end
62
57
  end
63
58
  end
@@ -35,10 +35,9 @@ module Contrast
35
35
  # Given a method, return a symbol in the format
36
36
  # <method_start>_unbound_<method_name>
37
37
  def build_unbound_method_name patcher_method
38
- (Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
39
- 'unbound' +
40
- Contrast::Utils::ObjectShare::UNDERSCORE +
41
- patcher_method.to_s).to_sym
38
+ "#{ Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START }unbound"\
39
+ "#{ Contrast::Utils::ObjectShare::UNDERSCORE }"\
40
+ "#{ patcher_method }".to_sym
42
41
  end
43
42
 
44
43
  # ===== PATCH APPLIERS =====
@@ -138,7 +137,7 @@ module Contrast
138
137
  if method_policy.source_node
139
138
  # If we were given a frozen return, and it was the target of a source, and we have frozen sources enabled,
140
139
  # we'll need to replace the return. Note, this is not the default case.
141
- source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret, args)
140
+ source_ret = Contrast::Agent::Assess::Policy::SourceMethod.apply_source(method_policy, object, ret, args)
142
141
  end
143
142
  if method_policy.propagation_node
144
143
  propagated_ret = Contrast::Agent::Assess::Policy::PropagationMethod.apply_propagation(
@@ -36,39 +36,24 @@ module Contrast
36
36
  return unless body
37
37
  return if defined?(Rack::File) && body.is_a?(Rack::File)
38
38
 
39
- case body
40
- when Rack::BodyProxy
41
- handle_rack_body_proxy(body)
42
- when ActionDispatch::Response::RackBody
43
- extract_body(body.body) if defined?(ActionDispatch::Response::RackBody)
44
- when Rack::Response
45
- extract_body(body.body)
46
- when Contrast::Utils::DuckUtils.quacks_to?(body, :each)
47
- acc = []
48
- body.each { |tmp| acc << read_or_string(tmp) }
49
- acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
50
- when ActionView::OutputBuffer
51
- # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
52
- body.to_str
53
- else
54
- read_or_string(body)
55
- end
56
- # if body.is_a?(Rack::BodyProxy)
57
- # handle_rack_body_proxy(body)
58
- # elsif (defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)) ||
59
- # body.is_a?(Rack::Response)
60
- #
61
- # extract_body(body.body)
62
- # elsif Contrast::Utils::DuckUtils.quacks_to?(body, :each)
63
- # acc = []
64
- # body.each { |tmp| acc << read_or_string(tmp) }
65
- # acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
66
- # elsif ActionView::OutputBuffer
67
- # # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
68
- # body.to_str
69
- # else
70
- # read_or_string(body)
71
- # end
39
+ return handle_rack_body_proxy(body) if body.is_a?(Rack::BodyProxy)
40
+ return extract_body(body.body) if sub_extractable?(body)
41
+ return enumerable_text_from(body) if Contrast::Utils::DuckUtils.quacks_to?(body, :each)
42
+ # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
43
+ return body.to_str if body.is_a?(ActionView::OutputBuffer)
44
+
45
+ read_or_string(body)
46
+ end
47
+
48
+ def sub_extractable? body
49
+ (defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)) ||
50
+ body.is_a?(Rack::Response)
51
+ end
52
+
53
+ def enumerable_text_from body
54
+ entries = body.map { |entry| read_or_string(entry) }
55
+ entries.compact!
56
+ entries.join(Contrast::Utils::ObjectShare::NEW_LINE)
72
57
  end
73
58
 
74
59
  def handle_rack_body_proxy body
@@ -1,8 +1,9 @@
1
1
  # Copyright (c) 2022 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/telemetry'
4
+ require 'contrast/agent/telemetry/base'
5
5
  require 'contrast/utils/telemetry_identifier'
6
+ require 'contrast/config/env_variables'
6
7
 
7
8
  module Contrast
8
9
  module Utils
@@ -37,7 +38,17 @@ module Contrast
37
38
  @_disclaimer = MESSAGE[:disclaimer]
38
39
  end
39
40
 
41
+ def self.exceptions_enabled?
42
+ # TODO: RUBY-1643 Obfuscate path
43
+ # Enabled this once the masking of stack frames is done.
44
+ # @_exceptions_enabled = telemetry_exceptions_enabled? if @_exceptions_enabled.nil?
45
+ @_exceptions_enabled = false if @_exceptions_enabled.nil?
46
+ @_exceptions_enabled
47
+ end
48
+
40
49
  class << self
50
+ include Contrast::Config::EnvVariables
51
+
41
52
  private
42
53
 
43
54
  # Create the mark file
@@ -48,7 +59,7 @@ module Contrast
48
59
  # @return[Boolean, nil] true if file is created, false if file already exist
49
60
  # and nil if Telemetry is disabled or on unsupported OS.
50
61
  def write_mark_file dir, file, config_dir
51
- return unless Contrast::Agent::Telemetry.enabled?
62
+ return unless Contrast::Agent::Telemetry::Base.enabled?
52
63
  return if Contrast::Utils::OS.windows?
53
64
 
54
65
  @dir = dir
@@ -73,6 +84,13 @@ module Contrast
73
84
  rescue StandardError => _e
74
85
  false
75
86
  end
87
+
88
+ def telemetry_exceptions_enabled?
89
+ opts_out_telemetry = return_value(:telemetry_opt_outs).to_s
90
+ return false if opts_out_telemetry.casecmp?('true') || opts_out_telemetry == '1'
91
+
92
+ true
93
+ end
76
94
  end
77
95
  end
78
96
  end
@@ -13,6 +13,7 @@ module Contrast
13
13
  # This module creates a Net::HTTP client and initiates a connection to the provided result
14
14
  class TelemetryClient < NetHttpBase
15
15
  ENDPOINT = 'api/v1/telemetry/metrics' # /TelemetryEvent.path
16
+ EXCEPTIONS = 'api/v1/telemetry/exceptions' # /TelemetryExceptions::Event.path
16
17
  SERVICE_NAME = 'Telemetry'
17
18
  include Contrast::Components::Logger::InstanceMethods
18
19
  # This method initializes the Net::HTTP client we'll need. it will validate
@@ -22,29 +23,34 @@ module Contrast
22
23
  # @param url [String]
23
24
  # @return [Net::HTTP, nil] Return open connection or nil
24
25
  def initialize_connection url
25
- super(SERVICE_NAME, url, false, false)
26
+ super(SERVICE_NAME, url, use_proxy: false, use_custom_cert: false)
26
27
  end
27
28
 
28
29
  # This method will be responsible for building the request. Because the telemetry collector expects to receive
29
30
  # multiple events in a single request, we must always wrap the event in an array, even if there is only one.
30
31
  #
31
- # @param event [Contrast::Agent::TelemetryEvent,Contrast::Agent::StartupMetricsTelemetryEvent]
32
+ # @param event [Contrast::Agent::Telemetry::Event, Array<Contrast::Agent::Telemetry::TelemetryException::Event>]
32
33
  # @return [Net::HTTP::Post]
33
34
  def build_request event
34
35
  return unless valid_event? event
35
36
 
36
- string_body = [event.to_hash].to_json
37
+ string_body = if Array(event).all?(Contrast::Agent::Telemetry::TelemetryException::Event)
38
+ event.map(&:to_controlled_hash).flatten!
39
+ else
40
+ [event.to_hash]
41
+ end
42
+
37
43
  header = {
38
44
  'User-Agent' => "<#{ Contrast::Utils::ObjectShare::RUBY }>-<#{ Contrast::Agent::VERSION }>",
39
45
  'Content-Type' => 'application/json'
40
46
  }
41
- request = Net::HTTP::Post.new(build_path(event.path), header)
42
- request.body = string_body
47
+ request = Net::HTTP::Post.new(build_path(event), header)
48
+ request.body = string_body.to_json
43
49
  request
44
50
  end
45
51
 
46
52
  # This method will create the actual request and send it
47
- # @param event[Contrast::Agent::TelemetryEvent]
53
+ # @param event[Contrast::Agent::Telemetry::Event]
48
54
  # @param connection[Net::HTTP]
49
55
  def send_request event, connection
50
56
  return if connection.nil? || event.nil?
@@ -68,10 +74,13 @@ module Contrast
68
74
  end
69
75
 
70
76
  # This method will be responsible for validating the event
71
- # @param event[Contrast::Agent::TelemetryEvent,Contrast::Agent::StartupMetricsTelemetryEvent]
77
+ # @param event[Contrast::Agent::Telemetry::Event,Contrast::Agent::Telemetry::StartupMetricsEvent,
78
+ # array<Contrast::Agent::Telemetry::TelemetryException::Event>]
72
79
  def valid_event? event
73
- return true if event.cs__is_a?(Contrast::Agent::TelemetryEvent)
74
- return true if event.cs__is_a?(Contrast::Agent::StartupMetricsTelemetryEvent)
80
+ return true if event.cs__is_a?(Contrast::Agent::Telemetry::Event)
81
+ return true if event.cs__is_a?(Contrast::Agent::Telemetry::StartupMetricsEvent)
82
+ # Batch
83
+ return true if Array(event).all?(Contrast::Agent::Telemetry::TelemetryException::Event)
75
84
 
76
85
  false
77
86
  end
@@ -81,9 +90,12 @@ module Contrast
81
90
  # The telemetry instance accepts any path to #{ Contrast::Agent::Telemetry::URL }#{ ENDPOINT }, using the
82
91
  # remainder of the path to segregate messages.
83
92
  #
93
+ # @param event [Contrast::Agent::Telemetry::Event, Contrast::Agent::Telemetry::TelemetryException::Event]
84
94
  # @return [String] the fully qualified path to send the request
85
- def build_path event_path
86
- "#{ Contrast::Agent::Telemetry::URL }#{ ENDPOINT }#{ event_path }"
95
+ def build_path event
96
+ endpoint = Array(event).all?(Contrast::Agent::Telemetry::TelemetryException::Event) ? EXCEPTIONS : ENDPOINT
97
+ path = endpoint == EXCEPTIONS ? Contrast::Agent::Telemetry::TelemetryException::Event.path : event.path
98
+ "#{ Contrast::Agent::Telemetry::Base::URL }#{ endpoint }#{ path }"
87
99
  end
88
100
  end
89
101
  end