Skip to content

Commit 25a6204

Browse files
authoredJan 8, 2025··
Replace uuidv4 generator with crypto.randomUUID() (#8600)
* 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>
1 parent 7bf2aec commit 25a6204

File tree

8 files changed

+12
-41
lines changed

8 files changed

+12
-41
lines changed
 

‎.changeset/fluffy-rules-pretend.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/app-check': patch
3+
'@firebase/util': patch
4+
---
5+
6+
Generate UUIDs with `crypto.randomUUID()` instead of custom uuidv4 function that uses `Math.random()`.

‎common/api-review/util.api.md

-3
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,6 @@ export interface Subscribe<T> {
476476
// @public (undocumented)
477477
export type Unsubscribe = () => void;
478478

479-
// @public
480-
export const uuidv4: () => string;
481-
482479
// Warning: (ae-missing-release-tag) "validateArgCount" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
483480
//
484481
// @public

‎packages/app-check/src/storage.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { FirebaseApp } from '@firebase/app';
19-
import { isIndexedDBAvailable, uuidv4 } from '@firebase/util';
19+
import { isIndexedDBAvailable } from '@firebase/util';
2020
import {
2121
readDebugTokenFromIndexedDB,
2222
readTokenFromIndexedDB,
@@ -77,7 +77,8 @@ export async function readOrCreateDebugTokenFromStorage(): Promise<string> {
7777

7878
if (!existingDebugToken) {
7979
// create a new debug token
80-
const newToken = uuidv4();
80+
// This function is only available in secure contexts. See https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts
81+
const newToken = crypto.randomUUID();
8182
// We don't need to block on writing to indexeddb
8283
// In case persistence failed, a new debug token will be generated every time the page is refreshed.
8384
// It renders the debug token useless because you have to manually register(whitelist) the new token in the firebase console again and again.

‎packages/data-connect/test/queries.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

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

@@ -51,11 +50,11 @@ interface TaskListResponse {
5150

5251
const SEEDED_DATA = [
5352
{
54-
id: uuidv4(),
53+
id: crypto.randomUUID(),
5554
content: 'task 1'
5655
},
5756
{
58-
id: uuidv4(),
57+
id: crypto.randomUUID(),
5958
content: 'task 2'
6059
}
6160
];

‎packages/database/test/helpers/util.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
import { FirebaseApp, initializeApp } from '@firebase/app';
19-
import { uuidv4 } from '@firebase/util';
2019
import { expect } from 'chai';
2120

2221
import {
@@ -105,7 +104,7 @@ export function waitFor(waitTimeInMS: number) {
105104

106105
// Creates a unique reference using uuid
107106
export function getUniqueRef(db: Database) {
108-
const path = uuidv4();
107+
const path = crypto.randomUUID();
109108
return ref(db, path);
110109
}
111110

‎packages/util/index.node.ts

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export * from './src/sha1';
3838
export * from './src/subscribe';
3939
export * from './src/validation';
4040
export * from './src/utf8';
41-
export * from './src/uuid';
4241
export * from './src/exponential_backoff';
4342
export * from './src/formatters';
4443
export * from './src/compat';

‎packages/util/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export * from './src/sha1';
3333
export * from './src/subscribe';
3434
export * from './src/validation';
3535
export * from './src/utf8';
36-
export * from './src/uuid';
3736
export * from './src/exponential_backoff';
3837
export * from './src/formatters';
3938
export * from './src/compat';

‎packages/util/src/uuid.ts

-29
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.