Sha256: fece4004d6cd67e299b648c1b4b5226700df0ba49793e6b6fd072c5e1a88ebe9

Contents?: true

Size: 1.17 KB

Versions: 74

Compression:

Stored size: 1.17 KB

Contents

require 'openssl'

module PactBroker
  module Webhooks
    class CheckHostWhitelist

      def self.call(host, whitelist = PactBroker.configuration.webhook_host_whitelist)
        whitelist.select{ | whitelist_host | match?(host, whitelist_host) }
      end

      def self.match?(host, whitelist_host)
        if parse_ip_address(host)
          ip_address_matches_range(host, whitelist_host)
        elsif whitelist_host.is_a?(Regexp)
          host_matches_regexp(host, whitelist_host)
        elsif whitelist_host.start_with?("*")
          OpenSSL::SSL.verify_hostname(host, whitelist_host)
        else
          host == whitelist_host
        end
      end

      def self.parse_ip_address(addr)
        IPAddr.new(addr)
      rescue IPAddr::Error
        nil
      end

      def self.ip_address_matches_range(host, maybe_whitelist_range)
        parse_ip_address(maybe_whitelist_range) === parse_ip_address(host)
      end

      def self.host_matches_regexp(host, whitelist_regexp)
        host =~ whitelist_regexp
      end

      def self.host_matches_domain_with_wildcard(host, whitelist_domain)
        OpenSSL::SSL.verify_hostname(host, whitelist_domain)
      end
    end
  end
end

Version data entries

74 entries across 74 versions & 1 rubygems

Version Path
pact_broker-2.79.1 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.79.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.78.1 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.78.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.77.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.76.2 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.76.1 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.76.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.75.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.74.1 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.74.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.73.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.72.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.71.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.70.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.69.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.68.1 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.68.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.67.0 lib/pact_broker/webhooks/check_host_whitelist.rb
pact_broker-2.66.0 lib/pact_broker/webhooks/check_host_whitelist.rb