fix(db): fall back to crypto.getRandomValues when randomUUID is unavailable#1557
fix(db): fall back to crypto.getRandomValues when randomUUID is unavailable#1557spokodev wants to merge 1 commit into
Conversation
…ilable `crypto.randomUUID()` is restricted to secure contexts in browsers, so it is unavailable on pages served over plain HTTP from a non-localhost host, for example a dev server reached via a LAN IP. `@tanstack/db` was calling it directly from transactions, mutations, local-storage, local-only and the default collection-id path, so the first write would throw `TypeError: crypto.randomUUID is not a function` before user code could handle it. Centralise UUID generation in `safeRandomUUID()` (in `utils.ts`) which: - delegates to `crypto.randomUUID()` when present (the common case), - falls back to RFC 4122 v4 generation via `crypto.getRandomValues()` when it is not, with the version/variant bits set per spec, - throws an explicit Error if neither Web Crypto API is available. Replace the seven direct call sites with `safeRandomUUID()` and add unit tests covering the delegate path, the fallback path (validating a v4 shape), uniqueness across calls in fallback mode, and both error paths when crypto is missing or partial. Closes TanStack#1541
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Thanks so much for putting this together! 🙏 Closing in favor of #1593, which takes the same approach (helper renamed to |
Problem
crypto.randomUUID()is restricted to secure contexts per the Web Crypto spec, so it is unavailable on pages served over plain HTTP from a non-localhost host (for example a dev server reached via a LAN IP).@tanstack/dbcalls it directly from collections, transactions, mutations, local-only, and local-storage, so the first write throwsbefore user code can handle it.
crypto.getRandomValues()remains available in non-secure contexts, so a v4 UUID is still reachable; we just have to assemble it ourselves when the higher-level API is missing.Repro
Fix
Add a
safeRandomUUID()helper inpackages/db/src/utils.ts:crypto.randomUUID()when available (the common case).crypto.getRandomValues()with the version/variant bits set per spec §4.4.Errorif neither Web Crypto API is available.Replace the seven direct call sites with the helper:
packages/db/src/collection/index.ts(default collection id)packages/db/src/collection/mutations.ts(insert / update / delete mutation ids; three sites)packages/db/src/local-only.ts(local-only collection id)packages/db/src/local-storage.ts(version-tracking uuid)packages/db/src/transactions.ts(transaction id)The issue body listed four files; while replacing those I also found three additional
crypto.randomUUID()call sites inmutations.ts(the per-operationmutationId) and one inlocal-storage.tsthat have the same failure mode, and rolled them into the helper as well so the fallback is consistent across@tanstack/db.Tests
packages/db/tests/safe-random-uuid.test.tscovers both axes:crypto.randomUUIDexists, the helper returns its value unchanged and the stub is called exactly once.crypto.getRandomValuesis present, the result matches a strict RFC 4122 v4 regex (/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/).randomUUIDis missing.{}object, norandomUUID, nogetRandomValues): same explicit error, so we never silently fall through to native crypto.All five new tests pass with the patched helper (
pnpm --filter @tanstack/db test). The pre-existing 163 test failures onmain(localStorage.getItem is not a functioninunion-all.test.tsand related jsdom interactions) are not introduced by this change; the new test count of2213 passedmatches2208 baseline + 5 new.Lint clean (
pnpm --filter @tanstack/db lint, no new warnings). Build clean (pnpm --filter @tanstack/db build).Closes #1541
Note on disclosure
Bug discovery and reproduction were independent of this contributor (issue author
@kj55-devfiled a clean writeup with a suggested fix). I implemented the helper, located the additional call sites inmutations.tsandlocal-storage.ts, wrote the tests, and reviewed every change. AI assistance was used while drafting parts of the test cases and this PR body, in line with the AGENTS.md guidance in the repo.