Skip to content

Commit f193c81

Browse files
committed
Rework payload handling.
1 parent 38ea47d commit f193c81

File tree

16 files changed

+619
-482
lines changed

16 files changed

+619
-482
lines changed

.github/workflows/test.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ jobs:
2121
- macos
2222

2323
ruby:
24-
- "2.5"
25-
- "2.6"
26-
- "2.7"
27-
- "3.0"
2824
- "3.1"
2925
- "3.2"
3026
- "3.3"

config/sus.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
TEST_PATTERN = "sus/**/*.rb"
7+
8+
def test_paths
9+
return Dir.glob(TEST_PATTERN, base: @root)
10+
end

gems.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
gemspec
99

10+
gem "rake"
11+
1012
group :maintenance, optional: true do
1113
if RUBY_VERSION > "3.1"
1214
gem "bake"
@@ -24,6 +26,12 @@
2426
end
2527

2628
group :test do
29+
gem "sus"
30+
2731
gem "bake-test"
2832
gem "bake-test-external"
33+
34+
gem "minitest", "~> 5.0"
35+
gem "minitest-global_expectations"
36+
gem "minitest-sprint"
2937
end

lib/rack/session/abstract/id.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
require_relative '../constants'
1616

1717
module Rack
18-
1918
module Session
20-
2119
class SessionId
2220
ID_VERSION = 2
2321

lib/rack/session/cookie.rb

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,26 +156,30 @@ def decode(str)
156156

157157
attr_reader :coder, :encryptors
158158

159-
def initialize(app, options = {})
160-
# support both :secrets and :secret for backwards compatibility
161-
secrets = [*(options[:secrets] || options[:secret])]
159+
def initialize(app, coder: Marshal, serialize_json: false, key: nil, purpose: nil, secrets: [], secret: nil, **options)
160+
# Support both :secrets and :secret for backwards compatibility:
161+
if secret
162+
secrets << secret
163+
end
164+
165+
# `serialize_json` is awefully specific... allow a general `coder` option:
166+
if serialize_json
167+
coder ||= JSON
168+
end
162169

163-
encryptor_opts = {
164-
purpose: options[:key], serialize_json: options[:serialize_json]
165-
}
170+
# Let's consider `key` to be legacy:
171+
purpose ||= key
166172

167-
# For each secret, create an Encryptor. We have iterate this Array at
168-
# decryption time to achieve key rotation.
173+
# For each secret, create an Encryptor, to support key rotation:
169174
@encryptors = secrets.map do |secret|
170-
Rack::Session::Encryptor.new secret, encryptor_opts
175+
Rack::Session::Encryptor.new(secret, delegate: coder, purpose: purpose)
171176
end
172177

173-
# If a legacy HMAC secret is present, initialize those features.
174-
# Fallback to :secret for backwards compatibility.
175-
if options.has_key?(:legacy_hmac_secret) || options.has_key?(:secret)
178+
# If a legacy HMAC secret is present, initialize those features:
179+
if options.has_key?(:legacy_hmac_secret) || secret
176180
@legacy_hmac = options.fetch(:legacy_hmac, 'SHA1')
177181

178-
@legacy_hmac_secret = options[:legacy_hmac_secret] || options[:secret]
182+
@legacy_hmac_secret = options[:legacy_hmac_secret] || secret
179183
@legacy_hmac_coder = options.fetch(:legacy_hmac_coder, Base64::Marshal.new)
180184
else
181185
@legacy_hmac = false
@@ -216,7 +220,7 @@ def unpacked_cookie_data(request)
216220
session_data = nil
217221

218222
# Try to decrypt the session data with our encryptors
219-
encryptors.each do |encryptor|
223+
@encryptors.each do |encryptor|
220224
begin
221225
session_data = encryptor.decrypt(cookie_data)
222226
break
@@ -290,10 +294,10 @@ def legacy_generate_hmac(data)
290294
end
291295

292296
def encode_session_data(session)
293-
if encryptors.empty?
297+
if @encryptors.empty?
294298
coder.encode(session)
295299
else
296-
encryptors.first.encrypt(session)
300+
@encryptors.first.encrypt(session)
297301
end
298302
end
299303

0 commit comments

Comments
 (0)