Class: Contrast::Agent::Assess::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/agent/assess/tag.rb

Overview

A Tag represents a range in a given piece of data. It is used by the Agent to determine if a vulnerable dataflow has occurred.

Constant Summary collapse

BELOW =
'BELOW'
LOW_SPAN =
'LOW_SPAN'
WITHIN =
'WITHIN'
WITHOUT =
'WITHOUT'
HIGH_SPAN =
'HIGH_SPAN'
ABOVE =
'ABOVE'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, length, start_idx = 0) ⇒ Tag

Initialize a new tag

Parameters:

  • label (String)

    the label of the tag

  • length (Integer)

    the length of the string described with this tag

  • start_idx (Integer) (defaults to: 0)

    (0) the starting position in the string for this tag



22
23
24
25
# File 'lib/contrast/agent/assess/tag.rb', line 22

def initialize label, length, start_idx = 0
  @label = label
  update_range(start_idx, start_idx + length)
end

Instance Attribute Details

#end_idxObject (readonly)

the label of this tag



10
11
12
# File 'lib/contrast/agent/assess/tag.rb', line 10

def end_idx
  @end_idx
end

#labelObject (readonly)

the label of this tag



10
11
12
# File 'lib/contrast/agent/assess/tag.rb', line 10

def label
  @label
end

#lengthObject (readonly)

the label of this tag



10
11
12
# File 'lib/contrast/agent/assess/tag.rb', line 10

def length
  @length
end

#start_idxObject (readonly)

the label of this tag



10
11
12
# File 'lib/contrast/agent/assess/tag.rb', line 10

def start_idx
  @start_idx
end

Instance Method Details

#above?(idx) ⇒ Boolean

Return true if the tag is above the given position in the string

Parameters:

  • idx (Integer)

    the index to check

Returns:

  • (Boolean)


38
39
40
# File 'lib/contrast/agent/assess/tag.rb', line 38

def above? idx
  idx < start_idx
end

#compare_range(start, stop) ⇒ Object

The tag is __ the range rrrrrrr == self.range, the range of the tag



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/contrast/agent/assess/tag.rb', line 121

def compare_range start, stop
  # the range starts below the given values
  if @start_idx < start
    # r starts and stops below
    # rrrrrrrrrrrrr
    #               start       stop
    return BELOW if @end_idx <= start
    # r starts below and finishes within
    # rrrrrrrrrrrrr
    #    start       stop
    return LOW_SPAN if @end_idx > start && @end_idx <= stop
    # r starts below and finishes above stop
    #  rrrrrrrrrrrrrrrrrrrrrrrr
    #     start       stop
    return WITHOUT if @end_idx > stop
  end

  # the range starts at or above the given values
  # r is between start and stop
  #        rrrrrrrrrrrrrrr
  # start                   stop
  return WITHIN if @start_idx < stop && @end_idx <= stop
  # r starts within and finishes above stop
  #           rrrrrrrrrrrrr
  #   start       stop
  return HIGH_SPAN if @start_idx < stop && @end_idx > stop

  # the range is above the given values
  # starts and stops above
  #                   rrrrrrrrrrrrr
  #  start       stop
  ABOVE
end

#copy_modified(shift) ⇒ Object

Modification to tracked String can change the position and length of the tracked tag shift : negative value moves left



98
99
100
101
102
103
104
105
# File 'lib/contrast/agent/assess/tag.rb', line 98

def copy_modified shift
  start = start_idx + shift
  # Tags cannot start below 0
  new_start_idx = start >= 0 ? start : 0
  # If a tag were to go negative, cut off the negative portion from length
  new_length = start >= 0 ? length : (length + start)
  Contrast::Agent::Assess::Tag.new(label, new_length, new_start_idx)
end

#covers?(idx) ⇒ Boolean

Return true if the tag covers the given position in the string

Parameters:

  • idx (Integer)

    the index to check

Returns:

  • (Boolean)


31
32
33
# File 'lib/contrast/agent/assess/tag.rb', line 31

def covers? idx
  idx >= start_idx && idx < end_idx
end

#extends_beyond_string_size?(string_length) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/contrast/agent/assess/tag.rb', line 50

def extends_beyond_string_size? string_length
  @end_idx > string_length
end

#merge(other) ⇒ Object

Given a tag, merge its ranges with this one such that the lowest start and highest end become the values of this tag

Returns true if the other tag was merged into this tag



88
89
90
91
92
93
94
# File 'lib/contrast/agent/assess/tag.rb', line 88

def merge other
  return unless overlaps?(other.start_idx, other.end_idx)

  start = other.start_idx < @start_idx ? other.start_idx : @start_idx
  finish = other.end_idx > @end_idx ? other.end_idx : @end_idx
  update_range(start, finish)
end

#overlaps?(start_idx, end_idx) ⇒ Boolean

Return if a given tag overlaps this one

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/contrast/agent/assess/tag.rb', line 55

def overlaps? start_idx, end_idx
  return true if @start_idx <  start_idx && @end_idx >= start_idx  # we start below range & end in it
  return true if @start_idx >= start_idx && @end_idx <= end_idx    # we start and end in range

  @start_idx <= end_idx && @end_idx > end_idx                      # we start in range & end above it
end

#rangeRange

Return the range that this tag covers, from start (inclusive) to end (exclusive).

Returns:

  • (Range)


46
47
48
# File 'lib/contrast/agent/assess/tag.rb', line 46

def range
  start_idx...end_idx
end

#repurpose(start_idx, end_idx) ⇒ Object



78
79
80
# File 'lib/contrast/agent/assess/tag.rb', line 78

def repurpose start_idx, end_idx
  update_range(start_idx, end_idx)
end

#shift(idx) ⇒ Object



62
63
64
# File 'lib/contrast/agent/assess/tag.rb', line 62

def shift idx
  update_range(@start_idx + idx, @end_idx + idx)
end

#shift_end(idx) ⇒ Object



66
67
68
# File 'lib/contrast/agent/assess/tag.rb', line 66

def shift_end idx
  update_range(@start_idx, @end_idx + idx)
end

#str_valObject Also known as: to_s



107
108
109
# File 'lib/contrast/agent/assess/tag.rb', line 107

def str_val
  @_str_val ||= "[#{ start_idx },#{ end_idx }]"
end

#update_end(end_idx) ⇒ Object



74
75
76
# File 'lib/contrast/agent/assess/tag.rb', line 74

def update_end end_idx
  update_range(@start_idx, end_idx)
end

#update_start(start_idx) ⇒ Object



70
71
72
# File 'lib/contrast/agent/assess/tag.rb', line 70

def update_start start_idx
  update_range(start_idx, @end_idx)
end