contrast-agent 6.6.3 → 6.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.gitmodules +0 -3
  4. data/ext/cs__scope/cs__scope.c +1 -1
  5. data/lib/contrast/agent/assess/contrast_event.rb +2 -24
  6. data/lib/contrast/agent/assess/events/source_event.rb +7 -61
  7. data/lib/contrast/agent/assess/finalizers/hash.rb +11 -0
  8. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +0 -55
  9. data/lib/contrast/agent/assess/policy/policy_node.rb +3 -3
  10. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +0 -1
  11. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  12. data/lib/contrast/agent/assess/policy/source_method.rb +24 -1
  13. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +7 -5
  14. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +6 -1
  15. data/lib/contrast/agent/assess/policy/trigger_method.rb +38 -119
  16. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -3
  17. data/lib/contrast/agent/assess/property/evented.rb +2 -12
  18. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +42 -82
  19. data/lib/contrast/agent/assess/rule/response/base_rule.rb +11 -27
  20. data/lib/contrast/agent/assess/rule/response/body_rule.rb +1 -3
  21. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +77 -62
  22. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +1 -1
  23. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +6 -1
  24. data/lib/contrast/agent/assess/rule/response/header_rule.rb +5 -5
  25. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +1 -1
  26. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +1 -1
  27. data/lib/contrast/agent/assess/tracker.rb +1 -7
  28. data/lib/contrast/agent/at_exit_hook.rb +1 -7
  29. data/lib/contrast/agent/excluder.rb +206 -0
  30. data/lib/contrast/agent/exclusion_matcher.rb +6 -0
  31. data/lib/contrast/agent/inventory/database_config.rb +18 -23
  32. data/lib/contrast/agent/middleware.rb +0 -1
  33. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +4 -0
  34. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +1 -0
  35. data/lib/contrast/agent/protect/rule/base.rb +64 -24
  36. data/lib/contrast/agent/protect/rule/base_service.rb +1 -0
  37. data/lib/contrast/agent/protect/rule/cmd_injection.rb +18 -104
  38. data/lib/contrast/agent/protect/rule/cmdi/cmdi_backdoors.rb +129 -0
  39. data/lib/contrast/agent/protect/rule/cmdi/cmdi_base_rule.rb +169 -0
  40. data/lib/contrast/agent/protect/rule/deserialization.rb +7 -5
  41. data/lib/contrast/agent/protect/rule/path_traversal.rb +9 -7
  42. data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +16 -14
  43. data/lib/contrast/agent/protect/rule/sqli/sqli_base_rule.rb +51 -0
  44. data/lib/contrast/agent/protect/rule/sqli/sqli_semantic/sqli_dangerous_functions.rb +67 -0
  45. data/lib/contrast/agent/protect/rule/sqli.rb +6 -31
  46. data/lib/contrast/agent/protect/rule/xxe.rb +11 -6
  47. data/lib/contrast/agent/protect/rule.rb +3 -1
  48. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +8 -0
  49. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +91 -36
  50. data/lib/contrast/agent/reporting/attack_result/user_input.rb +11 -0
  51. data/lib/contrast/agent/reporting/details/bot_blocker_details.rb +29 -0
  52. data/lib/contrast/agent/reporting/details/cmd_injection_details.rb +30 -0
  53. data/lib/contrast/agent/reporting/details/details.rb +18 -0
  54. data/lib/contrast/agent/reporting/details/http_method_tempering_details.rb +27 -0
  55. data/lib/contrast/agent/reporting/details/ip_denylist_details.rb +27 -0
  56. data/lib/contrast/agent/reporting/details/no_sqli_details.rb +36 -0
  57. data/lib/contrast/agent/reporting/details/path_traversal_details.rb +24 -0
  58. data/lib/contrast/agent/reporting/details/path_traversal_semantic_analysis_details.rb +32 -0
  59. data/lib/contrast/agent/reporting/details/protect_rule_details.rb +17 -0
  60. data/lib/contrast/agent/reporting/details/sqli_dangerous_functions.rb +22 -0
  61. data/lib/contrast/agent/reporting/details/sqli_details.rb +36 -0
  62. data/lib/contrast/agent/reporting/details/untrusted_deserialization_details.rb +27 -0
  63. data/lib/contrast/agent/reporting/details/virtual_patch_details.rb +24 -0
  64. data/lib/contrast/agent/reporting/details/xss_details.rb +33 -0
  65. data/lib/contrast/agent/reporting/details/xss_match.rb +30 -0
  66. data/lib/contrast/agent/reporting/details/xxe_details.rb +36 -0
  67. data/lib/contrast/agent/reporting/details/xxe_match.rb +25 -0
  68. data/lib/contrast/agent/reporting/details/xxe_wrapper.rb +25 -0
  69. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +1 -1
  70. data/lib/contrast/agent/reporting/masker/masker.rb +78 -65
  71. data/lib/contrast/agent/reporting/masker/masker_utils.rb +1 -30
  72. data/lib/contrast/agent/reporting/reporter.rb +1 -2
  73. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +2 -2
  74. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +81 -15
  75. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +13 -25
  76. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +17 -22
  77. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +46 -125
  78. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +5 -16
  79. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +10 -18
  80. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +6 -14
  81. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +1 -1
  82. data/lib/contrast/agent/reporting/reporting_events/architecture_component.rb +7 -21
  83. data/lib/contrast/agent/reporting/reporting_events/finding.rb +19 -49
  84. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +12 -9
  85. data/lib/contrast/agent/reporting/reporting_events/finding_event_signature.rb +1 -1
  86. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +23 -21
  87. data/lib/contrast/agent/reporting/reporting_events/finding_event_stack.rb +5 -18
  88. data/lib/contrast/agent/reporting/reporting_events/finding_event_taint_range.rb +1 -0
  89. data/lib/contrast/{api/decorators/trace_taint_range_tags.rb → agent/reporting/reporting_events/finding_event_taint_range_tags.rb} +7 -6
  90. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +45 -10
  91. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +1 -1
  92. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +2 -2
  93. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +10 -14
  94. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +11 -0
  95. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +3 -1
  96. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +11 -23
  97. data/lib/contrast/agent/reporting/reporting_events/route_discovery_observation.rb +8 -26
  98. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +1 -1
  99. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +4 -7
  100. data/lib/contrast/agent/reporting/reporting_utilities/headers.rb +1 -1
  101. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +2 -1
  102. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +3 -3
  103. data/lib/contrast/agent/request.rb +4 -2
  104. data/lib/contrast/agent/request_context.rb +12 -15
  105. data/lib/contrast/agent/request_context_extend.rb +67 -69
  106. data/lib/contrast/agent/request_handler.rb +1 -11
  107. data/lib/contrast/agent/response.rb +0 -18
  108. data/lib/contrast/agent/service_heartbeat.rb +1 -1
  109. data/lib/contrast/agent/telemetry/events/event.rb +1 -1
  110. data/lib/contrast/agent/telemetry/events/metric_event.rb +1 -1
  111. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +3 -3
  112. data/lib/contrast/agent/version.rb +1 -1
  113. data/lib/contrast/api/communication/messaging_queue.rb +2 -3
  114. data/lib/contrast/api/communication/socket_client.rb +4 -4
  115. data/lib/contrast/api/communication/speedracer.rb +4 -8
  116. data/lib/contrast/api/decorators/agent_startup.rb +5 -6
  117. data/lib/contrast/api/decorators/application_settings.rb +2 -1
  118. data/lib/contrast/api/decorators/application_startup.rb +6 -6
  119. data/lib/contrast/api/decorators/message.rb +0 -4
  120. data/lib/contrast/api/decorators/rasp_rule_sample.rb +0 -6
  121. data/lib/contrast/api/decorators.rb +0 -6
  122. data/lib/contrast/api/dtm.pb.rb +0 -489
  123. data/lib/contrast/components/agent.rb +16 -12
  124. data/lib/contrast/components/api.rb +10 -10
  125. data/lib/contrast/components/app_context.rb +9 -9
  126. data/lib/contrast/components/app_context_extend.rb +1 -1
  127. data/lib/contrast/components/assess.rb +92 -38
  128. data/lib/contrast/components/assess_rules.rb +36 -0
  129. data/lib/contrast/components/config.rb +54 -12
  130. data/lib/contrast/components/contrast_service.rb +8 -8
  131. data/lib/contrast/components/heap_dump.rb +1 -1
  132. data/lib/contrast/components/protect.rb +5 -5
  133. data/lib/contrast/components/ruby_component.rb +81 -0
  134. data/lib/contrast/components/sampling.rb +1 -1
  135. data/lib/contrast/components/security_logger.rb +23 -0
  136. data/lib/contrast/components/service.rb +55 -0
  137. data/lib/contrast/components/settings.rb +12 -4
  138. data/lib/contrast/config/base_configuration.rb +1 -1
  139. data/lib/contrast/config/protect_rules_configuration.rb +17 -3
  140. data/lib/contrast/config/server_configuration.rb +1 -1
  141. data/lib/contrast/config.rb +0 -6
  142. data/lib/contrast/configuration.rb +81 -17
  143. data/lib/contrast/extension/assess/exec_trigger.rb +3 -1
  144. data/lib/contrast/extension/assess/marshal.rb +3 -2
  145. data/lib/contrast/extension/assess/string.rb +0 -1
  146. data/lib/contrast/extension/extension.rb +1 -1
  147. data/lib/contrast/framework/base_support.rb +0 -5
  148. data/lib/contrast/framework/grape/support.rb +1 -23
  149. data/lib/contrast/framework/manager.rb +0 -10
  150. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -6
  151. data/lib/contrast/framework/rails/support.rb +5 -58
  152. data/lib/contrast/framework/sinatra/support.rb +2 -21
  153. data/lib/contrast/logger/cef_log.rb +21 -3
  154. data/lib/contrast/logger/log.rb +1 -11
  155. data/lib/contrast/tasks/config.rb +4 -2
  156. data/lib/contrast/utils/assess/event_limit_utils.rb +28 -12
  157. data/lib/contrast/utils/assess/trigger_method_utils.rb +10 -18
  158. data/lib/contrast/utils/findings.rb +6 -5
  159. data/lib/contrast/utils/hash_digest.rb +9 -24
  160. data/lib/contrast/utils/hash_digest_extend.rb +6 -6
  161. data/lib/contrast/utils/invalid_configuration_util.rb +21 -58
  162. data/lib/contrast/utils/log_utils.rb +47 -17
  163. data/lib/contrast/utils/net_http_base.rb +7 -8
  164. data/lib/contrast/utils/patching/policy/patch_utils.rb +3 -2
  165. data/lib/contrast/utils/stack_trace_utils.rb +0 -25
  166. data/lib/contrast/utils/string_utils.rb +9 -0
  167. data/lib/contrast/utils/telemetry_client.rb +13 -7
  168. data/lib/contrast.rb +5 -10
  169. metadata +39 -28
  170. data/lib/contrast/agent/reporting/reporting_events/trace_event_source.rb +0 -30
  171. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +0 -43
  172. data/lib/contrast/api/decorators/activity.rb +0 -33
  173. data/lib/contrast/api/decorators/architecture_component.rb +0 -36
  174. data/lib/contrast/api/decorators/finding.rb +0 -29
  175. data/lib/contrast/api/decorators/route_coverage.rb +0 -91
  176. data/lib/contrast/api/decorators/trace_event.rb +0 -120
  177. data/lib/contrast/api/decorators/trace_event_object.rb +0 -63
  178. data/lib/contrast/api/decorators/trace_event_signature.rb +0 -69
  179. data/lib/contrast/api/decorators/trace_taint_range.rb +0 -52
  180. data/lib/contrast/config/assess_configuration.rb +0 -93
  181. data/lib/contrast/config/assess_rules_configuration.rb +0 -32
  182. data/lib/contrast/config/root_configuration.rb +0 -90
  183. data/lib/contrast/config/ruby_configuration.rb +0 -81
  184. data/lib/contrast/config/service_configuration.rb +0 -49
  185. data/lib/contrast/utils/preflight_util.rb +0 -13
