cookies_and_cream 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +28 -0
- data/Gemfile +23 -0
- data/Guardfile +13 -0
- data/LICENSE +21 -0
- data/README.md +85 -0
- data/Rakefile +4 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/cookies_and_cream.gemspec +39 -0
- data/lib/cookies_and_cream/cookie.rb +135 -0
- data/lib/cookies_and_cream/middleware.rb +55 -0
- data/lib/cookies_and_cream/railtie.rb +13 -0
- data/lib/cookies_and_cream/version.rb +5 -0
- data/lib/cookies_and_cream.rb +114 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b1b68c606da8711313d347a5754b6a59591b2def168085ce40c9a79fee093987
|
4
|
+
data.tar.gz: ea0648da9e6c645f4ef26d9c753d129ad8b83d82810bcfe3175792c483b1f601
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a23eb72521c4e09347a518c5fbfa1de3c7db4b33f1904a58fcafe9a4ae7020c518c923db45fff154b1bafe12478c2f6d1fd7054e4753996a79a8ecfbfdc25b39
|
7
|
+
data.tar.gz: f3ae4bd73ec7c0ba11275d3f41107c7ae145de0db978443fc3750c78ef3c30d6d968d8f96c99ef343ba50b902775931fc94e816706806ce1d6be5ecb5ca28881
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.1
|
data/.travis.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- ruby-head
|
5
|
+
- 2.6.1
|
6
|
+
- 2.5.0
|
7
|
+
- 2.4.3
|
8
|
+
- jruby-head
|
9
|
+
|
10
|
+
env:
|
11
|
+
- SUITE=rspec spec
|
12
|
+
- SUITE=rubocop
|
13
|
+
|
14
|
+
script: bundle exec $SUITE
|
15
|
+
|
16
|
+
matrix:
|
17
|
+
allow_failures:
|
18
|
+
- rvm: jruby-head
|
19
|
+
- rvm: ruby-head
|
20
|
+
|
21
|
+
before_install:
|
22
|
+
- gem update --system
|
23
|
+
- gem --version
|
24
|
+
- gem update bundler
|
25
|
+
bundler_args: --without guard -j 3
|
26
|
+
|
27
|
+
sudo: false
|
28
|
+
cache: bundler
|
data/Gemfile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
source "https://rubygems.org"
|
3
|
+
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem "coveralls"
|
8
|
+
gem "json"
|
9
|
+
gem "pry-nav"
|
10
|
+
gem "rack"
|
11
|
+
gem "rspec"
|
12
|
+
gem "rubocop"
|
13
|
+
gem "rubocop-github"
|
14
|
+
gem "term-ansicolor"
|
15
|
+
gem "tins"
|
16
|
+
end
|
17
|
+
|
18
|
+
group :guard do
|
19
|
+
gem "growl"
|
20
|
+
gem "guard-rspec"
|
21
|
+
gem "rb-fsevent"
|
22
|
+
gem "terminal-notifier-guard"
|
23
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
guard :rspec, cmd: "bundle exec rspec", all_on_start: true, all_after_pass: true do
|
3
|
+
require "guard/rspec/dsl"
|
4
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
5
|
+
|
6
|
+
# RSpec files
|
7
|
+
rspec = dsl.rspec
|
8
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
9
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
10
|
+
watch(rspec.spec_files)
|
11
|
+
|
12
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
13
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Neil Matatall
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/oreoshake/cookies_and_cream.svg?branch=master)](https://travis-ci.org/oreoshake/cookies_and_cream)
|
2
|
+
|
3
|
+
# CookiesAndCream
|
4
|
+
|
5
|
+
CookiesAndCream is an extract of the cookie functionality from [secure_headers](https://github.com/twitter/secure_headers). Rails has good header support but the cookie support is still lacking. Maybe one day this functionality will be added to rails core.
|
6
|
+
|
7
|
+
Note: the railtie currently isn't working (see #1) so there's a bit of manual setup for now.
|
8
|
+
|
9
|
+
Gemfile:
|
10
|
+
```ruby
|
11
|
+
gem "cookies_and_cream"
|
12
|
+
```
|
13
|
+
|
14
|
+
application.rb:
|
15
|
+
```ruby
|
16
|
+
require "cookies_and_cream"
|
17
|
+
|
18
|
+
module Foo
|
19
|
+
class Application < Rails::Application
|
20
|
+
...
|
21
|
+
config.middleware.use CookiesAndCream::Middleware
|
22
|
+
```
|
23
|
+
|
24
|
+
## Configuration
|
25
|
+
|
26
|
+
These can be defined in the form of a boolean, or as a Hash for more refined configuration.
|
27
|
+
|
28
|
+
#### Defaults
|
29
|
+
|
30
|
+
By default, all cookies will get both `Secure`, `HttpOnly`, and `SameSite=Lax`.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
CookiesAndCream.config = {
|
34
|
+
secure: true, # defaults to true but will be a no op on non-HTTPS requests
|
35
|
+
httponly: true, # defaults to true
|
36
|
+
samesite: { # defaults to set `SameSite=Lax`
|
37
|
+
lax: true
|
38
|
+
}
|
39
|
+
}
|
40
|
+
```
|
41
|
+
|
42
|
+
#### Boolean-based configuration
|
43
|
+
|
44
|
+
Boolean-based configuration is intended to globally enable or disable a specific cookie attribute. *Note: As of 4.0, you must use OPT_OUT rather than false to opt out of the defaults.*
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
CookiesAndCream.config = {
|
48
|
+
secure: true, # mark all cookies as Secure
|
49
|
+
httponly: OPT_OUT, # do not mark any cookies as HttpOnly
|
50
|
+
}
|
51
|
+
```
|
52
|
+
|
53
|
+
#### Hash-based configuration
|
54
|
+
|
55
|
+
Hash-based configuration allows for fine-grained control.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
CookiesAndCream.config = {
|
59
|
+
secure: { except: ['_guest'] }, # mark all but the `_guest` cookie as Secure
|
60
|
+
httponly: { only: ['_rails_session'] }, # only mark the `_rails_session` cookie as HttpOnly
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
#### SameSite cookie configuration
|
65
|
+
|
66
|
+
SameSite cookies permit either `Strict` or `Lax` enforcement mode options.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
CookiesAndCream.config = {
|
70
|
+
samesite: {
|
71
|
+
strict: true # mark all cookies as SameSite=Strict
|
72
|
+
}
|
73
|
+
}
|
74
|
+
```
|
75
|
+
|
76
|
+
`Strict` and `Lax` enforcement modes can also be specified using a Hash.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
CookiesAndCream.config = {
|
80
|
+
samesite: {
|
81
|
+
strict: { only: ['_rails_session'] },
|
82
|
+
lax: { only: ['_guest'] }
|
83
|
+
}
|
84
|
+
}
|
85
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "cookies_and_cream"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "cookies_and_cream/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "cookies_and_cream"
|
9
|
+
spec.version = CookiesAndCream::VERSION
|
10
|
+
spec.authors = ["Neil Matatall"]
|
11
|
+
spec.email = ["oreoshake@users.noreply.github.com"]
|
12
|
+
|
13
|
+
spec.summary = %q{Automatically marks all cookies as secure, httponly, and samesite=lax}
|
14
|
+
spec.description = %q{Secure your cookies with an API for opting out}
|
15
|
+
spec.homepage = "https://github.com/oreoshake/cookies_and_cream"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
spec.metadata["source_code_uri"] = "https://github.com/oreoshake/cookies_and_cream"
|
22
|
+
spec.metadata["changelog_uri"] = "https://github.com/oreoshake/cookies_and_cream/CHANGELOG"
|
23
|
+
else
|
24
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
25
|
+
"public gem pushes."
|
26
|
+
end
|
27
|
+
|
28
|
+
# Specify which files should be added to the gem when it is released.
|
29
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
30
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
31
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
32
|
+
end
|
33
|
+
spec.bindir = "exe"
|
34
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ["lib"]
|
36
|
+
|
37
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
38
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
39
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "cgi"
|
3
|
+
|
4
|
+
module CookiesAndCream
|
5
|
+
class CookiesConfigError < StandardError; end
|
6
|
+
class Cookie
|
7
|
+
attr_reader :raw_cookie, :config
|
8
|
+
|
9
|
+
COOKIE_DEFAULTS = {
|
10
|
+
httponly: true,
|
11
|
+
secure: true,
|
12
|
+
samesite: { lax: true },
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
def initialize(cookie, config)
|
16
|
+
@raw_cookie = cookie
|
17
|
+
unless config == OPT_OUT
|
18
|
+
config ||= {}
|
19
|
+
config = COOKIE_DEFAULTS.merge(config)
|
20
|
+
end
|
21
|
+
@config = config
|
22
|
+
@attributes = {
|
23
|
+
httponly: nil,
|
24
|
+
samesite: nil,
|
25
|
+
secure: nil,
|
26
|
+
}
|
27
|
+
|
28
|
+
parse(cookie)
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
@raw_cookie.dup.tap do |c|
|
33
|
+
c << "; secure" if secure?
|
34
|
+
c << "; HttpOnly" if httponly?
|
35
|
+
c << "; #{samesite_cookie}" if samesite?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def secure?
|
40
|
+
flag_cookie?(:secure) && !already_flagged?(:secure)
|
41
|
+
end
|
42
|
+
|
43
|
+
def httponly?
|
44
|
+
flag_cookie?(:httponly) && !already_flagged?(:httponly)
|
45
|
+
end
|
46
|
+
|
47
|
+
def samesite?
|
48
|
+
flag_samesite? && !already_flagged?(:samesite)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def parsed_cookie
|
54
|
+
@parsed_cookie ||= CGI::Cookie.parse(raw_cookie)
|
55
|
+
end
|
56
|
+
|
57
|
+
def already_flagged?(attribute)
|
58
|
+
@attributes[attribute]
|
59
|
+
end
|
60
|
+
|
61
|
+
def flag_cookie?(attribute)
|
62
|
+
return false if config == OPT_OUT
|
63
|
+
case config[attribute]
|
64
|
+
when TrueClass
|
65
|
+
true
|
66
|
+
when Hash
|
67
|
+
conditionally_flag?(config[attribute])
|
68
|
+
else
|
69
|
+
false
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def conditionally_flag?(configuration)
|
74
|
+
if(Array(configuration[:only]).any? && (Array(configuration[:only]) & parsed_cookie.keys).any?)
|
75
|
+
true
|
76
|
+
elsif(Array(configuration[:except]).any? && (Array(configuration[:except]) & parsed_cookie.keys).none?)
|
77
|
+
true
|
78
|
+
else
|
79
|
+
false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def samesite_cookie
|
84
|
+
if flag_samesite_lax?
|
85
|
+
"SameSite=Lax"
|
86
|
+
elsif flag_samesite_strict?
|
87
|
+
"SameSite=Strict"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def flag_samesite?
|
92
|
+
return false if config == OPT_OUT || config[:samesite] == OPT_OUT
|
93
|
+
flag_samesite_lax? || flag_samesite_strict?
|
94
|
+
end
|
95
|
+
|
96
|
+
def flag_samesite_lax?
|
97
|
+
flag_samesite_enforcement?(:lax)
|
98
|
+
end
|
99
|
+
|
100
|
+
def flag_samesite_strict?
|
101
|
+
flag_samesite_enforcement?(:strict)
|
102
|
+
end
|
103
|
+
|
104
|
+
def flag_samesite_enforcement?(mode)
|
105
|
+
return unless config[:samesite]
|
106
|
+
|
107
|
+
if config[:samesite].is_a?(TrueClass) && mode == :lax
|
108
|
+
return true
|
109
|
+
end
|
110
|
+
|
111
|
+
case config[:samesite][mode]
|
112
|
+
when Hash
|
113
|
+
conditionally_flag?(config[:samesite][mode])
|
114
|
+
when TrueClass
|
115
|
+
true
|
116
|
+
else
|
117
|
+
false
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def parse(cookie)
|
122
|
+
return unless cookie
|
123
|
+
|
124
|
+
cookie.split(/[;,]\s?/).each do |pairs|
|
125
|
+
name, values = pairs.split("=", 2)
|
126
|
+
name = CGI.unescape(name)
|
127
|
+
|
128
|
+
attribute = name.downcase.to_sym
|
129
|
+
if @attributes.has_key?(attribute)
|
130
|
+
@attributes[attribute] = values || true
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module CookiesAndCream
|
3
|
+
class Middleware
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
# merges the hash of headers into the current header set.
|
9
|
+
def call(env)
|
10
|
+
req = Rack::Request.new(env)
|
11
|
+
status, headers, response = @app.call(env)
|
12
|
+
|
13
|
+
unless CookiesAndCream.config == OPT_OUT
|
14
|
+
flag_cookies!(headers, override_secure(env, CookiesAndCream.config))
|
15
|
+
end
|
16
|
+
|
17
|
+
[status, headers, response]
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# inspired by https://github.com/tobmatth/rack-ssl-enforcer/blob/6c014/lib/rack/ssl-enforcer.rb#L183-L194
|
23
|
+
def flag_cookies!(headers, config)
|
24
|
+
if cookies = headers["Set-Cookie"]
|
25
|
+
# Support Rails 2.3 / Rack 1.1 arrays as headers
|
26
|
+
cookies = cookies.split("\n") unless cookies.is_a?(Array)
|
27
|
+
|
28
|
+
headers["Set-Cookie"] = cookies.map do |cookie|
|
29
|
+
CookiesAndCream::Cookie.new(cookie, config).to_s
|
30
|
+
end.join("\n")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# disable cookies and cream for non-https requests
|
35
|
+
def override_secure(env, config = {})
|
36
|
+
if scheme(env) != "https" && config != OPT_OUT
|
37
|
+
config = config.dup
|
38
|
+
config[:secure] = OPT_OUT
|
39
|
+
end
|
40
|
+
|
41
|
+
config
|
42
|
+
end
|
43
|
+
|
44
|
+
# derived from https://github.com/tobmatth/rack-ssl-enforcer/blob/6c014/lib/rack/ssl-enforcer.rb#L119
|
45
|
+
def scheme(env)
|
46
|
+
if env["HTTPS"] == "on" || env["HTTP_X_SSL_REQUEST"] == "on"
|
47
|
+
"https"
|
48
|
+
elsif env["HTTP_X_FORWARDED_PROTO"]
|
49
|
+
env["HTTP_X_FORWARDED_PROTO"].split(",")[0]
|
50
|
+
else
|
51
|
+
env["rack.url_scheme"]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# rails 3.1+
|
3
|
+
if defined?(Rails::Railtie)
|
4
|
+
module CookiesAndCream
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
isolate_namespace CookiesAndCream if defined? isolate_namespace # rails 3.0
|
7
|
+
|
8
|
+
initializer "cookies_and_cream.middleware" do
|
9
|
+
Rails.application.config.middleware.insert_before 0, CookiesAndCream::Middleware
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "cookies_and_cream/cookie"
|
4
|
+
require "cookies_and_cream/middleware"
|
5
|
+
require "cookies_and_cream/railtie"
|
6
|
+
require "cookies_and_cream/version"
|
7
|
+
|
8
|
+
module CookiesAndCream
|
9
|
+
OPT_OUT = Object.new
|
10
|
+
|
11
|
+
DEFAULT_CONFIG = {
|
12
|
+
secure: true, # defaults to true but will be a no op on non-HTTPS requests
|
13
|
+
httponly: true, # defaults to true
|
14
|
+
samesite: { # defaults to set `SameSite=Lax`
|
15
|
+
lax: true
|
16
|
+
}
|
17
|
+
}
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def config
|
21
|
+
@config || DEFAULT_CONFIG
|
22
|
+
end
|
23
|
+
|
24
|
+
def config=(configuration)
|
25
|
+
raise RuntimeError, "Already configured" if @config
|
26
|
+
@config = configuration
|
27
|
+
@config.freeze
|
28
|
+
validate!
|
29
|
+
end
|
30
|
+
|
31
|
+
def validate!
|
32
|
+
return if config == OPT_OUT
|
33
|
+
validate_config!
|
34
|
+
validate_secure_config! unless config[:secure].nil?
|
35
|
+
validate_httponly_config! unless config[:httponly].nil?
|
36
|
+
validate_samesite_config! unless config[:samesite].nil?
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def validate_config!
|
42
|
+
raise CookiesConfigError.new("config must be a hash.") unless is_hash?(config)
|
43
|
+
end
|
44
|
+
|
45
|
+
def validate_secure_config!
|
46
|
+
validate_hash_or_true_or_opt_out!(:secure)
|
47
|
+
validate_exclusive_use_of_hash_constraints!(config[:secure], :secure)
|
48
|
+
end
|
49
|
+
|
50
|
+
def validate_httponly_config!
|
51
|
+
validate_hash_or_true_or_opt_out!(:httponly)
|
52
|
+
validate_exclusive_use_of_hash_constraints!(config[:httponly], :httponly)
|
53
|
+
end
|
54
|
+
|
55
|
+
def validate_samesite_config!
|
56
|
+
return if config[:samesite] == OPT_OUT
|
57
|
+
raise CookiesConfigError.new("samesite cookie config must be a hash") unless is_hash?(config[:samesite])
|
58
|
+
|
59
|
+
validate_samesite_boolean_config!
|
60
|
+
validate_samesite_hash_config!
|
61
|
+
end
|
62
|
+
|
63
|
+
# when configuring with booleans, only one enforcement is permitted
|
64
|
+
def validate_samesite_boolean_config!
|
65
|
+
if config[:samesite].key?(:lax) && config[:samesite][:lax].is_a?(TrueClass) && config[:samesite].key?(:strict)
|
66
|
+
raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure lax and strict enforcement is not permitted.")
|
67
|
+
elsif config[:samesite].key?(:strict) && config[:samesite][:strict].is_a?(TrueClass) && config[:samesite].key?(:lax)
|
68
|
+
raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure lax and strict enforcement is not permitted.")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def validate_samesite_hash_config!
|
73
|
+
# validate Hash-based samesite configuration
|
74
|
+
if is_hash?(config[:samesite][:lax])
|
75
|
+
validate_exclusive_use_of_hash_constraints!(config[:samesite][:lax], "samesite lax")
|
76
|
+
|
77
|
+
if is_hash?(config[:samesite][:strict])
|
78
|
+
validate_exclusive_use_of_hash_constraints!(config[:samesite][:strict], "samesite strict")
|
79
|
+
validate_exclusive_use_of_samesite_enforcement!(:only)
|
80
|
+
validate_exclusive_use_of_samesite_enforcement!(:except)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def validate_hash_or_true_or_opt_out!(attribute)
|
86
|
+
if !(is_hash?(config[attribute]) || is_true_or_opt_out?(config[attribute]))
|
87
|
+
raise CookiesConfigError.new("#{attribute} cookie config must be a hash, true, or CookiesAndCream::OPT_OUT")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# validate exclusive use of only or except but not both at the same time
|
92
|
+
def validate_exclusive_use_of_hash_constraints!(conf, attribute)
|
93
|
+
return unless is_hash?(conf)
|
94
|
+
if conf.key?(:only) && conf.key?(:except)
|
95
|
+
raise CookiesConfigError.new("#{attribute} cookie config is invalid, simultaneous use of conditional arguments `only` and `except` is not permitted.")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# validate exclusivity of only and except members within strict and lax
|
100
|
+
def validate_exclusive_use_of_samesite_enforcement!(attribute)
|
101
|
+
if (intersection = (config[:samesite][:lax].fetch(attribute, []) & config[:samesite][:strict].fetch(attribute, []))).any?
|
102
|
+
raise CookiesConfigError.new("samesite cookie config is invalid, cookie(s) #{intersection.join(', ')} cannot be enforced as lax and strict")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def is_hash?(obj)
|
107
|
+
obj && obj.is_a?(Hash)
|
108
|
+
end
|
109
|
+
|
110
|
+
def is_true_or_opt_out?(obj)
|
111
|
+
obj && (obj.is_a?(TrueClass) || obj == OPT_OUT)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cookies_and_cream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Neil Matatall
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-26 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
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
|
+
description: Secure your cookies with an API for opting out
|
42
|
+
email:
|
43
|
+
- oreoshake@users.noreply.github.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- ".ruby-version"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- Guardfile
|
54
|
+
- LICENSE
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- bin/console
|
58
|
+
- bin/setup
|
59
|
+
- cookies_and_cream.gemspec
|
60
|
+
- lib/cookies_and_cream.rb
|
61
|
+
- lib/cookies_and_cream/cookie.rb
|
62
|
+
- lib/cookies_and_cream/middleware.rb
|
63
|
+
- lib/cookies_and_cream/railtie.rb
|
64
|
+
- lib/cookies_and_cream/version.rb
|
65
|
+
homepage: https://github.com/oreoshake/cookies_and_cream
|
66
|
+
licenses: []
|
67
|
+
metadata:
|
68
|
+
homepage_uri: https://github.com/oreoshake/cookies_and_cream
|
69
|
+
source_code_uri: https://github.com/oreoshake/cookies_and_cream
|
70
|
+
changelog_uri: https://github.com/oreoshake/cookies_and_cream/CHANGELOG
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubygems_version: 3.0.1
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: Automatically marks all cookies as secure, httponly, and samesite=lax
|
90
|
+
test_files: []
|