safe_cookies 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Njc0YjdlMzZlMTRmNjk3NGQ1NWQ5NDU0YjU0ZGQwMTA0OGYyM2Y1MQ==
4
+ YjY2ZmQ0ZDEzMWVhMDRmNWE0MmM2ODRjYTU5MzEyZmQxN2JkMGQzMg==
5
5
  data.tar.gz: !binary |-
6
- YzQ4OWE2ODk3NWU4M2U0OTQ3MjRlZDdiYzIzM2MyNjFjNmE0YzRmOQ==
6
+ N2E2Yzg5OGQzMzkyYWI3N2E1MWJlZWJmZjI1NGM0ZjY2MjkzNDVkZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NmJjNjgzM2VjZTdmY2E4ZmM3OTQwOTEwNzgwNTQ1YTMxOTZlOWY4NDA3Nzgy
10
- YTJlZmRkOTQyMjQwNTE1NWUxN2I2M2VmNTNjMjMyZTk5NGQ0MTlmY2M0MWMz
11
- NmQwM2EwZmE4M2I4MjJlNmVjZDlmZmJmNWY0NDYxN2Q3ZWI2YmE=
9
+ MzU1ZTQyYjQzNmNhNzljNzA2Y2U2NWIyMDU5MTkyMTU5MDZjYmQ0ZDY4MjNl
10
+ M2MxMWJhYmQwMzJkZDdlYzJlZTkwYWFkNWJkNzE4ZTc5MTUxOWI0YjMwNmVk
11
+ NGM0YzUyZWU3MjJjMGZlMzgzYjk2OTg4OWU1MzY5ZGQzYjE0NzE=
12
12
  data.tar.gz: !binary |-
13
- YjU4NDU0NjUwNjgzZGNhYzNjNTBiM2JjOTQyMGZlZDY3YThjYjRiYTUzZDE5
14
- ZDU5NjVhOTNlYjM1NWE2NDUwMjI1YzZmZmU5OGZhZmI3M2I2ZjM5YzMyNzNl
15
- YWZjYWFjMGIwMWU5YTg2NThlMzJkNjExMDdjODgyMzhkY2ExYmY=
13
+ OTMzYTYzN2JkMzk3OGU5NzE4MzAwMWUxYWVmMmI1ZThmYTExNmQyNzgyYWU4
14
+ YjFiYzIyOTg4YTk0Njg3YTk0NzRiMjAyMzc1NmM0OWVhYjRhNWI3NDdlZjZm
15
+ NTMxYTkwNmJlYTJlMjBkY2NhMGYzM2MwYzRkYzcwMTViMTlmMzc=
data/README.md CHANGED
@@ -66,19 +66,22 @@ The middleware is not able to secure cookies without knowing their attributes
66
66
  if it stores the cookie with flags such as "secure" or which expiry date it
67
67
  currently has. Therefore, it is important to register all cookies that may be
68
68
  sent by the client, specifying their properties. Unregistered cookies cannot be
69
- secured.
70
-
71
- If a request contains a cookie that is not registered, the middleware will raise
72
- a `SafeCookies::UnknownCookieError`. Rails 3+ should handle the exception as any
73
- other in your application, but by default, **you will not be notified from Rails
74
- 2 applications** and the user will see a standard 500 Server Error. Override
75
- `SafeCookies::Middleware#handle_unknown_cookies(cookies)` in the config
76
- initializer for customized exception handling (like, notifying you per email).
77
-
78
- You should register any cookie that your application has to do with. However, there are cookies that you
79
- do not control, like Google's `__utma` & co. You can tell the middleware to ignore those with the
80
- `config.ignore_cookie` directive, which takes either a String or a Regex parameter. Be careful when using
81
- regular expressions!
69
+ secured by the middleware.
70
+
71
+ Unknown cookies are written to the Rails log. When you start implementing the
72
+ middleware, you should closely watch it to find cookies you forgot to register.
73
+ You may overwrite `SafeCookies::Middleware#handle_unknown_cookies(cookies)` in
74
+ the config initializer for customized behaviour (like, notifying you per email).
75
+
76
+ You should register any cookie that your application is using.
77
+
78
+
79
+ ## Ignoring cookies
80
+
81
+ Currently, ignoring cookies only prevents the middleware from writing them to the logs.
82
+
83
+ You can tell the middleware to ignore cookies with the `config.ignore_cookie`
84
+ directive, which takes either a String or a Regex parameter. Be careful when using regular expressions!
82
85
 
83
86
 
84
87
  ## Fix cookie paths
@@ -1,3 +1,3 @@
1
1
  module SafeCookies
2
- VERSION = '0.1.7'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/safe_cookies.rb CHANGED
@@ -18,6 +18,7 @@ module SafeCookies
18
18
  STORE_COOKIE_NAME = '_safe_cookies__known_cookies'
