Skip to content

Commit

Permalink
Do not raise error when verifying bad HMAC signature
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuk09 authored and anakinj committed Jun 9, 2023
1 parent 62f5fdb commit 7781a97
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
**Fixes and enhancements:**

- Handle invalid algorithm when decoding JWT [#559](https://github.com/jwt/ruby-jwt/pull/559) - [@nataliastanko](https://github.com/nataliastanko)
- Do not raise error when verifying bad HMAC signature [#563](https://github.com/jwt/ruby-jwt/pull/563) - [@hieuk09](https://github.com/hieuk09)
- Your contribution here

## [v2.7.0](https://github.com/jwt/ruby-jwt/tree/v2.7.0) (2023-02-01)
Expand Down
2 changes: 1 addition & 1 deletion lib/jwt/algos/hmac_rbnacl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def verify(algorithm, key, signing_input, signature)
else
Hmac.verify(algorithm, key, signing_input, signature)
end
rescue ::RbNaCl::BadAuthenticatorError
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/jwt/algos/hmac_rbnacl_fixed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def verify(algorithm, key, signing_input, signature)
else
Hmac.verify(algorithm, key, signing_input, signature)
end
rescue ::RbNaCl::BadAuthenticatorError
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
false
end

Expand Down
11 changes: 11 additions & 0 deletions spec/jwt/algos/hmac_rbnacl_fixed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
expect(OpenSSL::HMAC).to have_received(:digest).once
end
end

context 'when signature is invalid' do
let(:key) { 'a' * 100 }
let(:signature) { JWT::Base64.url_decode('some_random_signature') }

it 'can verify without error' do
allow(OpenSSL::HMAC).to receive(:digest).and_call_original
expect(described_class.verify('HS256', key, data, signature)).to eq(false)
expect(OpenSSL::HMAC).not_to have_received(:digest)
end
end
end

describe '.sign' do
Expand Down
11 changes: 11 additions & 0 deletions spec/jwt/algos/hmac_rbnacl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
expect(OpenSSL::HMAC).not_to have_received(:digest)
end
end

context 'when signature is invalid' do
let(:key) { 'a' * 100 }
let(:signature) { JWT::Base64.url_decode('some_random_signature') }

it 'can verify without error' do
allow(OpenSSL::HMAC).to receive(:digest).and_call_original
expect(described_class.verify('HS256', key, data, signature)).to eq(false)
expect(OpenSSL::HMAC).not_to have_received(:digest)
end
end
end

describe '.sign' do
Expand Down

0 comments on commit 7781a97

Please sign in to comment.