Class: Contrast::Extension::Assess::StringPropagator

Inherits:
Object
  • Object
show all
Includes:
Components::Interface
Defined in:
lib/contrast/extension/assess/string.rb,
ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c

Overview

This Class provides us with a way to invoke String propagation for those methods which are too complex to fit into one of the standard Contrast::Agent::Assess::Policy::Propagator molds without cluttering up the String Class or exposing our methods there.

Constant Summary collapse

NODE_HASH =
{
    'class_name' => 'String',
    'instance_method' => true,
    'method_name' => 'interpolate',
    'method_visibility' => 'public',
    'action' => 'CUSTOM',
    'source' => 'O,P0',
    'target' => 'R',
    'patch_class' => 'NOOP',
    'patch_method' => 'track_interpolation'
}.cs__freeze
INTERPOLATION_NODE =
Contrast::Agent::Assess::Policy::PropagationNode.new(NODE_HASH)

Class Method Summary collapse

Methods included from Components::Interface

included

Class Method Details

.instrument_stringObject



57
58
59
60
61
62
63
64
65
# File 'lib/contrast/extension/assess/string.rb', line 57

def instrument_string
  @_instrument_string ||= begin
    require 'cs__assess_string/cs__assess_string'
    true
  end
rescue StandardError, LoadError => e
  logger.error('Error loading hash track patch', e)
  false
end

.instrument_string_interpolationObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/contrast/extension/assess/string.rb', line 67

def instrument_string_interpolation
  if @_instrument_string_interpolation.nil?
    @_instrument_string_interpolation = begin
      require 'cs__assess_string_interpolation26/cs__assess_string_interpolation26' if AGENT.patch_interpolation? && Funchook.available?
      true
    rescue StandardError, LoadError => e
      logger.error('Error loading interpolation patch', e)
      false
    end
  end
  @_instrument_string_interpolation
end

.track_interpolation(inputs, result) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/contrast/extension/assess/string.rb', line 33

def track_interpolation inputs, result
  return unless AGENT.interpolation_enabled?
  return if in_contrast_scope?
  return unless inputs.any? { |input| Contrast::Agent::Assess::Tracker.tracked?(input) }

  with_contrast_scope do
    properties = Contrast::Agent::Assess::Tracker.properties(result)
    return unless properties

    parent_events = []
    offset = 0
    inputs.each do |input|
      properties.copy_from(input, result, offset)
      offset += input.length
      parent_event = Contrast::Agent::Assess::Tracker.properties(input)&.event
      parent_events << parent_event if parent_event
    end
    properties.build_event(INTERPOLATION_NODE, result, inputs, result, inputs)
    properties.event.instance_variable_set(:@_parent_events, parent_events)
  end
rescue StandardError => e
  logger.error('Unable to track interpolation', e)
end