contrast-agent 3.12.2 → 3.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/.dockerignore +0 -1
  3. data/.gitignore +1 -1
  4. data/.simplecov +1 -1
  5. data/Rakefile +31 -0
  6. data/ext/build_funchook.rb +0 -2
  7. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +2 -8
  8. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +0 -1
  9. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +1 -6
  10. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +1 -5
  11. data/ext/cs__assess_yield_track/cs__assess_yield_track.h +0 -1
  12. data/ext/cs__common/cs__common.c +24 -0
  13. data/ext/cs__common/cs__common.h +3 -0
  14. data/ext/cs__common/extconf.rb +0 -14
  15. data/ext/extconf_common.rb +0 -28
  16. data/lib/contrast.rb +3 -1
  17. data/lib/contrast/agent.rb +14 -2
  18. data/lib/contrast/agent/assess/contrast_event.rb +28 -167
  19. data/lib/contrast/agent/assess/events/source_event.rb +3 -7
  20. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +1 -1
  21. data/lib/contrast/agent/assess/policy/policy_node.rb +4 -98
  22. data/lib/contrast/agent/assess/policy/propagation_method.rb +1 -2
  23. data/lib/contrast/agent/assess/policy/propagation_node.rb +5 -1
  24. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  25. data/lib/contrast/agent/assess/policy/propagator/insert.rb +1 -4
  26. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +9 -1
  27. data/lib/contrast/agent/assess/policy/propagator/remove.rb +6 -11
  28. data/lib/contrast/agent/assess/policy/propagator/select.rb +4 -4
  29. data/lib/contrast/agent/assess/policy/propagator/split.rb +2 -2
  30. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +4 -4
  31. data/lib/contrast/agent/assess/policy/propagator/trim.rb +6 -10
  32. data/lib/contrast/agent/assess/policy/source_method.rb +1 -2
  33. data/lib/contrast/agent/assess/policy/trigger_method.rb +1 -9
  34. data/lib/contrast/agent/assess/policy/trigger_node.rb +16 -4
  35. data/lib/contrast/agent/assess/properties.rb +4 -382
  36. data/lib/contrast/agent/assess/property/evented.rb +78 -0
  37. data/lib/contrast/agent/assess/property/tagged.rb +339 -0
  38. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +2 -20
  39. data/lib/contrast/agent/assess/tag.rb +27 -12
  40. data/lib/contrast/agent/at_exit_hook.rb +3 -1
  41. data/lib/contrast/agent/exclusion_matcher.rb +2 -2
  42. data/lib/contrast/agent/inventory/policy/datastores.rb +0 -1
  43. data/lib/contrast/agent/middleware.rb +2 -14
  44. data/lib/contrast/agent/patching/policy/patch.rb +1 -1
  45. data/lib/contrast/agent/patching/policy/policy.rb +3 -3
  46. data/lib/contrast/agent/patching/policy/policy_node.rb +2 -2
  47. data/lib/contrast/agent/protect/policy/rule_applicator.rb +2 -2
  48. data/lib/contrast/agent/protect/rule/base.rb +19 -31
  49. data/lib/contrast/agent/protect/rule/base_service.rb +1 -1
  50. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +2 -7
  51. data/lib/contrast/agent/protect/rule/xxe.rb +1 -0
  52. data/lib/contrast/agent/reaction_processor.rb +3 -3
  53. data/lib/contrast/agent/request.rb +92 -331
  54. data/lib/contrast/agent/request_context.rb +15 -15
  55. data/lib/contrast/agent/request_handler.rb +1 -1
  56. data/lib/contrast/agent/response.rb +2 -14
  57. data/lib/contrast/agent/scope.rb +1 -1
  58. data/lib/contrast/agent/service_heartbeat.rb +7 -9
  59. data/lib/contrast/agent/static_analysis.rb +1 -1
  60. data/lib/contrast/agent/thread_watcher.rb +49 -0
  61. data/lib/contrast/agent/version.rb +1 -1
  62. data/lib/contrast/agent/worker_thread.rb +24 -0
  63. data/lib/contrast/api.rb +3 -5
  64. data/lib/contrast/api/communication.rb +20 -0
  65. data/lib/contrast/api/communication/connection_status.rb +41 -0
  66. data/lib/contrast/api/communication/messaging_queue.rb +79 -0
  67. data/lib/contrast/{utils/service_response_util.rb → api/communication/response_processor.rb} +9 -18
  68. data/lib/contrast/api/communication/service_lifecycle.rb +61 -0
  69. data/lib/contrast/api/communication/socket.rb +45 -0
  70. data/lib/contrast/api/communication/socket_client.rb +76 -0
  71. data/lib/contrast/api/communication/speedracer.rb +111 -0
  72. data/lib/contrast/api/communication/tcp_socket.rb +31 -0
  73. data/lib/contrast/api/communication/unix_socket.rb +27 -0
  74. data/lib/contrast/api/decorators.rb +10 -0
  75. data/lib/contrast/api/decorators/address.rb +60 -0
  76. data/lib/contrast/api/decorators/application_settings.rb +7 -3
  77. data/lib/contrast/api/decorators/application_update.rb +0 -9
  78. data/lib/contrast/api/decorators/http_request.rb +139 -0
  79. data/lib/contrast/api/decorators/message.rb +75 -0
  80. data/lib/contrast/api/decorators/rasp_rule_sample.rb +28 -0
  81. data/lib/contrast/api/decorators/route_coverage.rb +57 -0
  82. data/lib/contrast/api/decorators/trace_event.rb +99 -0
  83. data/lib/contrast/api/decorators/trace_event_object.rb +57 -0
  84. data/lib/contrast/api/decorators/trace_event_signature.rb +46 -0
  85. data/lib/contrast/api/decorators/trace_taint_range.rb +51 -0
  86. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +109 -0
  87. data/lib/contrast/api/decorators/user_input.rb +40 -0
  88. data/lib/contrast/components/app_context.rb +0 -7
  89. data/lib/contrast/components/config.rb +4 -9
  90. data/lib/contrast/components/interface.rb +1 -1
  91. data/lib/contrast/components/settings.rb +0 -6
  92. data/lib/contrast/extension/assess.rb +0 -1
  93. data/lib/contrast/extension/assess/assess_extension.rb +1 -2
  94. data/lib/contrast/extension/assess/fiber.rb +1 -1
  95. data/lib/contrast/extension/assess/string.rb +1 -1
  96. data/lib/contrast/extension/inventory.rb +0 -1
  97. data/lib/contrast/framework/base_support.rb +0 -23
  98. data/lib/contrast/framework/manager.rb +0 -9
  99. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -3
  100. data/lib/contrast/framework/rails/patch/assess_configuration.rb +3 -4
  101. data/lib/contrast/framework/rails/support.rb +3 -32
  102. data/lib/contrast/framework/sinatra/patch/base.rb +1 -1
  103. data/lib/contrast/framework/sinatra/support.rb +11 -22
  104. data/lib/contrast/funchook/funchook.rb +45 -0
  105. data/lib/contrast/logger/application.rb +1 -1
  106. data/lib/contrast/logger/format.rb +51 -0
  107. data/lib/contrast/logger/log.rb +8 -2
  108. data/lib/contrast/utils/assess/tracking_util.rb +45 -20
  109. data/lib/contrast/utils/hash_digest.rb +11 -2
  110. data/lib/contrast/utils/invalid_configuration_util.rb +1 -17
  111. data/lib/contrast/utils/inventory_util.rb +2 -7
  112. data/lib/contrast/utils/object_share.rb +0 -1
  113. data/lib/contrast/utils/os.rb +16 -4
  114. data/lib/contrast/utils/stack_trace_utils.rb +0 -1
  115. data/lib/contrast/utils/tag_util.rb +1 -1
  116. data/lib/contrast/utils/thread_tracker.rb +1 -14
  117. data/lib/contrast/utils/timer.rb +1 -17
  118. data/ruby-agent.gemspec +4 -4
  119. metadata +48 -72
  120. data/funchook/Makefile +0 -29
  121. data/funchook/autom4te.cache/output.0 +0 -4964
  122. data/funchook/autom4te.cache/requests +0 -77
  123. data/funchook/autom4te.cache/traces.0 +0 -361
  124. data/funchook/config.log +0 -651
  125. data/funchook/config.status +0 -1015
  126. data/funchook/configure +0 -4964
  127. data/funchook/src/Makefile +0 -70
  128. data/funchook/src/config.h +0 -101
  129. data/funchook/src/config.h.in +0 -100
  130. data/funchook/src/decoder.o +0 -0
  131. data/funchook/src/distorm.o +0 -0
  132. data/funchook/src/funchook.o +0 -0
  133. data/funchook/src/funchook_io.o +0 -0
  134. data/funchook/src/funchook_syscall.o +0 -0
  135. data/funchook/src/funchook_unix.o +0 -0
  136. data/funchook/src/funchook_x86.o +0 -0
  137. data/funchook/src/instructions.o +0 -0
  138. data/funchook/src/insts.o +0 -0
  139. data/funchook/src/libfunchook.dylib +0 -0
  140. data/funchook/src/mnemonics.o +0 -0
  141. data/funchook/src/operands.o +0 -0
  142. data/funchook/src/os_func.o +0 -0
  143. data/funchook/src/os_func_unix.o +0 -0
  144. data/funchook/src/prefix.o +0 -0
  145. data/funchook/src/printf_base.o +0 -0
  146. data/funchook/src/textdefs.o +0 -0
  147. data/funchook/src/wstring.o +0 -0
  148. data/funchook/test/Makefile +0 -43
  149. data/funchook/test/funchook_test +0 -0
  150. data/funchook/test/libfunchook_test.so +0 -0
  151. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +0 -20
  152. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  153. data/funchook/test/test_main.o +0 -0
  154. data/funchook/test/x86_64_test.o +0 -0
  155. data/lib/contrast/agent/assess/adjusted_span.rb +0 -27
  156. data/lib/contrast/agent/socket_client.rb +0 -134
  157. data/lib/contrast/api/connection_status.rb +0 -49
  158. data/lib/contrast/api/socket.rb +0 -43
  159. data/lib/contrast/api/speedracer.rb +0 -188
  160. data/lib/contrast/api/tcp_socket.rb +0 -29
  161. data/lib/contrast/api/unix_socket.rb +0 -25
  162. data/lib/contrast/framework/sinatra/application_helper.rb +0 -51
  163. data/lib/contrast/framework/view_technologies_descriptor.rb +0 -21
  164. data/lib/contrast/internal_exception.rb +0 -8
  165. data/lib/contrast/utils/cache.rb +0 -58
  166. data/lib/contrast/utils/service_sender_util.rb +0 -167
  167. data/lib/contrast/utils/sinatra_helper.rb +0 -49
