|
12 | 12 | it { is_expected.to eq(valid_signature) } |
13 | 13 | end |
14 | 14 |
|
15 | | - # Address OpenSSL 3.0 errors with empty hmac_secret - https://github.com/jwt/ruby-jwt/issues/526 |
| 15 | + # GHSA-c32j-vqhx-rx3x: empty/nil keys must be rejected before reaching OpenSSL, |
| 16 | + # so a forged token signed with "" cannot verify. |
16 | 17 | context 'when nil hmac_secret is passed' do |
17 | 18 | let(:hmac_secret) { nil } |
18 | | - context 'when OpenSSL 3.0 raises a malloc failure' do |
19 | | - before do |
20 | | - allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('EVP_PKEY_new_mac_key: malloc failure')) |
21 | | - end |
22 | | - |
23 | | - it 'raises JWT::DecodeError' do |
24 | | - expect { subject }.to raise_error(JWT::DecodeError, 'OpenSSL 3.0 does not support nil or empty hmac_secret') |
25 | | - end |
26 | | - end |
27 | 19 |
|
28 | | - context 'when OpenSSL raises any other error' do |
29 | | - before do |
30 | | - allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('Another Random Error')) |
31 | | - end |
32 | | - |
33 | | - it 'raises the original error' do |
34 | | - expect { subject }.to raise_error(OpenSSL::HMACError, 'Another Random Error') |
35 | | - end |
| 20 | + it 'raises JWT::DecodeError' do |
| 21 | + expect { subject }.to raise_error(JWT::DecodeError, 'HMAC key expected to be a String') |
36 | 22 | end |
37 | 23 |
|
38 | | - context 'when other versions of openssl do not raise an exception' do |
39 | | - let(:response) { Base64.decode64("Q7DO+ZJl+eNMEOqdNQGSbSezn1fG1nRWHYuiNueoGfs=\n") } |
40 | | - before do |
41 | | - allow(OpenSSL::HMAC).to receive(:digest).and_return(response) |
42 | | - end |
43 | | - |
44 | | - it { is_expected.to eql(response) } |
| 24 | + it 'does not call OpenSSL::HMAC.digest' do |
| 25 | + expect(OpenSSL::HMAC).not_to receive(:digest) |
| 26 | + expect { subject }.to raise_error(JWT::DecodeError) |
45 | 27 | end |
46 | 28 | end |
47 | 29 |
|
48 | 30 | context 'when blank hmac_secret is passed' do |
49 | 31 | let(:hmac_secret) { '' } |
50 | | - context 'when OpenSSL 3.0 raises a malloc failure' do |
51 | | - before do |
52 | | - allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('EVP_PKEY_new_mac_key: malloc failure')) |
53 | | - end |
54 | | - |
55 | | - it 'raises JWT::DecodeError' do |
56 | | - expect { subject }.to raise_error(JWT::DecodeError, 'OpenSSL 3.0 does not support nil or empty hmac_secret') |
57 | | - end |
58 | | - end |
59 | 32 |
|
60 | | - context 'when OpenSSL raises any other error' do |
61 | | - before do |
62 | | - allow(OpenSSL::HMAC).to receive(:digest).and_raise(OpenSSL::HMACError.new('Another Random Error')) |
63 | | - end |
64 | | - |
65 | | - it 'raises the original error' do |
66 | | - expect { subject }.to raise_error(OpenSSL::HMACError, 'Another Random Error') |
67 | | - end |
| 33 | + it 'raises JWT::DecodeError' do |
| 34 | + expect { subject }.to raise_error(JWT::DecodeError, 'HMAC key cannot be empty') |
68 | 35 | end |
69 | 36 |
|
70 | | - context 'when other versions of openssl do not raise an exception' do |
71 | | - let(:response) { Base64.decode64("Q7DO+ZJl+eNMEOqdNQGSbSezn1fG1nRWHYuiNueoGfs=\n") } |
72 | | - before do |
73 | | - allow(OpenSSL::HMAC).to receive(:digest).and_return(response) |
74 | | - end |
75 | | - |
76 | | - it { is_expected.to eql(response) } |
| 37 | + it 'does not call OpenSSL::HMAC.digest' do |
| 38 | + expect(OpenSSL::HMAC).not_to receive(:digest) |
| 39 | + expect { subject }.to raise_error(JWT::DecodeError) |
77 | 40 | end |
78 | 41 | end |
79 | 42 |
|
|
160 | 123 | end |
161 | 124 | end |
162 | 125 |
|
| 126 | + # GHSA-c32j-vqhx-rx3x |
| 127 | + context 'when verification_key is nil' do |
| 128 | + let(:signature) { valid_signature } |
| 129 | + let(:hmac_secret) { nil } |
| 130 | + |
| 131 | + it 'raises error and does not call OpenSSL::HMAC.digest' do |
| 132 | + expect(OpenSSL::HMAC).not_to receive(:digest) |
| 133 | + expect { subject }.to raise_error(JWT::DecodeError, 'HMAC key expected to be a String') |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + context 'when verification_key is empty' do |
| 138 | + let(:signature) { valid_signature } |
| 139 | + let(:hmac_secret) { '' } |
| 140 | + |
| 141 | + it 'raises error and does not call OpenSSL::HMAC.digest' do |
| 142 | + expect(OpenSSL::HMAC).not_to receive(:digest) |
| 143 | + expect { subject }.to raise_error(JWT::DecodeError, 'HMAC key cannot be empty') |
| 144 | + end |
| 145 | + end |
| 146 | + |
163 | 147 | context 'when enforce_hmac_key_length is enabled' do |
164 | 148 | before do |
165 | 149 | JWT.configuration.decode.enforce_hmac_key_length = true |
|
0 commit comments