@@ -6,6 +6,8 @@ require 'contrast/agent/protect/rule/deserialization'
6
6
  require 'contrast/agent/protect/rule/http_method_tampering'
7
7
  require 'contrast/agent/protect/rule/no_sqli'
8
8
  require 'contrast/api/dtm.pb'
9
+ require 'contrast/agent/reporting/attack_result/user_input'
10
+ require 'contrast/agent/reporting/attack_result/response_type'
9
11
  require 'contrast/components/logger'
10
12
 
11
13
  module Contrast
@@ -16,12 +18,20 @@ module Contrast
16
18
  class ApplicationDefendAttackSample
17
19
  include Contrast::Agent::Reporting::InputType
18
20
 
19
- # @return [Hash] => start: ms, elapsed: ms
20
- attr_reader :time_stamp
21
+ # @return [Contrast::Agent::Reporting::UserInput]
22
+ attr_accessor :user_input
23
+ # @return [Array<Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack>]
24
+ attr_reader :stack
25
+ # Details are per rule specific and should be set when the sample is build
26
+ #
27
+ # @return [Contrast::Agent::Reporting::Details::ProtectRuleDetails, {}]
28
+ attr_accessor :details
29
+ # @return [Integer] time in ms
30
+ attr_accessor :time_stamp
21
31
 