19
19
  SECURED_COOKIE_NAME = 'secured_old_cookies'
20
20
  HELPER_COOKIES_LIFETIME = 10 * 365 * 24 * 60 * 60 # 10 years
21
+
21
22
 
22
23
  class Middleware
23
24
 
@@ -57,9 +58,7 @@ module SafeCookies
57
58
  def reset_instance_variables
58
59
  @request, @headers, @application_cookies_string = nil
59
60
  end
60
-
61
- # Do something if a request has an unregistered cookie, because we do not
62
- # want any cookie to not be secured. By default, we raise an error.
61
+
63
62
  def check_if_request_has_unknown_cookies
64
63
  request_cookie_names = request_cookies.keys.map(&:to_s)
65
64
  unknown_cookie_names = request_cookie_names - known_cookie_names
@@ -129,7 +128,14 @@ module SafeCookies
129
128
 
130
129
  # API method
131
130
  def handle_unknown_cookies(cookie_names)
132
- raise SafeCookies::UnknownCookieError.new("Request for '#{@request.url}' had unknown cookies: #{cookie_names.join(', ')}")
131
+ log_error("Request for '#{@request.url}' had unknown cookies: #{cookie_names.join(', ')}")
132
+ end
133
+
134
+ def log_error(error_message)
135
+ message = '** [SafeCookies error] '
136
+ message << error_message
137
+
138
+ Rails.logger.error(message) if defined?(Rails)
133
139
  end
134
140
 
135
141
  end
@@ -207,16 +207,22 @@ describe SafeCookies::Middleware do
207
207
  end
208
208
 
209
209
  end
210
-
210
+
211
+
212
+ # The unknown cookies mechanism was more important when we were sending
213
+ # notifications on encountering unknown cookies. Perhaps, it will regain
214
+ # importance in the future.
211
215
  context 'when a request has unknown cookies,' do
212
216
 
213
- it 'raises an error if there is an unknown cookie' do
217
+ it 'logs an error message' do
218
+ stub_app_call(app)
214
219
  set_request_cookies(env, 'foo=bar')
215
-
216
- expect{ subject.call(env) }.to raise_error(SafeCookies::UnknownCookieError)
220
+
221
+ subject.should_receive(:log_error).with(/unknown cookies: foo/)
222
+ subject.call(env)
217
223
  end
218
224
 
219
- it 'does not raise an error if the (unregistered) cookie was initially set by the application' do
225
+ it 'does not log an error if the (unregistered) cookie was initially set by the application' do
220
226
  # application sets cookie
221
227
  stub_app_call(app, :application_cookies => 'foo=bar; path=/some/path; secure')
222
228
 
@@ -233,10 +239,11 @@ describe SafeCookies::Middleware do
233
239
  stub_app_call(other_app)
234
240
  set_request_cookies(env, *received_cookies)
235
241
 
242
+ other_subject.should_not_receive(:log_error)
236
243
  other_subject.call(env)
237
244
  end
238
245
 
239
- it 'does not raise an error if the cookie is listed in the cookie configuration' do
246
+ it 'does not log an error if the cookie is listed in the cookie configuration' do
240
247
  SafeCookies.configure do |config|
241
248
  config.register_cookie('foo', :expire_after => 3600)
242
249
  end
@@ -244,10 +251,11 @@ describe SafeCookies::Middleware do
244
251
  stub_app_call(app)
245
252
  set_request_cookies(env, 'foo=bar')
246
253
 
254
+ subject.should_not_receive(:log_error)
247
255
  subject.call(env)
248
256
  end
249
257
 
250
- it 'does not raise an error if the cookie is ignored' do
258
+ it 'does not log an error if the cookie is ignored' do
251
259
  SafeCookies.configure do |config|
252
260
  config.ignore_cookie '__utma'
253
261
  end
@@ -255,10 +263,11 @@ describe SafeCookies::Middleware do
255
263
  stub_app_call(app)
256
264
  set_request_cookies(env, '__utma=tracking')
257
265
 
266
+ subject.should_not_receive(:log_error)
258
267
  subject.call(env)
259
268
  end
260
269
 
261
- it 'allows overwriting the error mechanism' do
270
+ it 'allows overwriting the handling mechanism' do
262
271
  stub_app_call(app)
263
272
  set_request_cookies(env, 'foo=bar')
264
273
 
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.7
4
+ version: 0.2.0
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-11-04 00:00:00.000000000 Z
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  version: '0'
110
110
  requirements: []
111
111
  rubyforge_project:
112
- rubygems_version: 2.1.2
112
+ rubygems_version: 2.1.3
113
113
  signing_key:
114
114
  specification_version: 4
115
115
  summary: Make all cookies `secure` and `HttpOnly`.