rack-blacklist_cookies 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ca060fd79fc80cccf1c2ac8e294d002094e0998b
4
+ data.tar.gz: f0b4a211a78499c28dc104a7354062df68868374
5
+ SHA512:
6
+ metadata.gz: a077d2bdd2c2ccc7da39551d34761e7821cee26e2beacc3c4c617770043df6a8d5a54db15e0b86e05567a6b325a32c5b3288e4c4f0b52d28ea828f40ef599cca
7
+ data.tar.gz: ccde3df668b19623a3fbd88f186bfa8f6a2a0881c18abae384002ac2b8e40834820f3e5001634eac935a5fc7caebaf27983883aa2e0ede61e16f02a00f2295c2
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.reek ADDED
@@ -0,0 +1,11 @@
1
+ Attribute:
2
+ exclude:
3
+ - "Rack::BlacklistCookies::Configuration"
4
+
5
+ UtilityFunction:
6
+ public_methods_only: true
7
+
8
+ exclude_paths:
9
+ - bin/
10
+ - spec/
11
+ - vendor/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,96 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ DisplayStyleGuide: false
4
+ Exclude:
5
+ - 'vendor/**/*'
6
+ - 'spec/**/*'
7
+ Rails:
8
+ Enabled: true
9
+
10
+ Metrics/LineLength:
11
+ Max: 120
12
+
13
+ Style/AlignParameters:
14
+ # Alignment of parameters in multi-line method calls.
15
+ #
16
+ # The `with_first_parameter` style aligns the following lines along the same
17
+ # column as the first parameter.
18
+ #
19
+ # method_call(a,
20
+ # b)
21
+ #
22
+ # The `with_fixed_indentation` style aligns the following lines with one
23
+ # level of indentation relative to the start of the line with the method call.
24
+ #
25
+ # method_call(a,
26
+ # b)
27
+ EnforcedStyle: with_fixed_indentation
28
+ SupportedStyles:
29
+ - with_first_parameter
30
+ - with_fixed_indentation
31
+
32
+ # Multi-line method chaining should be done with trailing dots.
33
+ Style/DotPosition:
34
+ EnforcedStyle: trailing
35
+ SupportedStyles:
36
+ - leading
37
+ - trailing
38
+
39
+ Style/Documentation:
40
+ Description: 'Document classes and non-namespace modules.'
41
+ Enabled: false
42
+ Exclude:
43
+ - 'spec/**/*'
44
+ - 'test/**/*'
45
+
46
+ Style/FileName:
47
+ Enabled: false
48
+
49
+ Style/TrailingCommaInArguments:
50
+ # If `comma`, the cop requires a comma after the last argument, but only for
51
+ # parenthesized method calls where each argument is on its own line.
52
+ # If `consistent_comma`, the cop requires a comma after the last argument,
53
+ # for all parenthesized method calls with arguments.
54
+ EnforcedStyleForMultiline: comma
55
+
56
+ Style/TrailingCommaInLiteral:
57
+ # If `comma`, the cop requires a comma after the last item in an array or
58
+ # hash, but only when each item is on its own line.
59
+ # If `consistent_comma`, the cop requires a comma after the last item of all
60
+ # non-empty array and hash literals.
61
+ EnforcedStyleForMultiline: comma
62
+
63
+
64
+ Style/StringLiterals:
65
+ EnforcedStyle: double_quotes
66
+ SupportedStyles:
67
+ - single_quotes
68
+ - double_quotes
69
+ # If true, strings which span multiple lines using \ for continuation must
70
+ # use the same type of quotes on each line.
71
+ ConsistentQuotesInMultiline: false
72
+
73
+ Style/StringLiteralsInInterpolation:
74
+ EnforcedStyle: double_quotes
75
+ SupportedStyles:
76
+ - single_quotes
77
+ - double_quotes
78
+
79
+ Style/UnneededInterpolation:
80
+ Enabled: false
81
+
82
+ Style/HashSyntax:
83
+ EnforcedStyle: no_mixed_keys
84
+ SupportedStyles:
85
+ # checks for 1.9 syntax (e.g. {a: 1}) for all symbol keys
86
+ - ruby19
87
+ # checks for hash rocket syntax for all hashes
88
+ - hash_rockets
89
+ # forbids mixed key syntaxes (e.g. {a: 1, :b => 2})
90
+ - no_mixed_keys
91
+ # enforces both ruby19 and no_mixed_keys styles
92
+ - ruby19_no_mixed_keys
93
+ # Force hashes that have a symbol value to use hash rockets
94
+ UseHashRocketsWithSymbolValues: false
95
+ # Do not suggest { a?: 1 } over { :a? => 1 } in ruby19 style
96
+ PreferHashRocketsForNonAlnumEndingSymbols: true
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ # Specify your gem's dependencies in rack-blacklist_cookies.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Notonthehighstreet Enterprises Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Rack::BlacklistCookies
2
+
3
+ Rack middleware for removing cookies on the request and response at a route level.
4
+
5
+ Rack::BlacklistCookies is a rack middleware that will block certain cookies from an HTTP request, as well as strip
6
+ certain cookies from an HTTP response.
7
+
8
+ It does this by examining the `Cookies` headers on the request, and the `Set-Cookie` headers on the response, and
9
+ stripping out any cookie that has been explicitly blacklisted in the configuration. It also let's you do that on a
10
+ per route basis, allowing you to selectively strip certain cookies only for certain routes in your application.
11
+
12
+ This may be useful in situations where you want to continue setting cookies generally but want to apply a finer set of
13
+ rules to either the request or the response.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'rack-blacklist_cookies'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install rack-blacklist_cookies
30
+
31
+ ## Configuration
32
+
33
+ All this gem needs to run is a simple configuration file.
34
+
35
+ You can blacklist on either the request or the response by setting pairs of `"/url-string" => ["list", "of", "cookies"]`
36
+ values.
37
+
38
+ Take the following config as an example:
39
+
40
+ ```ruby
41
+ Rack::BlacklistCookies.configure do |config|
42
+ config.request_blacklist = {
43
+ "/some-url" => ["cookie_to_blacklist", "another_blacklisted_cookie"]
44
+ }
45
+ config.response_blacklist = {
46
+ "/" => ["do_not_set_this_cookie_on_homepage_response"]
47
+ }
48
+ end
49
+ ```
50
+
51
+ This will ensure requests getting into your application on the URL `/some-url` will not have the cookies
52
+ `cookie_to_blacklist` and `another_blacklisted_cookie`. Similarly, even if your web application returns a cookie with
53
+ the name `do_not_set_this_cookie_on_homepage_response` for requests to `/`, that cookie will not make it into the client
54
+ as the middleware will strip it out.
55
+
56
+
57
+ As this is a Rack middleware, it will respect and correctly ignore any `?querystring` and `#bookmark` params in the URL.
58
+
59
+ ## Using with Rails
60
+
61
+ If you are using this middleware with Rails, a typical place to set up the gem is in the `config/initializers` folder.
62
+
63
+ Don't forget to add the middleware to `config/application.rb` as well.
64
+
65
+ ```ruby
66
+ config.middleware.insert 0, Rack::BlacklistCookies
67
+ ```
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/notonthehighstreet/rack-blacklist_cookies.
76
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
77
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
78
+
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ require "rack/blacklist_cookies"
3
+ require "rack/blacklist_cookies/configuration"
4
+ require "rack/blacklist_cookies/scrubber"
5
+ require "rack/blacklist_cookies/version"
6
+
7
+ module Rack
8
+ # Rack::BlacklistCookies holds onto configuration values at the class level
9
+ class BlacklistCookies
10
+ def self.configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def self.configure
15
+ yield(configuration)
16
+ configuration.validate
17
+ rescue ConfigurationError => error
18
+ configuration.reset
19
+ raise error
20
+ end
21
+
22
+ def self.request_blacklist(env)
23
+ configuration.request_blacklist[env["PATH_INFO"]]
24
+ end
25
+
26
+ def self.response_blacklist(env)
27
+ configuration.response_blacklist[env["PATH_INFO"]]
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ module Rack
3
+ # Rack::BlacklistCookies is a middleware that removes selected cookies from the request and / or response.
4
+ class BlacklistCookies
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env["HTTP_COOKIE"] = "#{RequestScrubber.new(env, env["HTTP_COOKIE"])}" if scrub_request?(env)
11
+
12
+ status, headers, body = @app.call(env)
13
+
14
+ headers["Set-Cookie"] = "#{ResponseScrubber.new(env, headers["Set-Cookie"])}" if scrub_response?(env, headers)
15
+
16
+ [status, headers, body]
17
+ end
18
+
19
+ private
20
+
21
+ def scrub_request?(env)
22
+ !env["HTTP_COOKIE"].nil? && !env["HTTP_COOKIE"].empty? && BlacklistCookies.request_blacklist(env)
23
+ end
24
+
25
+ def scrub_response?(env, headers)
26
+ !headers["Set-Cookie"].nil? && !headers["Set-Cookie"].empty? && BlacklistCookies.response_blacklist(env)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+ module Rack
3
+ class BlacklistCookies
4
+ # Configuration defaults to an empty hash if it has not been set.
5
+ class Configuration
6
+ attr_accessor :request_blacklist, :response_blacklist
7
+
8
+ def initialize
9
+ @request_blacklist = {}
10
+ @response_blacklist = {}
11
+ end
12
+
13
+ def reset
14
+ @request_blacklist = {}
15
+ @response_blacklist = {}
16
+ end
17
+
18
+ # rubocop:disable MethodLength
19
+ def validate
20
+ [@request_blacklist, @response_blacklist].each do |blacklist|
21
+ raise ConfigurationError, "Blacklist is not a hash" unless blacklist.is_a?(Hash)
22
+ blacklist.each do |route, cookie_list|
23
+ raise ConfigurationError, "Blacklist key is not a string" unless route.is_a?(String)
24
+ raise ConfigurationError, "Blacklist value is not an array" unless cookie_list.is_a?(Array)
25
+ raise ConfigurationError, "Blacklist key is not a URL path" unless route.start_with?("/")
26
+ cookie_list.each do |cookie_name|
27
+ raise ConfigurationError, "Blacklist cookie is not a valid name string" unless cookie_name.is_a?(String)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ # ConfigurationError feeds configuration issues back to the user.
35
+ class ConfigurationError < StandardError
36
+ def initialize(message = "Failed to configure correctly")
37
+ @message = message
38
+ end
39
+
40
+ def to_s
41
+ "#{@message}. #{docs}"
42
+ end
43
+
44
+ def docs
45
+ "Docs are at https://github.com/notonthehighstreet/rack-blacklist_cookies "
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+ module Rack
3
+ class BlacklistCookies
4
+ # The Scrubber class is responsible for removing any unwanted cookies from a given cookies header.
5
+ # The base class provides the main #scrub method, while the subclasses are responsible
6
+ # for being able to deal with parsing the Request and Response headers and associated config.
7
+ class BaseScrubber
8
+ attr_reader :env
9
+
10
+ def initialize(env, cookies_header)
11
+ @env = env
12
+ @cookies_header = cookies_header
13
+ end
14
+
15
+ def to_s
16
+ return @cookies_header unless blacklist
17
+ scrub
18
+ end
19
+
20
+ private
21
+
22
+ def scrub
23
+ new_cookies_header = @cookies_header.split(splitter)
24
+ blacklist.each do |cookie_name|
25
+ new_cookies_header.reject! { |cookie| "#{cookie_name}=" == cookie[0..cookie_name.length] }
26
+ end
27
+
28
+ new_cookies_header.join(joiner)
29
+ end
30
+
31
+ def blacklist; end
32
+
33
+ def splitter; end
34
+
35
+ def joiner; end
36
+ end
37
+
38
+ # RequestScrubber is responsible for parsing and configuring the request according to RFC-6252
39
+ # https://tools.ietf.org/html/rfc6265#section-5.4
40
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie
41
+ class RequestScrubber < BaseScrubber
42
+ def blacklist
43
+ BlacklistCookies.request_blacklist(env)
44
+ end
45
+
46
+ def splitter
47
+ /[;,] */n
48
+ end
49
+
50
+ def joiner
51
+ "; "
52
+ end
53
+ end
54
+
55
+ # ResponseScrubber is responsible for parsing and configuring the response according to RFC-6252
56
+ # https://tools.ietf.org/html/rfc6265#section-4.1
57
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
58
+ class ResponseScrubber < BaseScrubber
59
+ def blacklist
60
+ BlacklistCookies.response_blacklist(env)
61
+ end
62
+
63
+ def splitter
64
+ "\n"
65
+ end
66
+
67
+ def joiner
68
+ "\n"
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module Rack
3
+ class BlacklistCookies
4
+ VERSION = "1.0.0".freeze
5
+ end
6
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "rack-blacklist_cookies"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "rack-blacklist_cookies"
9
+ spec.version = Rack::BlacklistCookies::VERSION
10
+ spec.authors = ["notonthehighstreet.com"]
11
+ spec.email = ["tech.contact@notonthehighstreet.com"]
12
+
13
+ spec.summary = "Blacklist cookies on the request and response HTTP headers"
14
+ spec.description = "Removes specified cookies from HTTP request and / or response on user defined pages."
15
+ spec.homepage = "https://github.com/notonthehighstreet/rack-blacklist_cookies"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry-byebug"
29
+ spec.add_development_dependency "rubocop"
30
+ spec.add_development_dependency "reek"
31
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-blacklist_cookies
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - notonthehighstreet.com
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.13.7
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.13.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: reek
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Removes specified cookies from HTTP request and / or response on user
98
+ defined pages.
99
+ email:
100
+ - tech.contact@notonthehighstreet.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".reek"
107
+ - ".rspec"
108
+ - ".rubocop.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - lib/rack-blacklist_cookies.rb
114
+ - lib/rack/blacklist_cookies.rb
115
+ - lib/rack/blacklist_cookies/configuration.rb
116
+ - lib/rack/blacklist_cookies/scrubber.rb
117
+ - lib/rack/blacklist_cookies/version.rb
118
+ - rack-blacklist_cookies.gemspec
119
+ homepage: https://github.com/notonthehighstreet/rack-blacklist_cookies
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.6.10
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Blacklist cookies on the request and response HTTP headers
143
+ test_files: []