Skip to content

Commit 37926ea

Browse files
committed
Throw if key is an PKCS1 PEM-encoded public key
1 parent e4c67b1 commit 37926ea

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

jwt/algorithms.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def prepare_key(self, key):
142142
invalid_strings = [
143143
b'-----BEGIN PUBLIC KEY-----',
144144
b'-----BEGIN CERTIFICATE-----',
145+
b'-----BEGIN RSA PUBLIC KEY-----',
145146
b'ssh-rsa'
146147
]
147148

tests/keys/testkey_pkcs1.pub.pem

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN RSA PUBLIC KEY-----
2+
MIGHAoGBAOV/0Vl/5VdHcYpnILYzBGWo5JQVzo9wBkbxzjAStcAnTwvv1ZJTMXs6
3+
fjz91f9hiMM4Z/5qNTE/EHlDWxVdj1pyRaQulZPUs0r9qJ02ogRRGLG3jjrzzbzF
4+
yj/pdNBwym0UJYC/Jmn/kMLwGiWI2nfa9vM5SovqZiAy2FD7eOtVAgED
5+
-----END RSA PUBLIC KEY-----

tests/test_algorithms.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ def test_hmac_should_throw_exception_if_key_is_x509_cert(self):
9797
with open(key_path('testkey2_rsa.pub.pem'), 'r') as keyfile:
9898
algo.prepare_key(keyfile.read())
9999

100+
def test_hmac_should_throw_exception_if_key_is_pkcs1_pem_public(self):
101+
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
102+
103+
with pytest.raises(InvalidKeyError):
104+
with open(key_path('testkey_pkcs1.pub.pem'), 'r') as keyfile:
105+
algo.prepare_key(keyfile.read())
106+
100107
def test_hmac_jwk_should_parse_and_verify(self):
101108
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
102109

0 commit comments

Comments
 (0)