lepture / authlib

The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.
https://authlib.org/
BSD 3-Clause "New" or "Revised" License
4.39k stars 436 forks source link

JWTClaims accepts True/False `iat`. #641

Open nairb774 opened 3 months ago

nairb774 commented 3 months ago

Describe the bug

According to https://www.rfc-editor.org/rfc/rfc7519.html#section-4.1.6 the iat field should be a numeric field. Creating a token with iat:true passes validation. This looks to be because _validate_numeric_time returns True for bool inputs.

To Reproduce

A minimal example to reproduce the behavior:

authlib.jose.rfc7519.JWTClaims({"iat": True}, {}).validate()
authlib.jose.rfc7519.JWTClaims({"iat": False}, {}).validate()

Expected behavior

Both of those validate calls should fail similarly to:

>>> authlib.jose.rfc7519.JWTClaims({"iat": "not-a-number"}, {}).validate()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/site-packages/authlib/jose/rfc7519/claims.py", line 103, in validate
    self.validate_iat(now, leeway)
  File "/usr/lib/python3.11/site-packages/authlib/jose/rfc7519/claims.py", line 207, in validate_iat
    raise InvalidClaimError('iat')
authlib.jose.errors.InvalidClaimError: invalid_claim: Invalid claim "iat"

Environment: