Sha256: 53bf241bfed360b977f2b6ac8ea9e38f44b641437b4cebdfbe226e1fa43b049d

Contents?: true

Size: 1.32 KB

Versions: 18

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true
module SecureHeaders
  class ReferrerPolicyConfigError < StandardError; end
  class ReferrerPolicy
    HEADER_NAME = "Referrer-Policy".freeze
    DEFAULT_VALUE = "origin-when-cross-origin"
    VALID_POLICIES = %w(
      no-referrer
      no-referrer-when-downgrade
      same-origin
      strict-origin
      strict-origin-when-cross-origin
      origin
      origin-when-cross-origin
      unsafe-url
    )

    class << self
      # Public: generate an Referrer Policy header.
      #
      # Returns a default header if no configuration is provided, or a
      # header name and value based on the config.
      def make_header(config = nil, user_agent = nil)
        return if config == OPT_OUT
        config ||= DEFAULT_VALUE
        [HEADER_NAME, Array(config).join(", ")]
      end

      def validate_config!(config)
        case config
        when nil, OPT_OUT
          # valid
        when String, Array
          config = Array(config)
          unless config.all? { |t| t.is_a?(String) && VALID_POLICIES.include?(t.downcase) }
            raise ReferrerPolicyConfigError.new("Value can only be one or more of #{VALID_POLICIES.join(", ")}")
          end
        else
          raise TypeError.new("Must be a string or array of strings. Found #{config.class}: #{config}")
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
secure_headers-7.0.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.7.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.6.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.5.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.4.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.3.4 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.3.3 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.3.2 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.3.1 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.3.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.2.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.1.2 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.1.1 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.1.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.0.0 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.0.0.alpha03 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.0.0.alpha02 lib/secure_headers/headers/referrer_policy.rb
secure_headers-6.0.0.alpha01 lib/secure_headers/headers/referrer_policy.rb