rails_session_key_rotator 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +34 -0
- data/Rakefile +9 -0
- data/lib/rails_session_key_rotator.rb +32 -0
- data/rails_session_key_rotator.gemspec +27 -0
- data/test/rails_session_key_rotator_test.rb +71 -0
- data/test/test_helper.rb +15 -0
- metadata +153 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0a7eb6c3e04ebdb47b6059cfb51c3d01d6ea0920
|
4
|
+
data.tar.gz: 64b730a193e4c8a609d59410d7a5015fbf189c65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 134dec9d2af666fb1df4b20d5c093af6a010112b8ee3cb2ce510fccb2eec55eeccf39fbffb3fc8956a83943041285661497d68c9f1275494ecfd134f20150c3e
|
7
|
+
data.tar.gz: 4b7d677acecad2ad011ace32d918d3d10971746992a96dabcc75ffc56e1f2c85498a768dfba56246a2ad4545f72b87ec4728927bae47b4ee48794e8ed46cd818
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Envato & Steve Hodgkiss
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# RailsSessionKeyRotator
|
2
|
+
|
3
|
+
Graceful secret key rotation for the signed cookie store in Rails. Use this when you'd rather not sign everyone out to rotate your session secret.
|
4
|
+
|
5
|
+
This gem provides a Rack middleware which will regenerate the session cookie with one generated by the new secret if it was written with the old secret. This way we don't have to monkey patch Rails internals and Rails only has to know about the new session key.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'rails_session_key_rotator'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
In config/application.rb:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
config.middleware.insert_before(ActionDispatch::Session::CookieStore, RailsSessionKeyRotator,
|
23
|
+
:old_secret => Secrets.old_session_secret,
|
24
|
+
:new_secret => Secrets.session_secret,
|
25
|
+
:key => 'myapp_session')
|
26
|
+
```
|
27
|
+
|
28
|
+
## Contributing
|
29
|
+
|
30
|
+
1. Fork it ( http://github.com/envato/rails_session_key_rotator/fork )
|
31
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
32
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
33
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
34
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'action_dispatch'
|
3
|
+
|
4
|
+
class RailsSessionKeyRotator
|
5
|
+
def initialize(app, options = {})
|
6
|
+
@app = app
|
7
|
+
@session_cookie_key = options.fetch(:key)
|
8
|
+
old_secret = options.fetch(:old_secret)
|
9
|
+
new_secret = options.fetch(:new_secret)
|
10
|
+
@old_verifier = ActiveSupport::MessageVerifier.new(old_secret)
|
11
|
+
@new_verifier = ActiveSupport::MessageVerifier.new(new_secret)
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
request = Rack::Request.new(env)
|
16
|
+
session_cookie = request.cookies[@session_cookie_key]
|
17
|
+
session_data = verify_old_session_data(session_cookie) if session_cookie.present?
|
18
|
+
if session_data.present?
|
19
|
+
request.cookies[@session_cookie_key] = @new_verifier.generate(session_data)
|
20
|
+
ActiveSupport::Notifications.instrument('rails_session_key_rotator.upgraded', request)
|
21
|
+
end
|
22
|
+
@app.call(env)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def verify_old_session_data(cookie)
|
28
|
+
@old_verifier.verify(cookie)
|
29
|
+
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rails_session_key_rotator"
|
7
|
+
spec.version = "0.0.2"
|
8
|
+
spec.authors = ["Steve Hodgkiss"]
|
9
|
+
spec.email = ["steve@hodgkiss.me"]
|
10
|
+
spec.summary = %q{Graceful session key rotation for the signed cookie store in Rails 3.}
|
11
|
+
spec.description = %q{}
|
12
|
+
spec.homepage = "https://github.com/envato/rails_session_key_rotator"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rack", "~> 1.4.5"
|
21
|
+
spec.add_dependency "actionpack", "> 3.0"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rack-test"
|
25
|
+
spec.add_development_dependency "activesupport", "~> 3.2"
|
26
|
+
spec.add_development_dependency "pry"
|
27
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe RailsSessionKeyRotator do
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
let(:session_data) { { user_id: 1 } }
|
7
|
+
let(:old_secret) { "8aa8356b662857587b23adf45e742dc4ff99c0254da89e983b491b103911b2bbbc5494a9bd5e2b420ae4bd00029dc1f9959b8b70ec1c278cd3ccb758e6fd4aa5" }
|
8
|
+
let(:new_secret) { "bd5dc20f2a7f7af27c137abde10ecaf0294eaa0a91e8c4c20995ae03d655cda7060b6ed178601a2eff8bf08f9423e4ebc08f2c3a077dba5647fb22f3f88c4090" }
|
9
|
+
let(:key) { 'myapp_session' }
|
10
|
+
let(:old_verifier) { ActiveSupport::MessageVerifier.new(old_secret) }
|
11
|
+
let(:new_verifier) { ActiveSupport::MessageVerifier.new(new_secret) }
|
12
|
+
let(:the_apps_session) { Marshal.load(last_response.body) }
|
13
|
+
|
14
|
+
def app
|
15
|
+
@app ||= begin
|
16
|
+
builder = Rack::Builder.new
|
17
|
+
builder.use RailsSessionKeyRotator, old_secret: old_secret, new_secret: new_secret, key: key
|
18
|
+
builder.use ActionDispatch::Session::CookieStore, secret: new_secret, key: key
|
19
|
+
builder.run proc { |env|
|
20
|
+
env["action_dispatch.secret_token"] = new_secret
|
21
|
+
request = ActionDispatch::Request.new(env)
|
22
|
+
cookie = request.cookie_jar.signed[key]
|
23
|
+
[200, {}, Marshal.dump(cookie) ]
|
24
|
+
}
|
25
|
+
builder.to_app
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with a session cookie signed with the old secret' do
|
30
|
+
before do
|
31
|
+
rack_mock_session.cookie_jar[key] = old_verifier.generate(session_data)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 're-writes the session cookie to be readable with the new secret by the app' do
|
35
|
+
get '/'
|
36
|
+
the_apps_session.must_equal(session_data)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'instruments upgrades with activesupport notifications' do
|
40
|
+
called = false
|
41
|
+
subscription = ActiveSupport::Notifications.subscribe('rails_session_key_rotator.upgraded') do |name, start, finish, request_id, req|
|
42
|
+
called = true
|
43
|
+
end
|
44
|
+
get '/'
|
45
|
+
called.must_equal true
|
46
|
+
ActiveSupport::Notifications.unsubscribe(subscription)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'with a session cookie signed with the new secret' do
|
51
|
+
before do
|
52
|
+
rack_mock_session.cookie_jar[key] = new_verifier.generate(session_data)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'does nothing (is still readable by the app)' do
|
56
|
+
get '/'
|
57
|
+
the_apps_session.must_equal(session_data)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with no session cookie' do
|
62
|
+
before do
|
63
|
+
rack_mock_session.cookie_jar[key] = nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'does nothing (is read as nil by the app)' do
|
67
|
+
get '/'
|
68
|
+
the_apps_session.must_be_nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'minitest/autorun'
|
5
|
+
$:.unshift 'lib'
|
6
|
+
|
7
|
+
require 'rails_session_key_rotator'
|
8
|
+
require 'rack/test'
|
9
|
+
require 'pry'
|
10
|
+
|
11
|
+
class MiniTest::Spec
|
12
|
+
class << self
|
13
|
+
alias context describe
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_session_key_rotator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Hodgkiss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.4.5
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
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: rack-test
|
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: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.2'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: ''
|
112
|
+
email:
|
113
|
+
- steve@hodgkiss.me
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE.txt
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- lib/rails_session_key_rotator.rb
|
124
|
+
- rails_session_key_rotator.gemspec
|
125
|
+
- test/rails_session_key_rotator_test.rb
|
126
|
+
- test/test_helper.rb
|
127
|
+
homepage: https://github.com/envato/rails_session_key_rotator
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
metadata: {}
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.2.2
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Graceful session key rotation for the signed cookie store in Rails 3.
|
151
|
+
test_files:
|
152
|
+
- test/rails_session_key_rotator_test.rb
|
153
|
+
- test/test_helper.rb
|