cookie_slasher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +21 -0
  2. data/README.md +68 -0
  3. data/lib/cookie_slasher.rb +53 -0
  4. metadata +50 -0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Olek Poplavsky
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,68 @@
1
+ # cookie_slasher
2
+
3
+ ## Synopsis
4
+
5
+ Rack middleware, removes cookies from responses that are likely to be accidentally cached.
6
+
7
+ ## Audience
8
+
9
+ Use this gem as an extra layer of protection if your system has any HTTP
10
+ accelerator in front of it, like Varnish. And by the way, Fastly is all
11
+ about Varnish.
12
+
13
+ ## Why?
14
+
15
+ It is often desirable to create configuration of accelerator that caches 404
16
+ (Page Not Found) and 301 (Permanent Redirect) responses. It is only too
17
+ easy to make a trivial mistake and cache those pages even when there are
18
+ cookies set on them.
19
+
20
+ ### Consequences of not using it
21
+
22
+ If session cookie is set on a 404 or 301 response (typical), and that
23
+ response is cached by HTTP accelerator, your users will suddengly see
24
+ themself logged in as somebody else, and user session swapping will go
25
+ wild. Then you will spend days or weeks troubleshooting this problem,
26
+ because even reproducing it is a challenge. All while users confidence
27
+ in your system plummets.
28
+
29
+ ### Chances of having 'session swapping' problem
30
+
31
+ Fairly small, but you are always only one step away from it, and
32
+ consequences are dire.
33
+
34
+ ## Usage
35
+
36
+ First, add this line to your Gemfile
37
+
38
+ gem 'cookie_slasher'
39
+
40
+ Second, if you have Rails app, add this line to config/application.rb
41
+
42
+ config.middleware.insert_before ActionDispatch::Cookies, CookieSlasher
43
+
44
+ If you have Rack/Sinatra/... app, you just have to 'use' CookieSlasher
45
+ middleware close to the top of your rackup configuration.
46
+
47
+ Third, test to make sure it actually works for you.
48
+
49
+ ### Logging
50
+
51
+ CookieSlasher always logs cookies it is removing from response to avoid
52
+ any surprises. If your app is Rails app, it logs to standard rails
53
+ logger. If not, it logs to 'rack.error' stream, or to logger provided in
54
+ configuration. If you feel like complaining that its log is too verbose
55
+ and noisy, read next paragraph.
56
+
57
+ ### Abuse
58
+
59
+ Relying on it to catch ALL your cookies (especially session cookies) ALL
60
+ the time will work, but is considered to be an abusive behavior.
61
+ CookieSlasher is just a safeguard, it is not intended to be actively
62
+ working all the time removing those cookies from requests. It can do
63
+ that, but that is just bad taste and design. If you see in your
64
+ application log that CookieSlasher is often removing cookies, please do work
65
+ on your application code to make it stop creating them in the first
66
+ place. If you have Rails app, this line of code may come in handy:
67
+
68
+ request.session_options[:skip] = true
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ # Rack middleware that removes all the cookies from 404 and 301
4
+ # responses, making them safe to be cached by varnish. Yes, it is a
5
+ # heavy-handed approach, but given how touchy-feely rails session
6
+ # handling is, it seems to be the only way to guarantee that no cookies
7
+ # are present on those cacheable responses.
8
+
9
+ class CookieSlasher
10
+ def initialize(app, logger=nil)
11
+ @app = app
12
+ @logger = logger
13
+ end
14
+
15
+ def call(env)
16
+ status, headers, body = @app.call(env)
17
+
18
+ case status
19
+ when 404, 301
20
+ # removes ALL cookies from the response
21
+ cookies_header = read_cookies_header(headers)
22
+
23
+ if cookies_header
24
+ log(env, cookies_header)
25
+ delete_cookies_header(headers)
26
+ end
27
+ end
28
+
29
+ [status, headers, body]
30
+ end
31
+
32
+ private
33
+
34
+ def read_cookies_header(headers)
35
+ headers['Set-Cookie']
36
+ end
37
+
38
+ def delete_cookies_header(headers)
39
+ headers.delete 'Set-Cookie'
40
+ end
41
+
42
+ def log(env, cookies_header)
43
+ path = env['PATH_INFO']
44
+
45
+ message = "CookieSlasher: slashing #{cookies_header.inspect} at #{path.inspect}"
46
+ if !@logger && defined?(Rails)
47
+ Rails.logger.warn(message)
48
+ else
49
+ logger = @logger || env['rack.errors']
50
+ logger.write('warn ' + message + "\n")
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cookie_slasher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Olek Poplavsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Use this gem as an extra layer of protection if your system has any HTTP
15
+ accelerators in front of it, like varnish.
16
+ email: olek@woodenbits.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/cookie_slasher.rb
22
+ - LICENSE
23
+ - README.md
24
+ homepage: http://github.com/olek/cookie_slasher
25
+ licenses:
26
+ - MIT
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 1.9.3
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.3.6
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.23
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Rack middleware, removes cookies from responses that are likely to be accidentally
49
+ cached.
50
+ test_files: []