sqreen 1.18.5 → 1.19.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/lib/sqreen/actions.rb +2 -0
  4. data/lib/sqreen/actions/actions_index.rb +16 -0
  5. data/lib/sqreen/actions/base.rb +4 -10
  6. data/lib/sqreen/actions/block_ip.rb +2 -0
  7. data/lib/sqreen/actions/block_user.rb +2 -0
  8. data/lib/sqreen/actions/ip_range_indexed_action_class.rb +4 -24
  9. data/lib/sqreen/actions/ip_ranges_index.rb +32 -11
  10. data/lib/sqreen/actions/redirect_ip.rb +2 -0
  11. data/lib/sqreen/actions/redirect_user.rb +2 -0
  12. data/lib/sqreen/actions/repository.rb +27 -8
  13. data/lib/sqreen/actions/unknown_action_type.rb +4 -0
  14. data/lib/sqreen/actions/user_action_class.rb +5 -30
  15. data/lib/sqreen/actions/users_index.rb +35 -0
  16. data/lib/sqreen/agent.rb +2 -1
  17. data/lib/sqreen/attack_blocked.rb +2 -0
  18. data/lib/sqreen/backport.rb +2 -0
  19. data/lib/sqreen/backport/clock_gettime.rb +74 -0
  20. data/lib/sqreen/backport/original_name.rb +2 -0
  21. data/lib/sqreen/binding_accessor.rb +2 -0
  22. data/lib/sqreen/binding_accessor/path_elem.rb +2 -0
  23. data/lib/sqreen/binding_accessor/transforms.rb +8 -1
  24. data/lib/sqreen/call_countable.rb +2 -0
  25. data/lib/sqreen/capped_queue.rb +2 -0
  26. data/lib/sqreen/cb.rb +2 -0
  27. data/lib/sqreen/cb_tree.rb +2 -0
  28. data/lib/sqreen/condition_evaluator.rb +2 -0
  29. data/lib/sqreen/conditionable.rb +2 -0
  30. data/lib/sqreen/configuration.rb +14 -0
  31. data/lib/sqreen/context.rb +2 -0
  32. data/lib/sqreen/default_cb.rb +2 -0
  33. data/lib/sqreen/deferred_logger.rb +2 -0
  34. data/lib/sqreen/deliveries.rb +2 -0
  35. data/lib/sqreen/deliveries/batch.rb +2 -0
  36. data/lib/sqreen/deliveries/simple.rb +2 -0
  37. data/lib/sqreen/dependency.rb +3 -1
  38. data/lib/sqreen/dependency/detector.rb +22 -14
  39. data/lib/sqreen/dependency/libsqreen.rb +4 -0
  40. data/lib/sqreen/dependency/new_relic.rb +2 -0
  41. data/lib/sqreen/dependency/rack.rb +10 -5
  42. data/lib/sqreen/dependency/rails.rb +4 -0
  43. data/lib/sqreen/dependency/sentry.rb +2 -0
  44. data/lib/sqreen/dependency/sinatra.rb +12 -1
  45. data/lib/sqreen/encoding_sanitizer.rb +2 -0
  46. data/lib/sqreen/error_handling_middleware.rb +2 -0
  47. data/lib/sqreen/event.rb +2 -0
  48. data/lib/sqreen/events/attack.rb +2 -0
  49. data/lib/sqreen/events/remote_exception.rb +2 -0
  50. data/lib/sqreen/events/request_record.rb +2 -0
  51. data/lib/sqreen/exception.rb +2 -0
  52. data/lib/sqreen/formatter_with_tid.rb +2 -0
  53. data/lib/sqreen/framework_cb.rb +2 -0
  54. data/lib/sqreen/frameworks.rb +2 -0
  55. data/lib/sqreen/frameworks/generic.rb +2 -0
  56. data/lib/sqreen/frameworks/rails.rb +1 -0
  57. data/lib/sqreen/frameworks/rails3.rb +2 -0
  58. data/lib/sqreen/frameworks/request_recorder.rb +2 -0
  59. data/lib/sqreen/frameworks/sinatra.rb +2 -0
  60. data/lib/sqreen/frameworks/sqreen_test.rb +2 -0
  61. data/lib/sqreen/graft.rb +12 -0
  62. data/lib/sqreen/graft/call.rb +150 -0
  63. data/lib/sqreen/{dependency → graft}/callback.rb +12 -4
  64. data/lib/sqreen/graft/hook.rb +316 -0
  65. data/lib/sqreen/{dependency → graft}/hook_point.rb +152 -33
  66. data/lib/sqreen/graft/hook_point_error.rb +10 -0
  67. data/lib/sqreen/invalid_signature_exception.rb +2 -0
  68. data/lib/sqreen/js.rb +2 -0
  69. data/lib/sqreen/js/call_context.rb +2 -0
  70. data/lib/sqreen/js/context_pool.rb +2 -0
  71. data/lib/sqreen/js/exec_js_runnable.rb +2 -0
  72. data/lib/sqreen/js/execjs_adapter.rb +2 -0
  73. data/lib/sqreen/js/executable_js.rb +2 -0
  74. data/lib/sqreen/js/js_service.rb +2 -0
  75. data/lib/sqreen/js/js_service_adapter.rb +2 -0
  76. data/lib/sqreen/js/mini_racer_adapter.rb +2 -0
  77. data/lib/sqreen/js/mini_racer_executable_js.rb +2 -0
  78. data/lib/sqreen/js/thread_local_exec_js_runnable.rb +2 -0
  79. data/lib/sqreen/legacy.rb +8 -0
  80. data/lib/sqreen/{instrumentation.rb → legacy/instrumentation.rb} +31 -2
  81. data/lib/sqreen/log.rb +2 -0
  82. data/lib/sqreen/log/loggable.rb +28 -0
  83. data/lib/sqreen/logger.rb +2 -0
  84. data/lib/sqreen/metrics.rb +2 -0
  85. data/lib/sqreen/metrics/average.rb +2 -0
  86. data/lib/sqreen/metrics/base.rb +2 -0
  87. data/lib/sqreen/metrics/binning.rb +2 -0
  88. data/lib/sqreen/metrics/collect.rb +2 -0
  89. data/lib/sqreen/metrics/sum.rb +2 -0
  90. data/lib/sqreen/metrics_store.rb +2 -0
  91. data/lib/sqreen/metrics_store/already_registered_metric.rb +2 -0
  92. data/lib/sqreen/metrics_store/unknown_metric.rb +2 -0
  93. data/lib/sqreen/metrics_store/unregistered_metric.rb +2 -0
  94. data/lib/sqreen/middleware.rb +2 -0
  95. data/lib/sqreen/mono_time.rb +2 -0
  96. data/lib/sqreen/node.rb +2 -0
  97. data/lib/sqreen/not_implemented_yet.rb +2 -0
  98. data/lib/sqreen/null_logger.rb +2 -0
  99. data/lib/sqreen/payload_creator.rb +2 -0
  100. data/lib/sqreen/payload_creator/header_section.rb +2 -0
  101. data/lib/sqreen/performance_notifications.rb +2 -0
  102. data/lib/sqreen/performance_notifications/binned_metrics.rb +2 -0
  103. data/lib/sqreen/performance_notifications/log.rb +2 -0
  104. data/lib/sqreen/performance_notifications/log_performance.rb +2 -0
  105. data/lib/sqreen/performance_notifications/metrics.rb +2 -0
  106. data/lib/sqreen/performance_notifications/newrelic.rb +2 -0
  107. data/lib/sqreen/prefix.rb +2 -0
  108. data/lib/sqreen/rails_middleware.rb +2 -0
  109. data/lib/sqreen/remote_command.rb +2 -0
  110. data/lib/sqreen/remote_command/failure_output.rb +5 -0
  111. data/lib/sqreen/rules.rb +2 -0
  112. data/lib/sqreen/rules/attrs.rb +2 -0
  113. data/lib/sqreen/rules/auth_track_cb.rb +2 -0
  114. data/lib/sqreen/rules/binding_accessor_matcher_cb.rb +2 -0
  115. data/lib/sqreen/rules/binding_accessor_metrics.rb +2 -0
  116. data/lib/sqreen/rules/blacklist_ips_cb.rb +2 -0
  117. data/lib/sqreen/rules/count_http_codes.rb +2 -0
  118. data/lib/sqreen/rules/crawler_user_agent_matches_cb.rb +2 -0
  119. data/lib/sqreen/rules/crawler_user_agent_matches_metrics_cb.rb +2 -0
  120. data/lib/sqreen/rules/custom_error_cb.rb +2 -0
  121. data/lib/sqreen/rules/devise_auth_track_cb.rb +2 -0
  122. data/lib/sqreen/rules/devise_signup_track_cb.rb +2 -0
  123. data/lib/sqreen/rules/execjs_cb.rb +2 -0
  124. data/lib/sqreen/rules/headers_insert_cb.rb +7 -0
  125. data/lib/sqreen/rules/matcher_rule.rb +2 -0
  126. data/lib/sqreen/rules/not_found_cb.rb +7 -0
  127. data/lib/sqreen/rules/rails_parameters_cb.rb +2 -0
  128. data/lib/sqreen/rules/record_request_context.rb +2 -0
  129. data/lib/sqreen/rules/regexp_rule_cb.rb +2 -0
  130. data/lib/sqreen/rules/rule_cb.rb +2 -0
  131. data/lib/sqreen/rules/run_req_start_actions.rb +3 -1
  132. data/lib/sqreen/rules/run_user_actions.rb +3 -1
  133. data/lib/sqreen/rules/shell_env_cb.rb +2 -0
  134. data/lib/sqreen/rules/signup_track_cb.rb +2 -0
  135. data/lib/sqreen/rules/update_request_context.rb +2 -0
  136. data/lib/sqreen/rules/url_matches_cb.rb +2 -0
  137. data/lib/sqreen/rules/user_agent_matches_cb.rb +2 -0
  138. data/lib/sqreen/rules/waf_cb.rb +28 -5
  139. data/lib/sqreen/rules/xss_cb.rb +2 -0
  140. data/lib/sqreen/run_when_called_cb.rb +2 -0
  141. data/lib/sqreen/runner.rb +25 -7
  142. data/lib/sqreen/runtime_infos.rb +2 -0
  143. data/lib/sqreen/safe_json.rb +2 -0
  144. data/lib/sqreen/sdk.rb +4 -0
  145. data/lib/sqreen/sensitive_data_redactor.rb +2 -0
  146. data/lib/sqreen/serializer.rb +2 -0
  147. data/lib/sqreen/session.rb +2 -0
  148. data/lib/sqreen/shared_storage.rb +2 -0
  149. data/lib/sqreen/shared_storage23.rb +2 -0
  150. data/lib/sqreen/shrink_wrap.rb +16 -0
  151. data/lib/sqreen/signature_verifier.rb +2 -0
  152. data/lib/sqreen/sinatra_middleware.rb +2 -0
  153. data/lib/sqreen/sqreen_signed_verifier.rb +2 -0
  154. data/lib/sqreen/token_invalid_exception.rb +2 -0
  155. data/lib/sqreen/token_not_found_exception.rb +2 -0
  156. data/lib/sqreen/trie.rb +2 -0
  157. data/lib/sqreen/unauthorized.rb +2 -0
  158. data/lib/sqreen/util.rb +5 -0
  159. data/lib/sqreen/util/capped_array.rb +2 -0
  160. data/lib/sqreen/util/capped_hash.rb +2 -0
  161. data/lib/sqreen/util/capped_string.rb +2 -0
  162. data/lib/sqreen/util/capper.rb +2 -0
  163. data/lib/sqreen/version.rb +3 -1
  164. data/lib/sqreen/waf_error.rb +2 -0
  165. data/lib/sqreen/weave.rb +12 -0
  166. data/lib/sqreen/weave/hardcoded.rb +19 -0
  167. data/lib/sqreen/weave/instrumentor.rb +48 -0
  168. data/lib/sqreen/weave/legacy.rb +12 -0
  169. data/lib/sqreen/weave/legacy/instrumentation.rb +406 -0
  170. data/lib/sqreen/web_server.rb +2 -0
  171. data/lib/sqreen/web_server/generic.rb +2 -0
  172. data/lib/sqreen/web_server/passenger.rb +2 -0
  173. data/lib/sqreen/web_server/puma.rb +2 -0
  174. data/lib/sqreen/web_server/rainbows.rb +2 -0
  175. data/lib/sqreen/web_server/thin.rb +2 -0
  176. data/lib/sqreen/web_server/unicorn.rb +2 -0
  177. data/lib/sqreen/web_server/webrick.rb +2 -0
  178. data/lib/sqreen/worker.rb +2 -0
  179. metadata +28 -9
  180. data/lib/sqreen/dependency/hook.rb +0 -102
