From 8ceadc2baf685232c942fd354b756e8b5bb9389b Mon Sep 17 00:00:00 2001 From: Dmitry Andreev Date: Sun, 11 Jul 2021 14:54:25 +0300 Subject: [PATCH] jwe: use runtime checks instead of assertions Optimization strips all assertions, so assert shouldn't be used for control flow. In this case, jwe.JWEnc.is_jwe returned True for all tokens after optimization. --- src/jwkest/jwe.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/jwkest/jwe.py b/src/jwkest/jwe.py index a2e5885..70ef9f3 100644 --- a/src/jwkest/jwe.py +++ b/src/jwkest/jwe.py @@ -303,18 +303,14 @@ def is_jwe(self): if "typ" in self.headers and self.headers["typ"].lower() == "jwe": return True - try: - assert "alg" in self.headers and "enc" in self.headers - except AssertionError: - return False - else: - for typ in ["alg", "enc"]: - try: - assert self.headers[typ] in SUPPORTED[typ] - except AssertionError: - logger.debug("Not supported %s algorithm: %s" % ( - typ, self.headers[typ])) - return False + for typ in ["enc", "alg"]: + if typ not in self.headers: + return False + if self.headers[typ] not in SUPPORTED[typ]: + logger.debug("Not supported %s algorithm: %s" % ( + typ, self.headers[typ])) + return False + return True def __len__(self):