contrast-agent 6.1.0 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -1
  3. data/Rakefile +1 -1
  4. data/ext/build_funchook.rb +3 -3
  5. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -1
  6. data/ext/extconf_common.rb +1 -1
  7. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -2
  8. data/lib/contrast/agent/assess/policy/policy.rb +9 -10
  9. data/lib/contrast/agent/assess/policy/policy_node.rb +9 -10
  10. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -3
  11. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -3
  12. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  13. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +2 -1
  14. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -1
  15. data/lib/contrast/agent/assess/policy/propagator/splat.rb +1 -1
  16. data/lib/contrast/agent/assess/policy/propagator/split.rb +2 -2
  17. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  18. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  19. data/lib/contrast/agent/assess/policy/trigger_method.rb +7 -7
  20. data/lib/contrast/agent/assess/policy/trigger_node.rb +16 -16
  21. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  22. data/lib/contrast/agent/assess/property/evented.rb +2 -2
  23. data/lib/contrast/agent/assess/property/tagged.rb +2 -2
  24. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +6 -8
  25. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +6 -7
  26. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +5 -5
  27. data/lib/contrast/agent/assess/rule/response/base_rule.rb +2 -3
  28. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +8 -9
  29. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +4 -4
  30. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +6 -6
  31. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +4 -4
  32. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +4 -4
  33. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +4 -4
  34. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +3 -4
  35. data/lib/contrast/agent/assess/tag.rb +13 -14
  36. data/lib/contrast/agent/at_exit_hook.rb +12 -1
  37. data/lib/contrast/agent/middleware.rb +6 -3
  38. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -3
  39. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +2 -2
  40. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +4 -4
  41. data/lib/contrast/agent/patching/policy/patch.rb +9 -9
  42. data/lib/contrast/agent/patching/policy/patch_status.rb +10 -3
  43. data/lib/contrast/agent/patching/policy/policy.rb +13 -15
  44. data/lib/contrast/agent/patching/policy/policy_node.rb +19 -21
  45. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  46. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +125 -125
  47. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  48. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +1 -1
  49. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +1 -1
  50. data/lib/contrast/agent/protect/policy/rule_applicator.rb +4 -4
  51. data/lib/contrast/agent/protect/rule/base.rb +30 -18
  52. data/lib/contrast/agent/protect/rule/base_service.rb +31 -14
  53. data/lib/contrast/agent/protect/rule/cmd_injection.rb +16 -9
  54. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +3 -3
  55. data/lib/contrast/agent/protect/rule/default_scanner.rb +2 -1
  56. data/lib/contrast/agent/protect/rule/deserialization.rb +18 -7
  57. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +74 -74
  58. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +71 -53
  59. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +3 -3
  60. data/lib/contrast/agent/protect/rule/no_sqli.rb +15 -16
  61. data/lib/contrast/agent/protect/rule/path_traversal.rb +13 -3
  62. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -2
  63. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -1
  64. data/lib/contrast/agent/protect/rule/sqli.rb +16 -23
  65. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +61 -61
  66. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +29 -29
  67. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +32 -32
  68. data/lib/contrast/agent/protect/rule/xss.rb +17 -0
  69. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +14 -13
  70. data/lib/contrast/agent/protect/rule/xxe.rb +25 -3
  71. data/lib/contrast/agent/reaction_processor.rb +1 -1
  72. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +36 -36
  73. data/lib/contrast/agent/reporting/masker/masker.rb +10 -10
  74. data/lib/contrast/agent/reporting/masker/masker_utils.rb +2 -2
  75. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +8 -10
  76. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +53 -5
  77. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +25 -19
  78. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +129 -17
  79. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +20 -21
  80. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack.rb +22 -0
  81. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +26 -12
  82. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +5 -5
  83. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +7 -5
  84. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +4 -10
  85. data/lib/contrast/agent/reporting/reporting_events/discovered_route.rb +1 -1
  86. data/lib/contrast/agent/reporting/reporting_utilities/headers.rb +1 -2
  87. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +6 -6
  88. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +1 -1
  89. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +1 -1
  90. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +7 -7
  91. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +15 -15
  92. data/lib/contrast/agent/reporting/settings/application_settings.rb +1 -1
  93. data/lib/contrast/agent/reporting/settings/assess.rb +5 -5
  94. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +3 -3
  95. data/lib/contrast/agent/reporting/settings/exclusions.rb +3 -3
  96. data/lib/contrast/agent/reporting/settings/protect.rb +20 -5
  97. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +5 -5
  98. data/lib/contrast/agent/reporting/settings/reaction.rb +3 -3
  99. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +2 -2
  100. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +2 -2
  101. data/lib/contrast/agent/reporting/settings/server_features.rb +2 -2
  102. data/lib/contrast/agent/request.rb +2 -2
  103. data/lib/contrast/agent/request_context.rb +23 -19
  104. data/lib/contrast/agent/request_context_extend.rb +10 -23
  105. data/lib/contrast/agent/request_handler.rb +1 -1
  106. data/lib/contrast/agent/rule_set.rb +2 -2
  107. data/lib/contrast/agent/scope.rb +1 -1
  108. data/lib/contrast/agent/telemetry/base.rb +9 -5
  109. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +119 -0
  110. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +2 -2
  111. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +1 -1
  112. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +1 -1
  113. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +16 -18
  114. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +2 -2
  115. data/lib/contrast/agent/version.rb +1 -1
  116. data/lib/contrast/api/communication/messaging_queue.rb +1 -1
  117. data/lib/contrast/api/communication/service_lifecycle.rb +1 -1
  118. data/lib/contrast/api/communication/socket.rb +1 -1
  119. data/lib/contrast/api/communication/socket_client.rb +1 -1
  120. data/lib/contrast/api/communication/speedracer.rb +2 -2
  121. data/lib/contrast/api/decorators/agent_startup.rb +10 -9
  122. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  123. data/lib/contrast/api/decorators/application_startup.rb +4 -4
  124. data/lib/contrast/api/decorators/response_type.rb +4 -17
  125. data/lib/contrast/components/agent.rb +1 -1
  126. data/lib/contrast/components/base.rb +1 -1
  127. data/lib/contrast/components/config.rb +6 -6
  128. data/lib/contrast/components/contrast_service.rb +4 -1
  129. data/lib/contrast/components/sampling.rb +1 -1
  130. data/lib/contrast/components/settings.rb +52 -28
  131. data/lib/contrast/config/assess_rules_configuration.rb +1 -1
  132. data/lib/contrast/config/protect_rules_configuration.rb +1 -1
  133. data/lib/contrast/config/root_configuration.rb +1 -1
  134. data/lib/contrast/configuration.rb +4 -4
  135. data/lib/contrast/extension/assess/array.rb +1 -1
  136. data/lib/contrast/extension/assess/erb.rb +1 -1
  137. data/lib/contrast/extension/assess/marshal.rb +1 -1
  138. data/lib/contrast/extension/assess/string.rb +1 -1
  139. data/lib/contrast/extension/extension.rb +2 -2
  140. data/lib/contrast/framework/base_support.rb +8 -8
  141. data/lib/contrast/framework/grape/support.rb +3 -3
  142. data/lib/contrast/framework/manager.rb +5 -5
  143. data/lib/contrast/framework/manager_extend.rb +1 -1
  144. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  145. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +14 -3
  146. data/lib/contrast/framework/rails/patch/assess_configuration.rb +3 -3
  147. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  148. data/lib/contrast/framework/rails/patch/support.rb +1 -1
  149. data/lib/contrast/framework/rails/support.rb +2 -2
  150. data/lib/contrast/framework/sinatra/support.rb +1 -1
  151. data/lib/contrast/logger/aliased_logging.rb +29 -22
  152. data/lib/contrast/logger/cef_log.rb +14 -14
  153. data/lib/contrast/logger/format.rb +1 -1
  154. data/lib/contrast/logger/log.rb +8 -8
  155. data/lib/contrast/tasks/config.rb +12 -12
  156. data/lib/contrast/tasks/service.rb +2 -2
  157. data/lib/contrast/utils/assess/tracking_util.rb +4 -4
  158. data/lib/contrast/utils/class_util.rb +4 -4
  159. data/lib/contrast/utils/findings.rb +3 -3
  160. data/lib/contrast/utils/hash_digest.rb +6 -7
  161. data/lib/contrast/utils/head_dump_utils_extend.rb +1 -1
  162. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  163. data/lib/contrast/utils/log_utils.rb +4 -4
  164. data/lib/contrast/utils/lru_cache.rb +1 -1
  165. data/lib/contrast/utils/metrics_hash.rb +1 -1
  166. data/lib/contrast/utils/middleware_utils.rb +5 -5
  167. data/lib/contrast/utils/net_http_base.rb +4 -4
  168. data/lib/contrast/utils/os.rb +1 -1
  169. data/lib/contrast/utils/patching/policy/patch_utils.rb +2 -2
  170. data/lib/contrast/utils/request_utils.rb +2 -2
  171. data/lib/contrast/utils/sha256_builder.rb +4 -4
  172. data/lib/contrast/utils/stack_trace_utils.rb +31 -13
  173. data/lib/contrast/utils/telemetry.rb +6 -9
  174. data/lib/contrast/utils/telemetry_client.rb +5 -5
  175. data/lib/contrast/utils/telemetry_hash.rb +1 -1
  176. data/lib/contrast/utils/telemetry_identifier.rb +2 -2
  177. data/lib/contrast/utils/timer.rb +1 -1
  178. data/resources/assess/policy.json +1 -1
  179. metadata +15 -13
