fix(db): fall back to crypto.getRandomValues when randomUUID is unavailable#1557
Draft
spokodev wants to merge 1 commit into
Draft
fix(db): fall back to crypto.getRandomValues when randomUUID is unavailable#1557spokodev wants to merge 1 commit into
spokodev 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
Contributor
|
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 |
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.
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.