Skip to content

refactor: leverage native crypto.randomUUID() for UUID generation#433

Open
Nithin0620 wants to merge 2 commits into
google:mainfrom
Nithin0620:fix/secure-random-uuid
Open

refactor: leverage native crypto.randomUUID() for UUID generation#433
Nithin0620 wants to merge 2 commits into
google:mainfrom
Nithin0620:fix/secure-random-uuid

Conversation

@Nithin0620

Copy link
Copy Markdown
Contributor

Pull Request Description

Problem:

The existing randomUUID() implementation always generates UUIDs using a custom algorithm, even in environments where the native Web Crypto API provides a built-in crypto.randomUUID() method. This misses an opportunity to use a standardized, well-tested, and potentially more secure UUID generation mechanism.

Solution:

Updated the randomUUID() function to first check whether globalThis.crypto.randomUUID() is available. When supported, the function now delegates UUID generation to the native implementation. For environments where the API is unavailable, the existing UUID generation logic is preserved as a fallback to maintain backward compatibility.

Benefits of this approach:

  • Uses the platform's native UUID implementation when available.
  • Improves reliability and standards compliance.
  • Maintains compatibility with older environments through the existing fallback logic.
  • No breaking changes to the public API.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Summary of passed npm test results:

  • Ran npm run test:unit locally.
  • All 142 test files and 1782/1782 tests passed successfully.

Manual End-to-End (E2E) Tests:

Verified that compiling the workspaces via npm run build succeeds without any TypeScript diagnostic errors

Checklist

  • I have read the [CONTRIBUTING.md](https://github.com/google/adk-js/blob/main/CONTRIBUTING.md) document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

This change is backward compatible and does not alter the public API. It simply prioritizes the native Web Crypto UUID generation API when available while preserving the existing custom implementation as a fallback.

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.

1 participant