Sha256: a69914ca7f0fbfbeebbf7a9cf3bbe6a9020c51514a2d783d27cd29be3bd88901

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

module CMSScanner
  # Scope system logic
  class Target < WebSite
    # @return [ Array<PublicSuffix::Domain, String> ]
    def scope
      @scope ||= Scope.new
    end

    # @param [ String ] url An absolute URL
    #
    # @return [ Boolean ] true if the url given is in scope
    def in_scope?(url)
      scope.include?(Addressable::URI.parse(url.strip).host)
    rescue
      false
    end

    # @param [ Typhoeus::Response ] res
    # @param [ String ] xpath
    # @param [ Array<String> ] attributes
    #
    # @yield [ String, Nokogiri::XML::Element ] The in scope url and its associated tag
    #
    # @return [ Array<String> ] The in scope absolute URLs detected in the response's body
    def in_scope_urls(res, xpath = '//link|//script|//style|//img|//a', attributes = %w[href src])
      found = []

      urls_from_page(res, xpath, attributes) do |url, tag|
        next unless in_scope?(url)

        yield url, tag if block_given?

        found << url
      end

      found
    end

    # Scope Implementation
    class Scope
      # @return [ Array<PublicSuffix::Domain ] The valid domains in scope
      def domains
        @domains ||= []
      end

      # @return [ Array<String> ] The invalid domains in scope (such as IP addresses etc)
      def invalid_domains
        @invalid_domains ||= []
      end

      def <<(element)
        if PublicSuffix.valid?(element)
          domains << PublicSuffix.parse(element)
        else
          invalid_domains << element
        end
      end

      # @return [ Boolean ] Wether or not the host is in the scope
      def include?(host)
        if PublicSuffix.valid?(host)
          domain = PublicSuffix.parse(host)

          domains.each { |d| return true if domain.match(d) }
        else
          invalid_domains.each { |d| return true if host == d }
        end

        false
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cms_scanner-0.0.37.12 lib/cms_scanner/target/scope.rb
cms_scanner-0.0.37.11 lib/cms_scanner/target/scope.rb
cms_scanner-0.0.37.10 lib/cms_scanner/target/scope.rb