JWT exp/nbf Clock Skew: Seconds vs Milliseconds, Leeway, and “Expired on Issue”
Symptom: signature OK, time wrong
A frequent 401 pattern in integration tests:
- The JWT decoder shows a clean payload and a green HS256 check
- The API still returns
Token expired,jwt expired, or the confusingToken used before issued/nbf
The secret is often fine. NumericDate semantics or clocks are not. When TLS is healthy (certificates are a separate story) but auth fails, start here.
Units in RFC 7519
Standard claims exp / iat / nbf are NumericDate = seconds since 1970-01-01 UTC, not JavaScript milliseconds.
| Rough magnitude | Likely meaning |
|---|---|
| ~10 digits (~1e9) | Seconds — RFC-correct |
| ~13 digits (~1e12) | Milliseconds — common mis-issue |
| 16+ digits | Often ns / snowflake IDs, not a standard NumericDate |
Issuers that write Date.now() (ms) into exp while verifiers compare in seconds produce absurd relative times. The on-site decoder applies a display-time heuristic for obvious millisecond magnitudes and shows UTC plus offsets such as “expired 2 hours ago”. Production libraries should still treat seconds as law and fix the issuer.
Three clock traps
1. Distributed drift
A few seconds of skew across Kubernetes nodes or regions is normal. Verifiers should allow 30–60 seconds of leeway: accept when now is in [nbf - leeway, exp + leeway]. Leeway absorbs NTP jitter; it is not a product feature to “stretch” expired sessions. Multi-minute leeway widens replay windows.
2. nbf in the future
Token used before issued usually means:
- The issuer clock is ahead of the verifier, or
nbfwas set to a round wall-clock time and traffic arrived a few seconds early
Compare date -u on both sides and check NTP before blaming algorithms.
3. Trusting frontend exp alone
Browsers may render “session remaining” from local clocks, but authorization must be decided server-side (signature + exp/nbf + aud/iss + revocation). A green frontend exp can still hide a revoked jti.
Alongside alg=none and large integer claims
- alg=none: unsigned tokens. Historical libraries that accepted none let attackers rewrite the header and drop the signature. A red warning on the decoder is intentional; production needs an algorithm allowlist. See the JWT decode guide.
- Snowflake / huge integers: if
subor business fields are 19-digit integers, naiveJSON.parserounds them. This site’s decode path displays them losslessly; your APIs should use strings or BigInt strategies so IDs do not silently mutate in logs.
Do not mix this up with HTTPS certificates
| Layer | Proves | Tools |
|---|---|---|
| TLS certificate | Server identity / chain completeness | Certificate decoder, incomplete chain guide |
| JWT | Claimed user / scopes | JWT decoder, this article |
When handshake failures and HTTP 401s appear together, reproduce them separately before changing configs.
Issuer-side habits that prevent the ticket
Most “clock” incidents are really issuance bugs that look like infrastructure:
- Prefer
Math.floor(Date.now() / 1000)(or language equivalents) whenever you write NumericDate fields — never paste rawDate.now()intoexp. - Set
exp = iat + ttlSecondsfrom one clock reading so the three claims cannot disagree with each other by accident. - Log both the raw claim integers and an ISO-8601 rendering in auth failures; support engineers should not have to decode Base64 under pressure.
- In tests, freeze time (
sinon/freezegun/timecop) instead of sleeping acrossexpboundaries — sleeps flake in CI and hide unit mistakes.
If your stack speaks milliseconds internally, convert at the JWT boundary once and document that boundary in the service README. Ambiguity at the boundary is how 13-digit exp values survive code review.
What the decoder’s leeway banner means
After decode, the JWT decoder timeline now shows a local time-window line using a default ±60s leeway: accept when browser now sits in [nbf − leeway, exp + leeway]. Colors and wording are a preview, not your API’s policy:
- Green “within window” does not prove the token is authorized — signature,
aud/iss, and revocation still matter - Amber “outside window” with a millisecond hint usually means the issuer wrote
Date.now()into NumericDate fields - Changing leeway in production libraries (jose, jsonwebtoken, Spring) is a server config; this page does not mint that config for you
Use the banner to decide whether the next debug step is clock/NTP, unit conversion, or issuer TTL math—before rotating secrets.
Multi-region skew patterns
Cross-region auth failures often look like intermittent 401s rather than permanent expiry:
| Pattern | What you see | First check |
|---|---|---|
| Issuer ahead of verifier | Burst of nbf / “used before issued” near deploy | date -u on both pods; NTP/chrony |
| Verifier ahead of issuer | Fresh tokens already “expired” by a few seconds | Same clocks; shorten deploy clock skew |
| Edge cache holding old JWKS | Signature fails after key rotation (not this article’s focus) | See signature invalid |
| Mobile client clock wrong | Only some users fail | Do not trust device clocks for auth; keep server-side decision |
Leeway of 30–60s absorbs typical NTP jitter. Multi-minute leeway is a product decision that widens replay windows—document it if you exceed the common band.
Five-minute checklist
- Decode and count digits on
exp/iat/nbf: seconds or milliseconds? - Does the timeline offset look sane (issued now but “expired years ago” → wrong unit)?
- How many seconds apart are issuer and verifier
date -u? Is leeway 30–60s? - Does the decoder’s local window line match the API error (
expiredvsnbf)? - Is header
algnoneor outside your allowlist? - Is TLS actually succeeding (certificate issues are a different ticket)?
Implementer's note
The on-site timeline formats standard time claims as UTC + relative offset; Copy stays in the toolbar above so the UX does not block the primary action. The leeway window helper (evaluateJwtTimeWindow) is display-only with a 60s default. Millisecond normalization is a display heuristic, not a rewrite of your issuance code. HS256 verify runs locally in the browser; RS256/JWKS fetch is not built in — verify those server-side. Same privacy boundary as the certificate decoder: material is not uploaded.
Related reading
- JWT explained: structure and security
- JWT signature invalid troubleshooting
- Base64 vs Base64URL: JWT segments and PEM
- Incomplete certificate chain
- Certificate decoder guide
From the ToolVault developer notebook. The homepage collects more browser-local tools.
Related Tools
Related Articles
WebP to JPG: Converting Back Without Wrecking Quality (and Why You Sometimes Must)
WebP won the web but old software still rejects it. How WebP→JPG conversion works, where quality is inevitably lost, what quality setting to pick, and a no-upload converter for the job.
WebP to JPG Without Wrecking Quality: Boundaries, Parameters, and a No-Upload Converter
WebP won the web but old software still rejects it. What actually happens in a WebP→JPG conversion, which losses are unavoidable, which quality setting to pick — with a local browser converter.
JPG to WebP Converter: How Batch Conversion Works Without Uploading Your Photos
A practical guide to converting JPG to WebP — what quality setting actually does, why batch mode differs from single-file conversion, and how local browser conversion keeps photos off third-party servers.