@@ -20,131 +20,131 @@ module Contrast
20
20
  # InputAnalyzer will extract input form current request context and will analyze it.
21
21
  # This will be used in for the SQLI and CMDI worth_watching_v2 implementations.
22
22
  module InputAnalyzer
23
- DISPOSITION_NAME = 'name'
24
- DISPOSITION_FILENAME = 'filename'
25
-
26
- class << self
27
- include Contrast::Agent::Reporting::InputType
28
- include Contrast::Agent::Reporting::ScoreLevel
29
- include Contrast::Agent::Protect::Rule::SqliWorthWatching
30
- include Contrast::Utils::ObjectShare
31
- include Contrast::Agent::Protect::Rule::CmdiWorthWatching
32
-
33
- PROTECT_RULES = {
34
- sqli: {
35
- rule_name: 'sql-injection',
36
- classification: Contrast::Agent::Protect::Rule::SqliInputClassification
37
- },
38
- cmdi: {
39
- rule_name: 'cmd-injection',
40
- classification: Contrast::Agent::Protect::Rule::CmdiInputClassification
41
- },
42
- method_tampering: {
43
- rule_name: 'method-tampering',
44
- classification: Contrast::Agent::Protect::Rule::HttpMethodTamperingInputClassification
45
- },
46
- unsafe_file_upload: {
47
- rule_name: 'unsafe-file-upload',
48
- classification: Contrast::Agent::Protect::Rule::UnsafeFileUploadInputClassification
49
- },
50
- nosqli: {
51
- rule_name: 'nosql-injection',
52
- classification: Contrast::Agent::Protect::Rule::NoSqliInputClassification
53
- }
54
- }.cs__freeze
55
-
56
- # This method with analyze the user input from the context of the
57
- # current request and run each of the protect rules against all
58
- # found input types
59
- #
60
- # @param request [Contrast::Agent::Request] current request context.
61
- # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
62
- def analyse request
63
- return unless Contrast::PROTECT.enabled?
64
- return if request.nil?
65
-
66
- inputs = extract_input request
67
- return unless inputs
68
-
69
- input_analysis = Contrast::Agent::Reporting::InputAnalysis.new
70
- input_analysis.request = request
71
- # each rule against each input
72
- input_classification inputs, input_analysis
73
- input_analysis
74
- end
75
-
76
- private
77
-
78
- # classify input by rule implementation of worth_watching_v2 for the rules supporting it.
79
- #
80
- # @param inputs [String, Array<String>] user input to be analysed.
81
- # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Here we will keep all the results
82
- # for each protect rule.
83
- # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
84
- def input_classification inputs, input_analysis
85
- # key = input type, value = user_input
86
- inputs.each do |input_type, value|
87
- next if value.nil? || value.empty?
88
-
89
- PROTECT_RULES.each do |_key, rule|
90
- protect_rule = Contrast::PROTECT.rule(rule[:rule_name])
91
- logger.debug("Rule #{ rule[:rule_name] } not recognised in Protect rules") if protect_rule.nil?
92
-
93
- # check if rule is enabled
94
- next unless protect_rule&.enabled?
95
-
96
- # method tampering doesn't take value
97
- if rule[:rule_name] == Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
98
- rule[:classification].send(:classify, input_type, input_analysis)
99
- else
100
- rule[:classification].send(:classify, input_type, value, input_analysis)
101
- end
102
- end
103
- end
104
- input_analysis
105
- end
106
-
107
- # Extract the inputs from the request context and label them with Protect
108
- # input type tags. Each tag will contain one or more user inputs.
109
- #
110
- # This methods is to be expanded and modified as needed by other Protect rules
111
- # and sub-rules for their requirements.
112
- #
113
- # @param request [Contrast::Agent::Request] current request context.
114
- # @return inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
115
- def extract_input request
116
- inputs = {}
117
- inputs[BODY] = request.body
118
- inputs[COOKIE_NAME] = request.cookies.keys
119
- inputs[COOKIE_VALUE] = request.cookies.values
120
- inputs[HEADER] = request.headers
121
- inputs[PARAMETER_NAME] = request.parameters.keys
122
- inputs[PARAMETER_VALUE] = request.parameters.values
123
- inputs[QUERYSTRING] = request.query_string
124
- inputs[METHOD] = request.request_method
125
- extract_multipart inputs, request
126
- inputs.compact!
127
- inputs
128
- end
129
-
130
- # Extract the filename and name of the Content Disposition Header.
131
- #
132
- # @param inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
133
- # @param request [Contrast::Agent::Request] current request context.
134
- def extract_multipart inputs, request
135
- disposition = request.rack_request.env['Content-Disposition']
136
- disposition = request.rack_request.env['CONTENT_DISPOSITION'] if disposition.nil? || disposition.empty?
137
- return unless disposition
138
-
139
- pairs = {}
140
- disposition.split(SEMICOLON).each do |elem|
141
- new_pair = elem.strip.split(EQUALS, 2)
142
- pairs[new_pair[0].downcase] = new_pair[1] if new_pair
143
- end
144
- inputs[MULTIPART_NAME] = pairs[DISPOSITION_NAME]
145
- inputs[MULTIPART_FIELD_NAME] = pairs[DISPOSITION_FILENAME]
146
- end
147
- end
23
+ # DISPOSITION_NAME = 'name'
24
+ # DISPOSITION_FILENAME = 'filename'
25
+ #
26
+ # class << self
27
+ # include Contrast::Agent::Reporting::InputType
28
+ # include Contrast::Agent::Reporting::ScoreLevel
29
+ # include Contrast::Agent::Protect::Rule::SqliWorthWatching
30
+ # include Contrast::Utils::ObjectShare
31
+ # include Contrast::Agent::Protect::Rule::CmdiWorthWatching
32
+ #
33
+ # PROTECT_RULES = {
34
+ # sqli: {
35
+ # rule_name: 'sql-injection',
36
+ # classification: Contrast::Agent::Protect::Rule::SqliInputClassification
37
+ # },
38
+ # cmdi: {
39
+ # rule_name: 'cmd-injection',
40
+ # classification: Contrast::Agent::Protect::Rule::CmdiInputClassification
41
+ # },
42
+ # method_tampering: {
43
+ # rule_name: 'method-tampering',
44
+ # classification: Contrast::Agent::Protect::Rule::HttpMethodTamperingInputClassification
45
+ # },
46
+ # unsafe_file_upload: {
47
+ # rule_name: 'unsafe-file-upload',
48
+ # classification: Contrast::Agent::Protect::Rule::UnsafeFileUploadInputClassification
49
+ # },
50
+ # nosqli: {
51
+ # rule_name: 'nosql-injection',
52
+ # classification: Contrast::Agent::Protect::Rule::NoSqliInputClassification
53
+ # }
54
+ # }.cs__freeze
55
+ #
56
+ # # This method with analyze the user input from the context of the
57
+ # # current request and run each of the protect rules against all
58
+ # # found input types
59
+ # #
60
+ # # @param request [Contrast::Agent::Request] current request context.
61
+ # # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
62
+ # def analyse request
63
+ # return unless Contrast::PROTECT.enabled?
64
+ # return if request.nil?
65
+ #
66
+ # inputs = extract_input request
67
+ # return unless inputs
68
+ #
69
+ # input_analysis = Contrast::Agent::Reporting::InputAnalysis.new
70
+ # input_analysis.request = request
71
+ # # each rule against each input
72
+ # input_classification inputs, input_analysis
73
+ # input_analysis
74
+ # end
75
+ #
76
+ # private
77
+ #
78
+ # # classify input by rule implementation of worth_watching_v2 for the rules supporting it.
79
+ # #
80
+ # # @param inputs [String, Array<String>] user input to be analysed.
81
+ # # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Here we will keep all the results
82
+ # # for each protect rule.
83
+ # # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
84
+ # def input_classification inputs, input_analysis
85
+ # # key = input type, value = user_input
86
+ # inputs.each do |input_type, value|
87
+ # next if value.nil? || value.empty?
88
+ #
89
+ # PROTECT_RULES.each do |_key, rule|
90
+ # protect_rule = Contrast::PROTECT.rule(rule[:rule_name])
91
+ # logger.debug("Rule #{ rule[:rule_name] } not recognised in Protect rules") if protect_rule.nil?
92
+ #
93
+ # # check if rule is enabled
94
+ # next unless protect_rule&.enabled?
95
+ #
96
+ # # method tampering doesn't take value
97
+ # if rule[:rule_name] == Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
98
+ # rule[:classification].send(:classify, input_type, input_analysis)
99
+ # else
100
+ # rule[:classification].send(:classify, input_type, value, input_analysis)
101
+ # end
102
+ # end
103
+ # end
104
+ # input_analysis
105
+ # end
106
+ #
107
+ # # Extract the inputs from the request context and label them with Protect
108
+ # # input type tags. Each tag will contain one or more user inputs.
109
+ # #
110
+ # # This methods is to be expanded and modified as needed by other Protect rules
111
+ # # and sub-rules for their requirements.
112
+ # #
113
+ # # @param request [Contrast::Agent::Request] current request context.
114
+ # # @return inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
115
+ # def extract_input request
116
+ # inputs = {}
117
+ # inputs[BODY] = request.body
118
+ # inputs[COOKIE_NAME] = request.cookies.keys
119
+ # inputs[COOKIE_VALUE] = request.cookies.values
120
+ # inputs[HEADER] = request.headers
121
+ # inputs[PARAMETER_NAME] = request.parameters.keys
122
+ # inputs[PARAMETER_VALUE] = request.parameters.values
123
+ # inputs[QUERYSTRING] = request.query_string
124
+ # inputs[METHOD] = request.request_method
125
+ # extract_multipart inputs, request
126
+ # inputs.compact!
127
+ # inputs
128
+ # end
129
+ #
130
+ # # Extract the filename and name of the Content Disposition Header.
131
+ # #
132
+ # # @param inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
133
+ # # @param request [Contrast::Agent::Request] current request context.
134
+ # def extract_multipart inputs, request
135
+ # disposition = request.rack_request.env['Content-Disposition']
136
+ # disposition = request.rack_request.env['CONTENT_DISPOSITION'] if disposition.nil? || disposition.empty?
137
+ # return unless disposition
138
+ #
139
+ # pairs = {}
140
+ # disposition.split(SEMICOLON).each do |elem|
141
+ # new_pair = elem.strip.split(EQUALS, 2)
142
+ # pairs[new_pair[0].downcase] = new_pair[1] if new_pair
143
+ # end
144
+ # inputs[MULTIPART_NAME] = pairs[DISPOSITION_NAME]
145
+ # inputs[MULTIPART_FIELD_NAME] = pairs[DISPOSITION_FILENAME]
146
+ # end
147
+ # end
148
148
  end
