Skip to content

Commit 7781a97

Browse files
hieuk09anakinj
authored andcommitted
Do not raise error when verifying bad HMAC signature
1 parent 62f5fdb commit 7781a97

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
**Fixes and enhancements:**
1414

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

1819
## [v2.7.0](https://github.com/jwt/ruby-jwt/tree/v2.7.0) (2023-02-01)

lib/jwt/algos/hmac_rbnacl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def verify(algorithm, key, signing_input, signature)
2828
else
2929
Hmac.verify(algorithm, key, signing_input, signature)
3030
end
31-
rescue ::RbNaCl::BadAuthenticatorError
31+
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
3232
false
3333
end
3434

lib/jwt/algos/hmac_rbnacl_fixed.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def verify(algorithm, key, signing_input, signature)
3636
else
3737
Hmac.verify(algorithm, key, signing_input, signature)
3838
end
39-
rescue ::RbNaCl::BadAuthenticatorError
39+
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
4040
false
4141
end
4242

spec/jwt/algos/hmac_rbnacl_fixed_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
expect(OpenSSL::HMAC).to have_received(:digest).once
3131
end
3232
end
33+
34+
context 'when signature is invalid' do
35+
let(:key) { 'a' * 100 }
36+
let(:signature) { JWT::Base64.url_decode('some_random_signature') }
37+
38+
it 'can verify without error' do
39+
allow(OpenSSL::HMAC).to receive(:digest).and_call_original
40+
expect(described_class.verify('HS256', key, data, signature)).to eq(false)
41+
expect(OpenSSL::HMAC).not_to have_received(:digest)
42+
end
43+
end
3344
end
3445

3546
describe '.sign' do

spec/jwt/algos/hmac_rbnacl_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
expect(OpenSSL::HMAC).not_to have_received(:digest)
3131
end
3232
end
33+
34+
context 'when signature is invalid' do
35+
let(:key) { 'a' * 100 }
36+
let(:signature) { JWT::Base64.url_decode('some_random_signature') }
37+
38+
it 'can verify without error' do
39+
allow(OpenSSL::HMAC).to receive(:digest).and_call_original
40+
expect(described_class.verify('HS256', key, data, signature)).to eq(false)
41+
expect(OpenSSL::HMAC).not_to have_received(:digest)
42+
end
43+
end
3344
end
3445

3546
describe '.sign' do

0 commit comments

Comments
 (0)