diff --git a/jwcrypto/jwt.py b/jwcrypto/jwt.py index 6481805..d47e387 100644 --- a/jwcrypto/jwt.py +++ b/jwcrypto/jwt.py @@ -451,11 +451,12 @@ def _check_array_or_string_claim(self, name, claims): def _check_integer_claim(self, name, claims): if name not in claims or claims[name] is None: return - try: - int(claims[name]) - except ValueError as e: + if isinstance(claims[name], bool): + raise JWTInvalidClaimFormat( + "Claim %s is not an integer" % (name, )) + if not isinstance(claims[name], int): raise JWTInvalidClaimFormat( - "Claim %s is not an integer" % (name, )) from e + "Claim %s is not an integer" % (name, )) def _check_exp(self, claim, limit, leeway): if claim < limit - leeway: @@ -494,26 +495,35 @@ def _check_check_claims(self, check_claims): self._check_string_claim('jti', check_claims) self._check_string_claim('typ', check_claims) - def _check_provided_claims(self): - # check_claims can be set to False to skip any check - if self._check_claims is False: - return - + def _check_provided_claims(self, payload): try: - claims = json_decode(self.claims) + if isinstance(payload, dict): + claims = copy.deepcopy(payload) + else: + claims = json_decode(payload) + if not isinstance(claims, dict): raise ValueError() except ValueError as e: - if self._check_claims is not None: - raise JWTInvalidClaimFormat("Claims check requested " - "but claims is not a json " - "dict") from e - return + if self._check_claims is None: + return payload + raise JWTInvalidClaimFormat("Claims check requested " + "but claims is not a json " + "dict") from e + + # check_claims can be set to False to skip any check + if self._check_claims is False: + return json_encode(claims) + + for k, v in claims.items(): + if v is None: + raise JWTInvalidClaimFormat( + "Invalid Claim format for %s" % (k, )) self._check_default_claims(claims) if self._check_claims is None: - return + return json_encode(claims) for name, value in self._check_claims.items(): if name not in claims: @@ -587,6 +597,8 @@ def _check_provided_claims(self): "Invalid '%s' value. Expected '%s' got '%s'" % ( name, value, claims[name])) + return json_encode(claims) + def norm_typ(self, val): lc = val.lower() if '/' in lc: @@ -671,8 +683,7 @@ def validate(self, key): payload = self.token.payload if isinstance(payload, bytes): payload = payload.decode('utf-8') - self.claims = payload - self._check_provided_claims() + self._claims = self._check_provided_claims(payload) @staticmethod def _is_json_serialization(jwt): diff --git a/jwcrypto/tests.py b/jwcrypto/tests.py index 471861e..2c2bc3c 100644 --- a/jwcrypto/tests.py +++ b/jwcrypto/tests.py @@ -2190,6 +2190,44 @@ def test_claim_aud(self): check_claim = {"aud": ["www.example.com", "123"]} jwt.JWT(key=key, jwt=sertok, check_claims=check_claim) + def test_claim_integer_format(self): + claims = {"exp": 1300819380} + key = jwk.JWK(generate='oct', size=256) + token = jwt.JWT(header={"alg": "HS256"}, claims=claims) + token.make_signed_token(key) + sertok = token.serialize() + for invalid in ["123", True, False, 12.34, [1], {"a": 1}]: + self.assertRaises(jwt.JWTInvalidClaimFormat, jwt.JWT, key=key, + jwt=sertok, check_claims={"exp": invalid}) + self.assertRaises(jwt.JWTInvalidClaimFormat, jwt.JWT, key=key, + jwt=sertok, check_claims={"nbf": invalid}) + self.assertRaises(jwt.JWTInvalidClaimFormat, jwt.JWT, key=key, + jwt=sertok, check_claims={"iat": invalid}) + + def test_payload_claims_format(self): + key = jwk.JWK(generate='oct', size=256) + bad_payloads = [ + '{"iss": null}', + '{"sub": null}', + '{"aud": null}', + '{"jti": null}', + '{"typ": null}', + '{"exp": null}', + '{"nbf": null}', + '{"iat": null}', + '{"custom": null}', + '{"iat": true}', + '{"nbf": false}', + '{"exp": "9999999999"}', + '{"exp": [1]}', + ] + for bad in bad_payloads: + s = jws.JWS(bad) + s.add_signature(key, protected={"alg": "HS256"}) + tok = s.serialize(compact=True) + self.assertRaises(jwt.JWTInvalidClaimFormat, jwt.JWT, + jwt=tok, key=key) + def test_unexpected(self): key = jwk.JWK(generate='oct', size=256) claims = {"testclaim": "test"}