149
149
  end
150
150
  end
@@ -55,9 +55,9 @@ module Contrast
55
55
  end
56
56
 
57
57
  def extract_mongo_data operation
58
- if operation.cs__respond_to? :selector
58
+ if operation.cs__respond_to?(:selector)
59
59
  operation.selector
60
- elsif operation.cs__respond_to? :documents
60
+ elsif operation.cs__respond_to?(:documents)
61
61
  operation.documents
62
62
  end
63
63
  end
@@ -69,7 +69,7 @@ module Contrast
69
69
 
70
70
  rule.infilter(Contrast::Agent::REQUEST_TRACKER.current, method, path)
71
71
  rescue Contrast::SecurityException => e
72
- raise e
72
+ raise(e)
73
73
  rescue StandardError => e
74
74
  logger.error('Error applying path traversal', e, module: object.cs__class.cs__name, method: method)
75
75
  end
@@ -111,7 +111,7 @@ module Contrast
111
111
  end
112
112
  end
113
113
  rescue Contrast::SecurityException => e
114
- raise e
114
+ raise(e)
115
115
  rescue StandardError => e
116
116
  parser ||= Contrast::Utils::ObjectShare::UNKNOWN
117
117
  logger.error('Error applying xxe', e, module: potential_parser.cs__class.cs__name, method: method,
@@ -38,7 +38,7 @@ module Contrast
38
38
  def apply_rule method, exception, properties, object, args
39
39
  invoke(method, exception, properties, object, args)
40
40
  rescue Contrast::SecurityException => e
41
- raise e
41
+ raise(e)
42
42
  rescue StandardError => e
43
43
  logger.error('Error applying protect rule', e, module: object.cs__class.cs__name, method: method,
44
44
  rule: rule_name)
@@ -64,14 +64,14 @@ module Contrast
64
64
  # @raise [Contrast::SecurityException] on block, will pass the
65
65
  # exception from the rule
66
66
  def invoke _method, _exception, _properties, _object, _args
67
- raise NoMethodError, 'This is abstract, override it.'
67
+ raise(NoMethodError, 'This is abstract, override it.')
68
68
  end
69
69
 
70
70
  # The name of the rule, as expected by the Contrast Service and Contrast UI.
71
71
  #
72
72
  # @return [String]
73
73
  def rule_name
74
- raise NoMethodError, 'This is abstract, override it.'
74
+ raise(NoMethodError, 'This is abstract, override it.')
75
75
  end
76
76
 
77
77
  # The rule for which this applicator applies. It'll be a concrete
@@ -80,7 +80,7 @@ module Contrast
80
80
  #
81
81
  # @return [Contrast::Agent::Protect::Rule::Base]
82
82
  def rule
83
- ::Contrast::PROTECT.rule rule_name
83
+ ::Contrast::PROTECT.rule(rule_name)
84
84
  end
85
85
 
86
86
  # Should we skip analysis for this rule for this method invocation?
@@ -180,13 +180,13 @@ module Contrast
180
180
  # @param attack [Symbol] the type of message we want to send
181
181
  # @param value [String] the input value we want to log
182
182
  def cef_logging result, attack = :ineffective_attack, value = nil
183
- sample_to_json = Contrast::Api::Dtm::RaspRuleSample.to_controlled_hash result.samples[0]
184
- outcome = if result.response.cs__is_a? Hash
183
+ sample_to_json = Contrast::Api::Dtm::RaspRuleSample.to_controlled_hash(result.samples[0])
184
+ outcome = if result.response.cs__is_a?(Hash)
185
185
  Contrast::Agent::Reporting::ResponseType.cs__const_get(result.response[:name])
186
186
  else
187
187
  Contrast::Api::Dtm::AttackResult::ResponseType.get_name_by_tag(result.response)
188
188
  end
189
- input_type = extract_input_type sample_to_json[:user_input].input_type
189
+ input_type = extract_input_type(sample_to_json[:user_input].input_type)
190
190
  input_value = value || sample_to_json[:user_input].value
191
191
  cef_logger.send(attack, result.rule_id, outcome, input_type, input_value)
192
192
  end
@@ -231,7 +231,7 @@ module Contrast
231
231
  # @param _kwargs [Hash] key-value pairs used by the rule to build a
232
232
  # report.
233
233
  def find_attacker _context, _potential_attack_string, **_kwargs
234
- raise NoMethodError, "Rule #{ rule_name } did not implement find_attack"
234
+ raise(NoMethodError, "Rule #{ rule_name } did not implement find_attack")
235
235
  end
236
236
 
237
237
  def update_successful_attack_response context, ia_result, result, attack_string = nil
@@ -248,6 +248,13 @@ module Contrast
248
248
  result
249
249
  end
250
250
 
251
+ # @param context [Contrast::Agent::RequestContext] the context of the
252
+ # request in which this input is evaluated.
253
+ # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the
254
+ # analysis of the input that was determined to be an attack
255
+ # @param result [Contrast::Api::Dtm::AttackResult] previous
256
+ # attack result for this rule, if one exists, in the case of
257
+ # multiple inputs being found to violate the protection criteria
251
258
  def update_perimeter_attack_response context, ia_result, result
252
259
  if mode == Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER
253
260
  result.response = if blocked_rule?(ia_result)
@@ -257,7 +264,7 @@ module Contrast
257
264
  end
258
265
  log_rule_matched(context, ia_result, result.response)
259
266
  elsif ia_result.nil? || ia_result.attack_count.zero?
260
- result.response = assign_reporter_response_type ia_result
267
+ result.response = assign_reporter_response_type(ia_result)
261
268
  log_rule_probed(context, ia_result)
262
269
  end
263
270
 
@@ -320,7 +327,7 @@ module Contrast
320
327
  # @param enum [Enumerable]
321
328
  # @return [Symbol]
322
329
  def extract_input_type enum
323
- Contrast::Api::Dtm::UserInput::InputType.get_name_by_tag enum
330
+ Contrast::Api::Dtm::UserInput::InputType.get_name_by_tag(enum)
324
331
  end
325
332
 
326
333
  private
@@ -339,21 +346,26 @@ module Contrast
339
346
  name: ia_result&.key, input: ia_result&.value)