@@ -3,6 +3,7 @@
3
3
 
4
4
  cs__scoped_require 'contrast/agent/assess/policy/trigger/reflected_xss'
5
5
  cs__scoped_require 'contrast/agent/assess/policy/trigger/xpath'
6
+ cs__scoped_require 'contrast/api/decorators/trace_taint_range_tags'
6
7
 
7
8
  module Contrast
8
9
  module Agent
@@ -167,8 +168,8 @@ module Contrast
167
168
 
168
169
  tags.each do |tag|
169
170
  raise(ArgumentError, "Rule #{ rule_id } had an invalid tag. #{ tag } is not a known value.") unless
170
- Contrast::Agent::Assess::Policy::PolicyNode::VALID_TAGS.include?(tag) ||
171
- Contrast::Agent::Assess::Policy::PolicyNode::VALID_SOURCE_TAGS.include?(tag)
171
+ Contrast::Api::Decorators::TraceTaintRangeTags::VALID_TAGS.include?(tag) ||
172
+ Contrast::Api::Decorators::TraceTaintRangeTags::VALID_SOURCE_TAGS.include?(tag)
172
173
  end
173
174
  end
174
175
 
@@ -191,10 +192,21 @@ module Contrast
191
192
  return find_ranges_by_any_tag(cs__properties, tags) if tags.length == 1