22
32
  class << self
23
- # @param attack_result [Contrast::Api::Dtm::AttackResult]
24
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
33
+ # @param attack_result [Contrast::Agent::Reporting::AttackResult]
34
+ # @param attack_sample [Contrast::Agent::Reporting::ApplicationDefendAttackSample]
25
35
  def convert attack_result, attack_sample
26
36
  activity = new
27
37
  activity.attach_data(attack_result, attack_sample)
@@ -30,151 +40,62 @@ module Contrast
30
40
  end
31
41
 
32
42
  def initialize
43
+ @time_stamp = Contrast::Agent::REQUEST_TRACKER.current&.timer&.start_ms || 0 # in ms
33
44
  @blocked = false
34
45
  @event_type = :application_defend_attack_sample
46
+ @user_input = Contrast::Agent::Reporting::UserInput.new
47
+ @request = Contrast::Agent::REQUEST_TRACKER.current&.activity&.request
48
+ @stack = Contrast::Utils::StackTraceUtils.build_protect_report_stack_array
49
+ @details = {}
35
50
  end
36
51
 
37
52
  def to_controlled_hash
38
53
  {
39
54
  blocked: @blocked,
40
- input: @input,
41
- details: @details,
55
+ input: build_input(@user_input),
56
+ details: @details&.to_controlled_hash,
42
57
  request: @request&.to_controlled_hash,
43
58
  stack: @stack&.map(&:to_controlled_hash),
44
- timestamp: time_stamp
59
+ timestamp: build_time_stamp
45
60
  }
46
61
  end
47
62
 
48
- # @param attack_result [Contrast::Api::Dtm::AttackResult]
49
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
50
- def attach_data attack_result, attack_sample
51
- @blocked = attack_result.response == ::Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED
52
- @input = build_input(attack_sample)
53
- @details = build_details(attack_result.rule_id, attack_sample)
54
- @time_stamp = build_time_stamp(attack_sample.timestamp_ms)
55
- @request = FindingRequest.convert(Contrast::Agent::REQUEST_TRACKER.current&.request)
56
- @stack = Contrast::Utils::StackTraceUtils.build_protect_report_stack_array
57
- end
58
-
59
- # @param start [Integer]
60
- # @return [Hash]
61
- def build_time_stamp start
62
- {
63
- start: start,
64
- elapsed: Contrast::Utils::Timer.now_ms - start
65
- }
63
+ # @param response_type [Contrast::Api::Dtm::AttackResult::ResponseType]
64
+ # @return [Boolean] check if response type is blocked
65
+ def blocked? response_type
66
+ @blocked = response_type == Contrast::Agent::Reporting::ResponseType::BLOCKED
66
67
  end
