-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise DecodeError on empty hmac_secret addressing OpenSSL 3.0 issue; F…
…ixes #526
- Loading branch information
Showing
3 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ::JWT::Algos::Hmac do | ||
describe '.sign' do | ||
subject { described_class.sign('HS256', 'test', hmac_secret) } | ||
|
||
# Address OpenSSL 3.0 errors with empty hmac_secret - https://github.com/jwt/ruby-jwt/issues/526 | ||
context 'when nil hmac_secret is passed' do | ||
let(:hmac_secret) { nil } | ||
context 'when OpenSSL 3.0 raises a malloc failure' do | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('EVP_PKEY_new_mac_key: malloc failure')) | ||
end | ||
|
||
it 'raises JWT::DecodeError' do | ||
expect { subject }.to raise_error(JWT::DecodeError, 'OpenSSL 3.0 does not support nil or empty hmac_secret') | ||
end | ||
end | ||
|
||
context 'when OpenSSL raises any other error' do | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('Another Random Error')) | ||
end | ||
|
||
it 'raises the original error' do | ||
expect { subject }.to raise_error(OpenSSL::HMACError, 'Another Random Error') | ||
end | ||
end | ||
|
||
context 'when other versions of openssl do not raise an exception' do | ||
let(:response) { Base64.decode64("Q7DO+ZJl+eNMEOqdNQGSbSezn1fG1nRWHYuiNueoGfs=\n") } | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_return(response) | ||
end | ||
|
||
it { is_expected.to eql(response) } | ||
end | ||
end | ||
|
||
context 'when blank hmac_secret is passed' do | ||
let(:hmac_secret) { '' } | ||
context 'when OpenSSL 3.0 raises a malloc failure' do | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('EVP_PKEY_new_mac_key: malloc failure')) | ||
end | ||
|
||
it 'raises JWT::DecodeError' do | ||
expect { subject }.to raise_error(JWT::DecodeError, 'OpenSSL 3.0 does not support nil or empty hmac_secret') | ||
end | ||
end | ||
|
||
context 'when OpenSSL raises any other error' do | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('Another Random Error')) | ||
end | ||
|
||
it 'raises the original error' do | ||
expect { subject }.to raise_error(OpenSSL::HMACError, 'Another Random Error') | ||
end | ||
end | ||
|
||
context 'when other versions of openssl do not raise an exception' do | ||
let(:response) { Base64.decode64("Q7DO+ZJl+eNMEOqdNQGSbSezn1fG1nRWHYuiNueoGfs=\n") } | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_return(response) | ||
end | ||
|
||
it { is_expected.to eql(response) } | ||
end | ||
end | ||
|
||
context 'when hmac_secret is passed' do | ||
let(:hmac_secret) { 'test' } | ||
context 'when OpenSSL 3.0 raises a malloc failure' do | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('EVP_PKEY_new_mac_key: malloc failure')) | ||
end | ||
|
||
it 'raises the original error' do | ||
expect { subject }.to raise_error(OpenSSL::HMACError, 'EVP_PKEY_new_mac_key: malloc failure') | ||
end | ||
end | ||
|
||
context 'when OpenSSL raises any other error' do | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('Another Random Error')) | ||
end | ||
|
||
it 'raises the original error' do | ||
expect { subject }.to raise_error(OpenSSL::HMACError, 'Another Random Error') | ||
end | ||
end | ||
|
||
context 'when other versions of openssl do not raise an exception' do | ||
let(:response) { Base64.decode64("iM0hCLU0fZc885zfkFPX3UJwSHbYyam9ji0WglnT3fc=\n") } | ||
before do | ||
allow(OpenSSL::HMAC).to receive(:digest).and_return(response) | ||
end | ||
|
||
it { is_expected.to eql(response) } | ||
end | ||
end | ||
end | ||
end |