Skip to content

Commit 52def77

Browse files
committed
Catch JWSErrors in jwt.decode()
So far exceptions raised in `jws.verify()` weren't caught in the above function, which led to it raising (undocumented) exceptions from the underlying module. This commit transforms said exceptions. This includes cases of invalid payload padding, error handling for which had previously been attached to the `json.loads()` call.
1 parent ff5bf5c commit 52def77

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

jose/jwt.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from jose import jws
1212

13+
from .exceptions import JWSError
1314
from .exceptions import JWTClaimsError
1415
from .exceptions import JWTError
1516
from .exceptions import ExpiredSignatureError
@@ -112,12 +113,14 @@ def decode(token, key, algorithms=None, options=None, audience=None, issuer=None
112113
defaults.update(options)
113114

114115
verify_signature = defaults.get('verify_signature', True)
115-
payload = jws.verify(token, key, algorithms, verify=verify_signature)
116+
117+
try:
118+
payload = jws.verify(token, key, algorithms, verify=verify_signature)
119+
except JWSError as e:
120+
raise JWTError(e)
116121

117122
try:
118123
claims = json.loads(payload.decode('utf-8'))
119-
except (TypeError, binascii.Error):
120-
raise JWTError('Invalid payload padding')
121124
except ValueError as e:
122125
raise JWTError('Invalid payload string: %s' % e)
123126

0 commit comments

Comments
 (0)