67
68
 
68
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
69
- def build_input attack_sample
70
- user_input = attack_sample.user_input
69
+ # @param user_input [Contrast::Agent::Reporting::UserInput]
70
+ def build_input user_input
71
71
  {
72
72
  documentPath: user_input.path,
73
- documentType: build_document_type(user_input.document_type),
73
+ documentType: user_input.document_type.to_s,
74
74
  filters: user_input.matcher_ids,
75
75
  name: user_input.key,
76
- time: attack_sample.timestamp_ms,
77
- type: build_input_type(user_input.input_type),
76
+ time: time_stamp,
77
+ type: user_input.input_type.to_s,
78
78
  value: user_input.value
79
79
  }
80
80
  end
81
81
 
82
- # Convert the document type api enum to a String constant TeamServer can understand.
83
- #
84
- # @param type_enum [::Contrast::Api::Dtm::HttpRequest::DocumentType]
85
- # @return [String]
86
- def build_document_type type_enum
87
- case type_enum
88
- when ::Contrast::Api::Dtm::HttpRequest::DocumentType::JSON
89
- 'JSON'
90
- when ::Contrast::Api::Dtm::HttpRequest::DocumentType::XML
91
- 'XML'
92
- else
93
- 'NORMAL'
94
- end
95
- end
96
-
97
- # Convert the input type api enum to a String constant TeamServer can understand.
98
- #
99
- # @param type_enum [when ::Contrast::Api::Dtm::UserInput::InputType]
100
- # @return [String]
101
- def build_input_type type_enum
102
- case type_enum
103
- when ::Contrast::Api::Dtm::UserInput::InputType::UNDEFINED_TYPE
104
- 'UNDEFINED_TYPE'
105
- when ::Contrast::Api::Dtm::UserInput::InputType::BODY
106
- 'BODY'
107
- when ::Contrast::Api::Dtm::UserInput::InputType::COOKIE_NAME
108
- 'COOKIE_NAME'
109
- when ::Contrast::Api::Dtm::UserInput::InputType::COOKIE_VALUE
110
- 'COOKIE_VALUE'
111
- when ::Contrast::Api::Dtm::UserInput::InputType::HEADER
112
- 'HEADER'
113
- when ::Contrast::Api::Dtm::UserInput::InputType::PARAMETER_NAME
114
- 'PARAMETER_NAME'
115
- when ::Contrast::Api::Dtm::UserInput::InputType::PARAMETER_VALUE
116
- 'PARAMETER_VALUE'
117
- when ::Contrast::Api::Dtm::UserInput::InputType::QUERYSTRING
118
- 'QUERYSTRING'
119
- when ::Contrast::Api::Dtm::UserInput::InputType::URI
120
- 'URI'
121
- when ::Contrast::Api::Dtm::UserInput::InputType::SOCKET
122
- 'SOCKET'
123
- when ::Contrast::Api::Dtm::UserInput::InputType::JSON_VALUE
124
- 'JSON_VALUE'
125
- when ::Contrast::Api::Dtm::UserInput::InputType::JSON_ARRAYED_VALUE
126
- 'JSON_ARRAYED_VALUE'
127
- when ::Contrast::Api::Dtm::UserInput::InputType::MULTIPART_CONTENT_TYPE
128
- 'MULTIPART_CONTENT_TYPE'
129
- when ::Contrast::Api::Dtm::UserInput::InputType::MULTIPART_VALUE
130
- 'MULTIPART_VALUE'
131
- when ::Contrast::Api::Dtm::UserInput::InputType::MULTIPART_FIELD_NAME
132
- 'MULTIPART_FIELD_NAME'
133
- when ::Contrast::Api::Dtm::UserInput::InputType::MULTIPART_NAME
134
- 'MULTIPART_NAME'
135
- when ::Contrast::Api::Dtm::UserInput::InputType::XML_VALUE
136
- 'XML_VALUE'
137
- when ::Contrast::Api::Dtm::UserInput::InputType::DWR_VALUE
138
- 'DWR_VALUE'
139
- when ::Contrast::Api::Dtm::UserInput::InputType::METHOD
140
- 'METHOD'
141
- when ::Contrast::Api::Dtm::UserInput::InputType::REQUEST
142
- 'REQUEST'
143
- when ::Contrast::Api::Dtm::UserInput::InputType::URL_PARAMETER
144
- 'URL_PARAMETER'
145
- else # ::Contrast::Api::Dtm::UserInput::InputType::UNKNOWN
146
- 'UNKNOWN'
147
- end
82
+ # @param attack_result [Contrast::Agent::Reporting::AttackResult]
83
+ # @param attack_sample [Contrast::Agent::Reporting::RaspRuleSample]
84
+ def attach_data attack_result, attack_sample
85
+ @blocked = attack_result.response == ::Contrast::Agent::Reporting::ResponseType::BLOCKED
86
+ @user_input = attack_sample.user_input
87
+ @details = attack_sample.details
88
+ @time_stamp = attack_sample.time_stamp
89
+ @request = FindingRequest.convert(Contrast::Agent::REQUEST_TRACKER.current&.request)
90
+ @stack = Contrast::Utils::StackTraceUtils.build_protect_report_stack_array
148
91
  end
