Skip to content

Commit 9cb9401

Browse files
authored
Handling 'ImmatureSignatureError' for issued_at time (#794)
* Handling 'ImmatureSignatureError' for issued_at time when it is a future time * adding changelog and test cases
1 parent 8ccb825 commit 9cb9401

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed
1717

1818
Added
1919
~~~~~
20+
- Adding validation for `issued_at` when `iat > (now + leeway)` as `ImmatureSignatureError` by @sriharan16 in https://github.com/jpadilla/pyjwt/pull/794
2021

2122
`v2.5.0 <https://github.com/jpadilla/pyjwt/compare/2.4.0...2.5.0>`__
2223
-----------------------------------------------------------------------

jwt/api_jwt.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,13 @@ def _validate_required_claims(self, payload, options):
210210
raise MissingRequiredClaimError(claim)
211211

212212
def _validate_iat(self, payload, now, leeway):
213+
iat = payload["iat"]
213214
try:
214-
int(payload["iat"])
215+
int(iat)
215216
except ValueError:
216217
raise InvalidIssuedAtError("Issued At claim (iat) must be an integer.")
218+
if iat > (now + leeway):
219+
raise ImmatureSignatureError("The token is not yet valid (iat)")
217220

218221
def _validate_nbf(self, payload, now, leeway):
219222
try:

tests/test_api_jwt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ def test_decode_raises_exception_if_iat_is_not_int(self, jwt):
219219
with pytest.raises(InvalidIssuedAtError):
220220
jwt.decode(example_jwt, "secret", algorithms=["HS256"])
221221

222+
def test_decode_raises_exception_if_iat_is_greater_than_now(self, jwt, payload):
223+
payload["iat"] = utc_timestamp() + 10
224+
secret = "secret"
225+
jwt_message = jwt.encode(payload, secret)
226+
227+
with pytest.raises(ImmatureSignatureError):
228+
jwt.decode(jwt_message, secret, algorithms=["HS256"])
229+
222230
def test_decode_raises_exception_if_nbf_is_not_int(self, jwt):
223231
# >>> jwt.encode({'nbf': 'not-an-int'}, 'secret')
224232
example_jwt = (

0 commit comments

Comments
 (0)