340
347
  end
341
348
 
342
- # Handles the Response type for different Protect rules.
343
- # Some rules need to report SUSPICIOUS over PROBED in
349
+ # Some rules are reported as suspicious, rather than exploited or probed, b/c they don't actually follow
350
+ # input tracing or other detection types that provide enough confidnece for a determination.
351
+ #
352
+ # @param ia_result
353
+ # @return [Boolean]
354
+ def suspicious_rule? ia_result
355
+ [
356
+ Contrast::Agent::Protect::Rule::UnsafeFileUpload::NAME,
357
+ Contrast::Agent::Protect::Rule::Xss::NAME
358
+ ].include?(ia_result&.rule_id)
359
+ end
360
+
361
+ # Handles the Response type for different Protect rules. Some rules need to report SUSPICIOUS over PROBED in
344
362
  # MONITORED mode.
345
363
  #
346
- # @param ia_result [Contrast::Agent::Reporting::InputAnalysis]
347
- # @return [Contrast::Agent::Reporting::ResponseType,
348
- # Contrast::Api::Dtm::AttackResult::ResponseType]
364
+ # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the analysis of the input that was
365
+ # determined to be an attack
349
366
  def assign_reporter_response_type ia_result
350
- if (ia_result&.rule_id == Contrast::Agent::Protect::Rule::Xss::NAME ||
351
- Contrast::Agent::Protect::Rule::UnsafeFileUpload::NAME) &&
352
- Contrast::CONTRAST_SERVICE.use_agent_communication?
353
-
354
- # Here we'll have xss or unsafe file upload in monitoring mode
355
- # and we have to report it as SUSPICIOUS, not as PROBED.
356
- Contrast::Agent::Reporting::ResponseType::SUSPICIOUS
367
+ if suspicious_rule?(ia_result) && Contrast::CONTRAST_SERVICE.use_agent_communication?
368
+ Contrast::Api::Dtm::AttackResult::ResponseType::SUSPICIOUS
357
369
  else
