Skip to content

fix(uuid): guard uuidv7.timestamp against non-v7 UUIDs - #117

Merged
jkomyno merged 1 commit into
fix/uuid-v4-randomuuid-guardfrom
fix/uuid-versioned-tobytes
Nov 16, 2026
Merged

jkomyno merged 1 commit into
fix/uuid-v4-randomuuid-guardfrom
fix/uuid-versioned-tobytes

Conversation

@jkomyno

@jkomyno jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Stacked on #116.

Problem

uuidv7.timestamp() read its first six bytes through the version-agnostic byte codec:

function timestamp(id: string): number {
  const bytes = parseUuid(id)   // any 128-bit UUID form
  ...
}

So uuidv7.timestamp(someV4) returned a number that decodes to a plausible date. typeid.timestamp() already refused that input with UUID_NOT_V7; the two timestamp readers disagreed about what a timestamp is.

The adjacent surprise is that isValid is version-strict while toBytes is not. That asymmetry is intentional — toBytes is a byte codec that has to parse arbitrary namespaces, and the RFC 9562 v5 namespaces are version 1 UUIDs (6ba7b810-9dad-11d1-...). Making toBytes version-strict breaks uuidv5.toBytes(uuidv5.DNS), which existing tests use and which callers legitimately need. It was undocumented, though, which is what makes it look like a bug.

Fix

  • uuidv7.timestamp() requires the RFC 9562 version 7 and 10xx variant bits and throws InvalidInputError with code UUID_NOT_V7 otherwise, matching typeid.timestamp(). The NIL and MAX sentinels keep working, since RFC 9562 defines them without a version.
  • Extract isUuidVersion7 and isNilOrMaxBytes into uuid/common/uuid.ts and use them from both uuid/v7 and typeid, so the two checks cannot drift again.
  • Document why toBytes stays version-agnostic, in the type declaration of all three UUID modules, plus the migration table that maps uuid.parse() to it.
  • UUID_NOT_V7 now lists uuid and typeid as strategies.

Tests

timestamp version guard asserts UUID v4, UUID v5, and a v7-nibble/non-RFC-variant value all throw UUID_NOT_V7 with strategy: 'uuid', and that timestamp(NIL) === 0 and timestamp(MAX) === 0xffffffffffff still hold.

Scope

One deliberate behavior change: uuidv7.timestamp() on a non-v7 value now throws instead of returning garbage. Marked minor in the changeset.

@pkg-pr-new

pkg-pr-new Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

pnpm add https://pkg.pr.new/@uniku/cli@117
pnpm add https://pkg.pr.new/uniku@117

commit: c131077

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Preview removed because the pull request was closed.

@jkomyno
jkomyno force-pushed the fix/uuid-versioned-tobytes branch from 98fb0f3 to 64f1ad4 Compare November 15, 2026 22:44
@jkomyno

jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Codex review

Reviewed with codex exec (read-only sandbox) against fix/uuid-v4-randomuuid-guard.

No runtime correctness findings. The bit checks match the regexes, TypeID behavior is unchanged, and the sentinel asymmetry preserves existing behavior. No incompatible callers or additional version-specific validation gaps were found. InvalidInputError and the catalog metadata are consistent.

Direct checks passed across all 65,536 version/variant byte combinations.

It independently enumerated all 65,536 version/variant byte combinations against the three isValid regexes rather than trusting the predicate, and confirmed nothing in the CLI, examples, or typeid passes a non-v7 value to uuidv7.timestamp().

One finding, fixed in 64f1ad4

[P3] The migration doc overstated equivalence with uuid.parse().

I had written that toBytes() accepts "any 128-bit UUID form like parse()". That is wrong:

// [email protected]/dist/regex.js
/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|0000…|ffff…)$/i

