Sha256: 475992c3baa1675c335424ef55fe04440cbae62bf301dbc5fb8ec4e475a65465

Contents?: true

Size: 1.6 KB

Versions: 10

Compression:

Stored size: 1.6 KB

Contents

module SecureHeaders
  class ClearSiteDataConfigError < StandardError; end
  class ClearSiteData
    HEADER_NAME = "Clear-Site-Data".freeze

    # Valid `types`
    CACHE = "cache".freeze
    COOKIES = "cookies".freeze
    STORAGE = "storage".freeze
    EXECTION_CONTEXTS = "executionContexts".freeze
    ALL_TYPES = [CACHE, COOKIES, STORAGE, EXECTION_CONTEXTS]

    CONFIG_KEY = :clear_site_data

    class << self
      # Public: make an Clear-Site-Data header name, value pair
      #
      # Returns nil if not configured, returns header name and value if configured.
      def make_header(config=nil)
        case config
        when nil, OPT_OUT, []
          # noop
        when Array
          [HEADER_NAME, make_header_value(config)]
        when true
          [HEADER_NAME, make_header_value(ALL_TYPES)]
        end
      end

      def validate_config!(config)
        case config
        when nil, OPT_OUT, true
          # valid
        when Array
          unless config.all? { |t| t.is_a?(String) }
            raise ClearSiteDataConfigError.new("types must be Strings")
          end
        else
          raise ClearSiteDataConfigError.new("config must be an Array of Strings or `true`")
        end
      end

      # Public: Transform a Clear-Site-Data config (an Array of Strings) into a
      # String that can be used as the value for the Clear-Site-Data header.
      #
      # types - An Array of String of types of data to clear.
      #
      # Returns a String of quoted values that are comma separated.
      def make_header_value(types)
        types.map { |t| "\"#{t}\""}.join(", ")
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
secure_headers-3.9.0 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.8.0 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.7.4 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.7.3 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.7.2 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.7.1 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.7.0 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.6.7 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.6.6 lib/secure_headers/headers/clear_site_data.rb
secure_headers-3.6.5 lib/secure_headers/headers/clear_site_data.rb