149
92
 
150
- # This is a temporary method to facilitate the translation of API details to Reporting details. As we move off
151
- # of protobuf, this responsibility should shift from this class to the individual rules, as they do today when
152
- # they populate their details in Contrast::Api::Dtm::RaspRuleSample
153
- #
154
- # @param rule_id [String]
155
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
156
- # @return [Hash] the details for this specific rule
157
- def build_details rule_id, attack_sample
158
- case rule_id
159
- when Contrast::Agent::Protect::Rule::CmdInjection::NAME
160
- Contrast::Agent::Protect::Rule::CmdInjection.extract_details(attack_sample)
161
- when Contrast::Agent::Protect::Rule::Deserialization::NAME
162
- Contrast::Agent::Protect::Rule::Deserialization.extract_details(attack_sample)
163
- when Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
164
- Contrast::Agent::Protect::Rule::HttpMethodTampering.extract_details(attack_sample)
165
- when Contrast::Agent::Protect::Rule::NoSqli::NAME
166
- Contrast::Agent::Protect::Rule::NoSqli.extract_details(attack_sample)
167
- when Contrast::Agent::Protect::Rule::PathTraversal::NAME
168
- Contrast::Agent::Protect::Rule::PathTraversal.extract_details(attack_sample)
169
- when Contrast::Agent::Protect::Rule::Sqli::NAME
170
- Contrast::Agent::Protect::Rule::Sqli.extract_details(attack_sample)
171
- when Contrast::Agent::Protect::Rule::Xss::NAME
172
- Contrast::Agent::Protect::Rule::Xss.extract_details(attack_sample)
173
- when Contrast::Agent::Protect::Rule::Xxe::NAME
174
- Contrast::Agent::Protect::Rule::Xxe.extract_details(attack_sample)
175
- else # something unknown or Contrast::Agent::Protect::Rule::UnsafeFileUpload::NAME
176
- Contrast::Utils::ObjectShare::EMPTY_HASH
177
- end
93
+ # @return [Hash]
94
+ def build_time_stamp
95
+ {
96
+ start: time_stamp,
97
+ elapsed: Contrast::Utils::Timer.now_ms - time_stamp
98
+ }
178
99
  end
179
100
  end
180
101
  end
@@ -16,21 +16,11 @@ module Contrast
16
16
  # @return [Hash<Integer,Integer>] map of time from start in seconds to number of attacks in that second
17
17
  attr_reader :time_map
18
18
 
19
- class << self
20
- # @param attack_result [Contrast::Api::Dtm::AttackResult]
21
- def convert attack_result
22
- activity = new
23
- activity.attach_data(attack_result)
24
- activity
25
- end
26
- end
27
-
28
19
  def initialize
29
20
  @samples = []
30
21
  @start_time = Contrast::Agent::REQUEST_TRACKER.current&.timer&.start_ms || 0 # in ms
31
22
  @event_type = :application_defend_attack_sample_activity
32
23
  @time_map = Hash.new { |h, k| h[k] = 0 }
33
- super
34
24
  end
35
25
 
36
26
  def to_controlled_hash
@@ -42,17 +32,16 @@ module Contrast
42
32
  }
43
33
  end
44
34
 
45
- # @param attack_result [Contrast::Api::Dtm::AttackResult]
35
+ # @param attack_result [Contrast::Agent::Reporting::AttackResult]
46
36
  def attach_data attack_result
47
37
  attack_result.samples.each do |attack_sample|
48
38
  base_time = Contrast::Agent::REQUEST_TRACKER.current&.timer&.start_ms || 0
49
- dmt_time = attack_sample.timestamp_ms.to_i
50
- converted = Contrast::Agent::Reporting::ApplicationDefendAttackSample.convert(attack_result, attack_sample)
51
- samples << converted
52
- @start_time = if dmt_time.zero?
39
+ sample_time = attack_sample.time_stamp.to_i
40
+ samples << Contrast::Agent::Reporting::ApplicationDefendAttackSample.convert(attack_result, attack_sample)
41
+ @start_time = if sample_time.zero?
53
42
  @start_time
54
43
  else
55
- dmt_time
44
+ sample_time
56
45
  end
57
46
  attack_second = (@start_time - base_time) / 1000 # in seconds
58
47
  time_map[attack_second] += 1
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/components/logger'
5
+ require 'contrast/utils/object_share'
5
6
  require 'contrast/agent/reporting/reporting_events/application_defend_attack_activity'
6
7
 
7
8
  module Contrast
@@ -12,7 +13,7 @@ module Contrast
12
13
  class ApplicationDefendAttackerActivity
13
14
  # @return [Hash<String,Contrast::Agent::Reporting::ApplicationDefendAttackActivity>] map of rule-id to violated
14
15
  # samples for that rule
15
- attr_reader :protection_rules
16
+ attr_accessor :protection_rules
16
17
  # @return [String, nil] the IP address of the request from which the attack originated; used to identify unique