uuid.parse() requires version 1-8 and the RFC variant. uniku's toBytes() checks only length, separators, and hex characters — so it accepts 01890a5d-ac96-774b-ccce-b302099a8057, which parse() rejects. A migrating caller could reasonably have assumed the stricter behaviour carried over, which is exactly the kind of assumption that turns into a validation bypass at a trust boundary.

Both the migration row and the toBytes() JSDoc in all three UUID modules now say what is and is not checked, and point at isValid() for the version-specific guard.

Not changed

The NIL/MAX asymmetry with typeid.timestamp() (which deliberately rejects sentinels, and is covered by an existing test) was reviewed and left alone — preserving both tested behaviours was judged better than churning one of them in this PR.

`uuidv7.timestamp()` read its first six bytes through the version-agnostic byte
codec, so `uuidv7.timestamp(someV4)` returned a number that decodes to a
plausible date. `typeid.timestamp()` already refused that input with
`UUID_NOT_V7`; the two timestamp readers disagreed.

- `uuidv7.timestamp()` now requires the RFC 9562 version 7 and `10xx` variant
  bits and throws `InvalidInputError` with code `UUID_NOT_V7` otherwise. NIL and
  MAX still work, since RFC 9562 defines them without a version.
- Extract `isUuidVersion7` and `isNilOrMaxBytes` into `uuid/common/uuid.ts` and
  use them from both `uuid/v7` and `typeid`, so the two checks cannot drift.
- Document why `toBytes()` stays version-agnostic: it is a byte codec that has to
  parse arbitrary namespaces (the RFC v5 namespaces are version 1 UUIDs), while
  `isValid()` remains the version guard.

`UUID_NOT_V7` now lists `uuid` and `typeid` as strategies.
@jkomyno
jkomyno force-pushed the fix/uuid-v4-randomuuid-guard branch from dfc17b4 to 7cecc8b Compare November 15, 2026 23:03
@jkomyno
jkomyno force-pushed the fix/uuid-versioned-tobytes branch from 64f1ad4 to c131077 Compare November 15, 2026 23:03
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Results

Generation

ID uniku npm/regex Comparison
ULID 4.5M ops/s 27.8K ops/s 162.7x faster ⚠️ ±14.6%
UUID v7 3.4M ops/s 459.7K ops/s 7.4x faster ⚠️ ±2.1%
CUID2 43.8K ops/s 6.5K ops/s 6.7x faster ⚠️ ±4.9%
TypeID 2M ops/s 571.5K ops/s 3.5x faster
UUID v5 1.1M ops/s 364.5K ops/s 3x faster ⚠️ ±8.8%
TSID 7M ops/s 2.6M ops/s 2.6x faster
UUID v5 (256-char name) 401.1K ops/s 199.8K ops/s 2x faster ⚠️ ±15.3%
KSUID 364K ops/s 212.3K ops/s 1.7x faster ⚠️ ±3.9%
XID 7.2M ops/s 5.5M ops/s 1.3x faster
UUID v4 10.5M ops/s 8.6M ops/s 1.2x faster
NanoID 9.5M ops/s 14.7M ops/s npm 1.5x faster
NanoID (10) 11.7M ops/s 18.2M ops/s npm 1.6x faster
ObjectID 4.6M ops/s 7.8M ops/s npm 1.7x faster

Validation

ID uniku npm/regex Comparison
KSUID 8.4M ops/s 694.3K ops/s 12.1x faster
TypeID 4.4M ops/s 1.2M ops/s 3.6x faster
TSID 4.3M ops/s 1.4M ops/s 3x faster
XID 10M ops/s 3.6M ops/s 2.7x faster ⚠️ ±14.6%
ULID 9.3M ops/s 3.6M ops/s 2.6x faster
UUID v5 8.9M ops/s 4.8M ops/s 1.8x faster
UUID v7 8.6M ops/s 4.7M ops/s 1.8x faster
UUID v4 8.7M ops/s 4.8M ops/s 1.8x faster
CUID2 12.5M ops/s 13.8M ops/s npm 1.1x faster
NanoID 12.5M ops/s 14.3M ops/s regex 1.1x faster
ObjectID 9.4M ops/s 11.6M ops/s npm 1.2x faster

