Skip to content

Commit 6407f1d

Browse files
authoredOct 15, 2024
Merge pull request #123 from ccutrer/no-modify-argument
Don't try to modify the passed in hash
2 parents 22fce18 + 394de1b commit 6407f1d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed
 

‎lib/json/jwt.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ def initialize(claims = {})
2626
@content_type = 'application/jwt'
2727
self.typ = :JWT
2828
self.alg = :none
29+
update claims
2930
unless claims.nil?
3031
[:exp, :nbf, :iat].each do |key|
31-
claims[key] = claims[key].to_i if claims[key]
32+
self[key] = self[key].to_i if self[key]
3233
end
3334
end
34-
update claims
3535
end
3636

3737
def sign(private_key_or_secret, algorithm = :autodetect)
@@ -142,4 +142,4 @@ def pretty_generate(jwt_string)
142142
require 'json/jwk'
143143
require 'json/jwk/jwkizable'
144144
require 'json/jwk/set'
145-
require 'json/jwk/set/fetcher'
145+
require 'json/jwk/set/fetcher'

‎spec/json/jwt_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
JSON::JWT::VERSION.should_not be_blank
2424
end
2525

26+
describe '#initialize' do
27+
it "doesn't try to modify a frozen hash" do
28+
claims = { iss: 'joe', exp: '1300819380' }.freeze
29+
jwt = JSON::JWT.new(claims)
30+
expect(jwt[:exp]).to eql 1300819380
31+
expect(claims[:exp]).to eql '1300819380'
32+
end
33+
end
34+
2635
context 'when not signed nor encrypted' do
2736
it do
2837
jwt.to_s.should == no_signed

0 commit comments

Comments
 (0)