192
193
 
193
194
  ranges = []
195
+ # TODO: RUBY-946 clean this up, perhaps with
196
+ # tags.each { |tag| applicable << cs__properties.fetch_tag(tag) }
197
+ # return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless applicable.length == tags.length
198
+ # ...
194
199
  # find all the indicies on the source that have all the given tags
195
200
  (0..length).each do |idx|
196
- tags_at = cs__properties.tag_names_at(idx)
197
- ranges << idx if tags.all? { |tag| tags_at.include?(tag) }
201
+ tags_at = cs__properties.tags_at(idx)
202
+ ranges << idx if tags.all? do |tag|
203
+ found = false
204
+ tags_at.each do |tag_at|
205
+ found = tag_at.label == tag
206
+ break if found
207
+ end
208
+ found
209
+ end
198
210
  end
199
211
  # break early if no indicies satisfy all the tags
200
212
  return Contrast::Utils::ObjectShare::EMPTY_ARRAY if ranges.empty?
@@ -4,9 +4,8 @@
4
4
  cs__scoped_require 'set'
5
5
  cs__scoped_require 'base64'
6
6
  cs__scoped_require 'contrast/utils/prevent_serialization'
7
- cs__scoped_require 'contrast/utils/tag_util'
8
- cs__scoped_require 'contrast/agent/assess/events/event_factory'
9
- cs__scoped_require 'contrast/agent/assess/events/source_event'
7
+ cs__scoped_require 'contrast/agent/assess/property/evented'
8
+ cs__scoped_require 'contrast/agent/assess/property/tagged'
10
9
 
