fix(uuid): reject timestamps that exceed 48 bits in v7.generate() - #7284
Open
tomas-zijdemans wants to merge 1 commit into
Open
fix(uuid): reject timestamps that exceed 48 bits in v7.generate()#7284tomas-zijdemans wants to merge 1 commit into
tomas-zijdemans wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7284 +/- ##
==========================================
- Coverage 95.03% 95.03% -0.01%
==========================================
Files 617 619 +2
Lines 51637 51897 +260
Branches 9359 9410 +51
==========================================
+ Hits 49075 49322 +247
- Misses 2021 2030 +9
- Partials 541 545 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
generate()in@std/uuid/v7silently truncates timestamps to 48 bits. This PR makes it throw aRangeErrorinstead.The timestamp is written with
setBigUint64, which wraps modulo 2^64, so any bit above 48 disappears without a trace:generate(2 ** 48)returns a UUID whoseextractTimestamp()is0generate(Date.now() * 1000)(microseconds passed by mistake) returns a UUID carrying a garbage timestampBoth results pass
validate(), so nothing downstream catches the corruption.The fix extends the existing guard:
generate()already threwRangeErrorfor negative and non-integer timestamps, and now also rejects values above2 ** 48 - 1. The@throwsdoc states the full range, and tests cover the new throw path plus a round-trip at the maximum value.On the behavior change: inputs that previously produced a corrupted UUID now throw. An inline comment said "truncated to 48 bits", but the truncation never made it into the public JSDoc, and it breaks the round-trip the tests already assert (
extractTimestamp(generate(t)) === t). I read that comment as describing the bug, not the contract. Since2 ** 48 - 1milliseconds lands in the year 10889, no wall-clock timestamp is affected. Only mistakes are.I used Claude Code to help investigate and write this change.