@@ -1,12 +1,26 @@
1
+ # typed: false
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
4
6
  require 'sqreen/dependency'
5
7
  require 'sqreen/backport/original_name'
8
+ require 'sqreen/graft'
9
+ require 'sqreen/graft/hook_point_error'
6
10
 
7
11
  module Sqreen
8
- module Dependency
9
- class HookPointError < StandardError; end
12
+ module Graft
13
+ class HookSpot < Module
14
+ def initialize(key)
15
+ @key = key
16
+ end
17
+
18
+ attr_reader :key
19
+
20
+ def inspect
21
+ "#<#{self.class.name}: #{@key.inspect}>"
22
+ end
23
+ end
10
24
 
11
25
  class HookPoint
12
26
  def self.parse(hook_point)
@@ -22,8 +36,9 @@ module Sqreen
22
36
 
23
37
  attr_reader :klass_name, :method_kind, :method_name
24
38
 
25
- def initialize(hook_point)
26
- @klass_name, @method_kind, @method_name = Sqreen::Dependency::HookPoint.parse(hook_point)
39
+ def initialize(hook_point, strategy = :chain)
40
+ @klass_name, @method_kind, @method_name = Sqreen::Graft::HookPoint.parse(hook_point)
41
+ @strategy = strategy
27
42
  end