11
10
  module Contrast
12
11
  module Agent
@@ -19,6 +18,8 @@ module Contrast
19
18
  # user input.
20
19
  class Properties
21
20
  include Contrast::Utils::PreventSerialization
21
+ include Contrast::Agent::Assess::Property::Evented
22
+ include Contrast::Agent::Assess::Property::Tagged
22
23
 
23
24
  # CONTRAST-36937
24
25
  # Creating these on Properties is expensive. We want to delay this for
@@ -27,18 +28,6 @@ module Contrast
27
28
  @_properties ||= {}
28
29
  end
29
30
 
30
- def events
31
- @_events ||= []
32
- end
33
-
34
- def tracked?
35
- tags? && tags.any?
36
- end
37
-
38
- def tagged? label
39
- tags? && tags.key?(label)
40
- end
41
-
42
31
  def add_properties hash
43
32
  return unless hash
44
33
 
@@ -50,373 +39,6 @@ module Contrast
50
39
 
51
40
  properties[name] = value
52
41
  end
53
-
54
- def any_tags_between? start, finish
55
- return false unless tags?
56
-
57
- range = Contrast::Agent::Assess::Tag.new(start + finish, start)
58
- tags.each_value do |tag_array|
59
- return true if tag_array.any? { |tag| tag.overlaps?(range) }
60
- end
61
- false
62
- end
63
-
64
- # Find all of the ranges that span a given index. This is used
65
- # in propagation when we need to shift tags about. For instance, in
66
- # the append method when we need to see if any tag at the end needs
67
- # to be expanded out to the size of the new String.
68
- #
69
- # Note: Tags do not know their key, so this is only the range covered
70
- #
71
- # @param idx [Integer] the index to check for tags
72
- # @return [Array<Contrast::Agent::Assess::Tag>] a set of all the Tags
73
- # covering the given index. This is only the ranges, not the names.
74
- def tags_at idx
75
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags?
76
-
77
- at = []
78
- tags.each_value do |tag_array|
79
- tag_array.each do |tag|
80
- if tag.covers?(idx)
81
- at << tag
82
- elsif tag.above?(idx)
83
- break
84
- end
85
- end
86
- end
87
- at
88
- end
89
-
90
- # Find all of the tag names that span a given index.
91
- #
92
- # @param idx [Integer] the index to check for tags
93
- # @return [Set<String>] a set of all the tags covering the given index.
94
- # This is only the names of the tags, not their ranges.
95
- def tag_names_at idx
96
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags?
97
-
98
- at = Set.new
99
- tags.each_pair do |tag_name, tag_array|
100
- tag_array.each do |tag|
101
- if tag.covers?(idx)
102
- at << tag_name
103
- elsif tag.above?(idx)
104
- break
105
- end
106
- end
107
- end
108
- at
109
- end
110
-
111
- # given a range, select all tags in that range the selected tags are
112
- # shifted such that the start index of the new tag (0) aligns with the
113
- # given start index in the range
114
- #
115
- # current tags: 5-15
116
- # range : 5-10
117
- # result : 0-05
118
- def tags_at_range range
119
- return Contrast::Utils::ObjectShare::EMPTY_HASH unless tags?
120
-
121
- at = Hash.new { |h, k| h[k] = [] }
122
- length = range.stop - range.start
123
- tags.each_pair do |key, value|
124
- add = []
125
- value.each do |tag|
126
- comparison = tag.compare_range(range.start, range.stop)
127
- # BELOW and ABOVE are applicable to this check and are removed.
128
- case comparison
129
- # part of the tag is being selected
130
- when Contrast::Agent::Assess::Tag::LOW_SPAN
131
- add << Contrast::Agent::Assess::Tag.new(length)
132
- # the tag exists in the requested range, figure out the boundaries
133
- when Contrast::Agent::Assess::Tag::WITHIN
134
- start = tag.start_idx - range.start
135
- finish = length - start
136
- add << Contrast::Agent::Assess::Tag.new(finish, start)
137
- # the tag spans the requested range.
138
- when Contrast::Agent::Assess::Tag::WITHOUT
139
- add << Contrast::Agent::Assess::Tag.new(length)
140
- # part of the tag is being selected
141
- when Contrast::Agent::Assess::Tag::HIGH_SPAN
142
- add << Contrast::Agent::Assess::Tag.new(length)
143
- end
144
- end
145
- next if add.empty?
146
-
147
- at[key] = add
148
- end
149
- at
150
- end
151
-
152
- # Given a tag name and range object, add a new tag to this
153
- # collection. If the given range touches an existing tag,
154
- # we'll combine the two, adjusting the existing one and
155
- # dropping this new one.
156
- #
157
- # @param label [String] the name of the tag
158
- # @param range [Contrast::Agent::Assess::AdjustedSpan] the span that
159
- # the tag covers
160
- def add_tag label, range
161
- length = range.stop - range.start
162
- tag = Contrast::Agent::Assess::Tag.new(length, range.start)
163
- existing = fetch_tag(label)
164
- tags[label] = Contrast::Utils::TagUtil.ordered_merge(existing, tag)
165
- end
166
-
167
- def set_tags label, tag_ranges
168
- tags[label] = tag_ranges
169
- end
170
-
171
- # Remove all tags with a given label
172
- def delete_tags label
173
- return unless tags?
174
-
175
- tags.delete(label)
176
- end
177
-
178
- # Reset the tag hash
179
- def clear_tags
180
- return unless tags?
181
-
182
- tags.clear
183
- end
184
-
185
- # Returns a list of all current tag labels, most likely to be used for
186
- # a splat operation
187
- def tag_keys
188
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags?
189
-
190
- tags.keys
191
- end
192
-
193
- # Calls merge to combine touching or overlapping tags
194
- # Deletes empty tags
195
- def cleanup_tags
196
- return unless tags?
197
-
198
- Contrast::Utils::TagUtil.merge_tags(tags)
199
- tags.delete_if { |_, value| value.empty? }
200
- end
201
-
202
- # We'll use this as a helper method to retrieve tags from the hash.
203
- # Because the hash auto-populates an empty array when we try to access
204
- # a tag in it, we cannot use the [] method without side effect. To get
205
- # around this, we'll use a fetch work around.
206
- def fetch_tag label
207
- return unless tags?
208
-
209
- tags.fetch(label, nil)
210
- end
211
-
212
- # Convert the tags of this object into the TraceTaintRange requried to
213
- # be sent to the service
214
- def tags_to_dtm
215
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags?
216
-
217
- ranges = []
218
- tags.each_pair do |key, value|
219
- next if value.empty?
220
-
221
- value.each do |tag|
222
- range = Contrast::Api::Dtm::TraceTaintRange.new
223
- range.tag = Contrast::Utils::StringUtils.protobuf_safe_string(key)
224
- range.range = tag.start_idx.to_s + Contrast::Utils::ObjectShare::COLON + tag.end_idx.to_s
225
- ranges << range
226
- end
227
- end
228
- ranges
229
- end
230
-
231
- # Remove all tags within the given ranges.
232
- # This does not delete an entire tag if part of that tag is
233
- # outside this range, meaning we may reduce sizes of tags
234
- # or split them.
235
- #
236
- # If shift is true, it is assumed the characters at those ranges were
237
- # removed. If shift is false, it is assumed those ranges were replaced
238
- # by the same number of characters and no shift is needed.
239
- #
240
- # current tags: 0-15
241
- # range: 5-10
242
- # result: 0-5, 10-15
243
- def delete_tags_at_ranges ranges, shift = true
244
- return unless tags?
245
-
246
- # Stage one - delete the tags w/o changing their
247
- # location.
248
- ranges.each do |range|
249
- remove_tags(range)
250
- end
251
-
252
- return unless shift
253
-
254
- # the amount we've already removed from the string
255
- shift = 0
256
- # Stage two - shift the tags to the left to account
257
- # for the sections that were deleted.
258
- ranges.each do |range|
259
- shift_tags_for_deletion(range, shift)
260
- shift += (range.stop - range.start)
261
- end
262
-
263
- # Clean up and merge any touching tags
264
- Contrast::Utils::TagUtil.merge_tags(tags)
265
- end
266
-
267
- # Shift all the tags in this object by the given ranges.
268
- # This method assumes the ranges are sorted, meaning
269
- # the leftmost (lowest) range is first
270
- #
271
- # current tags: 0-15
272
- # range: 5-10
273
- # result: 0-5, 10-20
274
- def shift_tags ranges
275
- return unless tags?
276
-
277
- ranges.each do |range|
278
- shift_tags_for_insertion(range)
279
- end
280
- end
281
-
282
- # Add an event to these properties. It will be used to build
283
- # a trace if this object ends up in a trigger.
284
- def add_event event
285
- events << event
286
- self
287
- end
288
-
289
- def build_event policy_node, tagged, object, ret, args, source_type = nil, source_name = nil
290
- event = Contrast::Agent::Assess::Events::EventFactory.build(policy_node, tagged, object, ret, args, source_type, source_name)
291
- add_event(event)
292
- report_sources(tagged, event)
293
- end
294
-
295
- private
296
-
297
- def report_sources tagged, event
298
- return unless tagged && !tagged.to_s.empty?
299
- return unless event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
300
- return unless event&.source_type
301
-
302
- current_request = Contrast::Agent::REQUEST_TRACKER.current
303
- return unless current_request
304
- return if current_request.observed_route.sources.any? { |source| source.type == event.forced_source_type && source.name == event.forced_source_name }
305
-
306
- event_source_dtm = event.build_event_source_dtm
307
- return unless event_source_dtm
308
-
309
- current_request.observed_route.sources << event_source_dtm
310
- end
311
-
312
- # Because of the auto-fill thing, we should not allow direct access to
313
- # the tags hash. Instead, the methods above should be used to do
314
- # operations like add, delete, and fetch.
315
- #
316
- # CONTRAST-22914
317
- # please do NOT expose this w/ an attr_reader / accessor. there are
318
- # helper methods in this class that safely access the hash. the tags
319
- # method is private to avoid the side effect of a direct lookup with
320
- # `[]` adding an empty array to the hash.
321
- def tags
322
- @_tags ||= Hash.new { |h, k| h[k] = [] }
323
- end
324
-
325
- # Creating Tags is expensive and we check for Tags all the time on
326
- # untracked things. ALWAYS!!! call this method before checking if
327
- # an object has tags
328
- def tags?
329
- instance_variable_defined?(:@_tags)
330
- end
331
-
332
- # Remove the tag ranges covering the given range
333
- def remove_tags range
334
- return unless tags?
335
-
336
- full_delete = []
337
- tags.each_pair do |key, value|
338
- remove = []
339
- add = []
340
- value.each do |tag|
341
- comparison = tag.compare_range(range.start, range.stop)
342
- # ABOVE and BELOW are not affected by this check
343
- case comparison
344
- when Contrast::Agent::Assess::Tag::LOW_SPAN
345
- tag.update_end(range.start)
346
- when Contrast::Agent::Assess::Tag::WITHIN
347
- remove << tag
348
- when Contrast::Agent::Assess::Tag::WITHOUT
349
- new_tag = tag.clone
350
- new_tag.update_start(range.stop)
351
- add << new_tag
352
- tag.update_end(range.start)
353
- when Contrast::Agent::Assess::Tag::HIGH_SPAN
354
- tag.update_start(range.stop)
355
- end
356
- end
357
- value.delete_if { |tag| remove.include?(tag) }
358
- Contrast::Utils::TagUtil.ordered_merge(value, add)
359
- full_delete << key if value.empty?
360
- end
361
- full_delete.each { |key| tags.delete(key) }
362
- end
363
-
364
- # Shift the tag ranges covering the given range
365
- # We assume this is for a deletion, meaning we
366
- # have to move tags to the left
367
- def shift_tags_for_deletion range, shift
368
- return unless tags?
369
-
370
- tags.each_value do |value|
371
- value.each do |tag|
372
- comparison = tag.compare_range(range.start - shift, range.stop - shift)
373
- length = range.stop - range.start
374
- case comparison
375
- # this is really the only thing we need to shift
376
- when Contrast::Agent::Assess::Tag::ABOVE
377
- tag.shift(0 - length)
378
- end
379
- end
380
- end
381
- end
382
-
383
- # Shift the tag ranges covering the given range
384
- # We assume this is for a insertion, meaning we
385
- # have to move tags to the right
386
- def shift_tags_for_insertion range
387
- return unless tags?
388
-
389
- tags.each_value do |value|
390
- add = []
391
- value.each do |tag|
392
- comparison = tag.compare_range(range.start, range.stop)
393
- length = range.stop - range.start
394
- # BELOW is not affected by this check
395
- case comparison
396
- # part of the tag is being inserted on
397
- when Contrast::Agent::Assess::Tag::LOW_SPAN
398
- new_tag = tag.clone
399
- new_tag.update_start(range.start)
400
- new_tag.shift(length)
401
- add << new_tag
402
- tag.update_end(range.start)
403
- # the tag exists in the inserted range. it is partially shifted
404
- when Contrast::Agent::Assess::Tag::WITHIN
405
- tag.shift(length)
406
- # the tag spans the range. leave the part below alone
407
- when Contrast::Agent::Assess::Tag::WITHOUT
408
- new_tag = tag.clone
409
- new_tag.update_start(range.start)
410
- new_tag.shift(length)
411
- add << new_tag
412
- tag.update_end(range.start)
413
- when Contrast::Agent::Assess::Tag::HIGH_SPAN, Contrast::Agent::Assess::Tag::ABOVE
414
- tag.shift(length)
415
- end
416
- end
417
- Contrast::Utils::TagUtil.ordered_merge(value, add)
418
- end
419
- end
420
42
  end
421
43
  end
422
44
  end