Skip to content

Commit 6a84d73

Browse files
committed
Added a check so that asymmetric keys cannot be used as HMAC secrets to fix jpadilla#105
1 parent 1e6b6c5 commit 6a84d73

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

jwt/algorithms.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hmac
33

44
from .compat import constant_time_compare, string_types, text_type
5+
from .exceptions import InvalidAlgorithmError
56

67
try:
78
from cryptography.hazmat.primitives import interfaces, hashes
@@ -96,6 +97,12 @@ def prepare_key(self, key):
9697
if isinstance(key, text_type):
9798
key = key.encode('utf-8')
9899

100+
if (b'-----BEGIN PUBLIC KEY-----' in key
101+
or b'-----BEGIN CERTIFICATE-----' in key):
102+
raise InvalidAlgorithmError(
103+
'The specified key is an assymetric key or x509 certificate and'
104+
' should not be used as an HMAC secret.')
105+
99106
return key
100107

101108
def sign(self, msg, key):

jwt/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class InvalidAudienceError(InvalidTokenError):
1717
class InvalidIssuerError(InvalidTokenError):
1818
pass
1919

20+
class InvalidAlgorithmError(Exception):
21+
pass
22+
2023

2124
# Compatibility aliases (deprecated)
2225
ExpiredSignature = ExpiredSignatureError

0 commit comments

Comments
 (0)