17
18
  # attackers
18
19
  attr_reader :source_ip
@@ -20,25 +21,14 @@ module Contrast
20
21
  # identify unique attackers
21
22
  attr_reader :source_forwarded_for
22
23
 
23
- class << self
24
- # @param attack_result_dtm [Contrast::Api::Dtm::AttackResult]
25
- # @return [Contrast::Agent::Reporting::ApplicationDefendAttackerActivity]
26
- def convert attack_result_dtm
27
- activity = new
28
- activity.attach_data(attack_result_dtm)
29
- activity
30
- end
31
- end
32
-
33
24
  def initialize
34
25
  @protection_rules = {}
35
- req = Contrast::Agent::REQUEST_TRACKER.current.activity.http_request
26
+ req = Contrast::Agent::REQUEST_TRACKER.current&.request
36
27
  if req
37
- @source_ip = req.sender.ip
38
- @source_forwarded_for = req.request_headers['X-Forwarded-For']
28
+ @source_ip = req.ip || Contrast::Utils::ObjectShare::EMPTY_STRING
29
+ @source_forwarded_for = req.headers['X-Forwarded-For']
39
30
  end
40
31
  @event_type = :application_defend_attacker_activity
41
- super
42
32
  end
43
33
 
44
34
  def to_controlled_hash
@@ -51,10 +41,12 @@ module Contrast
51
41
  }
52
42
  end
53
43
 
54
- # @param attack_result [Contrast::Api::Dtm::AttackResult]
44
+ # @param attack_result [Contrast::Agent::Reporting::AttackResult]
55
45
  def attach_data attack_result
56
- @protection_rules[attack_result.rule_id] =
57
- Contrast::Agent::Reporting::ApplicationDefendAttackActivity.convert(attack_result)
46
+ @protection_rules[attack_result.rule_id] = Contrast::Agent::Reporting::ApplicationDefendAttackActivity.new.
47
+ tap do |activity|
48
+ activity.attach_data(attack_result)
49
+ end
58
50
  end
59
51
 
60
52
  def process_protection_rules
@@ -18,16 +18,6 @@ module Contrast
18
18
  # @ return [Array<String>, nil] - User-Agent Header value
19
19
  attr_reader :browsers
20
20
 
21
- class << self
22
- # @param activity_dtm [Contrast::Api::Dtm::ApplicationActivity]
23
- # @return [Contrast::Agent::Reporting::ApplicationInventoryActivity]
24
- def convert activity_dtm
25
- inventory = new
26
- inventory.attach_data(activity_dtm)
27
- inventory
28
- end
29
- end
30
-
31
21
  def initialize
32
22
  @event_type = :application_inventory_activity
33
23
  @browsers = []
@@ -43,11 +33,13 @@ module Contrast
43
33
  }
44
34
  end
45
35
 
46
- def attach_data activity
47
- activity.architectures.each do |architecture|
48
- @components << Contrast::Agent::Reporting::ArchitectureComponent.convert(architecture)
36
+ # @param architectures [Array<Contrast::Agent::Reporting::ArchitectureComponent>,
37
+ # Contrast::Agent::Reporting::ArchitectureComponent]
38
+ def attach_data architectures
39
+ Array(architectures).each do |architecture|
40
+ @components << architecture
49
41
  end
50
- request_headers = activity.http_request&.request_headers
42
+ request_headers = Contrast::Agent::REQUEST_TRACKER.current&.request&.headers
51
43
  @browsers << request_headers['USER_AGENT'] if request_headers
52
44
  end
53
45
 
@@ -27,7 +27,7 @@ module Contrast
27
27
  #
28
28
  # @return [Hash]
29
29
  def to_controlled_hash
30
- app_config = ::Contrast::CONFIG.root.application
30
+ app_config = ::Contrast::CONFIG.application
31
31
  {
32
32
  code: app_config.code,
33
33
  group: app_config.group,
@@ -20,37 +20,23 @@ module Contrast
20
20
  class ArchitectureComponent
21
21
  include Contrast::Components::Logger::InstanceMethods
22
22
  # required attributes
23
- attr_reader :type, :url
23
+ attr_accessor :type, :url
24
24
  # optional attributes
25
- attr_reader :remote_host, :remote_port, :vendor
25
+ attr_accessor :remote_host, :remote_port, :vendor
26
26
 
27
27
  # TeamServer only treats these specific values as valid for Architecture Components. It does not know how to
28
28
  # process a message with a different type.
29
+ AC_TYPE_DB = 'db'
29
30
  VALID_TYPES = %w[db ldap ws].cs__freeze
30
31
 
31
32
  class << self
32
- # Convert a DTM for SpeedRacer to an Event for TeamServer.
33
- #
34
- # @param component_dtm [Contrast::Api::Dtm::ArchitectureComponent]
35
- # @return [Contrast::Agent::Reporting::ArchitectureComponent]
36
- def convert component_dtm
37
- report = new
38
- report.attach_data(component_dtm)
39
- report
33
+ def build_database
34
+ msg = new
35
+ msg.type = AC_TYPE_DB
36
+ msg
40
37
  end
41
38
  end
42
39
 
43
- # Attach the data from the protobuf models to this reporter so that it can be sent to TeamServer directly.
44
- #
45
- # @param component_dtm [Contrast::Api::Dtm::ArchitectureComponent]
46
- def attach_data component_dtm
47
- @remote_host = component_dtm.remote_host
48
- @remote_port = component_dtm.remote_port
49
- @type = component_dtm.type
50
- @url = component_dtm.url
51
- @vendor = component_dtm.vendor
52
- end
53
-
54
40
  # Convert the instance variables on the class, and other information, into the identifiers required for
55
41
  # TeamServer to process the JSON form of this message.
56
42
  #
@@ -29,14 +29,14 @@ module Contrast
29
29
  # @return [Array<Contrast::Agent::Reporting::FindingEvent>] the events associated with this finding, if the
30
30
  # finding is event (dataflow) based.
31
31
  attr_reader :events
32
- # @return [String] the evidence associated with this finding, if the finding is event based. deprecated in
33
- # favor of properties
32
+ # # @return [String] the evidence associated with this finding, if the finding is event based. deprecated in
33
+ # # favor of properties
34
34
  # attr_reader :evidence
35
35
  # @return [Hash<String,String>] properties that prove the violation of the rule for this finding
36
36
  attr_reader :properties
37
37
  # @return [Contrast::Agent::Reporting::FindingRequest] the request associated with this finding, if the finding
38
38
  # is request based
39
- attr_reader :request
39
+ attr_accessor :request
40
40
  # @return [String] the uniquely identifying hash of this finding
41
41
  attr_accessor :hash_code
42
42
 
@@ -54,16 +54,6 @@ module Contrast
54
54
  xxssprotection-header-disabled
55
55
  ].cs__freeze
56
56
 
57
- class << self
58
- # @param finding_dtm [Contrast::Api::Dtm::Finding]
59
- # @return [Contrast::Agent::Reporting::Finding]
60
- def convert finding_dtm
61
- report = new(finding_dtm.rule_id)
62
- report.attach_property_data(finding_dtm)
63
- report
64
- end
65
- end
66
-
67
57
  def initialize rule_id
68
58
  @event_method = :PUT
69
59
  @event_endpoint = "#{ Contrast::API.api_url }/api/ng/traces"
@@ -100,28 +90,10 @@ module Contrast
100
90
  event_data = Contrast::Agent::Assess::Events::EventData.new(trigger_node, source, object, ret, args)
101
91
  contrast_event = Contrast::Agent::Assess::ContrastEvent.new(event_data)
102
92
  events << Contrast::Agent::Reporting::FindingEvent.convert(contrast_event)
103
- attach_properties
104
93
  return unless request
105
94
 
106
95
  @request = Contrast::Agent::Reporting::FindingRequest.convert(request)
107
- @routes << Contrast::Agent::Reporting::RouteDiscovery.convert(request.route) if request.route
108
- end
109
-
110
- # Attach the data from a Contrast::Api::Dtm::Finding required for property based findings generated during
111
- # response analysis.
112
- #
113
- # @param finding_dtm [Contrast::Api::Dtm::Finding]
114
- def attach_property_data finding_dtm
115
- @hash_code = finding_dtm.hash_code
116
- @rule_id = finding_dtm.rule_id
117
- finding_dtm.properties.each_pair do |key, value|
118
- @properties[key] = value
119
- end
120
- finding_dtm.routes.each do |route|
121
- @routes << Contrast::Agent::Reporting::RouteDiscovery.convert(route)
122
- end
123
- request = Contrast::Agent::REQUEST_TRACKER.current&.request
124
- @request = Contrast::Agent::Reporting::FindingRequest.convert(request) if request
96
+ @routes << request.discovered_route if request.discovered_route
125
97
  end
126
98
 
127
99
  # Convert the instance variables on the class, and other information, into the identifiers required for
@@ -137,16 +109,9 @@ module Contrast
137
109
  return
138
110
  end
139
111
 
140
- hsh = {
141
- created: created,
142
- hash: hash_code.to_s,
143
- ruleId: rule_id,
144
- session_id: ::Contrast::ASSESS.session_id,
145
- version: 4
146
- }
147
- hsh[:events] = events.map(&:to_controlled_hash) if event_based?
148
- # hsh[:evidence] = evidence unless event_based? || property_based?
149
- hsh[:properties] = properties if property_based?
112
+ hsh = base_hash
113
+ hsh[:events] = events.map(&:to_controlled_hash) if events.any?
114
+ hsh[:properties] = properties if properties.any?
150
115
  hsh[:tags] = Contrast::ASSESS.tags if Contrast::ASSESS.tags
151
116
  return hsh unless request_based?
152
117
 
@@ -155,6 +120,17 @@ module Contrast
155
120
  hsh
156
121
  end
157
122
 
123
+ # @return [Hash] the base of every finding, regardless of type
124
+ def base_hash
125
+ {
126
+ created: created,
127
+ hash: hash_code.to_s,
128
+ ruleId: rule_id,
129
+ session_id: ::Contrast::ASSESS.session_id,
130
+ version: 4
131
+ }
132
+ end
133
+
158
134
  # @raise [ArgumentError]
159
135
  def validate
160
136
  raise(ArgumentError, "#{ self } did not have a proper rule. Unable to continue.") unless @rule_id
@@ -174,12 +150,6 @@ module Contrast
174
150
 
