Skip to content

Commit 1f1fe15

Browse files
authored
Add a deprecation warning when jwt.decode() is called with the legacy verify= argument (jpadilla#742)
Since the arbitrary/unused `**kwargs` can't quite be dropped (as jpadilla#657 would do) without a major version bump (as reverted in jpadilla#701), it's still a good idea to warn users if they are attempting to use contradictory arguments for the security-sensitive `verify=` argument.
1 parent 35fa28e commit 1f1fe15

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

jwt/api_jwt.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import warnings
23
from calendar import timegm
34
from collections.abc import Iterable, Mapping
45
from datetime import datetime, timedelta, timezone
@@ -75,6 +76,17 @@ def decode_complete(
7576
else:
7677
options.setdefault("verify_signature", True)
7778

79+
# If the user has set the legacy `verify` argument, and it doesn't match
80+
# what the relevant `options` entry for the argument is, inform the user
81+
# that they're likely making a mistake.
82+
if "verify" in kwargs and kwargs["verify"] != options["verify_signature"]:
83+
warnings.warn(
84+
"The `verify` argument to `decode` does nothing in PyJWT 2.0 and newer. "
85+
"The equivalent is setting `verify_signature` to False in the `options` dictionary. "
86+
"This invocation has a mismatch between the kwarg and the option entry.",
87+
category=DeprecationWarning,
88+
)
89+
7890
if not options["verify_signature"]:
7991
options.setdefault("verify_exp", False)
8092
options.setdefault("verify_nbf", False)

tests/test_api_jwt.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,19 @@ def test_decode_no_algorithms_verify_signature_false(self, jwt, payload):
658658
jwt_message = jwt.encode(payload, secret)
659659

660660
jwt.decode(jwt_message, secret, options={"verify_signature": False})
661+
662+
def test_decode_legacy_verify_warning(self, jwt, payload):
663+
secret = "secret"
664+
jwt_message = jwt.encode(payload, secret)
665+
666+
with pytest.deprecated_call():
667+
# The implicit default for options.verify_signature is True,
668+
# but the user sets verify to False.
669+
jwt.decode(jwt_message, secret, verify=False, algorithms=["HS256"])
670+
671+
with pytest.deprecated_call():
672+
# The user explicitly sets verify=True,
673+
# but contradicts it in verify_signature.
674+
jwt.decode(
675+
jwt_message, secret, verify=True, options={"verify_signature": False}
676+
)

0 commit comments

Comments
 (0)