Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace uuidv4 generator with crypto.randomUUID() #8600

Merged
merged 4 commits into from
Jan 8, 2025
Merged

Replace uuidv4 generator with crypto.randomUUID() #8600

merged 4 commits into from
Jan 8, 2025

Conversation

dlarocque
Copy link
Contributor

@dlarocque dlarocque commented Oct 25, 2024

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

The places where the uuidv4 generator are used don'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. I believe the reason this wasn't used originally was because it wasn't supported in Node <15. Since this is a more modern JS built-in, it's only defined in secure contexts. Is this something we're concerned about? Are there any App Check users with apps running in non-secure environments?

Fixes #6462
Also closes b/283984828

Copy link

changeset-bot bot commented Oct 25, 2024

🦋 Changeset detected

Latest commit: 6ed7315

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 30 packages
Name Type
@firebase/app-check Patch
@firebase/util Patch
@firebase/app-check-compat Patch
firebase Patch
@firebase/analytics-compat Patch
@firebase/analytics Patch
@firebase/app-compat Patch
@firebase/app Patch
@firebase/auth-compat Patch
@firebase/auth Patch
@firebase/component Patch
@firebase/data-connect Patch
@firebase/database-compat Patch
@firebase/database-types Patch
@firebase/database Patch
@firebase/firestore-compat Patch
@firebase/firestore Patch
@firebase/functions-compat Patch
@firebase/functions Patch
@firebase/installations-compat Patch
@firebase/installations Patch
@firebase/messaging-compat Patch
@firebase/messaging Patch
@firebase/performance-compat Patch
@firebase/performance Patch
@firebase/remote-config-compat Patch
@firebase/remote-config Patch
@firebase/storage-compat Patch
@firebase/storage Patch
@firebase/vertexai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Oct 25, 2024

Size Report 1

Affected Products

  • @firebase/app-check

    TypeBase (32bf021)Merge (3f8a971)Diff
    browser26.3 kB26.3 kB+4 B (+0.0%)
    main27.3 kB27.3 kB+6 B (+0.0%)
    module26.3 kB26.3 kB+4 B (+0.0%)
  • @firebase/util

    TypeBase (32bf021)Merge (3f8a971)Diff
    browser23.4 kB23.2 kB-168 B (-0.7%)
    main29.2 kB29.0 kB-222 B (-0.8%)
    module23.4 kB23.2 kB-168 B (-0.7%)
  • bundle

    TypeBase (32bf021)Merge (3f8a971)Diff
    app-check (CustomProvider)37.5 kB37.3 kB-142 B (-0.4%)
    app-check (ReCaptchaEnterpriseProvider)40.0 kB39.8 kB-144 B (-0.4%)
    app-check (ReCaptchaV3Provider)39.9 kB39.8 kB-144 B (-0.4%)
  • firebase

    TypeBase (32bf021)Merge (3f8a971)Diff
    firebase-app-check-compat.js23.4 kB23.2 kB-136 B (-0.6%)
    firebase-app-check.js25.0 kB24.9 kB-107 B (-0.4%)
    firebase-compat.js797 kB797 kB-133 B (-0.0%)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/erZPTOadzN.html

@dlarocque dlarocque requested review from a team as code owners October 25, 2024 14:03
Copy link
Contributor

Changeset File Check ⚠️

  • Warning: This PR modifies files in the following packages but they have not been included in the changeset file:%0A - @firebase/data-connect%0A - @firebase/database%0A%0A Make sure this was intentional.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Oct 25, 2024

Size Analysis Report 1

Affected Products

  • @firebase/app-check

    • initializeAppCheck

      Size

      TypeBase (32bf021)Merge (3f8a971)Diff
      size11.2 kB11.2 kB+1 B (+0.0%)
      size-with-ext-deps36.5 kB36.3 kB-144 B (-0.4%)

      External Dependency

      ModuleBase (32bf021)Merge (3f8a971)Diff
      @firebase/util

      Deferred
      ErrorFactory
      base64
      getGlobal
      getModularInstance
      isIndexedDBAvailable
      uuidv4

      Deferred
      ErrorFactory
      base64
      getGlobal
      getModularInstance
      isIndexedDBAvailable

      - uuidv4

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/qay7dZYpqg.html

@hsubox76
Copy link
Contributor

hsubox76 commented Nov 5, 2024

Since this is a more modern JS built-in, it's only defined in secure contexts. Is this something we're concerned about? Are there any App Check users with apps running in non-secure environments?

What's the effect of this, if you deploy an app with App Check to a site and call it with HTTP, do you get an error?

dlarocque and others added 3 commits January 7, 2025 10:13
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?
@dlarocque
Copy link
Contributor Author

dlarocque commented Jan 7, 2025

Since this is a more modern JS built-in, it's only defined in secure contexts. Is this something we're concerned about? Are there any App Check users with apps running in non-secure environments?

What's the effect of this, if you deploy an app with App Check to a site and call it with HTTP, do you get an error?

@hsubox76 If you call crypto.UUID() on a site deployed with HTTP (non-secure context), you will get TypeError: crypto.randomUUID is not a function. Tested this here http://httpforever.com/.

It's worth mentioning that sites served locally on HTTP (http://localhost) are considered secure contexts.

Also, in App Check, crypto.randomUUID is only used for testing/debugging in readOrCreateDebugTokenFromStorage.

Copy link
Member

@yuchenshi yuchenshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems only used in tests in data-connect and database too. I support this change for these reasons

  1. It's really a small change touching just a few lines of code so the risk is low
  2. We can avoid rolling our own UUID implementation

I don't think the security considerations apply to our use cases. When we do need a secure implementation in the future, we'll likely have to do a security review anyway.

@dlarocque dlarocque merged commit 25a6204 into main Jan 8, 2025
50 checks passed
@dlarocque dlarocque deleted the dl/uuid branch January 8, 2025 16:20
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.

Don't use Math.random() to generate UUIDs
4 participants