28
43
 
29
44
  def to_s
@@ -34,11 +49,12 @@ module Sqreen
34
49
  return false unless Sqreen::Dependency.const_exist?(@klass_name)
35
50
 
36
51
  if klass_method?
37
- (klass.methods + klass.protected_methods + klass.private_methods).include?(@method_name)
52
+ (klass.singleton_class.public_instance_methods(false) + klass.singleton_class.protected_instance_methods(false) + klass.singleton_class.private_instance_methods(false)).include?(@method_name)
38
53
  elsif instance_method?
39
- (klass.instance_methods + klass.protected_instance_methods + klass.private_instance_methods).include?(@method_name)
54
+ (klass.public_instance_methods(false) + klass.protected_instance_methods(false) + klass.private_instance_methods(false)).include?(@method_name)
40
55
  else
41
- raise HookPointError, 'unknown hook point kind'
56
+ Sqreen::Graft.logger.error { "[#{Process.pid}] #{self} unknown hook point kind" }
57
+ raise HookPointError, "#{self} unknown hook point kind"
42
58
  end
43
59
  end
44
60
 
@@ -56,7 +72,8 @@ module Sqreen
56
72
  elsif instance_method?
57
73
  klass.private_instance_methods.include?(@method_name)
58
74
  else
59
- raise HookPointError, 'unknown hook point kind'
75
+ Sqreen::Graft.logger.error { "[#{Process.pid}] #{self} unknown hook point kind" }
76
+ raise HookPointError, "#{self} unknown hook point kind"
60
77
  end
61
78
  end
62
79
 
@@ -66,7 +83,8 @@ module Sqreen
66
83
  elsif instance_method?
67
84
  klass.protected_instance_methods.include?(@method_name)
68
85
  else
69
- raise HookPointError, 'unknown hook point kind'
86
+ Sqreen::Graft.logger.error { "[#{Process.pid}] #{self} unknown hook point kind" }
87
+ raise HookPointError, "#{self} unknown hook point kind"
70
88
  end
71
89
  end
72
90
 
@@ -74,58 +92,159 @@ module Sqreen
74
92
  @method_kind == :instance_method
75
93
  end
76
94
 
77
- def installed?(suffix)
78
- if klass_method?
79
- (klass.methods + klass.protected_methods + klass.private_methods).include?(:"#{method_name}_with_#{suffix}")
80
- elsif instance_method?
81
- (klass.instance_methods + klass.protected_instance_methods + klass.private_instance_methods).include?(:"#{method_name}_with_#{suffix}")
82
- else
83
- raise HookPointError, 'unknown hook point kind'
95
+ def installed?(key)
96
+ case @strategy
97
+ when :chain then defined(key)
98
+ when :prepend then prepended?(key) && overridden?(key)
84
99
  end
85
100
  end
86
101
 
102
+ def super?
103
+ @strategy == :prepend
104
+ end
105
+
87
106
  def apply(obj, suffix, *args, &block)
107
+ raise 'use super' if super?
108
+
88
109
  obj.send("#{method_name}_without_#{suffix}", *args, &block)
89
110
  end
90
111
 
