Skip to content

fix(uuid): load uniku/uuid/v4 without a secure context - #116

Merged
jkomyno merged 1 commit into
mainfrom
fix/uuid-v4-randomuuid-guard
Nov 16, 2026
Merged

jkomyno merged 1 commit into
mainfrom
fix/uuid-v4-randomuuid-guard

Conversation

@jkomyno

@jkomyno jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Problem

Crypto.randomUUID is available only in secure contexts, while Crypto.getRandomValues is not. On an insecure browser origin (plain http:// on a LAN address, an embedded panel, some webviews) crypto exists but randomUUID does not.

src/uuid/v4.ts captured the method at module scope:

const randomUUID = /*@__PURE__*/ globalThis.crypto.randomUUID.bind(globalThis.crypto)

So the import itself threw:

uuid/v4 import FAILED: TypeError Cannot read properties of undefined (reading 'bind')
ulid      import OK -> 01M7GKYR666A5CB5W7CTG99YAT

That took down the entire entry point, including toBytes, fromBytes, and isValid, none of which touch randomness. It contradicts the README claim that the library runs in browsers.

Fix

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 — the path _v4 already used for uuidv4({}).

if (randomUUID) {
  return randomUUID()
}

// Insecure context: fall back to the shared CSPRNG pool and the local formatter.
writeV4BytesUnchecked(rng(), reusableBuf, 0)
return formatUuidUnchecked(reusableBuf)

uuid@14 guards the same call (if (!buf && !options && crypto.randomUUID)), so this also aligns the two implementations.

Tests

New without crypto.randomUUID (insecure origin) block stubs crypto down to getRandomValues, resets modules, and asserts that the entry point imports, generates valid v4 strings, keeps the version and variant bits, returns distinct IDs from the fallback buffer, and still exposes the byte helpers.

Scope

No change to any existing output. On a secure context the code path is unchanged.

Stacked: this is PR 1 of 5.

@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@116
pnpm add https://pkg.pr.new/uniku@116

commit: 7cecc8b

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Preview removed because the pull request was closed.

@jkomyno

jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Codex review

Reviewed with codex exec (read-only sandbox) against main. Verdict: no substantive findings.

The guard fixes insecure-origin imports. The fallback preserves 122 unbiased random bits, leaves the shared pool intact, and returns strings independent of the reusable buffer. The tests reload modules after stubbing crypto, so the original implementation would fail them. No equivalent missed binding or secure-context regression surfaced.

In-memory checks reproduced the original failure and verified 10,000 fallback UUIDs, byte helpers, and native-path dispatch.

What it verified independently

It did not take the PR description at face value — it built every entry point with Bun.build for the browser target, replaced globalThis.crypto with a { getRandomValues }-only stub, and then:

Baseline import: TypeError without randomUUID
Head fallback: import, 10000 unique valid IDs, and helpers pass
All public entry points without external dependencies import without randomUUID
Secure path: bound native randomUUID called without fallback

Two things worth carrying forward from that run:

  • The main baseline fails and the head passes under the same stub, so the new tests are not passing trivially.
  • No other entry point has the same defect. It enumerated all public entry points and imported each one under the stub, which is the check I would have wanted for finding item 4 (other modules eagerly binding crypto methods).

`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.
@jkomyno
jkomyno force-pushed the fix/uuid-v4-randomuuid-guard branch from dfc17b4 to 7cecc8b Compare November 15, 2026 23:03
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Results

Generation

ID uniku npm/regex Comparison
ULID 4.6M ops/s 28.5K ops/s 163.1x faster ⚠️ ±14.3%
UUID v7 3.4M ops/s 466.8K ops/s 7.4x faster
CUID2 44.2K ops/s 6.6K ops/s 6.7x faster ⚠️ ±4.8%
TypeID 2M ops/s 579.3K ops/s 3.5x faster
UUID v5 1.1M ops/s 361.2K ops/s 3.1x faster ⚠️ ±9.4%
TSID 6.9M ops/s 2.7M ops/s 2.6x faster
UUID v5 (256-char name) 407.3K ops/s 200.2K ops/s 2x faster ⚠️ ±15.2%
KSUID 369.2K ops/s 213.5K ops/s 1.7x faster ⚠️ ±3.8%
XID 7.5M ops/s 5.5M ops/s 1.4x faster
UUID v4 10.6M ops/s 8.8M ops/s 1.2x faster
NanoID 9.8M ops/s 14.8M ops/s npm 1.5x faster
NanoID (10) 11.9M ops/s 18.3M ops/s npm 1.5x faster
ObjectID 4.6M ops/s 7.9M ops/s npm 1.7x faster

Validation

ID uniku npm/regex Comparison
KSUID 8.3M ops/s 691.1K ops/s 12x faster
TypeID 4.4M ops/s 1.2M ops/s 3.6x faster
TSID 4.5M ops/s 1.5M ops/s 3x faster
XID 9.8M ops/s 3.7M ops/s 2.6x faster ⚠️ ±15.8%
ULID 9.4M ops/s 3.7M ops/s 2.6x faster
UUID v4 8.8M ops/s 4.9M ops/s 1.8x faster
UUID v5 8.8M ops/s 4.9M ops/s 1.8x faster
UUID v7 8.6M ops/s 4.9M ops/s 1.8x faster
NanoID 12.8M ops/s 14.3M ops/s regex 1.1x faster
CUID2 12.5M ops/s 14.1M ops/s npm 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 29.8 ms / 33.6 ms 5.3 ms / 5.5 ms
uuid/v5 27.7 ms / 29.3 ms 3.8 ms / 4.0 ms
uuid/v7 30.0 ms / 32.5 ms 5.8 ms / 6.0 ms
ulid 30.3 ms / 32.6 ms 5.7 ms / 6.0 ms
typeid 31.6 ms / 34.5 ms 6.4 ms / 6.6 ms
cuid2 38.2 ms / 40.4 ms 13.5 ms / 14.6 ms
cuid/v2 37.3 ms / 40.0 ms 13.6 ms / 14.4 ms
nanoid 29.1 ms / 32.7 ms 5.0 ms / 5.1 ms
ksuid 30.7 ms / 34.6 ms 6.1 ms / 6.4 ms
objectid 30.6 ms / 32.7 ms 6.1 ms / 6.3 ms
tsid 29.3 ms / 32.8 ms 5.3 ms / 5.5 ms
xid 31.5 ms / 33.9 ms 6.4 ms / 6.7 ms

Comparison vs Main

Benchmark Baseline Current Change
tests/bench/compat.bench.ts > Validation > KSUID > uniku 8.6M ops/s 8.3M ops/s ⚪ -3.3% (+/-9.0% RME)
tests/bench/compat.bench.ts > Generation > ObjectID > uniku 4.7M ops/s 4.6M ops/s ⚪ -3.2% (+/-8.5% RME)
tests/bench/compat.bench.ts > Generation > XID > uniku 7.2M ops/s 7.5M ops/s ⚪ +3.0% (+/-2.4% RME)
tests/bench/compat.bench.ts > Validation > XID > uniku 10.1M ops/s 9.8M ops/s ⚪ -2.7% (+/-4.5% RME)
tests/bench/compat.bench.ts > Validation > NanoID > uniku 12.5M ops/s 12.8M ops/s ⚪ +2.5% (+/-4.6% RME)
tests/bench/compat.bench.ts > Validation > TSID > uniku 4.4M ops/s 4.5M ops/s ⚪ +2.1% (+/-9.4% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 > uniku 1.1M ops/s 1.1M ops/s ⚪ +1.8% (+/-1.8% RME)
tests/bench/compat.bench.ts > Validation > ObjectID > uniku 9.5M ops/s 9.4M ops/s ⚪ -1.7% (+/-4.6% RME)
tests/bench/compat.bench.ts > Validation > CUID2 > uniku 12.7M ops/s 12.5M ops/s ⚪ -1.7% (+/-3.1% RME)
tests/bench/compat.bench.ts > Validation > ULID > uniku 9.5M ops/s 9.4M ops/s ⚪ -1.2% (+/-1.9% RME)
tests/bench/compat.bench.ts > Generation > UUID v4 > uniku 10.7M ops/s 10.6M ops/s ⚪ -1.1% (+/-5.4% RME)
tests/bench/compat.bench.ts > Generation > UUID v7 > uniku 3.5M ops/s 3.4M ops/s ⚪ -1.0% (+/-4.6% RME)
tests/bench/compat.bench.ts > Validation > UUID v5 > uniku 8.9M ops/s 8.8M ops/s ⚪ -0.9% (+/-5.2% RME)
tests/bench/compat.bench.ts > Validation > TypeID > uniku 4.5M ops/s 4.4M ops/s ⚪ -0.8% (+/-3.9% RME)
tests/bench/compat.bench.ts > Validation > UUID v7 > uniku 8.6M ops/s 8.6M ops/s ⚪ -0.8% (+/-8.9% RME)
tests/bench/compat.bench.ts > Validation > UUID v4 > uniku 8.9M ops/s 8.8M ops/s ⚪ -0.5% (+/-4.9% RME)
tests/bench/compat.bench.ts > Generation > NanoID > uniku 9.8M ops/s 9.8M ops/s ⚪ +0.5% (+/-1.8% RME)
tests/bench/compat.bench.ts > Generation > TSID > uniku 6.9M ops/s 6.9M ops/s ⚪ +0.3% (+/-0.8% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > uniku 408.2K ops/s 407.3K ops/s ⚪ -0.2% (+/-7.4% RME)
tests/bench/compat.bench.ts > Generation > NanoID (10) > uniku 11.9M ops/s 11.9M ops/s ⚪ -0.2% (+/-3.6% RME)
tests/bench/compat.bench.ts > Generation > KSUID > uniku 368.8K ops/s 369.2K ops/s ⚪ +0.1% (+/-1.0% RME)
tests/bench/compat.bench.ts > Generation > TypeID > uniku 2M ops/s 2M ops/s ⚪ +0.1% (+/-9.0% RME)
tests/bench/compat.bench.ts > Generation > CUID2 > uniku 44.2K ops/s 44.2K ops/s ⚪ +0.1% (+/-2.1% RME)
tests/bench/compat.bench.ts > Generation > ULID > uniku 4.6M ops/s 4.6M ops/s ⚪ +0.0% (+/-0.8% RME)
tests/bench/compat.bench.ts > Validation > UUID v5 > npm 4.8M ops/s 4.9M ops/s ℹ️ +2.5% (+/-0.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > XID > npm 3.8M ops/s 3.7M ops/s ℹ️ -2.3% (+/-23.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v4 > npm 8.7M ops/s 8.8M ops/s ℹ️ +1.4% (+/-4.2% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v7 > npm 4.9M ops/s 4.9M ops/s ℹ️ -1.4% (+/-5.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > CUID2 > npm 13.9M ops/s 14.1M ops/s ℹ️ +1.3% (+/-1.5% RME) (ref)
tests/bench/compat.bench.ts > Validation > TypeID > npm 1.2M ops/s 1.2M ops/s ℹ️ -1.2% (+/-3.3% RME) (ref)
tests/bench/compat.bench.ts > Generation > TSID > npm 2.6M ops/s 2.7M ops/s ℹ️ +1.2% (+/-5.8% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 > npm 365K ops/s 361.2K ops/s ℹ️ -1.0% (+/-18.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v4 > npm 4.9M ops/s 4.9M ops/s ℹ️ -0.9% (+/-4.7% RME) (ref)
tests/bench/compat.bench.ts > Generation > CUID2 > npm 6.5K ops/s 6.6K ops/s ℹ️ +0.6% (+/-9.6% RME) (ref)
tests/bench/compat.bench.ts > Validation > ULID > npm 3.6M ops/s 3.7M ops/s ℹ️ +0.6% (+/-1.3% RME) (ref)
tests/bench/compat.bench.ts > Validation > NanoID > regex 14.2M ops/s 14.3M ops/s ℹ️ +0.5% (+/-1.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID (10) > npm 18.4M ops/s 18.3M ops/s ℹ️ -0.5% (+/-0.9% RME) (ref)
tests/bench/compat.bench.ts > Generation > TypeID > npm 582.2K ops/s 579.3K ops/s ℹ️ -0.5% (+/-4.9% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v7 > npm 468.3K ops/s 466.8K ops/s ℹ️ -0.3% (+/-3.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > npm 199.6K ops/s 200.2K ops/s ℹ️ +0.3% (+/-31.3% RME) (ref)
tests/bench/compat.bench.ts > Generation > KSUID > npm 214.1K ops/s 213.5K ops/s ℹ️ -0.3% (+/-7.7% RME) (ref)
tests/bench/compat.bench.ts > Generation > ULID > npm 28.4K ops/s 28.5K ops/s ℹ️ +0.3% (+/-29.2% RME) (ref)
tests/bench/compat.bench.ts > Validation > TSID > npm 1.5M ops/s 1.5M ops/s ℹ️ -0.2% (+/-3.3% RME) (ref)
tests/bench/compat.bench.ts > Generation > XID > npm 5.5M ops/s 5.5M ops/s ℹ️ +0.2% (+/-2.1% RME) (ref)
tests/bench/compat.bench.ts > Generation > ObjectID > npm 7.9M ops/s 7.9M ops/s ℹ️ -0.2% (+/-0.7% RME) (ref)
tests/bench/compat.bench.ts > Validation > KSUID > npm 692.4K ops/s 691.1K ops/s ℹ️ -0.2% (+/-2.8% RME) (ref)
tests/bench/compat.bench.ts > Validation > ObjectID > npm 11.6M ops/s 11.6M ops/s ℹ️ -0.1% (+/-0.9% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID > npm 14.8M ops/s 14.8M ops/s ℹ️ -0.1% (+/-2.0% RME) (ref)

Bundle Impact

Import Minified + gzipped
uniku/uuid/v4 ~1.3 KB
uniku/uuid/v5 ~1.8 KB
uniku/uuid/v7 ~1.7 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 43772700751

@jkomyno
jkomyno merged commit 128402c into main Nov 16, 2026
16 checks passed
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