358
370
  Contrast::Api::Dtm::AttackResult::ResponseType::PROBED
359
371
  end
@@ -21,18 +21,23 @@ module Contrast
21
21
  'Contrast Security Protect Rule Triggered. Response blocked.'
22
22
  end
23
23
 
24
+ # @param context [Contrast::Agent::RequestContext]
24
25
  def infilter? context
25
- return false unless context&.speedracer_input_analysis&.results
26
26
  return false unless enabled?
27
+ return false unless context&.speedracer_input_analysis&.results&.any? do |result|
28
+ result.rule_id == rule_name
29
+ end
30
+
27
31
  return false if protect_excluded_by_code?
28
32
 
29
33
  true
30
34
  end
31
35
 
32
36
  # Override for rules that need the response
33
- # Currently postfilter can be applied to streamed responses,
34
- # if any logic within postfilter changes to modify the response
35
- # streamed responses will break
37
+ # Currently postfilter can be applied to streamed responses, if any logic within postfilter changes to modify
38
+ # the response streamed responses will break
39
+ # @param context [Contrast::Agent::RequestContext]
40
+ # @raise [Contrast::SecurityException]
36
41
  def postfilter context
37
42
  return unless enabled? && POSTFILTER_MODES.include?(mode)
38
43
  if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
@@ -44,27 +49,38 @@ module Contrast
44
49
  result = find_postfilter_attacker(context, nil)