91
- def install(suffix, &block)
92
- if installed?(suffix)
93
- Sqreen.log.debug "[#{Process.pid}] #{self} already installed"
112
+ def install(key, &block)
113
+ if installed?(key)
114
+ Sqreen::Graft.logger.debug { "[#{Process.pid}] #{self} already installed" }
115
+ return
94
116
  end
95
117
  unless exist?
96
- Sqreen.log.debug "[#{Process.pid}] #{self} hook point not found"
118
+ Sqreen::Graft.logger.debug { "[#{Process.pid}] #{self} hook point not found" }
119
+ return
97
120
  end
98
121
 
99
- define(suffix, &block)
100
- enable(suffix)
122
+ case @strategy
123
+ when :chain
124
+ define(key, &block)
125
+ chain(key)
126
+ when :prepend
127
+ prepend(key)
128
+ override(key, &block)
129
+ end
101
130
  end
102
131
 
103
- def uninstall(suffix)
104
- disable(suffix)
105
- remove(suffix)
132
+ def uninstall(key)
133
+ unless installed?(key)
134
+ Sqreen::Graft.logger.debug { "[#{Process.pid}] #{self} not installed" }
135
+ return
136
+ end
137
+ unless exist?
138
+ Sqreen::Graft.logger.debug { "[#{Process.pid}] #{self} hook point not found" }
139
+ return
140
+ end
141
+
142
+ case @strategy
143
+ when :chain
144
+ disable(key)
145
+ remove(key)
146
+ when :prepend
147
+ unoverride(key) if overridden?(key)
148
+ end
106
149
  end
107
150
 
108
- def enable(suffix)
109
- chain(suffix)
151
+ def enable(key)
152
+ case @strategy
153
+ when :chain
154
+ chain(key)
155
+ when :prepend
156
+ raise HookPointError, "enable called on prepend mode"
157
+ end
110
158
  end
111
159
 
112
- def disable(suffix)
113
- unchain(suffix)
160
+ def disable(key)
161
+ case @strategy
162
+ when :chain
163
+ unchain(key)
164
+ when :prepend
165
+ unoverride(key)
166
+ end
114
167
  end
115
168
 
116
- def disabled?(suffix)
117
- !chained?(suffix)
169
+ def disabled?(key)
170
+ case @strategy
171
+ when :chain
172
+ !chained?(key)
173
+ when :prepend
174
+ !overridden?(key)
175
+ end
118
176
  end
119
177
 
120
178
  private
121
179
 
180
+ def prepend(key)
181
+ target = klass_method? ? klass.singleton_class : klass
182
+ mod = target.ancestors.each { |e| break if e == target; break(e) if e.class == HookSpot && e.key == key }
183
+ mod ||= HookSpot.new(key)
184
+ target.instance_eval { prepend(mod) }
185
+ end
186
+
187
+ def prepended?(key)
188
+ target = klass_method? ? klass.singleton_class : klass
189
+ mod = target.ancestors.each { |e| break if e == target; break(e) if e.class == HookSpot && e.key == key }
190
+
191
+ mod != nil
192
+ end
193
+
194
+ def overridden?(key)
195
+ target = klass_method? ? klass.singleton_class : klass
196
+ mod = target.ancestors.each { |e| break if e == target; break(e) if e.class == HookSpot && e.key == key }
197
+
198
+ (mod.instance_methods(false) + mod.protected_instance_methods(false) + mod.private_instance_methods(false)).include?(method_name)
199
+ end
200
+
201
+ def override(key, &block)
202
+ hook_point = self
203
+ method_name = @method_name
204
+
205
+ target = klass_method? ? klass.singleton_class : klass
206
+ mod = target.ancestors.each { |e| break if e == target; break(e) if e.class == HookSpot && e.key == key }
207
+
208
+ mod.instance_eval do
209
+ if hook_point.private_method?
210
+ private
211
+ elsif hook_point.protected_method?
212
+ protected
213
+ else
214
+ public
215
+ end
216
+
217
+ define_method(:"#{method_name}", &block)
218
+ end
219
+ end
220
+
221
+ def unoverride(key)
222
+ method_name = @method_name
223
+
224
+ target = klass_method? ? klass.singleton_class : klass
225
+ mod = target.ancestors.each { |e| break if e == target; break(e) if e.class == HookSpot && e.key == key }
226
+
227
+ mod.instance_eval { remove_method(method_name) }
228
+ end
229
+
230
+ def defined(suffix)
231
+ if klass_method?
232
+ (klass.methods + klass.protected_methods + klass.private_methods).include?(:"#{method_name}_with_#{suffix}")
233
+ elsif instance_method?
234
+ (klass.instance_methods + klass.protected_instance_methods + klass.private_instance_methods).include?(:"#{method_name}_with_#{suffix}")
235
+ else
236
+ Sqreen::Graft.logger.error { "[#{Process.pid}] #{self} unknown hook point kind" }
237
+ raise HookPointError, "#{self} unknown hook point kind"
238
+ end
239
+ end
240
+
122
241
  def define(suffix, &block)
