Practical technical guides / Security

Clock Skew Rejects Fresh Tokens: iat and nbf Checks, Leeway Size and What Leeway Extends

Measured with PyJWT 2.15.1: an issuer clock 2 seconds fast fails every token without leeway, because a future iat is rejected. Size leeway from measured skew.

By Sandeep Belgavi · Published 2026-11-30

Measured with PyJWT 2.15.1: an issuer clock 2 seconds fast fails every token without leeway, because a future iat is rejected. Size leeway from measured skew.

Two clocks, one token

A JSON Web Token carries times written by the issuer's clock and checked against the verifier's clock. RFC 7519 defines three of them: iat (issued at), nbf (not before) and exp (expiration). For nbf and exp, the RFC says implementers may provide a small leeway, "usually no more than a few minutes", to account for clock skew. Skew is the difference between the two clocks, and it is never exactly zero in a distributed system.

When skew is larger than the leeway, the verifier rejects valid tokens. A token minted on an identity service whose clock runs ahead looks like it is from the future; a token from a clock that runs behind looks older than it is. The symptoms are intermittent 401 responses that depend on which issuer replica and which verifier host a request hits, the kind of bug that disappears when you look at it.

This guide measures how PyJWT 2.15.1 treats skew, shows a surprise in how it checks iat, sizes leeway from measured skew, and explains what leeway costs. The companion example jwt_clock_skew_leeway contains the probe and a model that agrees with it.

What PyJWT checks, from its source

PyJWT's claim validation in jwt/api_jwt.py (version 2.15.1) reads the current time once and applies three rules:

  • reject with ImmatureSignatureError if iat > now + leeway
  • reject with ImmatureSignatureError if nbf > now + leeway
  • reject with ExpiredSignatureError if exp <= now - leeway

The iat rule is not in the RFC, which treats iat as information. PyJWT added it in version 2.6.0, according to its changelog: "Adding validation for issued_at when iat > (now + leeway) as ImmatureSignatureError". The practical consequence is that even tokens with no nbf claim fail when the issuer's clock is ahead, as long as they carry iat, which most do.

Measured: skew against leeway

The probe mints HS256 tokens with iat = nbf = issuer now and a 300-second lifetime, where the issuer's clock is the verifier's clock plus a chosen skew, and decodes them immediately with PyJWT 2.15.1. A positive skew means the issuer is ahead.

Issuer skew (seconds)Leeway 0Leeway 60
-400expiredexpired
-301expiredaccepted
-295acceptedaccepted
-30acceptedaccepted
0acceptedaccepted
+2not yet valid (iat)accepted
+30not yet valid (iat)accepted
+90not yet valid (iat)not yet valid (iat)

Two seconds is enough. An issuer only 2 seconds ahead of the verifier produces tokens that a zero-leeway verifier rejects on arrival, because their iat is in the verifier's future. Removing nbf from the token changes nothing: the probe's iat-only tokens fail identically. With 60 seconds of leeway, skews up to 60 seconds in either direction are absorbed, and a token from a clock running 90 seconds ahead still fails.

The behind direction matters less for fresh tokens but sets a hidden limit on lifetime. A token from an issuer 301 seconds behind is already expired on arrival when its lifetime is 300 seconds. Short-lived tokens, such as a 60-second token for a single agent tool call, are much more sensitive to slow clocks than hour-long sessions are.

The model behind the table

With iat = nbf = mint time on the issuer clock and a check age seconds after minting on the verifier's clock, the rules reduce to two inequalities:

def model(skew, age, ttl, leeway):
    if skew > age + leeway:
        return "ImmatureSignatureError (iat)"
    if skew + ttl + leeway <= age:
        return "ExpiredSignatureError"
    return "accepted"

The companion test compares this model with live PyJWT decoding for skews from -400 to +90 seconds and leeway 0 and 60, and with the recorded probe, and they agree on every case.

Where large skew comes from

Well-run time sync keeps hosts within milliseconds of each other, so seconds of skew usually point to a specific cause. Common ones are a host or virtual machine without a running time service, a VM restored from a snapshot that resumes with an old clock until sync corrects it, a laptop or edge device that has been asleep, a container platform whose nodes use different time sources, and a clock that sync refuses to step because the offset exceeds its limit. Each of these produces skew in one direction for a period, which matches the pattern of tokens failing from some hosts and not others.

Sizing leeway from measured skew

Leeway should come from what your clocks actually do, not from a round number. Measure skew where it matters, between issuer hosts and verifier hosts, using the NTP offset each host reports (chronyc tracking on chrony, w32tm /query /status on Windows) or by comparing the issuer's iat with the verifier's receive time in logs. Take the worst absolute offset you observe over a representative period, then add a margin for network delay and for the one-second truncation of integer timestamps.

The example's helper does exactly that: observed offsets of 0.4, -1.8, 2.6 and -0.2 seconds plus a 5-second margin give a leeway of 7 seconds. Teams that run NTP well usually see offsets well under a second, and a leeway of 5 to 30 seconds is common. A leeway of several minutes usually hides a broken clock that should be fixed instead.

What leeway costs

Leeway is symmetric in PyJWT: the same value relaxes the future check and the expiry check. It therefore extends the life of every token, not only the ones affected by skew. With a 300-second lifetime and 60 seconds of leeway, a token is accepted until 360 seconds after minting. The model confirms it: accepted at 359 seconds, expired at 360.

That extra minute applies to stolen tokens and revoked sessions too. If you rely on short lifetimes instead of a revocation list, the effective lifetime is lifetime plus leeway, and that is the number to use in your threat model. For one-time actions, such as an approval token for a high-impact agent action, keep the lifetime short and the leeway small, and record the token's jti to reject replays within the window.

Some libraries allow separate leeway for each check. With PyJWT you can approximate an asymmetric policy by setting leeway for the clock-ahead case and enforcing a tighter maximum age yourself from iat.

Failure modes

  • Fixing skew by raising leeway. It makes the 401s stop and doubles the window for misuse. Fix the clock and keep the leeway small.
  • Containers without time sync. Containers share the host kernel's clock, but virtual machines, edge devices and some sandboxes do not. Check the clock source for every issuer and verifier.
  • Minting iat from a cached time. An issuer that computes now once per batch stamps later tokens with an old time, which looks like a slow clock. Read the time per token.
  • Mixing verifier libraries. One service using PyJWT rejects future iat; another library may ignore iat entirely. The same token is valid in one place and not another. Standardise the checks, or at least document them.
  • Testing with real time. Tests that mint and verify in the same process never see skew. Inject the skew, as the probe does.

Verification

Run the companion tests with PyJWT 2.15.1 installed. They compare the model with live decoding and with the recorded run, check that a 2-second fast issuer fails without leeway and passes with 5 seconds, that 60 seconds of leeway extends a 300-second token to 360 seconds, and that the sizing helper returns 7 seconds for the sample offsets. In production, log the difference between token iat and the verifier's receive time; its distribution is your measured skew plus network delay, and it tells you whether your leeway is too small or merely large.

Primary references and authorship

Sources checked 2026-11-30. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/jwt_clock_skew_leeway. Views are my own and do not represent any employer, past or present.