contrast-agent 3.11.0 → 3.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. checksums.yaml +4 -4
  2. data/.flayignore +1 -0
  3. data/ext/cs__assess_active_record_named/cs__active_record_named.c +7 -2
  4. data/ext/cs__assess_active_record_named/cs__active_record_named.h +1 -0
  5. data/ext/cs__assess_array/cs__assess_array.c +2 -1
  6. data/ext/cs__assess_array/cs__assess_array.h +1 -0
  7. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +3 -7
  8. data/ext/cs__assess_basic_object/cs__assess_basic_object.h +2 -1
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
  10. data/ext/cs__assess_module/cs__assess_module.c +5 -7
  11. data/ext/cs__assess_module/cs__assess_module.h +3 -0
  12. data/ext/cs__common/cs__common.c +1 -1
  13. data/ext/cs__protect_kernel/cs__protect_kernel.c +4 -2
  14. data/ext/cs__protect_kernel/cs__protect_kernel.h +1 -0
  15. data/funchook/autom4te.cache/output.0 +13 -1
  16. data/funchook/autom4te.cache/requests +49 -48
  17. data/funchook/autom4te.cache/traces.0 +3 -0
  18. data/funchook/config.log +217 -378
  19. data/funchook/config.status +24 -23
  20. data/funchook/configure +13 -1
  21. data/funchook/src/Makefile +7 -7
  22. data/funchook/src/config.h +2 -2
  23. data/funchook/src/decoder.o +0 -0
  24. data/funchook/src/distorm.o +0 -0
  25. data/funchook/src/funchook.o +0 -0
  26. data/funchook/src/funchook_io.o +0 -0
  27. data/funchook/src/funchook_syscall.o +0 -0
  28. data/funchook/src/funchook_unix.o +0 -0
  29. data/funchook/src/funchook_x86.o +0 -0
  30. data/funchook/src/instructions.o +0 -0
  31. data/funchook/src/insts.o +0 -0
  32. data/funchook/src/libfunchook.so +0 -0
  33. data/funchook/src/mnemonics.o +0 -0
  34. data/funchook/src/operands.o +0 -0
  35. data/funchook/src/os_func.o +0 -0
  36. data/funchook/src/os_func_unix.o +0 -0
  37. data/funchook/src/prefix.o +0 -0
  38. data/funchook/src/printf_base.o +0 -0
  39. data/funchook/src/textdefs.o +0 -0
  40. data/funchook/src/wstring.o +0 -0
  41. data/funchook/test/Makefile +2 -2
  42. data/funchook/test/funchook_test +0 -0
  43. data/funchook/test/libfunchook_test.so +0 -0
  44. data/funchook/test/test_main.o +0 -0
  45. data/funchook/test/x86_64_test.o +0 -0
  46. data/lib/contrast.rb +0 -1
  47. data/lib/contrast/agent.rb +19 -22
  48. data/lib/contrast/agent/assess.rb +0 -9
  49. data/lib/contrast/agent/assess/policy/patcher.rb +1 -0
  50. data/lib/contrast/agent/assess/policy/policy_node.rb +1 -1
  51. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  52. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -0
  53. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  54. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -3
  55. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +90 -0
  56. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +57 -0
  57. data/lib/contrast/agent/assess/policy/trigger_method.rb +3 -7
  58. data/lib/contrast/agent/assess/policy/trigger_node.rb +4 -1
  59. data/lib/contrast/agent/assess/rule/base.rb +0 -15
  60. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +22 -5
  61. data/lib/contrast/agent/assess/rule/redos.rb +0 -1
  62. data/lib/contrast/agent/at_exit_hook.rb +2 -2
  63. data/lib/contrast/agent/class_reopener.rb +9 -4
  64. data/lib/contrast/agent/exclusion_matcher.rb +0 -1
  65. data/lib/contrast/agent/inventory/policy/datastores.rb +54 -0
  66. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  67. data/lib/contrast/agent/middleware.rb +38 -34
  68. data/lib/contrast/agent/patching/policy/after_load_patch.rb +11 -2
  69. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +51 -56
  70. data/lib/contrast/agent/patching/policy/patch.rb +2 -1
  71. data/lib/contrast/agent/patching/policy/patcher.rb +10 -12
  72. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  73. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  74. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +63 -0
  75. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +52 -0
  76. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +68 -0
  77. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +117 -0
  78. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +54 -0
  79. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +129 -0
  80. data/lib/contrast/agent/protect/policy/policy.rb +6 -6
  81. data/lib/contrast/agent/protect/policy/rule_applicator.rb +51 -0
  82. data/lib/contrast/agent/protect/rule.rb +0 -5
  83. data/lib/contrast/agent/protect/rule/base.rb +6 -5
  84. data/lib/contrast/agent/protect/rule/cmd_injection.rb +3 -3
  85. data/lib/contrast/agent/protect/rule/path_traversal.rb +2 -7
  86. data/lib/contrast/agent/protect/rule/sqli.rb +4 -4
  87. data/lib/contrast/agent/railtie.rb +1 -0
  88. data/lib/contrast/agent/request.rb +2 -6
  89. data/lib/contrast/agent/request_context.rb +5 -6
  90. data/lib/contrast/agent/request_handler.rb +2 -2
  91. data/lib/contrast/agent/response.rb +0 -69
  92. data/lib/contrast/agent/service_heartbeat.rb +2 -2
  93. data/lib/contrast/agent/socket_client.rb +8 -8
  94. data/lib/contrast/agent/static_analysis.rb +2 -3
  95. data/lib/contrast/agent/version.rb +1 -1
  96. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  97. data/lib/contrast/api/speedracer.rb +1 -1
  98. data/lib/contrast/components/agent.rb +17 -12
  99. data/lib/contrast/components/app_context.rb +33 -1
  100. data/lib/contrast/components/assess.rb +25 -15
  101. data/lib/contrast/components/contrast_service.rb +23 -67
  102. data/lib/contrast/components/interface.rb +4 -12
  103. data/lib/contrast/components/inventory.rb +5 -1
  104. data/lib/contrast/components/logger.rb +2 -2
  105. data/lib/contrast/components/protect.rb +40 -4
  106. data/lib/contrast/components/scope.rb +2 -52
  107. data/lib/contrast/components/settings.rb +24 -18
  108. data/lib/contrast/config/protect_rules_configuration.rb +0 -1
  109. data/lib/contrast/{extensions/ruby_core → extension}/assess.rb +12 -14
  110. data/lib/contrast/extension/assess/array.rb +77 -0
  111. data/lib/contrast/{extensions/ruby_core → extension}/assess/assess_extension.rb +2 -2
  112. data/lib/contrast/{extensions/ruby_core → extension}/assess/erb.rb +0 -0
  113. data/lib/contrast/extension/assess/eval_trigger.rb +78 -0
  114. data/lib/contrast/{extensions/ruby_core → extension}/assess/exec_trigger.rb +1 -1
  115. data/lib/contrast/{extensions/ruby_core → extension}/assess/fiber.rb +6 -5
  116. data/lib/contrast/{extensions/ruby_core → extension}/assess/hash.rb +2 -2
  117. data/lib/contrast/extension/assess/kernel.rb +110 -0
  118. data/lib/contrast/{extensions/ruby_core → extension}/assess/regexp.rb +4 -4
  119. data/lib/contrast/{extensions/ruby_core → extension}/assess/string.rb +5 -5
  120. data/lib/contrast/{extensions/ruby_core → extension}/delegator.rb +0 -0
  121. data/lib/contrast/{extensions/ruby_core → extension}/inventory.rb +2 -2
  122. data/lib/contrast/extension/kernel.rb +54 -0
  123. data/lib/contrast/{extensions/ruby_core → extension}/module.rb +0 -0
  124. data/lib/contrast/{extensions/ruby_core → extension}/protect.rb +2 -2
  125. data/lib/contrast/extension/protect/kernel.rb +44 -0
  126. data/lib/contrast/{extensions/ruby_core → extension}/protect/psych.rb +1 -1
  127. data/lib/contrast/{extensions/ruby_core → extension}/thread.rb +0 -0
  128. data/lib/contrast/framework/base_support.rb +22 -0
  129. data/lib/contrast/framework/manager.rb +33 -8
  130. data/lib/contrast/framework/rack/patch/session_cookie.rb +126 -0
  131. data/lib/contrast/framework/rack/patch/support.rb +24 -0
  132. data/lib/contrast/framework/rack/support.rb +22 -0
  133. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +43 -0
  134. data/lib/contrast/framework/rails/patch/assess_configuration.rb +103 -0
  135. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +31 -0
  136. data/lib/contrast/framework/rails/patch/support.rb +67 -0
  137. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +34 -0
  138. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +39 -0
  139. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +73 -0
  140. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +33 -0
  141. data/lib/contrast/framework/rails/support.rb +115 -0
  142. data/lib/contrast/framework/sinatra/application_helper.rb +51 -0
  143. data/lib/contrast/framework/sinatra/patch/base.rb +83 -0
  144. data/lib/contrast/framework/sinatra/patch/support.rb +27 -0
  145. data/lib/contrast/framework/sinatra/support.rb +109 -0
  146. data/lib/contrast/logger/application.rb +80 -0
  147. data/lib/contrast/{agent/logger.rb → logger/log.rb} +23 -54
  148. data/lib/contrast/logger/time.rb +50 -0
  149. data/lib/contrast/tasks/config.rb +54 -0
  150. data/lib/contrast/tasks/service.rb +1 -5
  151. data/lib/contrast/utils/class_util.rb +1 -1
  152. data/lib/contrast/utils/gemfile_reader.rb +2 -2
  153. data/lib/contrast/utils/hash_digest.rb +2 -7
  154. data/lib/contrast/utils/invalid_configuration_util.rb +3 -3
  155. data/lib/contrast/utils/job_servers_running.rb +4 -2
  156. data/lib/contrast/utils/object_share.rb +0 -1
  157. data/lib/contrast/utils/service_response_util.rb +14 -12
  158. data/lib/contrast/utils/service_sender_util.rb +78 -21
  159. data/resources/assess/policy.json +9 -50
  160. data/resources/inventory/policy.json +2 -2
  161. data/resources/protect/policy.json +6 -6
  162. data/ruby-agent.gemspec +5 -1
  163. data/service_executables/VERSION +1 -1
  164. data/service_executables/linux/contrast-service +0 -0
  165. data/service_executables/mac/contrast-service +0 -0
  166. metadata +69 -83
  167. data/funchook/src/libfunchook.dylib +0 -0
  168. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +0 -20
  169. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  170. data/lib/contrast/agent/assess/rule/csrf.rb +0 -66
  171. data/lib/contrast/agent/assess/rule/csrf/csrf_action.rb +0 -28
  172. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +0 -53
  173. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +0 -136
  174. data/lib/contrast/agent/assess/rule/response_scanning_rule.rb +0 -47
  175. data/lib/contrast/agent/assess/rule/response_watcher.rb +0 -36
  176. data/lib/contrast/agent/assess/rule/watcher.rb +0 -36
  177. data/lib/contrast/agent/feature_state.rb +0 -346
  178. data/lib/contrast/agent/protect/rule/csrf.rb +0 -119
  179. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +0 -100
  180. data/lib/contrast/agent/protect/rule/csrf/csrf_token_injector.rb +0 -85
  181. data/lib/contrast/agent/settings_state.rb +0 -88
  182. data/lib/contrast/api/decorators/exclusion.rb +0 -20
  183. data/lib/contrast/extensions/framework/rack/cookie.rb +0 -24
  184. data/lib/contrast/extensions/framework/rack/request.rb +0 -24
  185. data/lib/contrast/extensions/framework/rack/response.rb +0 -23
  186. data/lib/contrast/extensions/framework/rails/action_controller_inheritance.rb +0 -39
  187. data/lib/contrast/extensions/framework/rails/action_controller_railties_helper_inherited.rb +0 -20
  188. data/lib/contrast/extensions/framework/rails/active_record.rb +0 -26
  189. data/lib/contrast/extensions/framework/rails/active_record_named.rb +0 -58
  190. data/lib/contrast/extensions/framework/rails/active_record_time_zone_inherited.rb +0 -21
  191. data/lib/contrast/extensions/framework/rails/buffer.rb +0 -28
  192. data/lib/contrast/extensions/framework/rails/configuration.rb +0 -27
  193. data/lib/contrast/extensions/framework/sinatra/base.rb +0 -59
  194. data/lib/contrast/extensions/ruby_core/assess/array.rb +0 -59
  195. data/lib/contrast/extensions/ruby_core/assess/basic_object.rb +0 -15
  196. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +0 -96
  197. data/lib/contrast/extensions/ruby_core/assess/module.rb +0 -14
  198. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +0 -78
  199. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +0 -40
  200. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -51
  201. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +0 -37
  202. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +0 -61
  203. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +0 -50
  204. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +0 -66
  205. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +0 -115
  206. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +0 -53
  207. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +0 -127
  208. data/lib/contrast/extensions/ruby_core/protect/kernel.rb +0 -30
  209. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +0 -50
  210. data/lib/contrast/framework/rails_support.rb +0 -104
  211. data/lib/contrast/framework/sinatra_application_helper.rb +0 -49
  212. data/lib/contrast/framework/sinatra_support.rb +0 -104
  213. data/lib/contrast/utils/data_store_util.rb +0 -23
  214. data/lib/contrast/utils/rack_assess_session_cookie.rb +0 -104
  215. data/lib/contrast/utils/rails_assess_configuration.rb +0 -95
  216. data/lib/contrast/utils/random_util.rb +0 -22
  217. data/resources/csrf/inject.js +0 -44
