Sha256: 2ec5aad86110558af53ad18b4d02b915e99bfb7d53d26d1e21df84e6a2750134

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true
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
    EXECUTION_CONTEXTS = "executionContexts".freeze
    ALL_TYPES = [CACHE, COOKIES, STORAGE, EXECUTION_CONTEXTS]

    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, user_agent = 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

1 entries across 1 versions & 1 rubygems

Version Path
secure_headers-7.1.0 lib/secure_headers/headers/clear_site_data.rb