175
151
  private
176
152
 
177
- # Our events have properties on them. To report them to TeamServer, we need to pull them from our object up to
178
- # the Contrast::Agent::Reporting::Finding level.
179
- #
180
- # TODO: RUBY-99999 put properties on events, not just on DTM
181
- def attach_properties; end
182
-
183
153
  def build_events events, event
184
154
  return unless event
185
155
 
@@ -196,7 +166,7 @@ module Contrast
196
166
  #
197
167
  # @return [Boolean]
198
168
  def event_based?
199
- !property_based? && !config_based?
169
+ !property_based? && !config_based? && !hardcoded?
200
170
  end
201
171
 
202
172
  # Rules which are property based must have a property to be sent to TeamServer. Eventually, each rule may own
@@ -100,6 +100,10 @@ module Contrast
100
100
  end
101
101
  end
102
102
 
103
+ def initialize
104
+ @event_sources = []
105
+ end
106
+
103
107
  # Parse the data from a Contrast::Agent::Assess::ContrastEvent to attach what is required for reporting to
104
108
  # TeamServer to this Contrast::Agent::Reporting::FindingEvent
105
109
  #
@@ -208,11 +212,9 @@ module Contrast
208
212
  #
209
213
  # @param event [Contrast::Agent::Assess::ContrastEvent]
210
214
  def event_sources! event
211
- @event_sources = []
212
215
  return unless event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
213
216
 
214
- source = Contrast::Agent::Reporting::FindingEventSource.convert(event)
215
- event_sources << source if source
217
+ event_sources << event.event_source if event.event_source
216
218
  end
217
219
 
218
220
  # Convert the parent id's of the given ContrastEvent to the reportable form for this FindingEvent.
@@ -237,12 +239,13 @@ module Contrast
237
239
  #
238
240
  # @param event [Contrast::Agent::Assess::ContrastEvent]
239
241
  def stack! event
240
- @stack = []
241
- event.stack_trace.each do |stack_event|
242
- if (report = Contrast::Agent::Reporting::FindingEventStack.convert(stack_event))
243
- stack << report
244
- end
245
- end
242
+ @stack = if event.stack_trace
243
+ event.stack_trace.compact.map! do |stack_event|
244
+ Contrast::Agent::Reporting::FindingEventStack.new(stack_event)
245
+ end
246
+ else
247
+ Contrast::Utils::ObjectShare::EMPTY_ARRAY
248
+ end
246
249
  end
247
250
 
248
251
  # Convert the taint ranges of the given ContrastEvent to the reportable form for this FindingEvent.
@@ -61,7 +61,7 @@ module Contrast
61
61
  # 8 is STATIC in Java... we have to placate them for now it has been requested that flags be removed since it
62
62
  # isn't used
63
63
  @flags = 8 unless node.instance_method?
64
- @method_name = node.method_name
64
+ @method_name = node.method_name.to_s
65
65
  @return_type = type_name(event.ret)
66
66
  # if there's a ret, then this method isn't nil. not 100% full proof since you can return nil, but this is the
67
67
  # best we've got currently.
@@ -2,8 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'base64'
5
- require 'contrast/agent/assess/contrast_event'
6
- require 'contrast/agent/assess/events/source_event'
7
5
  require 'contrast/components/logger'
8
6
 
9
7
  module Contrast
@@ -21,25 +19,11 @@ module Contrast
21
19
  # @return [String] the type of the source
22
20
  attr_reader :type
23
21
 
24
- class << self
25
- # @param event [Contrast::Agent::Assess::Events::ContrastEvent] the event to pull the source off of
26
- # @return [Contrast::Agent::Reporting::FindingEventSource]
27
- def convert event
28
- return unless event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
29
-
30
- report = new
31
- report.attach_data(event)
32
- report
33
- end
34
- end
35
-
36
- # Parse the data from a Contrast::Agent::Assess::Events::SourceEvent to attach what is required for reporting
37
- # to TeamServer to this Contrast::Agent::Reporting::FindingEventSource
38
- #
39
- # @param event [Contrast::Agent::Assess::Events::SourceEvent] the event to pull the source off of
40
- def attach_data event
41
- @name = event.source_name
42
- @type = event.source_type
22
+ # @param type [String]
23
+ # @param name [String]
24
+ def initialize type, name
25
+ @type = type
26
+ @name = name
43
27
  end
44
28
 
45
29
  # Convert the instance variables on the class, and other information, into the identifiers required for
@@ -61,6 +45,24 @@ module Contrast
61
45
  }
62
46
  end
63
47
 
48
+ # Convert this EventSource into the format expected for route observation
49
+ #
50
+ # @return [Hash]
51
+ # @raise [ArgumentError]
52
+ def to_controlled_observation_hash
53
+ begin
54
+ validate
55
+ rescue ArgumentError => e
56
+ logger.error('FindingEventSource observation validation failed with: ', e)
57
+ return
58
+ end
59
+
60
+ {
61
+ name: name, # rubocop:disable Security/Module/Name
62
+ type: type
63
+ }
64
+ end
65
+
64
66
  # @raise [ArgumentError]
65
67
  def validate
66
68
  raise(ArgumentError, "#{ self } did not have a proper type. Unable to continue.") unless type && !type.empty?