scottwb-encrypted_cookie_store 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails', "~> 3.0.0"
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ gem 'bundler', "~> 1.0.0"
8
+ gem 'rspec', "~> 2.6.0"
9
+ gem 'jeweler', "~> 1.6.1"
10
+ end
11
+
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.7)
6
+ actionpack (= 3.0.7)
7
+ mail (~> 2.2.15)
8
+ actionpack (3.0.7)
9
+ activemodel (= 3.0.7)
10
+ activesupport (= 3.0.7)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.5.0)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.7)
19
+ activesupport (= 3.0.7)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.7)
23
+ activemodel (= 3.0.7)
24
+ activesupport (= 3.0.7)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.7)
28
+ activemodel (= 3.0.7)
29
+ activesupport (= 3.0.7)
30
+ activesupport (3.0.7)
31
+ arel (2.0.10)
32
+ builder (2.1.2)
33
+ diff-lcs (1.1.2)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ git (1.2.5)
37
+ i18n (0.5.0)
38
+ jeweler (1.6.1)
39
+ bundler (~> 1.0.0)
40
+ git (>= 1.2.5)
41
+ rake
42
+ mail (2.2.19)
43
+ activesupport (>= 2.3.6)
44
+ i18n (>= 0.4.0)
45
+ mime-types (~> 1.16)
46
+ treetop (~> 1.4.8)
47
+ mime-types (1.16)
48
+ polyglot (0.3.1)
49
+ rack (1.2.3)
50
+ rack-mount (0.6.14)
51
+ rack (>= 1.0.0)
52
+ rack-test (0.5.7)
53
+ rack (>= 1.0)
54
+ rails (3.0.7)
55
+ actionmailer (= 3.0.7)
56
+ actionpack (= 3.0.7)
57
+ activerecord (= 3.0.7)
58
+ activeresource (= 3.0.7)
59
+ activesupport (= 3.0.7)
60
+ bundler (~> 1.0)
61
+ railties (= 3.0.7)
62
+ railties (3.0.7)
63
+ actionpack (= 3.0.7)
64
+ activesupport (= 3.0.7)
65
+ rake (>= 0.8.7)
66
+ thor (~> 0.14.4)
67
+ rake (0.9.0)
68
+ rspec (2.6.0)
69
+ rspec-core (~> 2.6.0)
70
+ rspec-expectations (~> 2.6.0)
71
+ rspec-mocks (~> 2.6.0)
72
+ rspec-core (2.6.3)
73
+ rspec-expectations (2.6.0)
74
+ diff-lcs (~> 1.1.2)
75
+ rspec-mocks (2.6.0)
76
+ thor (0.14.6)
77
+ treetop (1.4.9)
78
+ polyglot (>= 0.3.1)
79
+ tzinfo (0.3.27)
80
+
81
+ PLATFORMS
82
+ ruby
83
+
84
+ DEPENDENCIES
85
+ bundler (~> 1.0.0)
86
+ jeweler (~> 1.6.1)
87
+ rails (~> 3.0.0)
88
+ rake
89
+ rspec (~> 2.6.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2009 - 2010 Phusion
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name of the Phusion nor the names of its contributors
13
+ may be used to endorse or promote products derived from this software
14
+ without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ EncryptedCookieStore
2
+ ====================
3
+ EncryptedCookieStore is similar to Ruby on Rails's CookieStore (it saves
4
+ session data in a cookie), but it uses encryption so that people can't read
5
+ what's in the session data. This makes it possible to store sensitive data
6
+ in the session.
7
+
8
+ This version of EncryptedCookieStore is written for Rails 3.0.0+. It will not work with Rails 3.0.0.beta3 or earlier. It does not yet work with Rails 3.1. It has been tested with:
9
+
10
+ * 3.0.0
11
+ * 3.0.7
12
+ * 3.0.8.rc4
13
+
14
+ The original version for Rails 2.3 can be found here: https://github.com/FooBarWidget/encrypted_cookie_store
15
+
16
+ For a version that probably works with Rails 3.0.0.beta - 3.0.0.beta3, check here: https://github.com/twoism-dev/encrypted_cookie_store
17
+
18
+ Installation and usage
19
+ ----------------------
20
+
21
+ First, install it:
22
+
23
+ gem install scottwb-encrypted_cookie_store
24
+
25
+ Then, add it to you bundler Gemfile:
26
+
27
+ gem 'scottwb-encrypted_cookie_store', :require => 'encrypted_cookie_store'
28
+
29
+ Then edit `config/initializers/session_store.rb` and set your session store to
30
+ EncryptedCookieStore:
31
+
32
+ MyApp::Application.config.session_store(
33
+ EncryptedCookieStore::EncryptedCookieStore,
34
+ :key => '_myapp_session',
35
+ :encryption_key => '966a4....'
36
+ )
37
+
38
+ The encryption key *must* be a hexadecimal string of exactly 32 bytes. It
39
+ should be entirely random, because otherwise it can make the encryption weak.
40
+
41
+ You can generate a new encryption key by running `rake secret:encryption_key`.
42
+ This command will output a random encryption key that you can then copy and
43
+ paste into your environment.rb.
44
+
45
+ You also need to make sure you have a secret token defined in `config/initializers/secret_token.rb`, just as you work for the standard CookieStore, e.g.:
46
+
47
+ MyApp::Application.config.secret_token = 'f75bb....'
48
+
49
+ Operational details
50
+ -------------------
51
+ Upon generating cookie data, EncryptedCookieStore generates a new, random
52
+ initialization vector for encrypting the session data. This initialization
53
+ vector is then encrypted with 128-bit AES in ECB mode. The session data is
54
+ first protected with an HMAC to prevent tampering. The session data, along
55
+ with the HMAC, are then encrypted using 256-bit AES in CFB mode with the
56
+ generated initialization vector. This encrypted session data + HMAC are
57
+ then stored, along with the encrypted initialization vector, into the cookie.
58
+
59
+ Upon unmarshalling the cookie data, EncryptedCookieStore decrypts the
60
+ encrypted initialization vector and use that to decrypt the encrypted
61
+ session data + HMAC. The decrypted session data is then verified against
62
+ the HMAC.
63
+
64
+ The reason why HMAC verification occurs after decryption instead of before
65
+ decryption is because we want to be able to detect changes to the encryption
66
+ key and changes to the HMAC secret key, as well as migrations from CookieStore.
67
+ Verifying after decryption allows us to automatically invalidate such old
68
+ session cookies.
69
+
70
+ EncryptedCookieStore is quite fast: it is able to marshal and unmarshal a
71
+ simple session object 5000 times in 8.7 seconds on a MacBook Pro with a 2.4
72
+ Ghz Intel Core 2 Duo (in battery mode). This is about 0.174 ms per
73
+ marshal+unmarshal action. See `rake benchmark` in the EncryptedCookieStore
74
+ sources for details.
75
+
76
+ EncryptedCookieStore vs other session stores
77
+ --------------------------------------------
78
+ EncryptedCookieStore inherits all the benefits of CookieStore:
79
+
80
+ * It works out of the box without the need to setup a seperate data store (e.g. database table, daemon, etc).
81
+ * It does not require any maintenance. Old, stale sessions do not need to be manually cleaned up, as is the case with PStore and ActiveRecordStore.
82
+ * Compared to MemCacheStore, EncryptedCookieStore can "hold" an infinite number of sessions at any time.
83
+ * It can be scaled across multiple servers without any additional setup.
84
+ * It is fast.
85
+ * It is more secure than CookieStore because it allows you to store sensitive data in the session.
86
+
87
+ There are of course drawbacks as well:
88
+
89
+ * It is prone to session replay attacks. These kind of attacks are explained in the [Ruby on Rails Security Guide](http://guides.rubyonrails.org/security.html#session-storage). Therefore you should never store anything along the lines of `is_admin` in the session.
90
+ * You can store at most a little less than 4 KB of data in the session because that's the size limit of a cookie. "A little less" because EncryptedCookieStore also stores a small amount of bookkeeping data in the cookie.
91
+ * Although encryption makes it more secure than CookieStore, there's still a chance that a bug in EncryptedCookieStore renders it insecure. We welcome everyone to audit this code. There's also a chance that weaknesses in AES are found in the near future which render it insecure. If you are storing *really* sensitive information in the session, e.g. social security numbers, or plans for world domination, then you should consider using ActiveRecordStore or some other server-side store.
92
+
93
+ JRuby: Illegal Key Size error
94
+ -----------------------------
95
+ If you get this error (and your code works with MRI)...
96
+
97
+ Illegal key size
98
+
99
+ [...]/vendor/plugins/encrypted_cookie_store/lib/encrypted_cookie_store.rb:62:in `marshal'
100
+
101
+ ...then it probably means you don't have the "unlimited strength" policy files
102
+ installed for your JVM.
103
+ [Download and install them.](http://www.ngs.ac.uk/tools/jcepolicyfiles)
104
+ You probably have the "strong" version if they are already there.
105
+
106
+ As a workaround, you can change the cipher type from 256-bit AES to 128-bit by
107
+ inserting the following in `config/initializer/session_store.rb`:
108
+
109
+ EncryptedCookieStore.data_cipher_type = 'aes-128-cfb'.freeze # was 256
110
+
111
+ Please note that after changing to 128-bit AES, EncryptedCookieStore still
112
+ requires a 32 bytes hexadecimal encryption key, although only half of the key
113
+ is actually used.
data/Rakefile ADDED
@@ -0,0 +1,81 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ gem.name = "scottwb-encrypted_cookie_store"
16
+ gem.homepage = "https://github.com/scottwb/encrypted_cookie_store"
17
+ gem.summary = "A Rails 3.0 version of Encrypted Cookie Store by FooBarWidget"
18
+ gem.description = "A Rails 3.0 version of Encrypted Cookie Store by FooBarWidget"
19
+ gem.email = "scottwb@gmail.com"
20
+ gem.authors = ["FooBarWidget", "Scott W. Bradley"]
21
+ end
22
+ Jeweler::RubygemsDotOrgTasks.new
23
+
24
+
25
+ require 'rspec/core'
26
+ require 'rspec/core/rake_task'
27
+ RSpec::Core::RakeTask.new(:spec) do |spec|
28
+ spec.pattern = FileList['spec/**/*_spec.rb']
29
+ end
30
+ task :default => :spec
31
+
32
+
33
+ desc "Run benchmark"
34
+ task :benchmark do
35
+ $LOAD_PATH.unshift(File.expand_path("lib"))
36
+ require 'rubygems'
37
+ require 'benchmark'
38
+ require "rails"
39
+ require 'action_controller'
40
+ require 'encrypted_cookie_store'
41
+
42
+ secret = "b6a30e998806a238c4bad45cc720ed55e56e50d9f00fff58552e78a20fe8262df61" <<
43
+ "42fcfdb0676018bb9767ed560d4a624fb7f3603b4e53c77ec189ae3853bd1"
44
+ encryption_key = "dd458e790c3b995e3606384c58efc53da431db892f585aa3ca2a17eabe6df75b"
45
+ store = EncryptedCookieStore::EncryptedCookieStore.new(
46
+ nil,
47
+ :secret => secret,
48
+ :key => 'my_app',
49
+ :encryption_key => encryption_key
50
+ )
51
+ object = {
52
+ :hello => "world",
53
+ :user_id => 1234,
54
+ :is_admin => true,
55
+ :shopping_cart => ["Tea x 1", "Carrots x 13", "Pocky x 20", "Pen x 4"],
56
+ :session_id => "b6a30e998806a238c4bad45cc720ed55e56e50d9f00ff"
57
+ }
58
+ count = 50_000
59
+
60
+ puts "Marshalling and unmarshalling #{count} times..."
61
+ result = Benchmark.measure do
62
+ count.times do
63
+ data = store.send(:set_session, nil, nil, object)
64
+ store.send(:unmarshal, data)
65
+ end
66
+ end
67
+ puts result
68
+ printf "%.3f ms per marshal+unmarshal action\n", result.real * 1000 / count
69
+ end
70
+
71
+
72
+ require "rake/rdoctask"
73
+ Rake::RDocTask.new do |rdoc|
74
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
75
+
76
+ rdoc.rdoc_dir = "rdoc"
77
+ rdoc.title = "scottwb-encrypted_cookie_store #{version}"
78
+ rdoc.rdoc_files.include("README*")
79
+ rdoc.rdoc_files.include("lib/**/*.rb")
80
+ #rdoc.main = "README.md"
81
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,2 @@
1
+ require 'encrypted_cookie_store/railtie'
2
+ require 'encrypted_cookie_store/encrypted_cookie_store'
@@ -0,0 +1,3 @@
1
+ module EncryptedCookieStoreConstants
2
+ ENCRYPTION_KEY_SIZE = 32
3
+ end
@@ -0,0 +1,145 @@
1
+ require 'openssl'
2
+ require 'encrypted_cookie_store/constants'
3
+
4
+ module EncryptedCookieStore
5
+ class EncryptedCookieStore < ActionDispatch::Session::CookieStore
6
+ OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError
7
+ include EncryptedCookieStoreConstants
8
+
9
+ class << self
10
+ attr_accessor :iv_cipher_type
11
+ attr_accessor :data_cipher_type
12
+ end
13
+
14
+ self.iv_cipher_type = "aes-128-ecb".freeze
15
+ self.data_cipher_type = "aes-256-cfb".freeze
16
+
17
+ def initialize(app, options = {})
18
+ ensure_encryption_key_secure(options[:encryption_key])
19
+ @encryption_key = unhex(options[:encryption_key]).freeze
20
+ @iv_cipher = OpenSSL::Cipher::Cipher.new(EncryptedCookieStore.iv_cipher_type)
21
+ @data_cipher = OpenSSL::Cipher::Cipher.new(EncryptedCookieStore.data_cipher_type)
22
+ super(app, options)
23
+ end
24
+
25
+ private
26
+ # Like ActiveSupport::MessageVerifier, but does not base64-encode data.
27
+ class MessageVerifier
28
+ def initialize(secret, digest = 'SHA1')
29
+ @secret = secret
30
+ @digest = digest
31
+ end
32
+
33
+ def verify(signed_message)
34
+ digest, data = signed_message.split("--", 2)
35
+ if digest != generate_digest(data)
36
+ raise ActiveSupport::MessageVerifier::InvalidSignature
37
+ else
38
+ Marshal.load(data)
39
+ end
40
+ end
41
+
42
+ def generate(value)
43
+ data = Marshal.dump(value)
44
+ digest = generate_digest(data)
45
+ "#{digest}--#{data}"
46
+ end
47
+
48
+ private
49
+ def generate_digest(data)
50
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@digest), @secret, data)
51
+ end
52
+ end
53
+
54
+ def set_session(env, sid, session_data)
55
+ # We hmac-then-encrypt instead of encrypt-then-hmac so that we
56
+ # can properly detect:
57
+ # - changes to the encryption key or initialization vector
58
+ # - a migration from the unencrypted CookieStore.
59
+ #
60
+ # Being able to detect these allows us to invalidate the old session data.
61
+
62
+ @iv_cipher.encrypt
63
+ @data_cipher.encrypt
64
+ @iv_cipher.key = @encryption_key
65
+ @data_cipher.key = @encryption_key
66
+
67
+ clear_session_data = super(env, sid, session_data)
68
+ iv = @data_cipher.random_iv
69
+ @data_cipher.iv = iv
70
+ encrypted_iv = @iv_cipher.update(iv) << @iv_cipher.final
71
+ encrypted_session_data = @data_cipher.update(Marshal.dump(clear_session_data)) << @data_cipher.final
72
+
73
+ "#{base64(encrypted_iv)}--#{base64(encrypted_session_data)}"
74
+ end
75
+
76
+ def unpacked_cookie_data(env)
77
+ env["action_dispatch.request.unsigned_session_cookie"] ||= begin
78
+ stale_session_check! do
79
+ request = ActionDispatch::Request.new(env)
80
+ if data = request.cookie_jar.signed[@key] && data.is_a?(String)
81
+ unmarshal(data)
82
+ else
83
+ {}
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ def unmarshal(cookie)
90
+ if cookie
91
+ b64_encrypted_iv, b64_encrypted_session_data = cookie.split("--", 2)
92
+ if b64_encrypted_iv && b64_encrypted_session_data
93
+ encrypted_iv = ActiveSupport::Base64.decode64(b64_encrypted_iv)
94
+ encrypted_session_data = ActiveSupport::Base64.decode64(b64_encrypted_session_data)
95
+
96
+ @iv_cipher.decrypt
97
+ @iv_cipher.key = @encryption_key
98
+ iv = @iv_cipher.update(encrypted_iv) << @iv_cipher.final
99
+
100
+ @data_cipher.decrypt
101
+ @data_cipher.key = @encryption_key
102
+ @data_cipher.iv = iv
103
+ session_data = Marshal.load(@data_cipher.update(encrypted_session_data) << @data_cipher.final) rescue nil
104
+ end
105
+ else
106
+ nil
107
+ end
108
+ rescue OpenSSLCipherError
109
+ nil
110
+ end
111
+
112
+ # To prevent users from using an insecure encryption key like "Password" we make sure that the
113
+ # encryption key they've provided is at least 30 characters in length.
114
+ def ensure_encryption_key_secure(encryption_key)
115
+ if encryption_key.blank?
116
+ raise ArgumentError, "An encryption key is required for encrypting the " +
117
+ "cookie session data. Please set config.action_controller.session = { " +
118
+ "..., :encryption_key => \"some random string of exactly " +
119
+ "#{ENCRYPTION_KEY_SIZE * 2} bytes\", ... } in config/environment.rb"
120
+ end
121
+
122
+ if encryption_key.size != ENCRYPTION_KEY_SIZE * 2
123
+ raise ArgumentError, "The EncryptedCookieStore encryption key must be a " +
124
+ "hexadecimal string of exactly #{ENCRYPTION_KEY_SIZE * 2} bytes. " +
125
+ "The value that you've provided, \"#{encryption_key}\", is " +
126
+ "#{encryption_key.size} bytes. You could use the following (randomly " +
127
+ "generated) string as encryption key: " +
128
+ ActiveSupport::SecureRandom.hex(ENCRYPTION_KEY_SIZE)
129
+ end
130
+ end
131
+
132
+ def verifier_for(secret, digest)
133
+ key = secret.respond_to?(:call) ? secret.call : secret
134
+ MessageVerifier.new(key, digest)
135
+ end
136
+
137
+ def base64(data)
138
+ ActiveSupport::Base64.encode64s(data)
139
+ end
140
+
141
+ def unhex(hex_data)
142
+ [hex_data].pack("H*")
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,10 @@
1
+ module EncryptedCookieStore
2
+ class Railtie < Rails::Railtie
3
+ initializer "encrypted_cookie_store_railtie.boot" do |app|
4
+ end
5
+
6
+ rake_tasks do
7
+ load 'tasks/encrypted_cookie_store.rake'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ namespace :secret do
2
+ desc "Generate an encryption key for EncryptedCookieStore that's cryptographically secure."
3
+ task :encryption_key do
4
+ require 'encrypted_cookie_store/constants'
5
+ puts ActiveSupport::SecureRandom.hex(EncryptedCookieStoreConstants::ENCRYPTION_KEY_SIZE)
6
+ end
7
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{scottwb-encrypted_cookie_store}
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["FooBarWidget", "Scott W. Bradley"]
12
+ s.date = %q{2011-05-31}
13
+ s.description = %q{A Rails 3.0 version of Encrypted Cookie Store by FooBarWidget}
14
+ s.email = %q{scottwb@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".rspec",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/encrypted_cookie_store.rb",
28
+ "lib/encrypted_cookie_store/constants.rb",
29
+ "lib/encrypted_cookie_store/encrypted_cookie_store.rb",
30
+ "lib/encrypted_cookie_store/railtie.rb",
31
+ "lib/tasks/encrypted_cookie_store.rake",
32
+ "scottwb-encrypted_cookie_store.gemspec",
33
+ "spec/encrypted_cookie_store_spec.rb"
34
+ ]
35
+ s.homepage = %q{https://github.com/scottwb/encrypted_cookie_store}
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.6.2}
38
+ s.summary = %q{A Rails 3.0 version of Encrypted Cookie Store by FooBarWidget}
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<rails>, ["~> 3.0.0"])
45
+ s.add_development_dependency(%q<rake>, [">= 0"])
46
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
47
+ s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.1"])
49
+ else
50
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
51
+ s.add_dependency(%q<rake>, [">= 0"])
52
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.6.1"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
58
+ s.add_dependency(%q<rake>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.6.1"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,88 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
2
+ require 'rubygems'
3
+ gem 'rails', '>= 3.0.0'
4
+ require 'rails'
5
+ require 'action_controller'
6
+ require 'encrypted_cookie_store'
7
+
8
+ describe EncryptedCookieStore::EncryptedCookieStore do
9
+ SECRET = "b6a30e998806a238c4bad45cc720ed55e56e50d9f00fff58552e78a20fe8262df61" <<
10
+ "42fcfdb0676018bb9767ed560d4a624fb7f3603b4e53c77ec189ae3853bd1"
11
+ GOOD_ENCRYPTION_KEY = "dd458e790c3b995e3606384c58efc53da431db892f585aa3ca2a17eabe6df75b"
12
+ ANOTHER_GOOD_ENCRYPTION_KEY = "ce6a45c34607d2048d735b0a31a769de4e1512eb83c7012059a66937158a8975"
13
+ OBJECT = { :user_id => 123, :admin => true, :message => "hello world!" }
14
+
15
+ def create(options = {})
16
+ EncryptedCookieStore::EncryptedCookieStore.new(nil, options.reverse_merge(
17
+ :key => 'key',
18
+ :secret => SECRET
19
+ ))
20
+ end
21
+
22
+ it "checks whether an encryption key is given" do
23
+ lambda { create }.should raise_error(ArgumentError, /encryption key is required/)
24
+ end
25
+
26
+ it "checks whether the encryption key has the correct size" do
27
+ encryption_key = "too small"
28
+ block = lambda { create(:encryption_key => encryption_key) }
29
+ block.should raise_error(ArgumentError, /must be a hexadecimal string of exactly \d+ bytes/)
30
+ end
31
+
32
+ specify "marshalling and unmarshalling data works" do
33
+ data = create(:encryption_key => GOOD_ENCRYPTION_KEY).send(:set_session, nil, nil, OBJECT)
34
+ object = create(:encryption_key => GOOD_ENCRYPTION_KEY).send(:unmarshal, data)
35
+ object[:user_id].should == 123
36
+ object[:admin].should be_true
37
+ object[:message].should == "hello world!"
38
+ end
39
+
40
+ it "uses a different initialization vector every time data is marshalled" do
41
+ store = create(:encryption_key => GOOD_ENCRYPTION_KEY)
42
+ data1 = store.send(:set_session, nil, nil, OBJECT)
43
+ data2 = store.send(:set_session, nil, nil, OBJECT)
44
+ data3 = store.send(:set_session, nil, nil, OBJECT)
45
+ data4 = store.send(:set_session, nil, nil, OBJECT)
46
+ data1.should_not == data2
47
+ data1.should_not == data3
48
+ data1.should_not == data4
49
+ end
50
+
51
+ it "invalidates the data if the encryption key is changed" do
52
+ data = create(:encryption_key => GOOD_ENCRYPTION_KEY).send(:set_session, nil, nil, OBJECT)
53
+ object = create(:encryption_key => ANOTHER_GOOD_ENCRYPTION_KEY).send(:unmarshal, data)
54
+ object.should be_nil
55
+ end
56
+
57
+ it "invalidates the data if the IV cannot be decrypted" do
58
+ store = create(:encryption_key => GOOD_ENCRYPTION_KEY)
59
+ data = store.send(:set_session, nil, nil, OBJECT)
60
+ iv_cipher = store.instance_variable_get(:@iv_cipher)
61
+ iv_cipher.should_receive(:update).and_raise(EncryptedCookieStore::EncryptedCookieStore::OpenSSLCipherError)
62
+ store.send(:unmarshal, data).should be_nil
63
+ end
64
+
65
+ # FIXME: This test case is broken. The super classes' structure have changed since Rails 3.0.0.beta3
66
+ # and we're no longer getting a string to unmarshal, since this have been pushed up to rack.
67
+ #it "invalidates the data if we just migrated from CookieStore" do
68
+ # old_store = ActionDispatch::Session::CookieStore.new(nil, :key => 'key', :secret => SECRET)
69
+ # legacy_data = old_store.send(:set_session, nil, nil, OBJECT)
70
+ # store = create(:encryption_key => GOOD_ENCRYPTION_KEY)
71
+ # store.send(:unmarshal, legacy_data).should be_nil
72
+ #end
73
+
74
+ it "invalidates the data if it was tampered with" do
75
+ store = create(:encryption_key => GOOD_ENCRYPTION_KEY)
76
+ data = store.send(:set_session, nil, nil, OBJECT)
77
+ b64_encrypted_iv, b64_encrypted_session_data = data.split("--", 2)
78
+ b64_encrypted_session_data[0..1] = "AA"
79
+ data = "#{b64_encrypted_iv}--#{b64_encrypted_session_data}"
80
+ store.send(:unmarshal, data).should be_nil
81
+ end
82
+
83
+ it "invalidates the data if it looks like garbage" do
84
+ store = create(:encryption_key => GOOD_ENCRYPTION_KEY)
85
+ garbage = "\202d\3477 jTf\274\360\200z\355\334N3\001\0036\321qLu\027\320\325*%:%\270D"
86
+ store.send(:unmarshal, garbage).should be_nil
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scottwb-encrypted_cookie_store
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
+ platform: ruby
12
+ authors:
13
+ - FooBarWidget
14
+ - Scott W. Bradley
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-05-31 00:00:00 -07:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ type: :runtime
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 3
33
+ - 0
34
+ - 0
35
+ version: 3.0.0
36
+ name: rails
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ prerelease: false
40
+ type: :development
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ name: rake
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ prerelease: false
54
+ type: :development
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 23
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 0
65
+ version: 1.0.0
66
+ name: bundler
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ prerelease: false
70
+ type: :development
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 23
77
+ segments:
78
+ - 2
79
+ - 6
80
+ - 0
81
+ version: 2.6.0
82
+ name: rspec
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ prerelease: false
86
+ type: :development
87
+ requirement: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ hash: 13
93
+ segments:
94
+ - 1
95
+ - 6
96
+ - 1
97
+ version: 1.6.1
98
+ name: jeweler
99
+ version_requirements: *id005
100
+ description: A Rails 3.0 version of Encrypted Cookie Store by FooBarWidget
101
+ email: scottwb@gmail.com
102
+ executables: []
103
+
104
+ extensions: []
105
+
106
+ extra_rdoc_files:
107
+ - LICENSE.txt
108
+ - README.md
109
+ files:
110
+ - .rspec
111
+ - Gemfile
112
+ - Gemfile.lock
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - VERSION
117
+ - lib/encrypted_cookie_store.rb
118
+ - lib/encrypted_cookie_store/constants.rb
119
+ - lib/encrypted_cookie_store/encrypted_cookie_store.rb
120
+ - lib/encrypted_cookie_store/railtie.rb
121
+ - lib/tasks/encrypted_cookie_store.rake
122
+ - scottwb-encrypted_cookie_store.gemspec
123
+ - spec/encrypted_cookie_store_spec.rb
124
+ has_rdoc: true
125
+ homepage: https://github.com/scottwb/encrypted_cookie_store
126
+ licenses: []
127
+
128
+ post_install_message:
129
+ rdoc_options: []
130
+
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: 3
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ requirements: []
152
+
153
+ rubyforge_project:
154
+ rubygems_version: 1.6.2
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: A Rails 3.0 version of Encrypted Cookie Store by FooBarWidget
158
+ test_files: []
159
+