fix: respect an iat of 0 instead of overwriting it with the current time (#874) - #1043
Open
jaideeppyne wants to merge 1 commit into
Open
fix: respect an iat of 0 instead of overwriting it with the current time (#874)#1043jaideeppyne wants to merge 1 commit into
iat of 0 instead of overwriting it with the current time (#874)#1043jaideeppyne wants to merge 1 commit into
Conversation
Signing a token with `iat: 0` (the Unix epoch) silently discarded the value and used the current time instead, because both `sign.js` and `lib/timespan.js` used `iat || Math.floor(Date.now() / 1000)` — and `0` is falsy. Since that timestamp is also the base `timespan()` uses to derive `exp`/`nbf`, those claims were corrupted too whenever `iat` was 0. Guard with an explicit numeric check (`typeof iat === 'number' && !isNaN(iat)`) at both sites. `0` is now preserved, while the existing contract is kept: `NaN` still falls back to the current time and `Infinity` still serializes to `null`. Both sites must change, otherwise `timespan()` re-applies its own `0 || …` and `exp`/`nbf` stay wrong. Fixes auth0#874.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #874. Signing a token with
iat: 0(the Unix epoch) silently discards the value and stamps the current time instead.The cause is a falsy-zero bug at two coupled sites:
0is falsy, so0 || nowevaluates tonow. Because that sametimestampis the basetimespan()uses to deriveexpandnbf, those claims are corrupted too wheneveriatis 0.Reproduction on
master:A non-zero
iat(e.g.100) already works — the bug only bites for0, which is a perfectly validNumericDate(isNumber(0) === true, so it passes claim validation).Fix
Replace the falsy check with an explicit numeric guard at both sites:
Both sites must change — patching only
sign.jsleavestimespan()re-applying its own0 || …, soexp/nbfwould stay corrupted.The
!isNaNguard deliberately preserves the existing contract already encoded intest/claim-iat.test.js: the only falsynumbervalues are0andNaN, so this rescues0while keepingNaN → current timeandInfinity → nullunchanged.Tests
Added to
test/claim-iat.test.js:iat: 0is preserved (decode(token).iat === 0);exp/nbfare anchored toiat: 0(exp === 86400,nbf === 60) — this covers thelib/timespan.jssite.Both fail on
masterand pass with the fix; the fullclaim-iatsuite (41 tests) is green, and the pre-existingNaN/Infinitycases are unchanged.Disclosure: this change was prepared with AI assistance and reviewed/verified by me before submission.