Skip to content

Commit

Permalink
Replace uuidv4 generator with crypto.randomUUID() (#8600)
Browse files Browse the repository at this point in the history
* Replace uuidv4 generator with `crypto.randomUUID()`

The uuidv4 generator in util used `Math.random()`, which does not provide strong uniqueness guarantees
(https://www.bocoup.com/blog/random-numbers).

The places where the uuidv4 generator were used didn't require strong
uniqueness guarantees (nothing security related), but I think it's good
to move away from this from util in case we try to use it in the future.

A better built-in alternative is `crypto.randomUUID()`, which does provide strong
uniqueness guarantees. Since this is a more modern JS built-in, it's
only [defined in secure
contexts](https://blog.mozilla.org/security/2018/01/15/secure-contexts-everywhere/).
Is this something we're concerned about? Are there any App Check users
with apps running in non-secure environments?

* Update API reports

* Add changeset

* Add comment about availability restricted to secure contexts

---------

Co-authored-by: dlarocque <dlarocque@users.noreply.github.com>
  • Loading branch information
dlarocque and dlarocque authored Jan 8, 2025
1 parent 7bf2aec commit 25a6204
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .changeset/fluffy-rules-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/app-check': patch
'@firebase/util': patch
---

Generate UUIDs with `crypto.randomUUID()` instead of custom uuidv4 function that uses `Math.random()`.
3 changes: 0 additions & 3 deletions common/api-review/util.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,6 @@ export interface Subscribe<T> {
// @public (undocumented)
export type Unsubscribe = () => void;

// @public
export const uuidv4: () => string;

// Warning: (ae-missing-release-tag) "validateArgCount" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public
Expand Down
5 changes: 3 additions & 2 deletions packages/app-check/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { FirebaseApp } from '@firebase/app';
import { isIndexedDBAvailable, uuidv4 } from '@firebase/util';
import { isIndexedDBAvailable } from '@firebase/util';
import {
readDebugTokenFromIndexedDB,
readTokenFromIndexedDB,
Expand Down Expand Up @@ -77,7 +77,8 @@ export async function readOrCreateDebugTokenFromStorage(): Promise<string> {

if (!existingDebugToken) {
// create a new debug token
const newToken = uuidv4();
// This function is only available in secure contexts. See https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts
const newToken = crypto.randomUUID();
// We don't need to block on writing to indexeddb
// In case persistence failed, a new debug token will be generated every time the page is refreshed.
// It renders the debug token useless because you have to manually register(whitelist) the new token in the firebase console again and again.
Expand Down
5 changes: 2 additions & 3 deletions packages/data-connect/test/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* limitations under the License.
*/

import { uuidv4 } from '@firebase/util';
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';

Expand Down Expand Up @@ -51,11 +50,11 @@ interface TaskListResponse {

const SEEDED_DATA = [
{
id: uuidv4(),
id: crypto.randomUUID(),
content: 'task 1'
},
{
id: uuidv4(),
id: crypto.randomUUID(),
content: 'task 2'
}
];
Expand Down
3 changes: 1 addition & 2 deletions packages/database/test/helpers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

import { FirebaseApp, initializeApp } from '@firebase/app';
import { uuidv4 } from '@firebase/util';
import { expect } from 'chai';

import {
Expand Down Expand Up @@ -105,7 +104,7 @@ export function waitFor(waitTimeInMS: number) {

// Creates a unique reference using uuid
export function getUniqueRef(db: Database) {
const path = uuidv4();
const path = crypto.randomUUID();
return ref(db, path);
}

Expand Down
1 change: 0 additions & 1 deletion packages/util/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export * from './src/sha1';
export * from './src/subscribe';
export * from './src/validation';
export * from './src/utf8';
export * from './src/uuid';
export * from './src/exponential_backoff';
export * from './src/formatters';
export * from './src/compat';
Expand Down
1 change: 0 additions & 1 deletion packages/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export * from './src/sha1';
export * from './src/subscribe';
export * from './src/validation';
export * from './src/utf8';
export * from './src/uuid';
export * from './src/exponential_backoff';
export * from './src/formatters';
export * from './src/compat';
Expand Down
29 changes: 0 additions & 29 deletions packages/util/src/uuid.ts

This file was deleted.

0 comments on commit 25a6204

Please sign in to comment.