45
50
  return unless result&.samples&.any?
46
51
 
47
- cef_logging result
52
+ cef_logging(result)
48
53
  append_to_activity(context, result)
49
54
  return unless result.response == :BLOCKED
50
55
 
51
- raise Contrast::SecurityException.new(self, "#{ rule_name } triggered in postfilter. Response blocked.")
56
+ raise(Contrast::SecurityException.new(self, "#{ rule_name } triggered in postfilter. Response blocked."))
52
57
  end
53
58
 
54
59
  protected
55
60
 
61
+ # @param context [Contrast::Agent::RequestContext]
62
+ # @return [Array<Contrast::Api::Settings::InputAnalysis>]
56
63
  def gather_ia_results context
57
64
  context.speedracer_input_analysis.results.select do |ia_result|
58
65
  ia_result.rule_id == rule_name
59
66
  end
60
67
  end
61
68
 
69
+ # @param context [Contrast::Agent::RequestContext]
70
+ # @param potential_attack_string [String, nil]
71
+ # @param **kwargs
72
+ # @return [Contrast::Api::Dtm::AttackResult]
62
73
  def find_attacker context, potential_attack_string, **kwargs
63
74
  ia_results = gather_ia_results(context)
64
75
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
65
76
  end
66
77
 
67
78
  # Allows for the InputAnalysis from service to be extracted early
