Sha256: f5127a5056a3d766471241bd096f828179341521bd072abdca7294479d6f7a04

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

class SiteInspector
  class Endpoint
    class Cookies < Check
      def any?(&block)
        if cookie_header.nil? || cookie_header.empty?
          false
        elsif block_given?
          all.any?(&block)
        else
          true
        end
      end
      alias cookies? any?

      def all
        @cookies ||= cookie_header.map { |c| CGI::Cookie.parse(c) } if cookies?
      end

      def [](key)
        all.find { |cookie| cookie.keys.first == key } if cookies?
      end

      def secure?
        pairs = cookie_header.join('; ').split('; ') # CGI::Cookies#Parse doesn't seem to like secure headers
        pairs.any? { |c| c.casecmp('secure').zero? } && pairs.any? { |c| c.casecmp('httponly').zero? }
      end

      def to_h
        {
          cookie?: any?,
          secure?: secure?
        }
      end

      private

      def cookie_header
        # Cookie header may be an array or string, always return an array
        [endpoint.headers.all['set-cookie']].flatten.compact
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
site-inspector-3.2.0 lib/site-inspector/checks/cookies.rb