123
242
  hook_point = self
124
243
  method_name = @method_name
125
244
 
126
245
  if klass_method?
127
246
  klass.singleton_class.instance_eval do
128
- if hook_point.protected_method?
247
+ if hook_point.private_method?
129
248
  private
130
249
  elsif hook_point.protected_method?
131
250
  protected
@@ -137,7 +256,7 @@ module Sqreen
137
256
  end
138
257
  elsif instance_method?
139
258
  klass.class_eval do
140
- if hook_point.protected_method?
259
+ if hook_point.private_method?
141
260
  private
142
261
  elsif hook_point.protected_method?
143
262
  protected
@@ -0,0 +1,10 @@
1
+ # typed: strong
2
+
3
+ # Copyright (c) 2015 Sqreen. All Rights Reserved.
4
+ # Please refer to our terms for more information: https://www.sqreen.com/terms.html
5
+
6
+ module Sqreen
7
+ module Graft
8
+ class HookPointError < StandardError; end
9
+ end
10
+ end
@@ -1,3 +1,5 @@
1
+ # typed: strong
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: strong
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -0,0 +1,8 @@
1
+ # typed: strong
2
+
3
+ # Copyright (c) 2015 Sqreen. All Rights Reserved.
4
+ # Please refer to our terms for more information: https://www.sqreen.com/terms.html
5
+
6
+ module Sqreen
7
+ module Legacy; end
8
+ end
@@ -1,6 +1,9 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
6
+ require 'sqreen/legacy'
4
7
  require 'sqreen/cb_tree'
5
8
  require 'sqreen/log'
6
9
  require 'sqreen/exception'
@@ -36,6 +39,8 @@ require 'set'
36
39
  # end
37
40
 
38
41
  module Sqreen
42
+
43
+ module Legacy
39
44
  class Instrumentation
40
45
  OVERTIME_METRIC = 'request_overtime'.freeze
41
46
 
@@ -84,6 +89,7 @@ module Sqreen
84
89
  start = Sqreen.time
85
90
  res = cb.pre(instance, args, budget, &block)
86
91
  stop = Sqreen.time
92
+ Sqreen.log.debug { "ran pre cb #{cb} => #{res.inspect}" }
87
93
  # The first few pre callbacks could not have a request & hence a budget just yet so we try harder to find it
88
94
  budget = framework.remaining_perf_budget if framework && !budget && Sqreen.performance_budget
89
95
  if budget
@@ -139,6 +145,7 @@ module Sqreen
139
145
  start = Sqreen.time
140
146
  res = cb.post(return_val, instance, args, budget, &block)
141
147
  stop = Sqreen.time
148
+ Sqreen.log.debug { "ran post cb #{cb} => #{res.inspect}" }
142
149
  if budget
143
150
  budget -= (stop - start)
144
151
  cb.overtime! if budget <= 0.0
@@ -193,6 +200,7 @@ module Sqreen
193
200
  start = Sqreen.time
194
201
  res = cb.failing(exception, instance, args, budget, &block)
195
202
  stop = Sqreen.time
203
+ Sqreen.log.debug { "ran failing cb #{cb} => #{res.inspect}" }
196
204
  if budget
197
205
  budget -= (stop - start)
198
206
  cb.overtime! if budget <= 0.0
@@ -508,6 +516,7 @@ module Sqreen
508
516
  saved_meth_name
509
517
  end
510
518
 
519
+ ### bad idea anyway
511
520
  # WARNING We do not actually remove `meth`
512
521
  def unoverride_class_method(klass, meth)
513
522
  saved_meth_name = get_saved_method_name(meth)
@@ -526,6 +535,7 @@ module Sqreen
526
535
  end
527
536
  end
528
537
 
538
+ ### useless now
529
539
  if RUBY_VERSION < '1.9'
530
540
  def adjust_method_name(method)
531
541
  method.to_s