79
+ # @param context [Contrast::Agent::RequestContext]
80
+ # @param potential_attack_string [String, nil]
81
+ # @param ia_results [Array<Contrast::Api::Settings::InputAnalysis>]
82
+ # @param **kwargs
83
+ # @return [Contrast::Api::Dtm::AttackResult, nil]
68
84
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
69
85
  logger.trace('Checking vectors for attacks', rule: rule_name, input: potential_attack_string)
70
86
 
@@ -84,17 +100,18 @@ module Contrast
84
100
 
85
101
  private
86
102
 
103
+ # @param context [Contrast::Agent::RequestContext]
104
+ # @param potential_attack_string [String, nil]
105
+ # @return [Contrast::Api::Dtm::AttackResult, nil]
87
106
  def find_postfilter_attacker context, potential_attack_string, **kwargs
88
107
  ia_results = gather_ia_results(context)
89
108
  ia_results.select! do |ia_result|
90
- ia_result.score_level == if ia_result.rule_id == Contrast::Agent::Protect::Rule::Sqli::NAME ||
91
- ia_result.rule_id == Contrast::Agent::Protect::Rule::CmdInjection::NAME
92
-
93
- Contrast::Agent::Reporting::ScoreLevel::WORTHWATCHING
94
- else
95
- # legacy implementation for DEFINITEATATACK
96
- Contrast::Api::Settings::InputAnalysisResult::ScoreLevel::DEFINITEATTACK
97
- end
109
+ required_level = if ia_result.cs__is_a?(Contrast::Api::Settings::InputAnalysisResult)
110
+ Contrast::Api::Settings::InputAnalysisResult::ScoreLevel::DEFINITEATTACK
111
+ else
112
+ Contrast::Agent::Reporting::ScoreLevel::DEFINITEATTACK
113
+ end
114
+ ia_result.score_level == required_level
98
115
  end
