lib/contrast/agent/assess/rule/response/body_rule.rb in contrast-agent-7.3.0 vs lib/contrast/agent/assess/rule/response/body_rule.rb in contrast-agent-7.3.1
- old
+ new
@@ -1,10 +1,11 @@
# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true
require 'rack'
require 'contrast/utils/hash_digest'
+require 'contrast/utils/duck_utils'
require 'contrast/utils/string_utils'
require 'contrast/agent/assess/rule/response/base_rule'
module Contrast
module Agent
@@ -42,24 +43,38 @@
#
# @param section [String,nil] html section to find element
# @param element_start_str [String] element to find in html section
# @return [Array<Hash>] the found elements of this section, as well as their start and end indexes.
def html_elements section, element_start_str = '', capture_overflow: false
+ return [] unless section
+ return [] unless (potentials = potential_elements(section, element_start_str).flatten).any?
+
elements = []
section_start = 0
- return [] unless section
- potential_elements(section, element_start_str).flatten.each do |potential_element|
+ potentials.each do |potential_element|
next unless potential_element
next unless element_openings.any? { |opening| potential_element.start_with?(opening) }
- section_start = section.index(element_start_str, section_start)
- next unless section_start
+ start = section&.index(element_start_str, section_start)
+ next if Contrast::Utils::DuckUtils.empty_duck?(start)
- element_stop = potential_element.index('>').to_i
- next unless element_stop
+ stop = potential_element.index('>').to_i
+ next if Contrast::Utils::DuckUtils.empty_duck?(stop)
- section_close = section_start + 6 + element_stop
+ section_close = start + 6 + stop
+ # Now we have valid tag section with start and stop.
+ # Save new boundaries. This is to make sure that If
+ # on previous iteration there were non valid section,
+ # the start_section will be assigned to nil, thus making
+ # the detection of new section not possible, and throwing
+ # an error. To that end old values are kept safe.
+ #
+ # Assign new start index.
+ section_start = start
+ # Assign new end index.
+ element_stop = stop
+
elements << capture(section, section_start, section_close, element_stop, overflow: capture_overflow)
section_start = section_close
end
elements
end