Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not raise error when verifying bad HMAC signature #563

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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