safe_cookies 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +18 -18
- data/lib/safe_cookies/configuration.rb +11 -2
- data/lib/safe_cookies/cookie_path_fix.rb +12 -10
- data/lib/safe_cookies/helpers.rb +7 -5
- data/lib/safe_cookies/util.rb +18 -13
- data/lib/safe_cookies/version.rb +1 -1
- data/lib/safe_cookies.rb +29 -25
- data/spec/safe_cookies_spec.rb +78 -35
- data/spec/util_spec.rb +27 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
M2M1NGJkNGIyYmRhNmQ5OWZmYjU2MTVmMTEwZTc3NzRjNzRlNzE5Zg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MTNiNGMwMjcyMzBkMmQzYzhmODVkNWIzYzg0N2FiNGZmNWE0ZTFkNQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjAyZWNhNTExNWFmZjk3MzAzYmVmYmY1NDAwZDQ4YzA1YWJkOTk0NTAzMDc0
|
10
|
+
OWE3YTM5YmZiMTYzMDg0ZmRiZjFhZGNiMTE5OWU3MWNjZTQ4MGRkNTNlYWNk
|
11
|
+
ZDhlYmM1Y2RjNWE1ZmRlYTY5OGJlYmIxZjJmMTUzODc5NzE1OTI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NThmZjk3M2M5ZGY5YmZiMmI5ZjRkMzA3MWUyOTRlZWMzMmEwY2VjZTRkMWFj
|
14
|
+
OWZjZDk5OGJlMDM0MGE2MDQ0NmM4NTgyMDA4Y2VjYzY5NzEyNjJjZjMzOTE3
|
15
|
+
YWEzYjlmNzkwZjQyZmRhZWI5YzhmNzcyZjlmMmE3NTIxZTRjODE=
|
data/README.md
CHANGED
@@ -8,22 +8,18 @@ This Gem brings a middleware that will make all cookies secure. In detail, it wi
|
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
11
|
+
### Step 1
|
11
12
|
Add this line to your application's Gemfile:
|
12
13
|
|
13
14
|
gem 'safe_cookies'
|
14
15
|
|
15
|
-
Then run
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
16
|
+
Then run `bundle`.
|
20
17
|
|
21
|
-
|
18
|
+
Though this gem is aimed at Rails applications, you may even use it without Rails. Install it then with
|
19
|
+
`gem install safe_cookies`.
|
22
20
|
|
23
21
|
|
24
|
-
|
25
|
-
|
26
|
-
### Step 1
|
22
|
+
### Step 2
|
27
23
|
**Rails 3**: add the following line in config/application.rb:
|
28
24
|
|
29
25
|
class Application < Rails::Application
|
@@ -39,9 +35,9 @@ Or install it yourself as:
|
|
39
35
|
config.middleware.insert_before ActionController::Session::CookieStore, SafeCookies::Middleware
|
40
36
|
end
|
41
37
|
|
42
|
-
### Step
|
38
|
+
### Step 3
|
43
39
|
Register cookies, either just after the lines you added in step 1 or in in an initializer
|
44
|
-
(e.g. config/initializers/safe_cookies.rb):
|
40
|
+
(e.g. in `config/initializers/safe_cookies.rb):
|
45
41
|
|
46
42
|
SafeCookies.configure do |config|
|
47
43
|
config.register_cookie :remember_token, :expire_after => 1.year
|
@@ -55,25 +51,29 @@ not made http-only. It will rewrite the `remember_token` with an expiry of one y
|
|
55
51
|
`last_action` cookie with an expiry of 30 days, making both of them secure and http-only.
|
56
52
|
Available options are: `:expire_after (required), :path, :secure, :http_only`.
|
57
53
|
|
58
|
-
### Step
|
59
|
-
Override `SafeCookies::Middleware#handle_unknown_cookies(cookies)` (see "Dealing with unregistered
|
54
|
+
### Step 4 (only for Rails 2)
|
55
|
+
Override `SafeCookies::Middleware#handle_unknown_cookies(cookies)` (see "Dealing with unregistered
|
56
|
+
cookies" below).
|
60
57
|
|
61
58
|
|
62
59
|
## Dealing with unregistered cookies
|
63
60
|
|
64
|
-
The middleware is not able to secure cookies without knowing their
|
65
|
-
expiry). Unfortunately, the
|
66
|
-
if the cookie
|
61
|
+
The middleware is not able to secure cookies without knowing their attributes (most important: their
|
62
|
+
expiry). Unfortunately, [the client won't ever tell us](http://tools.ietf.org/html/rfc6265#section-4.2.2)
|
63
|
+
if it stores the cookie with flags such as "secure" or which expiry date it currently has.
|
67
64
|
Therefore, it is important to register all cookies that users may come with, specifying their properties.
|
68
65
|
Unregistered cookies cannot be secured.
|
69
66
|
|
70
|
-
If a request brings a cookie that is not registered, the middleware will raise
|
67
|
+
If a request brings a cookie that is not registered, the middleware will raise a
|
71
68
|
`SafeCookies::UnknownCookieError`. Rails 3+ should handle the exception as any other in your application,
|
72
69
|
but by default, **you will not be notified from Rails 2 applications** and the user will see a standard
|
73
70
|
500 Server Error. Override `SafeCookies::Middleware#handle_unknown_cookies(cookies)` in the config
|
74
71
|
initializer for customized exception handling (like, notifying you per email).
|
75
72
|
|
76
|
-
You should
|
73
|
+
You should register any cookie that your application has to do with. However, there are cookies that you
|
74
|
+
do not control, like Google's `__utma` & co. You can tell the middleware to ignore those with the
|
75
|
+
`config.ignore_cookie` directive, which takes either a String or a Regex parameter. Be careful when using
|
76
|
+
regular expressions!
|
77
77
|
|
78
78
|
|
79
79
|
## Fix cookie paths
|
@@ -13,12 +13,13 @@ module SafeCookies
|
|
13
13
|
end
|
14
14
|
|
15
15
|
class Configuration
|
16
|
-
attr_reader :registered_cookies, :fix_cookie_paths, :correct_cookie_paths_timestamp
|
16
|
+
attr_reader :registered_cookies, :fix_cookie_paths, :correct_cookie_paths_timestamp, :ignored_cookies
|
17
17
|
|
18
18
|
def initialize
|
19
19
|
self.registered_cookies = {}
|
20
20
|
self.insecure_cookies = []
|
21
21
|
self.scriptable_cookies = []
|
22
|
+
self.ignored_cookies = []
|
22
23
|
end
|
23
24
|
|
24
25
|
# Register cookies you expect to receive. The middleware will rewrite all
|
@@ -45,6 +46,14 @@ module SafeCookies
|
|
45
46
|
scriptable_cookies << name if options[:http_only] == false
|
46
47
|
end
|
47
48
|
|
49
|
+
# Ignore cookies that you don't control like this:
|
50
|
+
#
|
51
|
+
# ignore_cookie 'ignored_cookie'
|
52
|
+
# ignore_cookie /^__utm/
|
53
|
+
def ignore_cookie(name_or_regex)
|
54
|
+
self.ignored_cookies << name_or_regex
|
55
|
+
end
|
56
|
+
|
48
57
|
def fix_paths(options = {})
|
49
58
|
options.has_key?(:for_cookies_secured_before) or raise MissingOptionError.new("Was told to fix paths without the :for_cookies_secured_before timestamp.")
|
50
59
|
|
@@ -63,7 +72,7 @@ module SafeCookies
|
|
63
72
|
private
|
64
73
|
|
65
74
|
attr_accessor :insecure_cookies, :scriptable_cookies
|
66
|
-
attr_writer :registered_cookies, :fix_cookie_paths, :correct_cookie_paths_timestamp
|
75
|
+
attr_writer :registered_cookies, :fix_cookie_paths, :correct_cookie_paths_timestamp, :ignored_cookies
|
67
76
|
|
68
77
|
end
|
69
78
|
|
@@ -2,23 +2,24 @@ module SafeCookies
|
|
2
2
|
module CookiePathFix
|
3
3
|
|
4
4
|
# Previously, the SafeCookies gem would not set a path when rewriting
|
5
|
-
# cookies. Browsers then would assume and store the current "directory"
|
6
|
-
# leading to multiple cookies per domain.
|
5
|
+
# cookies. Browsers then would assume and store the current "directory"
|
6
|
+
# (see below), leading to multiple cookies per domain.
|
7
7
|
#
|
8
|
-
# If cookies
|
9
|
-
#
|
8
|
+
# If the cookies were secured before the configured datetime, this method
|
9
|
+
# instructs the client to delete all cookies it sent with the request + the
|
10
10
|
# SECURED_COOKIE_NAME helper cookie.
|
11
11
|
# The middleware still sees the request cookies and will rewrite them as
|
12
|
-
# if it hadn't seen them before
|
13
|
-
|
14
|
-
def
|
12
|
+
# if it hadn't seen them before, setting them on the correct path (root,
|
13
|
+
# per default).
|
14
|
+
def delete_cookies_on_bad_path
|
15
15
|
registered_cookies_in_request.keys.each do |registered_cookie|
|
16
16
|
delete_cookie_for_current_directory(registered_cookie)
|
17
17
|
end
|
18
18
|
delete_cookie_for_current_directory(SafeCookies::SECURED_COOKIE_NAME)
|
19
19
|
|
20
|
-
# Delete this cookie here, so the middleware
|
21
|
-
|
20
|
+
# Delete this cookie here, so the middleware believes it hasn't secured
|
21
|
+
# the cookies yet.
|
22
|
+
@request.cookies.delete(SafeCookies::SECURED_COOKIE_NAME)
|
22
23
|
end
|
23
24
|
|
24
25
|
private
|
@@ -51,7 +52,8 @@ module SafeCookies
|
|
51
52
|
end
|
52
53
|
|
53
54
|
def current_directory_is_root?
|
54
|
-
|
55
|
+
# in words: "there are not three slashes before any query params"
|
56
|
+
!@request.path[%r(^/[^/]+/[^\?]+), 0]
|
55
57
|
end
|
56
58
|
|
57
59
|
def secured_old_cookies_timestamp
|
data/lib/safe_cookies/helpers.rb
CHANGED
@@ -9,8 +9,9 @@ module SafeCookies
|
|
9
9
|
cookies = cookies.join("\n") if cookies.is_a?(Array)
|
10
10
|
|
11
11
|
if cookies and cookies.length > 0
|
12
|
-
@
|
12
|
+
@application_cookies_string = cookies
|
13
13
|
end
|
14
|
+
# else, @application_cookies_string will be `nil`
|
14
15
|
end
|
15
16
|
|
16
17
|
def secure(cookie)
|
@@ -47,7 +48,7 @@ module SafeCookies
|
|
47
48
|
# getters
|
48
49
|
|
49
50
|
def stored_application_cookie_names
|
50
|
-
store_cookie =
|
51
|
+
store_cookie = @request.cookies[STORE_COOKIE_NAME] || ""
|
51
52
|
store_cookie.split(KNOWN_COOKIES_DIVIDER)
|
52
53
|
end
|
53
54
|
|
@@ -61,16 +62,17 @@ module SafeCookies
|
|
61
62
|
known += stored_application_cookie_names
|
62
63
|
known += @configuration.registered_cookies.keys
|
63
64
|
end
|
64
|
-
|
65
|
+
|
66
|
+
# returns the request cookies minus ignored cookies
|
65
67
|
def request_cookies
|
66
|
-
@request.cookies
|
68
|
+
Util.except!(@request.cookies.dup, *@configuration.ignored_cookies)
|
67
69
|
end
|
68
70
|
|
69
71
|
|
70
72
|
# boolean
|
71
73
|
|
72
74
|
def cookies_have_been_rewritten_before?
|
73
|
-
|
75
|
+
@request.cookies.has_key? SECURED_COOKIE_NAME
|
74
76
|
end
|
75
77
|
|
76
78
|
def should_be_secure?(cookie)
|
data/lib/safe_cookies/util.rb
CHANGED
@@ -1,17 +1,22 @@
|
|
1
|
-
|
2
|
-
class
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
}
|
1
|
+
class SafeCookies::Util
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def slice(hash, *allowed_keys)
|
5
|
+
sliced_hash = hash.select { |key, _value|
|
6
|
+
allowed_keys.include? key
|
7
|
+
}
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
# Normalize the result of Hash#select
|
10
|
+
# (Ruby 1.8 returns an Array, Ruby 1.9 returns a Hash)
|
11
|
+
Hash[sliced_hash]
|
12
|
+
end
|
13
|
+
|
14
|
+
# rejected_keys may be of type String or Regex
|
15
|
+
def except!(hash, *rejected_keys)
|
16
|
+
hash.delete_if do |key, _value|
|
17
|
+
rejected_keys.any? { |rejected| rejected === key }
|
13
18
|
end
|
14
|
-
|
15
19
|
end
|
20
|
+
|
16
21
|
end
|
17
|
-
end
|
22
|
+
end
|
data/lib/safe_cookies/version.rb
CHANGED
data/lib/safe_cookies.rb
CHANGED
@@ -9,8 +9,6 @@ require "rack"
|
|
9
9
|
# Naming:
|
10
10
|
# - application_cookies: cookies received from the application. The 'Set-Cookie' header is a string
|
11
11
|
# - request_cookies: cookies received from the client. Rack::Request#cookies returns a Hash of { 'name' => 'value' }
|
12
|
-
# - response_cookies: cookies to be sent to the client
|
13
|
-
# (= application_cookies + any cookies set in the middleware)
|
14
12
|
|
15
13
|
module SafeCookies
|
16
14
|
|
@@ -39,13 +37,14 @@ module SafeCookies
|
|
39
37
|
@request = Rack::Request.new(env)
|
40
38
|
ensure_no_unknown_cookies_in_request!
|
41
39
|
|
40
|
+
# calling the next middleware
|
42
41
|
status, @headers, body = @app.call(env)
|
43
42
|
cache_application_cookies_string
|
44
43
|
|
45
|
-
|
46
|
-
rewrite_application_cookies
|
44
|
+
enhance_application_cookies!
|
47
45
|
store_application_cookie_names
|
48
|
-
|
46
|
+
|
47
|
+
delete_cookies_on_bad_path if fix_cookie_paths?
|
49
48
|
rewrite_request_cookies unless cookies_have_been_rewritten_before?
|
50
49
|
|
51
50
|
[ status, @headers, body ]
|
@@ -54,9 +53,11 @@ module SafeCookies
|
|
54
53
|
private
|
55
54
|
|
56
55
|
def reset_instance_variables
|
57
|
-
@request, @headers, @
|
56
|
+
@request, @headers, @application_cookies_string = nil
|
58
57
|
end
|
59
|
-
|
58
|
+
|
59
|
+
# Make sure we get notified if a client comes with an unregistered cookie,
|
60
|
+
# because we do not want any cookie not to be secured.
|
60
61
|
def ensure_no_unknown_cookies_in_request!
|
61
62
|
request_cookie_names = request_cookies.keys.map(&:to_s)
|
62
63
|
unknown_cookie_names = request_cookie_names - known_cookie_names
|
@@ -65,19 +66,11 @@ module SafeCookies
|
|
65
66
|
handle_unknown_cookies(unknown_cookie_names)
|
66
67
|
end
|
67
68
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
request_cookies.delete(cookie)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def rewrite_application_cookies
|
79
|
-
if @application_cookies
|
80
|
-
cookies = @application_cookies.split("\n")
|
69
|
+
|
70
|
+
# Overwrites @header['Set-Cookie']
|
71
|
+
def enhance_application_cookies!
|
72
|
+
if @application_cookies_string
|
73
|
+
cookies = @application_cookies_string.split("\n")
|
81
74
|
|
82
75
|
# On Rack 1.1, cookie values sometimes contain trailing newlines.
|
83
76
|
# Example => ["foo=1; path=/\n", "bar=2; path=/"]
|
@@ -96,23 +89,33 @@ module SafeCookies
|
|
96
89
|
@headers['Set-Cookie'] = cookies.join("\n")
|
97
90
|
end
|
98
91
|
end
|
99
|
-
|
92
|
+
|
93
|
+
# Store the names of cookies that are set by the application. We are already
|
94
|
+
# securing those and therefore do not need to rewrite them.
|
100
95
|
def store_application_cookie_names
|
101
|
-
if @
|
102
|
-
application_cookie_names = stored_application_cookie_names + @
|
96
|
+
if @application_cookies_string
|
97
|
+
application_cookie_names = stored_application_cookie_names + @application_cookies_string.scan(COOKIE_NAME_REGEX)
|
103
98
|
application_cookies_string = application_cookie_names.uniq.join(KNOWN_COOKIES_DIVIDER)
|
104
99
|
|
105
100
|
set_cookie!(STORE_COOKIE_NAME, application_cookies_string, :expire_after => HELPER_COOKIES_LIFETIME)
|
106
101
|
end
|
107
102
|
end
|
108
103
|
|
109
|
-
# This method takes
|
104
|
+
# This method takes the cookies sent with the request and rewrites them,
|
110
105
|
# making them both secure and http-only (unless specified otherwise in
|
111
106
|
# the configuration).
|
112
107
|
# With the SECURED_COOKIE_NAME cookie we remember the exact time that we
|
113
108
|
# rewrote the cookies.
|
114
109
|
def rewrite_request_cookies
|
115
|
-
|
110
|
+
cookies_to_rewrite = request_cookies || []
|
111
|
+
|
112
|
+
# don't rewrite request cookies that the application is setting in the response
|
113
|
+
if @application_cookies_string
|
114
|
+
application_cookie_names = @application_cookies_string.scan(COOKIE_NAME_REGEX)
|
115
|
+
Util.except!(cookies_to_rewrite, *application_cookie_names)
|
116
|
+
end
|
117
|
+
|
118
|
+
if cookies_to_rewrite.any?
|
116
119
|
registered_cookies_in_request.each do |cookie_name, options|
|
117
120
|
value = request_cookies[cookie_name]
|
118
121
|
|
@@ -124,6 +127,7 @@ module SafeCookies
|
|
124
127
|
end
|
125
128
|
end
|
126
129
|
|
130
|
+
# API method
|
127
131
|
def handle_unknown_cookies(cookie_names)
|
128
132
|
raise SafeCookies::UnknownCookieError.new("Request for '#{@request.url}' had unknown cookies: #{cookie_names.join(', ')}")
|
129
133
|
end
|
data/spec/safe_cookies_spec.rb
CHANGED
@@ -7,7 +7,7 @@ describe SafeCookies::Middleware do
|
|
7
7
|
let(:app) { stub 'application' }
|
8
8
|
let(:env) { { 'HTTPS' => 'on' } }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'rewrites registered request cookies as secure and http-only, but only once' do
|
11
11
|
SafeCookies.configure do |config|
|
12
12
|
config.register_cookie('foo', :expire_after => 3600)
|
13
13
|
end
|
@@ -35,7 +35,7 @@ describe SafeCookies::Middleware do
|
|
35
35
|
headers['Set-Cookie'].to_s.should == ''
|
36
36
|
end
|
37
37
|
|
38
|
-
it '
|
38
|
+
it 'doesn’t make cookies secure if the request was not secure' do
|
39
39
|
stub_app_call(app, :application_cookies => 'filter-settings=sort_by_date')
|
40
40
|
env['HTTPS'] = 'off'
|
41
41
|
|
@@ -43,7 +43,7 @@ describe SafeCookies::Middleware do
|
|
43
43
|
headers['Set-Cookie'].should include("filter-settings=sort_by_date")
|
44
44
|
headers['Set-Cookie'].should_not match(/\bsecure\b/i)
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
it 'expires the secured_old_cookies helper cookie in ten years' do
|
48
48
|
Timecop.freeze(Time.parse('2013-09-17 17:53'))
|
49
49
|
|
@@ -59,43 +59,47 @@ describe SafeCookies::Middleware do
|
|
59
59
|
headers['Set-Cookie'].should =~ /secured_old_cookies.*expires=Fri, 15 Sep 2023 \d\d:\d\d:\d\d/
|
60
60
|
end
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
context 'cookie attributes' do
|
63
|
+
|
64
|
+
it 'sets cookies on the root path' do
|
65
|
+
SafeCookies.configure do |config|
|
66
|
+
config.register_cookie('my_old_cookie', :expire_after => 3600)
|
67
|
+
end
|
66
68
|
|
67
|
-
|
68
|
-
|
69
|
+
set_request_cookies(env, 'my_old_cookie=foobar')
|
70
|
+
stub_app_call(app)
|
69
71
|
|
70
|
-
|
72
|
+
code, headers, response = subject.call(env)
|
71
73
|
|
72
|
-
|
73
|
-
|
74
|
-
|
74
|
+
cookies = headers['Set-Cookie'].split("\n")
|
75
|
+
cookies.each do |cookie|
|
76
|
+
cookie.should include('; path=/;')
|
77
|
+
end
|
75
78
|
end
|
76
|
-
end
|
77
79
|
|
78
|
-
|
79
|
-
|
80
|
+
it 'should not alter cookie attributes coming from the application' do
|
81
|
+
stub_app_call(app, :application_cookies => 'cookie=data; path=/; expires=next_week')
|
80
82
|
|
81
|
-
|
82
|
-
|
83
|
-
|
83
|
+
code, headers, response = subject.call(env)
|
84
|
+
headers['Set-Cookie'].should =~ %r(cookie=data; path=/; expires=next_week; secure; HttpOnly)
|
85
|
+
end
|
84
86
|
|
85
|
-
|
86
|
-
|
87
|
+
it 'should respect cookie attributes set in the configuration' do
|
88
|
+
Timecop.freeze
|
87
89
|
|
88
|
-
|
89
|
-
|
90
|
-
|
90
|
+
SafeCookies.configure do |config|
|
91
|
+
config.register_cookie('foo', :expire_after => 3600, :path => '/special/path')
|
92
|
+
end
|
91
93
|
|
92
|
-
|
93
|
-
|
94
|
-
|
94
|
+
stub_app_call(app)
|
95
|
+
set_request_cookies(env, 'foo=bar')
|
96
|
+
env['PATH_INFO'] = '/special/path/subfolder'
|
95
97
|
|
96
|
-
|
97
|
-
|
98
|
-
|
98
|
+
code, headers, response = subject.call(env)
|
99
|
+
expected_expiry = Rack::Utils.rfc2822((Time.now + 3600).gmtime) # a special date format needed here
|
100
|
+
headers['Set-Cookie'].should =~ %r(foo=bar; path=/special/path; expires=#{expected_expiry}; secure; HttpOnly)
|
101
|
+
end
|
102
|
+
|
99
103
|
end
|
100
104
|
|
101
105
|
context 'cookies set by the application' do
|
@@ -173,18 +177,46 @@ describe SafeCookies::Middleware do
|
|
173
177
|
headers['Set-Cookie'].should =~ /js-data=json;.* secure/
|
174
178
|
headers['Set-Cookie'].should_not =~ /js-data=json;.* HttpOnly/
|
175
179
|
end
|
176
|
-
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
context 'ignored cookies' do
|
184
|
+
|
185
|
+
before do
|
186
|
+
stub_app_call(app)
|
187
|
+
set_request_cookies(env, '__utma=123', '__utmz=456')
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'does not rewrite ignored cookies given as string' do
|
191
|
+
SafeCookies.configure do |config|
|
192
|
+
config.ignore_cookie '__utma'
|
193
|
+
config.ignore_cookie '__utmz'
|
194
|
+
end
|
195
|
+
|
196
|
+
code, headers, response = subject.call(env)
|
197
|
+
headers['Set-Cookie'].should_not =~ /__utm/
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'does not rewrite ignored cookies given as regex' do
|
201
|
+
SafeCookies.configure do |config|
|
202
|
+
config.ignore_cookie /^__utm/
|
203
|
+
end
|
204
|
+
|
205
|
+
code, headers, response = subject.call(env)
|
206
|
+
headers['Set-Cookie'].should_not =~ /__utm/
|
207
|
+
end
|
208
|
+
|
177
209
|
end
|
178
210
|
|
179
211
|
context 'unknown request cookies' do
|
180
212
|
|
181
|
-
it '
|
213
|
+
it 'raises an error if there is an unknown cookie' do
|
182
214
|
set_request_cookies(env, 'foo=bar')
|
183
215
|
|
184
216
|
expect{ subject.call(env) }.to raise_error(SafeCookies::UnknownCookieError)
|
185
217
|
end
|
186
218
|
|
187
|
-
it '
|
219
|
+
it 'does not raise an error if the (unregistered) cookie was initially set by the application' do
|
188
220
|
# application sets cookie
|
189
221
|
stub_app_call(app, :application_cookies => 'foo=bar; path=/some/path; secure')
|
190
222
|
|
@@ -204,7 +236,7 @@ describe SafeCookies::Middleware do
|
|
204
236
|
other_subject.call(env)
|
205
237
|
end
|
206
238
|
|
207
|
-
it '
|
239
|
+
it 'does not raise an error if the cookie is listed in the cookie configuration' do
|
208
240
|
SafeCookies.configure do |config|
|
209
241
|
config.register_cookie('foo', :expire_after => 3600)
|
210
242
|
end
|
@@ -214,7 +246,18 @@ describe SafeCookies::Middleware do
|
|
214
246
|
|
215
247
|
subject.call(env)
|
216
248
|
end
|
217
|
-
|
249
|
+
|
250
|
+
it 'does not raise an error if the cookie is ignored' do
|
251
|
+
SafeCookies.configure do |config|
|
252
|
+
config.ignore_cookie '__utma'
|
253
|
+
end
|
254
|
+
|
255
|
+
stub_app_call(app)
|
256
|
+
set_request_cookies(env, '__utma=tracking')
|
257
|
+
|
258
|
+
subject.call(env)
|
259
|
+
end
|
260
|
+
|
218
261
|
it 'allows overwriting the error mechanism' do
|
219
262
|
stub_app_call(app)
|
220
263
|
set_request_cookies(env, 'foo=bar')
|
data/spec/util_spec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SafeCookies::Util do
|
4
|
+
|
5
|
+
describe '.except!' do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@hash = { 'a' => 1, 'ab' => 2, 'b' => 3 }
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'deletes the given keys from the original hash' do
|
12
|
+
SafeCookies::Util.except!(@hash, 'a')
|
13
|
+
@hash.should == { 'ab' => 2, 'b' => 3 }
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'deletes all keys that match the regex' do
|
17
|
+
SafeCookies::Util.except!(@hash, /b/)
|
18
|
+
@hash.should == { 'a' => 1 }
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'returns the original hash' do
|
22
|
+
SafeCookies::Util.except!(@hash, /(?!)/).should == @hash
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe_cookies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dominik Schöler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- spec/cookie_path_fix_spec.rb
|
90
90
|
- spec/safe_cookies_spec.rb
|
91
91
|
- spec/spec_helper.rb
|
92
|
+
- spec/util_spec.rb
|
92
93
|
homepage: http://www.makandra.de
|
93
94
|
licenses: []
|
94
95
|
metadata: {}
|
@@ -117,3 +118,4 @@ test_files:
|
|
117
118
|
- spec/cookie_path_fix_spec.rb
|
118
119
|
- spec/safe_cookies_spec.rb
|
119
120
|
- spec/spec_helper.rb
|
121
|
+
- spec/util_spec.rb
|