Skip to content

Commit 34b6525

Browse files
committed
Add global workaround for applications
Because the previous patch changes the behavoir of jwcrypto, this knob is a quick way for application developers to get back the old behavior temporarily without having to change the code immediately as it may require some significant refactoring, depending on how the application was written. This is not intended to be used in the long term and will be eventually deleted. Unfortunately I cannot decorate a simply global variable with the @deprecated decoration to make it clearer. Signed-off-by: Simo Sorce <[email protected]>
1 parent f4e912f commit 34b6525

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

docs/source/jwt.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ Classes
1414
:members:
1515
:show-inheritance:
1616

17+
Variables
18+
---------
19+
20+
.. autodata:: jwcrypto.jwt.JWTClaimsRegistry
21+
22+
.. autodata:: jwcrypto.jwt.JWT_expect_type
23+
1724
Examples
1825
--------
1926

jwcrypto/jwt.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222
'nbf': 'Not Before',
2323
'iat': 'Issued At',
2424
'jti': 'JWT ID'}
25+
"""Registry of RFC 7519 defined claims"""
26+
27+
28+
# do not use this unless you know about CVE-2022-3102
29+
JWT_expect_type = True
30+
"""This module parameter can disable the use of the expectation
31+
feature that has been introduced to fix CVE-2022-3102. This knob
32+
has been added as a workaround for applications that can't be
33+
immediately refactored to deal with the change in behavior but it
34+
is considered deprecated and will be removed in a future release.
35+
"""
2536

2637

2738
class JWTExpired(JWException):
@@ -542,11 +553,11 @@ def validate(self, key):
542553
validate_fn = None
543554

544555
if isinstance(self.token, JWS):
545-
if et != "JWS":
556+
if et != "JWS" and JWT_expect_type:
546557
raise TypeError("Expected {}, got JWS".format(et))
547558
validate_fn = self.token.verify
548559
elif isinstance(self.token, JWE):
549-
if et != "JWE":
560+
if et != "JWE" and JWT_expect_type:
550561
print("algs: {}".format(self._algs))
551562
raise TypeError("Expected {}, got JWE".format(et))
552563
validate_fn = self.token.decrypt

jwcrypto/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,11 @@ def test_unexpected(self):
17631763
token.make_encrypted_token(key)
17641764
enctok = token.serialize()
17651765

1766+
# test workaroud for older applications
1767+
jwt.JWT_expect_type = False
1768+
jwt.JWT(jwt=enctok, key=key)
1769+
jwt.JWT_expect_type = True
1770+
17661771
token.validate(key)
17671772
token.expected_type = "JWE"
17681773
token.validate(key)

0 commit comments

Comments
 (0)