hestia 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3380572d332563a5ed975221d56004cdfc3a64d
4
+ data.tar.gz: 81129fa82814411368a4bedb26dff30adfba7176
5
+ SHA512:
6
+ metadata.gz: e8336da91bca46b66e53de4cce6ed0950095bb0786e8a524ef97cec8757422db3a4bffbd121f34ab4fa3ca38fe7a39701562dba571290765d70a1f1da4e2c206
7
+ data.tar.gz: e258c450198f9a30b3a84097b0f4fa55ab8bb85b374539c91ba8c89be2182c21fff48a2c3fbbc2559f6bd2c5a0362dae46719e7396ec7ad4615d6cdf6d5daf02
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hestia.gemspec
4
+ gemspec
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2014 FreeAgent Central
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,64 @@
1
+ # Hestia
2
+
3
+ Add support for deprecating/rotating the signed cookie secret token in rails. Out of the box if you change `config.secret_token` in rails, as soon as you deploy the change all your existing signed cookies are rendered invalid with lovely side effects such as all of your users being logged out. Thing is, it would be nice to rotate the secret token occasionally, without that side effect.
4
+
5
+ Enter hestia! You can now change your `config.secret_token`, and move the old value to `config.deprecated_secret_token` to allow existing cookies to be read in as valid cookies, but all cookies being sent out of the app are signed using the new secret token value. After a while all your users that have been active since the change will have cookies signed by the new token, and you can remove the old token from `config.deprecated_secret_token`. Hey presto, you just changed your `config.secret_token` without logging anyone out or losing any existing cookies.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "hestia", :require => "hestia/railtie"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hestia
22
+
23
+ And then require the railtie during your application boot process somewhere:
24
+
25
+ ```ruby
26
+ require "hestia/railtie"
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Rails 3.2
32
+
33
+ You should already have `Rails.application.config.secret_token` set to a value (usually in `config/initializers/secret_token.rb`). To rotate to a new value, you need to:
34
+
35
+ 1. Install hestia into your app as instructed in the "Installation" section.
36
+
37
+ 2. Update your config file so the old secret token is considered deprecated and you've set a new secret token value *(Use `rake secret` to generate one)*
38
+
39
+ Rails.application.config.secret_token = "new token (from rake secret output)"
40
+ Rails.application.config.deprecated_secret_token = "old secret token value (previously on line above)"
41
+
42
+ 3. Deploy. Your existing cookies will Just Work™, but any outgoing cookies are signed with new token.
43
+
44
+ 4. A while later (couple of weeks?), remove the `config.deprecated_secret_token` line. (Any existing cookies that haven't been sent to the webserver are now rendered invalid.)
45
+
46
+ 5. Be happy you've changed your cookie secret without logging anyone out.
47
+
48
+ *You can also set `config.deprecated_secret_token` to an array of strings to allow incoming cookies to be valid when signed with any of the secrets.*
49
+
50
+ ### Rails 4
51
+
52
+ Untested thus far. Pull requests welcome! ;-)
53
+
54
+ ### Outside rails
55
+
56
+ If you're using `ActiveSupport::MessageVerifier` anywhere and you'd like to be able to rotate the secrets, you could use `Hestia::MessageMultiVerifier` instead to gain the ability to rotate secrets. See the documentation in the class for more information about how to use it.
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/fac/hestia/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new("spec") do |t|
5
+ t.test_files = Dir.glob("spec/**/*_spec.rb")
6
+ end
7
+
8
+ task(:default => :spec)
data/TODO.md ADDED
@@ -0,0 +1,6 @@
1
+ # TODO
2
+
3
+ * Rails 4 support? Has anything changed there?
4
+
5
+ * Technically ActionDispatch::Cookie is just middleware, I wonder if we replace it entirely to do our work in subclasses.
6
+ * We'd have to get `config.deprecated_secret_token` into the env if we wrote our own middleware. `Rails::Application#env_config` could be the answer?
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hestia/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hestia"
8
+ spec.version = Hestia::VERSION
9
+ spec.authors = ["Caius Durling"]
10
+ spec.email = ["caius@freeagent.com"]
11
+ spec.summary = %{Support for deprecating/rotating signed cookie secret tokens in rails}
12
+ spec.description = %{#{spec.summary}.\nHandles silently accepting cookies signed with different secrets and sending back cookies signed with new secret.}
13
+ spec.homepage = "https://github.com/fac/hestia"
14
+ spec.license = "Apache License, Version 2.0"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 2.0'
22
+
23
+ spec.add_runtime_dependency "rack"
24
+ spec.add_runtime_dependency "actionpack", "~> 3.2", ">= 3.2.21"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.7"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
@@ -0,0 +1,5 @@
1
+ module Hestia
2
+ autoload :MessageMultiVerifier, "hestia/message_multi_verifier"
3
+ autoload :SignedCookieJarExtension, "hestia/signed_cookie_jar_extension"
4
+ autoload :VERSION, "hestia/version"
5
+ end
@@ -0,0 +1,71 @@
1
+ require "active_support/message_verifier"
2
+
3
+ module Hestia
4
+ class MessageMultiVerifier
5
+
6
+ # Public: creates a verifier for verifying against multiple secrets
7
+ #
8
+ # API compatible with ActiveSupport::MessageVerifier once created. (initializer arguments are different.)
9
+ #
10
+ # current_secret: [String] The current secret token, used for signing *and* verifying cookies
11
+ # deprecated_secrets: [String, Array] The previous secret token(s), used for verifying cookies only
12
+ # options [Hash] options to be passed to the verifiers - see ActiveSupport::MessageVerifier#initialze for details
13
+ #
14
+ def initialize(current_secret:, deprecated_secrets: [], options: {})
15
+ @current_verifier = build_verifier(current_secret, options)
16
+ @deprecated_verifiers = Array(deprecated_secrets).map { |secret| build_verifier(secret, options) }.freeze
17
+
18
+ # Generate these here so they are static when accessed in #verify
19
+ @verifiers = [current_verifier, *deprecated_verifiers].freeze
20
+
21
+ # Don't need to change any attributes, so why stay mutable?
22
+ freeze
23
+ end
24
+
25
+ # Public: generate a signed message
26
+ #
27
+ # Uses only the `current_secret` to generate this.
28
+ #
29
+ # See ActiveSupport::MessageVerifier#generate for more information
30
+ #
31
+ # Returns String
32
+ def generate(value)
33
+ current_verifier.generate(value)
34
+ end
35
+
36
+ # Public: verify a signed message and convert back to original form
37
+ #
38
+ # See ActiveSupport::MessageVerifier#verify for more information
39
+ #
40
+ # Returns deserialized value
41
+ # Raises ActiveSupport::MessageVerifier::InvalidSignature
42
+ def verify(signed_message)
43
+ errored_verifier_count = 0
44
+
45
+ # Make sure we check *all* verifiers, every time we're called, to prevent timing attacks.
46
+ # We run in a consistent amount of time no matter which (or if any) verifiers return a valid result
47
+ results = verifiers.each_with_object([]) do |verifier, values|
48
+ begin
49
+ values << verifier.verify(signed_message)
50
+ rescue ActiveSupport::MessageVerifier::InvalidSignature
51
+ errored_verifier_count += 1
52
+ end
53
+ end
54
+
55
+ # If all the verifiers errored, then none of them thought the message was valid
56
+ if errored_verifier_count == verifiers.size
57
+ raise(ActiveSupport::MessageVerifier::InvalidSignature)
58
+ else
59
+ results.first
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :current_verifier, :deprecated_verifiers, :verifiers
66
+
67
+ def build_verifier(secret, options)
68
+ ActiveSupport::MessageVerifier.new(secret, options)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,13 @@
1
+ require "hestia"
2
+
3
+ module Hestia
4
+ class Railtie < ::Rails::Railtie
5
+ # Hooks into ActionDispatch::Session::SignedCookieJar to allow rotating secret tokens in signed cookies.
6
+ #
7
+ # See README.md for how to configure this in your application.
8
+ #
9
+ initializer "hestia.signed_cookie_jar_extension", before: :load_config_initializers do
10
+ ActionDispatch::Cookies::SignedCookieJar.prepend(Hestia::SignedCookieJarExtension)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module Hestia
2
+ module SignedCookieJarExtension
3
+ # Public: overridden #initialize method
4
+ #
5
+ # In rails, `secrets' will be given the value of `Rails.application.config.secret_token'. That's the current secret token.
6
+ # This also reads from `Rails.application.config.deprecated_secret_token` for deprecated token(s) to use. It can be undefined, a
7
+ # string or an array of string.
8
+ #
9
+ # parent_jar [ActionDispatch::Cookies] the parent jar creating this signed cookie jar
10
+ # secret [String] current secret token. Used to verify & sign cookies.
11
+ #
12
+ def initialize(parent_jar, secret)
13
+ super
14
+
15
+ # Find the deprecated secrets, if there are any
16
+ deprecated_secrets = if Rails.application.config.respond_to?(:deprecated_secret_token)
17
+ # This could be a single string!
18
+ Array(Rails.application.config.deprecated_secret_token)
19
+ else
20
+ []
21
+ end
22
+
23
+ # Ensure all the deprecated secret tokens are considered secure (__original_initalize__ checked the current secret for this)
24
+ deprecated_secrets.each { |secret| ensure_secret_secure(secret) }
25
+
26
+ # Finally, override @verifier with our own multi verifier containing all the secrets
27
+ @verifier = Hestia::MessageMultiVerifier.new(current_secret: secret, deprecated_secrets: deprecated_secrets)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Hestia
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,136 @@
1
+ require_relative "../spec_helper"
2
+
3
+ module Hestia
4
+ describe MessageMultiVerifier do
5
+ InvalidSignature = ActiveSupport::MessageVerifier::InvalidSignature
6
+
7
+ let(:message_verifier) { ActiveSupport::MessageVerifier.new("secret") }
8
+ let(:singular_verifier) { MessageMultiVerifier.new(current_secret: "secret") }
9
+ let(:multi_verifier) { MessageMultiVerifier.new(current_secret: "secret", deprecated_secrets: %w(previous_secret)) }
10
+
11
+ describe "creation" do
12
+ it "requires a current secret" do
13
+ -> { MessageMultiVerifier.new }.must_raise(ArgumentError, /current_secret/)
14
+ end
15
+
16
+ it "can be created with current & deprecated secrets" do
17
+ MessageMultiVerifier.new(current_secret: "secret", deprecated_secrets: %w(previous_secret legacy_secret))
18
+ end
19
+
20
+ it "can be created with current secret & options only" do
21
+ MessageMultiVerifier.new(current_secret: "secret", options: {digest: "MD5"})
22
+ end
23
+
24
+ it "can be created with array of secrets and options for the verifiers" do
25
+ MessageMultiVerifier.new(current_secret: "secret", deprecated_secrets: %w(previous_secret), options: {digest: "MD5"})
26
+ end
27
+
28
+ it "is immutable after creation" do
29
+ multi_verifier.must_be :frozen?
30
+ end
31
+ end
32
+
33
+ describe "generating messages with one secret set" do
34
+ it "is generated using first secret" do
35
+ singular_verifier.generate("cookie dough").must_equal message_verifier.generate("cookie dough")
36
+ end
37
+ end
38
+
39
+ describe "generating messages with multiple secrets set" do
40
+ it "is generated using first secret" do
41
+ multi_verifier.generate("cookie dough").must_equal message_verifier.generate("cookie dough")
42
+ end
43
+ end
44
+
45
+ describe "verify behaviour" do
46
+ let(:legacy_message_verifier) { ActiveSupport::MessageVerifier.new("previous_secret") }
47
+
48
+ let(:current_cookie) { message_verifier.generate("cookie dough") }
49
+ let(:legacy_cookie) { legacy_message_verifier.generate("cookie dough") }
50
+
51
+ it "errors when given blank message" do
52
+ -> { multi_verifier.verify(nil) }.must_raise(InvalidSignature)
53
+ -> { singular_verifier.verify("") }.must_raise(InvalidSignature)
54
+
55
+ -> { multi_verifier.verify(nil) }.must_raise(InvalidSignature)
56
+ -> { multi_verifier.verify("") }.must_raise(InvalidSignature)
57
+ end
58
+
59
+ it "errors without -- delimited data/digest" do
60
+ invalid_cookie = current_cookie.gsub("--", "-")
61
+ -> { singular_verifier.verify(invalid_cookie) }.must_raise(InvalidSignature)
62
+ -> { multi_verifier.verify(invalid_cookie) }.must_raise(InvalidSignature)
63
+ end
64
+
65
+ it "errors without data being present" do
66
+ invalid_cookie = current_cookie.gsub(/\A.+--/, "")
67
+ -> { singular_verifier.verify(invalid_cookie) }.must_raise(InvalidSignature)
68
+ -> { multi_verifier.verify(invalid_cookie) }.must_raise(InvalidSignature)
69
+ end
70
+
71
+ it "errors without digest being present" do
72
+ invalid_cookie = current_cookie.gsub(/--.+\z/, "")
73
+ -> { singular_verifier.verify(invalid_cookie) }.must_raise(InvalidSignature)
74
+ -> { multi_verifier.verify(invalid_cookie) }.must_raise(InvalidSignature)
75
+ end
76
+
77
+ it "errors without passed in digest matching computed digest" do
78
+ invalid_cookie = current_cookie.gsub(/^.+--/, "lol_error_not_a_digest_zomg")
79
+ -> { singular_verifier.verify(invalid_cookie) }.must_raise(InvalidSignature)
80
+ -> { multi_verifier.verify(invalid_cookie) }.must_raise(InvalidSignature)
81
+ end
82
+
83
+ it "errors with cookie signed using unknown secret" do
84
+ -> { singular_verifier.verify(legacy_cookie) }.must_raise(InvalidSignature)
85
+ end
86
+
87
+ it "verifies successfully with correct payload for any valid secret" do
88
+ singular_verifier.verify(current_cookie).must_equal "cookie dough"
89
+
90
+ multi_verifier.verify(current_cookie).must_equal "cookie dough"
91
+ multi_verifier.verify(legacy_cookie).must_equal "cookie dough"
92
+ end
93
+
94
+ it "verifies a message of `nil' successfully" do
95
+ nil_cookie = singular_verifier.generate(nil)
96
+
97
+ singular_verifier.verify(nil_cookie).must_equal(nil)
98
+ multi_verifier.verify(nil_cookie).must_equal(nil)
99
+ end
100
+
101
+ it "verifies successfully when using custom digest" do
102
+ multi_singular_secret = MessageMultiVerifier.new(current_secret: "secret", options: {digest: "MD5"})
103
+ multi_multiple_secret = MessageMultiVerifier.new(current_secret: "secret", deprecated_secrets: %w(previous_secret), options: {digest: "MD5"})
104
+
105
+ current_cookie = ActiveSupport::MessageVerifier.new("secret", :digest => "MD5").generate("cookie dough")
106
+ previous_cookie = ActiveSupport::MessageVerifier.new("previous_secret", :digest => "MD5").generate("cookie dough")
107
+
108
+ multi_singular_secret.verify(current_cookie).must_equal "cookie dough"
109
+ multi_multiple_secret.verify(current_cookie).must_equal "cookie dough"
110
+ multi_multiple_secret.verify(previous_cookie).must_equal "cookie dough"
111
+ end
112
+
113
+ it "verifies successfully when using custom serializer" do
114
+ reverser = Module.new do
115
+ extend self
116
+
117
+ def dump(string)
118
+ string.reverse
119
+ end
120
+
121
+ alias_method :load, :dump
122
+ end
123
+
124
+ multi_singular_secret = MessageMultiVerifier.new(current_secret: "secret", options: {serializer: reverser})
125
+ multi_multiple_secret = MessageMultiVerifier.new(current_secret: "secret", deprecated_secrets: %w(previous_secret), options: {serializer: reverser})
126
+
127
+ current_cookie = ActiveSupport::MessageVerifier.new("secret", :serializer => reverser).generate("cookie dough")
128
+ previous_cookie = ActiveSupport::MessageVerifier.new("previous_secret", :serializer => reverser).generate("cookie dough")
129
+
130
+ multi_singular_secret.verify(current_cookie).must_equal "cookie dough"
131
+ multi_multiple_secret.verify(current_cookie).must_equal "cookie dough"
132
+ multi_multiple_secret.verify(previous_cookie).must_equal "cookie dough"
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,31 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../support/fake_rails"
3
+
4
+ require "hestia/railtie"
5
+
6
+ module Hestia
7
+ describe Railtie do
8
+ before do
9
+ @name, @arguments, @block = Rails::Railtie.initializers.first
10
+ end
11
+
12
+ describe "initializer" do
13
+ it "adds an initializer" do
14
+ Rails::Railtie.initializers.wont_be_empty
15
+ end
16
+
17
+ it "has a name" do
18
+ @name.must_equal "hestia.signed_cookie_jar_extension"
19
+ end
20
+
21
+ it "is run before config initializers are loaded" do
22
+ @arguments[:before].must_equal :load_config_initializers
23
+ end
24
+
25
+ it "has a block to do some work" do
26
+ # The block itself is tested in the SignedCookieJarExtension spec
27
+ refute_nil @block
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,88 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../support/fake_rails"
3
+
4
+ # Call our railtie block to setup the initializers array
5
+ require "hestia/railtie"
6
+
7
+ module Hestia
8
+ describe SignedCookieJarExtension do
9
+ before do
10
+ Rails.clean
11
+ load_railtie
12
+ end
13
+
14
+ it "is prepended into signed cookie jar ancestors" do
15
+ ActionDispatch::Cookies::SignedCookieJar.ancestors.first.must_equal SignedCookieJarExtension
16
+ end
17
+
18
+ it "defines initialize" do
19
+ # #initialize doesn't show up in {instance_,}methods({false,true}) for some reason, so do this instead
20
+ # This will throw a NameError if we don't define it
21
+ SignedCookieJarExtension.instance_method(:initialize)
22
+ end
23
+
24
+ describe "signed cookie jar instance with no deprecated token" do
25
+ before do
26
+ @parent_jar = Object.new
27
+ @secret = "a" * 30
28
+ @jar = ActionDispatch::Cookies::SignedCookieJar.new(@parent_jar, @secret)
29
+ end
30
+
31
+ it "calls the original initialize method" do
32
+ @jar.instance_variable_get(:@parent_jar).must_equal @parent_jar
33
+ end
34
+
35
+ describe "validator" do
36
+ before do
37
+ @verifier = @jar.instance_variable_get(:@verifier)
38
+ end
39
+ it "is a multi message validator" do
40
+ @verifier.must_be_kind_of(MessageMultiVerifier)
41
+ end
42
+
43
+ it "has the correct secrets stored" do
44
+ secrets = @verifier.instance_variable_get(:@verifiers).map { |x| x.instance_variable_get(:@secret) }
45
+ secrets.must_equal [@secret]
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "signed cookie jar instance with deprecated token" do
51
+ before do
52
+ @parent_jar = Object.new
53
+ @secret = "a" * 30
54
+ @deprecated_secret = "b" * 30
55
+ Rails.application.config[:deprecated_secret_token] = @deprecated_secret
56
+ @jar = ActionDispatch::Cookies::SignedCookieJar.new(@parent_jar, @secret)
57
+ end
58
+
59
+ it "calls the original initialize method" do
60
+ @jar.instance_variable_get(:@parent_jar).must_equal @parent_jar
61
+ end
62
+
63
+ describe "validator" do
64
+ before do
65
+ @verifier = @jar.instance_variable_get(:@verifier)
66
+ end
67
+ it "is a multi message validator" do
68
+ @verifier.must_be_kind_of(MessageMultiVerifier)
69
+ end
70
+
71
+ it "has the correct secrets stored" do
72
+ secrets = @verifier.instance_variable_get(:@verifiers).map { |x| x.instance_variable_get(:@secret) }
73
+ secrets.must_equal [@secret, @deprecated_secret]
74
+ end
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def load_railtie
81
+ if (init = Rails::Railtie.initializers.first)
82
+ _, _, block = init
83
+ block.call
84
+ end
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ require "minitest/autorun"
2
+ require "minitest/pride"
3
+ require "minitest/spec"
4
+
5
+ require "hestia"
@@ -0,0 +1,30 @@
1
+ require "rack"
2
+ require "action_dispatch/middleware/cookies"
3
+
4
+ # Guard in case we're accidentally loaded when rails is
5
+ unless defined?(Rails)
6
+
7
+ # Fake out rails for testing Hestia::Railtie
8
+ class Rails
9
+ def self.clean
10
+ # Reset everything
11
+ @application = nil
12
+ end
13
+
14
+ def self.application
15
+ @application ||= OpenStruct.new(:config => OpenStruct.new)
16
+ end
17
+
18
+ # Hestia::Railtie will subclass this
19
+ class Railtie
20
+ def self.initializers
21
+ # Class variable to share with all subclasses
22
+ @@initializers ||= []
23
+ end
24
+
25
+ def self.initializer(*args, &block)
26
+ initializers << (args + [block])
27
+ end
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hestia
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Caius Durling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-12 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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.2'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 3.2.21
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '3.2'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.2.21
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.7'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.7'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ description: |-
76
+ Support for deprecating/rotating signed cookie secret tokens in rails.
77
+ Handles silently accepting cookies signed with different secrets and sending back cookies signed with new secret.
78
+ email:
79
+ - caius@freeagent.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - ".gitignore"
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - TODO.md
90
+ - hestia.gemspec
91
+ - lib/hestia.rb
92
+ - lib/hestia/message_multi_verifier.rb
93
+ - lib/hestia/railtie.rb
94
+ - lib/hestia/signed_cookie_jar_extension.rb
95
+ - lib/hestia/version.rb
96
+ - spec/hestia/message_multi_verifier_spec.rb
97
+ - spec/hestia/railtie_spec.rb
98
+ - spec/hestia/signed_cookie_jar_extension_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/support/fake_rails.rb
101
+ homepage: https://github.com/fac/hestia
102
+ licenses:
103
+ - Apache License, Version 2.0
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '2.0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.4.6
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Support for deprecating/rotating signed cookie secret tokens in rails
125
+ test_files:
126
+ - spec/hestia/message_multi_verifier_spec.rb
127
+ - spec/hestia/railtie_spec.rb
128
+ - spec/hestia/signed_cookie_jar_extension_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/support/fake_rails.rb