@@ -554,6 +564,8 @@ module Sqreen
554
564
  is_instance_method?(obj, method)
555
565
  end
556
566
 
567
+ ### is that actually used?
568
+ ### if so, do not attempt to alter frozen instances
557
569
  # Override a singleton method on an instance
558
570
  def override_singleton_method(instance, klass_name, meth)
559
571
  @@overriden_singleton_methods = true
@@ -647,8 +659,7 @@ module Sqreen
647
659
 
648
660
  already_overriden = @@overriden_methods.include? key
649
661
  unless already_overriden
650
- Sqreen.log.debug "#{key} not overriden, returning"
651
- return
662
+ Sqreen.log.debug "#{key} apparently not overridden"
652
663
  end
653
664
 
654
665
  defined_cbs = @@registered_callbacks.get(klass, method).flatten
@@ -674,6 +685,7 @@ module Sqreen
674
685
  elsif is_instance_method?(klass, method)
675
686
  unoverride_instance_method(klass, method)
676
687
  else
688
+ ### Module#prepend will take care of that
677
689
  # FIXME: Override define_method and other dynamic ways to
678
690
  # The following should be monitored to make sure we
679
691
  # don't forget dynamically added methods:
@@ -701,8 +713,13 @@ module Sqreen
701
713
  # @return [Array<Sqreen::CB>]
702
714
  def hardcoded_callbacks(framework)
703
715
  [
716
+ ### callback for performing sec responses based on ip
717
+ ### init redefined to implement smartass way to hook it upon the
718
+ ### framework's middleware #call
704
719
  Sqreen::Rules::RunReqStartActions.new(framework),
720
+ ### callback for performing sec responses based on user
705
721
  Sqreen::Rules::RunUserActions.new(Sqreen, :identify, 0),
722
+ ### callback for performing sec responses based on user
706
723
  Sqreen::Rules::RunUserActions.new(Sqreen, :auth_track, 1),
707
724
  ]
708
725
  end
@@ -711,6 +728,7 @@ module Sqreen
711
728
  # @param rules [Array<Hash>] Rules to instrument
712
729
  # @param framework [Sqreen::Frameworks::GenericFramework]
713
730
  def instrument!(rules, framework)
731
+ ### set up rule signature verifier
714
732
  verifier = nil
715
733
  if Sqreen.features['rules_signature'] &&
716
734
  Sqreen.config_get(:rules_verify_signature) == true &&
@@ -720,33 +738,44 @@ module Sqreen
720
738
  Sqreen.log.debug('Rules signature is not enabled')
721
739
  end
722
740
 
741
+ ### force clean instrumentation callback list
723
742
  remove_all_callbacks # Force cb tree to be empty before instrumenting
724
743
 
744
+ ### for each rule description, transform into format for adding callback
725
745
  rules.each do |rule|
726
746
  rcb = Sqreen::Rules.cb_from_rule(rule, self, metrics_engine, verifier)
727
747
  next unless rcb
748
+ ### attach framework to callback
728
749
  rcb.framework = framework
750
+ ### add callback
729
751
  add_callback(rcb)
730
752
  end
731
753
 
732
754
  # add hardcoded callbacks, observing priority
733
755
  hardcoded_callbacks(framework).each { |cb| add_callback(cb) }
734
756
 
757
+ ### globally declare instrumentation ready
758
+ ### from within instance method? not even thread local?
735
759
  Sqreen.instrumentation_ready = true
736
760
  end
737
761
 
738
762
  def initialize(metrics_engine = nil)
739
763
  self.metrics_engine = metrics_engine
740
764
  return if metrics_engine.nil?
765
+ ### init metric to count calls to sqreen
741
766
  metrics_engine.create_metric('name' => CallCountable::COUNT_CALLS,
742
767
  'period' => 60,
743
768
  'kind' => 'Sum')
769
+ ### init metric to count request whitelist matches (ip or path whitelist)
744
770
  metrics_engine.create_metric('name' => Sqreen::Rules::RecordRequestContext::WHITELISTED_METRIC,
745
771
  'period' => 60,
746
772
  'kind' => 'Sum')
773
+ ### init metric to count over budget hits
747
774
  metrics_engine.create_metric('name' => OVERTIME_METRIC,
748
775
  'period' => 60,
749
776
  'kind' => 'Sum')
750
777
  end
751
778
  end
752
779
  end
780
+
781
+ end