99
116
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
100
117
  end
@@ -24,6 +24,18 @@ module Contrast
24
24
  MULTIPART_FIELD_NAME, XML_VALUE, DWR_VALUE
25
25
  ].cs__freeze
26
26
 
27
+ class << self
28
+ # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
29
+ # @return [Hash] the details for this specific rule
30
+ def extract_details attack_sample
31
+ {
32
+ command: attack_sample.cmdi.command,
33
+ startIndex: attack_sample.cmdi.start_idx,
34
+ endIndex: attack_sample.cmdi.end_idx
35
+ }
36
+ end
37
+ end
38
+
27
39
  def rule_name
28
40
  NAME
29
41
  end
@@ -46,13 +58,13 @@ module Contrast
46
58
  return unless result
47
59
 
48
60
  append_to_activity(context, result)
49
- cef_logging result, :successful_attack
61
+ cef_logging(result, :successful_attack)
50
62
 
51
63
  return unless blocked?
52
64
 
53
- raise Contrast::SecurityException.new(self,
65
+ raise(Contrast::SecurityException.new(self,
54
66
  'Command Injection rule triggered. '\
55
- "Call to #{ classname }.#{ method } blocked.")
67
+ "Call to #{ classname }.#{ method } blocked."))
56
68
  end
57
69
 
58
70
  def build_attack_with_match context, input_analysis_result, result, candidate_string, **kwargs
@@ -90,6 +102,7 @@ module Contrast
90
102
  command = candidate_string || input_analysis_result.value
91
103
  command = Contrast::Utils::StringUtils.protobuf_safe_string(command)
92
104
  sample.cmdi.command = command
105
+ sample.cmdi.end_idx = command.length
93
106
 
94
107
  # This is a special case where the user input is UNKNOWN_USER_INPUT but
95
108
  # we want to send the attack value
@@ -134,12 +147,6 @@ module Contrast
134
147
  def report_any_command_execution?
135
148
  ::Contrast::PROTECT.report_any_command_execution?
136
149
  end
137
-
138
- def gather_ia_results context
139
- context.agent_input_analysis.results.select do |ia_result|
140
- ia_result.rule_id == rule_name
141
- end
142
- end
143
150
  end
144
151
  end
145
152
  end
@@ -64,15 +64,15 @@ module Contrast
64
64
  #
65
65
  # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
66
66
  def cmdi_create_new_input_result request, rule_id, input_type, value
67
- ia_result = new_ia_result rule_id, input_type, request.path, value
68
- if cmdi_worth_watching? value
67
+ ia_result = new_ia_result(rule_id, input_type, request.path, value)
68
+ if cmdi_worth_watching?(value)
69
69
  ia_result.score_level = WORTHWATCHING
70
70
  ia_result.ids << WORTHWATCHING_MATCH
71
71
  else
72
72
  ia_result.score_level = IGNORE
73
73
  end
74
74
 
75
- add_needed_key request, ia_result, input_type, value if CMDI_KEYS_NEEDED.include? input_type
75
+ add_needed_key(request, ia_result, input_type, value) if CMDI_KEYS_NEEDED.include?(input_type)
76
76
  ia_result
77
77
  end
78
78
  end
@@ -7,6 +7,8 @@
7
7
  # @deprecated RUBY-356: this class and those that extend it are being phased out
8
8
  # in favor of the more performant code in the Service.
9
9
  class Contrast::Agent::Protect::Rule::DefaultScanner # rubocop:disable Style/ClassAndModuleChildren
10
+ OPERATOR_PATTERN = %r{[+=*^/%><!-]}.cs__freeze
11
+
10
12
  # Potential states
11
13
  # :STATE_INSIDE_TOKEN
12
14
  # :STATE_INSIDE_NUMBER
@@ -220,7 +222,6 @@ class Contrast::Agent::Protect::Rule::DefaultScanner # rubocop:disable Style/Cla
220
222
  idx
221
223
  end
222
224
 
223
- OPERATOR_PATTERN = %r{[+=*^/%><!-]}.cs__freeze
224
225
  def operator? char
225
226
  char.match?(OPERATOR_PATTERN)
226
227
  end