@@ -1,119 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- cs__scoped_require 'rack'
5
- cs__scoped_require 'contrast/agent/protect/rule/base'
6
- cs__scoped_require 'contrast/agent/request'
7
- cs__scoped_require 'contrast/agent/response'
8
- cs__scoped_require 'contrast/utils/object_share'
9
- cs__scoped_require 'contrast/utils/stack_trace_utils'
10
- cs__scoped_require 'contrast/utils/timer'
11
-
12
- module Contrast
13
- module Agent
14
- module Protect
15
- module Rule
16
- # The Ruby implementation of the Protect Cross-Site Request Forgery
17
- # rule.
18
- class Csrf < Contrast::Agent::Protect::Rule::Base
19
- NAME = 'csrf'
20
-
21
- def name
22
- NAME
23
- end
24
-
25
- def stream_safe?
26
- false
27
- end
28
-
29
- def request_evaluator
30
- @_request_evaluator ||= Contrast::Agent::Protect::Rule::Csrf::CsrfEvaluator.new
31
- end
32
-
33
- def token_injector
34
- @_token_injector ||= Contrast::Agent::Protect::Rule::Csrf::CsrfTokenInjector.new
35
- end
36
-
37
- BLOCK_MESSAGE = 'CSRF rule triggered. Request blocked.'
38
-
39
- def prefilter context
40
- return unless enabled? && mode != :NO_ACTION
41
-
42
- request = context.request
43
- return if request_evaluator.can_ignore_check?(request)
44
-
45
- parameters = request.parameters
46
- return unless parameters&.any?
47
-
48
- tokens = parameters[token_injector.token_name]
49
- expected_token = token_injector.get_expected_token(context)
50
- return if valid_token?(expected_token, tokens)
51
-
52
- # unexpected token is interpreted as an attack with a match
53
- ia_result = Contrast::Api::Settings::InputAnalysisResult.new
54
- ia_result.input_type = :BODY
55
- ia_result.value = build_attack_string(context.request)
56
-
57
- result = build_attack_with_match(
58
- context,
59
- ia_result,
60
- nil,
61
- expected_token,
62
- details: build_details(expected_token, tokens))
63
-
64
- append_to_activity(context, result)
65
-
66
- raise Contrast::SecurityException.new(self, BLOCK_MESSAGE) if blocked?
67
- end
68
-
69
- def valid_token? expected, actual_tokens
70
- actual_tokens&.include?(expected)
71
- end
72
-
73
- def postfilter context
74
- return unless enabled? && POSTFILTER_MODES.include?(mode)
75
-
76
- token_injector.do_injection(context)
77
- end
78
-
79
- def build_sample context, evaluation, _url, **kwargs
80
- sample = build_base_sample(context, evaluation)
81
- sample.csrf = kwargs[:details]
82
- sample
83
- end
84
-
85
- # Build a subclass of the RaspRuleSample using the query string and the
86
- # evaluation
87
- def build_details expected, actual_tokens
88
- details = Contrast::Api::Dtm::CsrfDetails.new
89
- details.name = Contrast::Utils::StringUtils.protobuf_safe_string(token_injector.token_name)
90
- details.expected = Contrast::Utils::StringUtils.protobuf_safe_string(expected)
91
- presented = build_presented_token(actual_tokens)
92
- details.presented = Contrast::Utils::StringUtils.protobuf_safe_string(presented)
93
- details
94
- end
95
-
96
- def build_presented_token actual_tokens
97
- return Contrast::Utils::ObjectShare::EMPTY_STRING unless actual_tokens&.any?
98
-
99
- actual_tokens.first
100
- end
101
-
102
- # Convert the request parameters into an attack string
103
- def build_attack_string request
104
- arr = []
105
- request.dtm.normalized_request_params.each_with_index do |(name, value), index|
106
- arr << Contrast::Utils::ObjectShare::AND unless index.zero?
107
- arr += [name.to_s, Contrast::Utils::ObjectShare::EQUALS, value.values.to_s]
108
- end
109
- if arr.empty?
110
- request.query_string
111
- else
112
- arr.join
113
- end
114
- end
115
- end
116
- end
117
- end
118
- end
119
- end
@@ -1,100 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- cs__scoped_require 'contrast/utils/object_share'
5
- cs__scoped_require 'contrast/components/interface'
6
- cs__scoped_require 'uri'
7
-
8
- # This class is used by the CSRF rule to determine if the given Request is
9
- # susceptible to CSRF / if the rule applies to it.
10
- class Contrast::Agent::Protect::Rule::Csrf::CsrfEvaluator # rubocop:disable Style/ClassAndModuleChildren
11
- include Contrast::Components::Interface
12
- access_component :logging
13
-
14
- def can_ignore_check? request
15
- if !form_submittable_method?(request)
16
- logger.trace('Ignoring method', method: request.request_method, uri: request.uri)
17
- true
18
- elsif ajax_request?(request)
19
- logger.trace('Ignoring Ajax request', uri: request.uri)
20
- true
21
- elsif empty_post?(request)
22
- logger.trace('Ignoring empty POST', uri: request.uri)
23
- true
24
- elsif !form_content_type?(request)
25
- logger.trace(
26
- 'Ignoring POST request because of Content-Type',
27
- content_type: request.content_type,
28
- uri: request.uri)
29
- true
30
- elsif request.static_request?
31
- logger.trace('Ignoring static request', uri: request.uri)
32
- true
33
- elsif origin_is_referer?(request)
34
- logger.trace('Ignoring equivalent origin-referer', uri: request.uri)
35
- true
36
- elsif login_page?(request)
37
- logger.trace('Ignoring possible login page', uri: request.uri)
38
- true
39
- else
40
- false
41
- end
42
- rescue StandardError => e
43
- logger.debug('Unable to determine if CSRF can be ignored', e)
44
- end
45
-
46
- POST_METHOD = 'POST'
47
- def form_submittable_method? request
48
- POST_METHOD == request.request_method
49
- end
50
-
51
- REQUESTED_WITH = 'X-REQUESTED-WITH'
52
- def ajax_request? request
53
- requested_with = request.header_value(REQUESTED_WITH)
54
- !requested_with.nil? && !requested_with.empty?
55
- end
56
-
57
- def empty_post? request
58
- request.request_body_str.empty? && (request.query_string.nil? || request.query_string.empty?)
59
- end
60
-
61
- FORM_CONTENT_TYPES = %w[text/plain multipart/form-data application/x-www-form-urlencoded].cs__freeze
62
-
63
- def form_content_type? request
64
- content_type = request.content_type
65
- return true if content_type.nil? || content_type.empty?
66
-
67
- content_type.start_with?(*FORM_CONTENT_TYPES)
68
- end
69
-
70
- ORIGIN = 'ORIGIN'
71
- REFERER = 'REFERER'
72
- def origin_is_referer? request
73
- origin = request.header_value(ORIGIN)
74
- referer = request.header_value(REFERER)
75
- return false unless origin && referer
76
- return false if origin.empty? || referer.empty?
77
-
78
- origin = trim_origin(origin)
79
- referer = URI(referer).host
80
- origin == referer
81
- end
82
-
83
- HTTP = 'http://'
84
- HTTPS = 'https://'
85
- def trim_origin origin_header
86
- origin_header = if origin_header.to_s.start_with?(HTTP, HTTPS)
87
- URI(origin_header).host
88
- else
89
- idx = origin_header.index(Contrast::Utils::ObjectShare::COLON)
90
- idx.nil? ? origin_header : origin_header[0, idx]
91
- end
92
- origin_header
93
- end
94
-
95
- LOGIN_MARKERS = %w[login auth j_security verify validate sessions].cs__freeze
96
- def login_page? request
97
- url = request.url.downcase
98
- LOGIN_MARKERS.any? { |marker| url.index(marker) }
99
- end
100
- end
@@ -1,85 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- cs__scoped_require 'contrast/utils/object_share'
5
- cs__scoped_require 'contrast/utils/random_util'
6
-
7
- # This class is used by the CSRF rule to inject the Contrast CSRF token into
8
- # the Response's body, allowing for the detection of CSRF attacks.
9
- class Contrast::Agent::Protect::Rule::Csrf::CsrfTokenInjector # rubocop:disable Style/ClassAndModuleChildren
10
- TKN_NAME = 'cs_csrf_tkn'
11
- def token_name
12
- TKN_NAME
13
- end
14
-
15
- SESSION_TOKEN_PREFIX = '__CONTRAST__'
16
- def token_key
17
- @_token_key ||= SESSION_TOKEN_PREFIX + token_name
18
- end
19
-
20
- def javascript
21
- @_javascript ||= build_javascript
22
- end
23
-
24
- SCRIPT_LOC = File.join('csrf', 'inject.js').cs__freeze
25
- NAME_MARKER = '!TOKEN_NAME!'
26
- VALUE_MARKER = '!TOKEN_VALUE!'
27
- def build_javascript
28
- script = Contrast::Utils::ResourceLoader.load(SCRIPT_LOC)
29
- script&.sub!(NAME_MARKER, TKN_NAME)
30
- script
31
- end
32
-
33
- CSRF_TOKEN_LENGTH = 8
34
- def get_expected_token context
35
- cookies = context.request.request_cookies
36
- token = cookies[token_key]
37
- return token if token
38
- return unless context.response
39
-
40
- build_token(context)
41
- end
42
-
43
- def build_token context
44
- token = Contrast::Utils::RandomUtil.secure_random_string(CSRF_TOKEN_LENGTH)
45
- context&.response&.set_header(token_key, token)
46
- token
47
- end
48
-
49
- CONTENT_TYPE = 'CONTENT-TYPE'
50
-
51
- def wedge_token? response
52
- return true unless response
53
-
54
- content_type = response.header(CONTENT_TYPE)
55
- return true unless content_type
56
-
57
- content_type = content_type.to_s
58
- !content_type.start_with?(*DATA_CONTENT_TYPES)
59
- end
60
-
61
- END_BODY_TAG = %r{</body>}i.cs__freeze
62
- def do_injection context
63
- return unless wedge_token?(context&.response)
64
-
65
- body_string = context&.response&.body
66
- return unless body_string
67
-
68
- index = body_string.index(END_BODY_TAG)
69
- return unless index
70
-
71
- injection = get_expected_token(context)
72
- return unless injection
73
-
74
- injection = javascript.sub(VALUE_MARKER, injection)
75
- body_string.insert(index, injection)
76
- context&.response&.update_body(body_string)
77
- end
78
-
79
- DATA_CONTENT_TYPES = [
80
- 'image/', 'audio/', 'video/',
81
- 'application/json', 'application/xml',
82
- 'application/octet', 'application/force',
83
- 'text/json', 'text/xml'
84
- ].cs__freeze
85
- end
@@ -1,88 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- cs__scoped_require 'securerandom'
5
- cs__scoped_require 'set'
6
- cs__scoped_require 'singleton'
7
-
8
- cs__scoped_require 'contrast/components/interface'
9
-
10
- cs__scoped_require 'contrast/agent/reaction_processor'
11
-
12
- module Contrast
13
- module Agent
14
- # This class functions as a way to query the Agent for its current
15
- # configuration without having to expose other sections of code to the
16
- # decision tree needed to make that determination.
17
- # It should not be accessed directly, but should instead be inherited from
18
- #
19
- # @abstract Use the methods in FeatureState to access this data
20
- class SettingsState
21
- include Contrast::Components::Interface
22
-
23
- access_component :agent, :analysis, :settings, :config, :contrast_service
24
-
25
- # These are components.
26
- attr_accessor :last_update
27
-
28
- # These represent process-level attributes,
29
- # not directly related to Contrast function itself.
30
- attr_reader :pid,
31
- :ppid
32
-
33
- def initialize
34
- @instrumentation_mutex = Mutex.new
35
- @instrumented_packages = {}
36
-
37
- # Last we heard from the Contrast Service
38
- @last_update = nil
39
-
40
- # keep track of which process instantiated this instance
41
- @pid = Process.pid.to_i
42
- @ppid = Process.ppid.to_i
43
-
44
- check_config
45
- end
46
-
47
- def check_config
48
- SETTINGS.reset_state
49
-
50
- if CONFIG.invalid?
51
- AGENT.disable!
52
- logger.error('!!! CONFIG FILE IS INVALID - DISABLING CONTRAST AGENT !!!')
53
- elsif CONFIG.disabled?
54
- AGENT.disable!
55
- logger.warn('Contrast disabled by configuration. Continuing without instrumentation.')
56
- else
57
- AGENT.enable!
58
- end
59
- end
60
-
61
- def assess_rule name
62
- ASSESS.rule name
63
- end
64
-
65
- def instrument package
66
- return if @instrumented_packages[package] # double check is intentional
67
-
68
- @instrumentation_mutex.synchronize do
69
- return if @instrumented_packages[package]
70
-
71
- @instrumented_packages[package] = true
72
- end
73
-
74
- logger.trace_with_time("instrumenting #{ package } ") do
75
- cs__scoped_require(package)
76
- end
77
- rescue LoadError, StandardError => e
78
- @instrumented_packages[package] = false
79
- logger.error("failed instrumenting #{ package } ", e)
80
- end
81
-
82
- # Test only
83
- def protect_rule name
84
- PROTECT.rule name
85
- end
86
- end
87
- end
88
- end
@@ -1,20 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- cs__scoped_require 'contrast/api/settings_pb'
5
-
6
- module Contrast
7
- module Api
8
- module Decorators
9
- # Used to decorate the Contrast::Api::Settings::Exclusion protobuf model
10
- # to allow for renaming of fields.
11
- module Exclusion
12
- def denylist
13
- blacklist
14
- end
15
- end
16
- end
17
- end
18
- end
19
-
20
- Contrast::Api::Settings::Exclusion.include(Contrast::Api::Decorators::Exclusion)
@@ -1,24 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- cs__scoped_require 'contrast/components/interface'
5
-
6
- module Rack
7
- module Session
8
- # Our patch into the Rack::Session::Cookie Class, allowing for the
9
- # runtime detection of insecure configurations on individual cookies
10
- # within the application
11
- class Cookie
12
- include Contrast::Utils::InvalidConfigurationUtil
13
- include Contrast::Components::Interface
14
-
15
- access_component :scope
16
-
17
- alias_method :cs__patched_initialize, :initialize
18
- def initialize app, options = {}
19
- Contrast::Utils::RackAssessSessionCookie.analyze_cookie_initialization(options)
20
- cs__patched_initialize(app, options)
21
- end
22
- end
23
- end
24
- end
@@ -1,24 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- # In earlier versions of rack < 2.0.0 the Rack::Response and Rack::Request do
5
- # not have get_header or set_header methods. This is our way of working
6
- # around that.
7
- module Rack
8
- # Our patch into the Rack::Request class, allowing us to call set_header
9
- # and get_header in our code without worrying about older versions of
10
- # Rack not implementing these methods.
11
- class Request
12
- unless Rack::Request.instance_methods(true).include?(:set_header)
13
- def set_header name, value
14
- @env[name] = value
15
- end
16
- end
17
-
18
- unless Rack::Request.instance_methods(true).include?(:get_header)
19
- def get_header name
20
- @env[name]
21
- end
22
- end
23
- end
24
- end