Skip to content

Commit 9a30b4e

Browse files
committed
Allow alg header to be given
1 parent 29633b1 commit 9a30b4e

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Take a look at the [upgrade guide](UPGRADING.md) for more details.
1515

1616
**Features:**
1717
- JWT::EncodedToken#verify! method that bundles signature and claim validation [#647](https://github.com/jwt/ruby-jwt/pull/647) ([@anakinj](https://github.com/anakinj))
18+
- Do not override the alg header if already given [#659](https://github.com/jwt/ruby-jwt/pull/659) ([@anakinj](https://github.com/anakinj))
1819
- Your contribution here
1920

2021
**Fixes and enhancements:**

lib/jwt/token.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def sign!(algorithm:, key:)
9595
raise ::JWT::EncodeError, 'Token already signed' if @signature
9696

9797
JWA.resolve(algorithm).tap do |algo|
98-
header.merge!(algo.header)
98+
header.merge!(algo.header) { |_key, old, _new| old }
9999
@signature = algo.sign(data: signing_input, signing_key: key)
100100
end
101101

spec/jwt/encoded_token_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@
103103
end
104104
end
105105

106+
context 'when header has invalid alg value' do
107+
let(:header) { { 'alg' => 'HS123' } }
108+
109+
it 'does not raise' do
110+
expect(token.header).to eq(header)
111+
expect(token.verify_signature!(algorithm: 'HS256', key: 'secret')).to eq(nil)
112+
end
113+
end
114+
106115
context 'when payload is detached' do
107116
let(:encoded_token) { detached_payload_token.jwt }
108117

spec/jwt/jwt_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@
586586
end
587587

588588
context 'when the alg value is given as a header parameter' do
589-
it 'does not override the actual algorithm used' do
589+
it 'overrides the actual algorithm used' do
590590
headers = JSON.parse(JWT::Base64.url_decode(JWT.encode('Hello World', 'secret', 'HS256', { alg: 'HS123' }).split('.').first))
591-
expect(headers['alg']).to eq('HS256')
591+
expect(headers['alg']).to eq('HS123')
592592
end
593593

594594
it 'should generate the same token' do

spec/jwt/token_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@
4141
expect { token.jwt }.to raise_error(JWT::EncodeError)
4242
end
4343
end
44+
45+
context 'when alg is given in header' do
46+
let(:header) { { 'alg' => 'HS123' } }
47+
48+
before do
49+
token.sign!(algorithm: 'HS256', key: 'secret')
50+
end
51+
52+
it 'returns a signed and encoded token' do
53+
expect(JWT::EncodedToken.new(token.jwt).header).to eq({ 'alg' => 'HS123' })
54+
end
55+
end
4456
end
4557

4658
describe '#detach_payload!' do

0 commit comments

Comments
 (0)