Cold Start (Node)

Generator Process to exit (median / p95) Import + first ID (median / p95)
uuid/v4 31.4 ms / 36.9 ms 5.4 ms / 6.0 ms
uuid/v5 27.9 ms / 29.3 ms 3.9 ms / 4.1 ms
uuid/v7 31.1 ms / 34.5 ms 5.8 ms / 6.1 ms
ulid 31.2 ms / 35.5 ms 5.8 ms / 6.0 ms
typeid 31.6 ms / 34.2 ms 6.5 ms / 6.7 ms
cuid2 39.2 ms / 41.4 ms 13.9 ms / 14.5 ms
cuid/v2 37.9 ms / 40.3 ms 13.5 ms / 14.3 ms
nanoid 30.0 ms / 33.4 ms 5.1 ms / 5.4 ms
ksuid 31.4 ms / 32.8 ms 6.2 ms / 6.5 ms
objectid 32.2 ms / 35.1 ms 6.6 ms / 7.1 ms
tsid 30.9 ms / 33.7 ms 5.6 ms / 5.8 ms
xid 31.2 ms / 32.9 ms 6.4 ms / 6.6 ms

Comparison vs Main

Benchmark Baseline Current Change
tests/bench/compat.bench.ts > Generation > ULID > uniku 4.6M ops/s 4.5M ops/s ⚪ -2.8% (+/-2.4% RME)
tests/bench/compat.bench.ts > Generation > NanoID > uniku 9.8M ops/s 9.5M ops/s ⚪ -2.7% (+/-6.6% RME)
tests/bench/compat.bench.ts > Generation > TypeID > uniku 2M ops/s 2M ops/s ⚪ -2.7% (+/-5.8% RME)
tests/bench/compat.bench.ts > Validation > KSUID > uniku 8.6M ops/s 8.4M ops/s ⚪ -2.4% (+/-6.6% RME)
tests/bench/compat.bench.ts > Generation > UUID v7 > uniku 3.5M ops/s 3.4M ops/s ⚪ -2.2% (+/-0.6% RME)
tests/bench/compat.bench.ts > Generation > NanoID (10) > uniku 11.9M ops/s 11.7M ops/s ⚪ -2.2% (+/-5.1% RME)
tests/bench/compat.bench.ts > Validation > TypeID > uniku 4.5M ops/s 4.4M ops/s ⚪ -1.9% (+/-2.0% RME)
tests/bench/compat.bench.ts > Validation > UUID v4 > uniku 8.9M ops/s 8.7M ops/s ⚪ -1.8% (+/-7.0% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > uniku 408.2K ops/s 401.1K ops/s ⚪ -1.8% (+/-7.9% RME)
tests/bench/compat.bench.ts > Generation > ObjectID > uniku 4.7M ops/s 4.6M ops/s ⚪ -1.6% (+/-4.3% RME)
tests/bench/compat.bench.ts > Validation > ObjectID > uniku 9.5M ops/s 9.4M ops/s ⚪ -1.4% (+/-4.7% RME)
tests/bench/compat.bench.ts > Validation > TSID > uniku 4.4M ops/s 4.3M ops/s ⚪ -1.4% (+/-11.6% RME)
tests/bench/compat.bench.ts > Validation > ULID > uniku 9.5M ops/s 9.3M ops/s ⚪ -1.4% (+/-1.8% RME)
tests/bench/compat.bench.ts > Generation > UUID v4 > uniku 10.7M ops/s 10.5M ops/s ⚪ -1.3% (+/-2.7% RME)
tests/bench/compat.bench.ts > Validation > XID > uniku 10.1M ops/s 10M ops/s ⚪ -1.3% (+/-5.8% RME)
tests/bench/compat.bench.ts > Generation > KSUID > uniku 368.8K ops/s 364K ops/s ⚪ -1.3% (+/-1.5% RME)
tests/bench/compat.bench.ts > Validation > CUID2 > uniku 12.7M ops/s 12.5M ops/s ⚪ -1.2% (+/-6.0% RME)
tests/bench/compat.bench.ts > Generation > CUID2 > uniku 44.2K ops/s 43.8K ops/s ⚪ -0.7% (+/-2.2% RME)
tests/bench/compat.bench.ts > Generation > TSID > uniku 6.9M ops/s 7M ops/s ⚪ +0.7% (+/-1.0% RME)
tests/bench/compat.bench.ts > Validation > NanoID > uniku 12.5M ops/s 12.5M ops/s ⚪ -0.5% (+/-9.0% RME)
tests/bench/compat.bench.ts > Validation > UUID v5 > uniku 8.9M ops/s 8.9M ops/s ⚪ -0.4% (+/-9.1% RME)
tests/bench/compat.bench.ts > Generation > XID > uniku 7.2M ops/s 7.2M ops/s ⚪ -0.3% (+/-3.4% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 > uniku 1.1M ops/s 1.1M ops/s ⚪ +0.3% (+/-3.3% RME)
tests/bench/compat.bench.ts > Validation > UUID v7 > uniku 8.6M ops/s 8.6M ops/s ⚪ -0.3% (+/-7.7% RME)
tests/bench/compat.bench.ts > Validation > UUID v7 > npm 4.9M ops/s 4.7M ops/s ℹ️ -4.9% (+/-4.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > XID > npm 3.8M ops/s 3.6M ops/s ℹ️ -4.4% (+/-22.5% RME) (ref)
tests/bench/compat.bench.ts > Generation > ULID > npm 28.4K ops/s 27.8K ops/s ℹ️ -2.3% (+/-29.5% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v4 > npm 4.9M ops/s 4.8M ops/s ℹ️ -2.0% (+/-0.7% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v7 > npm 468.3K ops/s 459.7K ops/s ℹ️ -1.9% (+/-3.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > TypeID > npm 582.2K ops/s 571.5K ops/s ℹ️ -1.8% (+/-3.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > TypeID > npm 1.2M ops/s 1.2M ops/s ℹ️ -1.5% (+/-4.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > ObjectID > npm 7.9M ops/s 7.8M ops/s ℹ️ -1.3% (+/-2.0% RME) (ref)
tests/bench/compat.bench.ts > Validation > TSID > npm 1.5M ops/s 1.4M ops/s ℹ️ -1.1% (+/-4.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v4 > npm 8.7M ops/s 8.6M ops/s ℹ️ -1.0% (+/-2.2% RME) (ref)
tests/bench/compat.bench.ts > Validation > CUID2 > npm 13.9M ops/s 13.8M ops/s ℹ️ -0.9% (+/-6.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > XID > npm 5.5M ops/s 5.5M ops/s ℹ️ +0.9% (+/-1.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > KSUID > npm 214.1K ops/s 212.3K ops/s ℹ️ -0.8% (+/-7.8% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID > npm 14.8M ops/s 14.7M ops/s ℹ️ -0.8% (+/-1.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID (10) > npm 18.4M ops/s 18.2M ops/s ℹ️ -0.8% (+/-0.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v5 > npm 4.8M ops/s 4.8M ops/s ℹ️ +0.7% (+/-2.5% RME) (ref)
tests/bench/compat.bench.ts > Validation > ULID > npm 3.6M ops/s 3.6M ops/s ℹ️ -0.7% (+/-0.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > TSID > npm 2.6M ops/s 2.6M ops/s ℹ️ +0.5% (+/-4.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > NanoID > regex 14.2M ops/s 14.3M ops/s ℹ️ +0.5% (+/-1.9% RME) (ref)
tests/bench/compat.bench.ts > Generation > CUID2 > npm 6.5K ops/s 6.5K ops/s ℹ️ -0.4% (+/-9.7% RME) (ref)
tests/bench/compat.bench.ts > Validation > KSUID > npm 692.4K ops/s 694.3K ops/s ℹ️ +0.3% (+/-1.7% RME) (ref)
tests/bench/compat.bench.ts > Validation > ObjectID > npm 11.6M ops/s 11.6M ops/s ℹ️ -0.2% (+/-1.8% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 > npm 365K ops/s 364.5K ops/s ℹ️ -0.2% (+/-17.8% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > npm 199.6K ops/s 199.8K ops/s ℹ️ +0.1% (+/-31.4% RME) (ref)

Bundle Impact

Import Minified + gzipped
uniku/uuid/v4 ~1.3 KB
uniku/uuid/v5 ~1.8 KB
uniku/uuid/v7 ~1.8 KB
uniku/ulid ~1.9 KB
uniku/typeid ~2.9 KB
uniku/cuid2 ~1.0 KB*
uniku/cuid/v2 ~1.0 KB*
uniku/nanoid ~1.2 KB
uniku/ksuid ~1.5 KB
uniku/objectid ~1.6 KB
uniku/tsid ~1.7 KB
uniku/xid ~2.1 KB
uniku/errors ~445 B
uniku/generators ~101 B

*Excludes external dependencies (e.g., @noble/hashes)

Generated by workflow run 43772703242

@jkomyno
jkomyno merged commit e90172d into fix/uuid-v4-randomuuid-guard Nov 16, 2026
16 checks passed
jkomyno added a commit that referenced this pull request Nov 16, 2026
…e branches) (#123)

* fix(uuid): load uniku/uuid/v4 without a secure context

`Crypto.randomUUID` is secure-context-only while `Crypto.getRandomValues` is
not, so an insecure browser origin exposes `crypto` with `randomUUID` missing.
The module captured `globalThis.crypto.randomUUID.bind(globalThis.crypto)` at
module scope, which threw during module evaluation there:

    TypeError: Cannot read properties of undefined (reading 'bind')

That took down the whole entry point, including `toBytes`, `fromBytes`, and
`isValid`, none of which touch randomness.

Resolve `randomUUID` once and guard it. The no-argument generator keeps using
`crypto.randomUUID()` where available and otherwise falls back to the shared
CSPRNG pool plus the local formatter, which is the path `_v4` already used.

* fix(uuid): guard uuidv7.timestamp against non-v7 UUIDs

`uuidv7.timestamp()` read its first six bytes through the version-agnostic byte
codec, so `uuidv7.timestamp(someV4)` returned a number that decodes to a
plausible date. `typeid.timestamp()` already refused that input with
`UUID_NOT_V7`; the two timestamp readers disagreed.

- `uuidv7.timestamp()` now requires the RFC 9562 version 7 and `10xx` variant
  bits and throws `InvalidInputError` with code `UUID_NOT_V7` otherwise. NIL and
  MAX still work, since RFC 9562 defines them without a version.
- Extract `isUuidVersion7` and `isNilOrMaxBytes` into `uuid/common/uuid.ts` and
  use them from both `uuid/v7` and `typeid`, so the two checks cannot drift.
- Document why `toBytes()` stays version-agnostic: it is a byte codec that has to
  parse arbitrary namespaces (the RFC v5 namespaces are version 1 UUIDs), while
  `isValid()` remains the version guard.

`UUID_NOT_V7` now lists `uuid` and `typeid` as strategies.

* fix(ulid): unify monotonic overflow with uuidv7

UUID v7 and ULID hit the same situation with two different answers. When the
per-time-unit sequence is exhausted, `uuidv7()` advanced its timestamp by one
millisecond and carried on; `ulid()` filled the remaining random bytes with
`0xff` and threw `RANDOM_OVERFLOW`, which also left every later call in that
millisecond failing.

Both now advance virtual time by one millisecond and continue from a zeroed
counter, so `incrementBytesInPlace` leaving the buffer at zero on overflow is
exactly the state the new bucket wants.

That unification exposed a bug both generators already had: neither bounded the
advance against the end of the 48-bit timestamp field. At the maximum
millisecond, uuidv7 wrote a timestamp that wrapped to zero, ULID's string path
emitted `80000000000000000000000000` (which its own `isValid()` rejects), and
ULID's buffer path also wrapped the timestamp. Both now saturate and throw
`TIMESTAMP_OUT_OF_RANGE`, and ULID keeps the `fill(0xff)` that makes the
saturated state fail closed on every later call.

`RANDOM_OVERFLOW` is removed from `ERROR_CODES`: the random-portion overflow now
recovers instead of failing, so there is no such failure to report. Pre-1.0, this
is a breaking change to the error surface; `migration/v1.mdx` documents the code
as removed with no replacement, and drops the `ULID_RANDOM_OVERFLOW` mapping.

* chore: correct stale comments and contracts that contradicted the code

Six small statements disagreed with the implementation. None changes a valid
input's output; two tighten input handling that was already documented as strict.

- `nanoid(0)` and `nanoid({ length: 0 })` returned `''` while the JSDoc advertised
  `@throws Length must be between 0 and 2048` and the CLI documented `1-256`.
  Length is now one integer check over `1..2048` with a single message. An empty
  string is never a usable identifier, and every other invalid length threw.
- `nanoid.isValid()` cannot check a generated length because a nanoid carries none.
  JSDoc and the reference page now say that outright instead of leaving the gap
  implicit for anyone reaching for it as a boundary validator.
- `nanoid` read the bare `crypto` global while every other module uses
  `globalThis.crypto`, which is the convention AGENTS.md states.
- `encodeBase62` and `encodeObjectIdHex` accepted over-long inputs and encoded only
  the leading bytes, while their public `fromBytes()` wrappers required the exact
  length. Both now require the exact length.
- `docs/STABILITY.md` called the ObjectID and XID counters "always-incrementing".
  Both wrap to zero after 2^24 values in the same second; the contract now says so.
- Corrected two comments: `incrementBytesInPlace` credited KSUID with a call it
  never makes, and the `objectid/hex.ts` decode-table note reported 128 KiB for a
  64 KiB allocation.

* docs(bench): report benchmark dispersion and stop overstating ratios (#120)

The performance guide reported every ratio to two significant figures with no
measure of uncertainty. The harness does not support that precision: the same row
moved between 1.2x and 1.6x across runs of the same command on one machine, and
the ULID row is 163x in the published CI snapshot versus ~85x on other hardware.

Reporting:

- `bench-summary` gains a `Spread` column built from each row's
  `withinActionRme`/`crossActionRme`, and reports `parity (within spread)` when a
  ratio's deviation from 1x is smaller than its own combined dispersion. A
  snapshot with no multi-run data prints `—` rather than an invented number.
- `benchmarkDispersion` and `combineDispersion` live in `bench-results.ts` next to
  the other statistics, prefer multi-run dispersion over a single run's RME, and
  combine independent dispersions in quadrature.
- The performance page reports ranges rounded to about one significant figure and
  says what each row does and does not compare: the UUID v4 row is a control that
  compares two identical `crypto.randomUUID()` calls, the TypeID/KSUID/TSID rows
  measure API shape as well as encoding, the ULID row is dominated by the
  reference re-detecting its PRNG per call, and every number is V8-only.

The bundle-size table is unchanged: it reproduces byte-for-byte with
`pnpm bundle:summary`, so it never had a precision problem.

Co-authored-by: jkomyno <[email protected]>

---------

Co-authored-by: jkomyno <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant