Sha256: 9d1b79c3a39cbc38c460ee89a8fe3a26b80988f9ad203ab1fa5fd6af1bc794c5

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true
module Rack
  class BlacklistCookies
    # The Scrubber class is responsible for removing any unwanted cookies from a given cookies header.
    # The base class provides the main #scrub method, while the subclasses are responsible
    # for being able to deal with parsing the Request and Response headers and associated config.
    class BaseScrubber
      attr_reader :env

      def initialize(env, cookies_header)
        @env = env
        @cookies_header = cookies_header
      end

      def to_s
        return @cookies_header unless blacklist
        scrub
      end

      private

      def scrub
        new_cookies_header = @cookies_header.split(splitter)
        blacklist.each do |cookie_name|
          new_cookies_header.reject! { |cookie| "#{cookie_name}=" == cookie[0..cookie_name.length] }
        end

        new_cookies_header.join(joiner)
      end

      def blacklist; end

      def splitter; end

      def joiner; end
    end

    # RequestScrubber is responsible for parsing and configuring the request according to RFC-6252
    # https://tools.ietf.org/html/rfc6265#section-5.4
    # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie
    class RequestScrubber < BaseScrubber
      def blacklist
        BlacklistCookies.request_blacklist(env)
      end

      def splitter
        /[;,] */n
      end

      def joiner
        "; "
      end
    end

    # ResponseScrubber is responsible for parsing and configuring the response according to RFC-6252
    # https://tools.ietf.org/html/rfc6265#section-4.1
    # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
    class ResponseScrubber < BaseScrubber
      def blacklist
        BlacklistCookies.response_blacklist(env)
      end

      def splitter
        "\n"
      end

      def joiner
        "\n"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-blacklist_cookies-1.0.0 lib/rack/blacklist_cookies/scrubber.rb