Skip to content

dataconnect(change): Replace SecureRandom with process-unique ID generation#8154

Merged
dconeybe merged 44 commits into
mainfrom
dconeybe/dataconnect/InsecureRandom
May 13, 2026
Merged

dataconnect(change): Replace SecureRandom with process-unique ID generation#8154
dconeybe merged 44 commits into
mainfrom
dconeybe/dataconnect/InsecureRandom

Conversation

@dconeybe
Copy link
Copy Markdown
Contributor

@dconeybe dconeybe commented May 13, 2026

Summary

Replaces the use of SecureRandom with a more efficient, process-unique ID generation strategy using standard Random for Firebase Data Connect request and logging IDs.

Highlights

  • Performance Optimization: Replaced SecureRandom with standard Random for non-security-sensitive request IDs, eliminating the overhead and potential thread blocking associated with entropy collection in SecureRandom.
  • New ID Generation Strategy: Introduced IdStringGenerator, a utility class that generates process-unique ID strings by combining a prefix, random non-hexadecimal padding, and an atomically incremented hexadecimal sequence number.
  • Guaranteed Uniqueness: The new IdStringGenerator ensures process-wide unique IDs while maintaining a readable format and avoiding characters that are easily confused (e.g., 'i', 'l').
  • Simplified Threading: Removed unnecessary background thread initialization and lazy loading previously required to safely use SecureRandom without blocking the main thread.
  • Comprehensive Testing: Added IdStringGeneratorUnitTest to verify the uniqueness, monotonicity, and concurrency safety of the new ID generation logic.

Changelog

17 files changed
  • CHANGELOG.md: Added an entry for the switch from SecureRandom to Random.
  • DataConnectAppCheck.kt: Updated to accept an IdStringGenerator and pass it to the base class.
  • DataConnectAuth.kt: Updated to accept an IdStringGenerator and pass it to the base class.
  • DataConnectCredentialsTokenManager.kt: Refactored to use IdStringGenerator for invocation IDs.
  • FirebaseDataConnectFactory.kt: Removed SecureRandom management and updated FirebaseDataConnectImpl instantiation.
  • FirebaseDataConnectImpl.kt: Switched from SecureRandom to IdStringGenerator for internal request and emulator IDs.
  • Logger.kt: Updated to use a shared IdStringGenerator instance for logger IDs.
  • MutationRefImpl.kt: Refactored to use the idStringGenerator from the dataConnect instance for request IDs.
  • RealtimeQueryRefImpl.kt: Simplified RealtimeQuerySubscriptionImpl instantiation.
  • RealtimeQuerySubscriptionImpl.kt: Switched to IdStringGenerator for request IDs and removed internal ID generation logic.
  • LiveQuery.kt: Refactored to use IdStringGenerator for query request IDs.
  • IdStringGenerator.kt: (New file) Implements the process-unique ID generation logic.
  • DataConnectAuthUnitTest.kt: Updated to provide an IdStringGenerator in tests.
  • FirebaseDataConnectImplUnitTest.kt: Updated to provide an IdStringGenerator in tests.
  • MutationRefImplUnitTest.kt: Refactored to use IdStringGenerator and removed tests specific to SecureRandom seeding.
  • arbs.kt: Removed SecureRandom arbitraries from Kotest property tests.
  • IdStringGeneratorUnitTest.kt: (New file) Comprehensive unit tests for the new ID generation logic, including concurrency and uniqueness tests.

dconeybe added 29 commits May 11, 2026 18:02
…sDataConnectGrpcStreamingServer.kt and TurbineUtils.kt

This enables these classes to be used from both `androidTest` to `test`
(thanks to the `@SharedWithAndroidTest` annotation and the associated gradle plugin)
instead of just in `androidTest`. A future test will make use of this.
… streaming connection with the backend for realtime queries.
…le(): T` (moved from ConnectRPCIntegrationTest.kt)
…ared errors with itself instead of with the expected list [skip actions]
…sts if ConnectorStreamServiceCoroutineStub.connect() throws an exception
…tineScope() and CoroutineScope.createChildSupervisorScope() and modified the code base to use them

This change improves the "dryness" of the code by factoring out repeated
logic into helper methods. This will also benefit future code that also
wants to create "child" CoroutineScope objects.
…neration [skip actions]

Reverts the use of `SecureRandom` (introduced in #7910) for generating request IDs.

`SecureRandom` was adopted to ensure unique IDs, but it does not guarantee uniqueness. Furthermore, it introduces performance overhead and can block threads waiting for entropy, which required offloading ID generation to an I/O dispatcher. Security-grade randomness is not required for this feature.

This change switches back to standard `Random` and uses an algorithm that guarantees process-wide uniqueness for request IDs. This simplifies the code by removing the need for background thread execution and lazy initialization.
@dconeybe dconeybe self-assigned this May 13, 2026
@dconeybe
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces SecureRandom with Random across the firebase-dataconnect module to improve performance in non-security-sensitive contexts and introduces a new nextIdString utility for generating unique process-wide IDs. Feedback highlighted critical issues in the nextIdString implementation, specifically regarding potential ID collisions caused by incorrect hexadecimal padding for large sequence numbers and the inclusion of hexadecimal digits in the random padding alphabet. It is recommended to use Long.toString(16) for simplicity and correctness, and to restrict the padding alphabet to non-hexadecimal characters to guarantee uniqueness.

@dconeybe
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces SecureRandom with Random across the firebase-dataconnect module to improve performance in non-security-sensitive contexts. It introduces a new utility, nextIdString, which ensures process-wide uniqueness by combining an atomic sequence number with random padding. Review feedback suggests a minor grammatical fix in the CHANGELOG and recommends using the class-level random instance instead of the global Random companion object in FirebaseDataConnectImpl.kt to enhance consistency and testability.

Comment thread firebase-dataconnect/CHANGELOG.md Outdated
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces SecureRandom with Random throughout the Data Connect SDK to optimize performance in non-security-sensitive contexts. It introduces a new nextIdString utility in IdStringGenerator.kt that ensures process-wide uniqueness by combining a prefix, random padding, and an atomic sequence number. The reviewer suggested using the injected random property instead of the global Random companion object in FirebaseDataConnectImpl to enhance testability and maintain consistency across the codebase.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces SecureRandom with Random throughout the firebase-dataconnect module, as high-security randomness is not required for the current use cases. A new utility function, nextIdString, has been introduced to generate unique process-wide identifiers using an atomic sequence and random padding. Feedback suggests utilizing the injected random instance within FirebaseDataConnectImpl rather than Random.Default to improve testability and maintain consistency.

@dconeybe dconeybe marked this pull request as ready for review May 13, 2026 04:04
Base automatically changed from dconeybe/dataconnect/CoroutineUtilsMoreHelpers to main May 13, 2026 17:28
@dconeybe
Copy link
Copy Markdown
Contributor Author

/gemini review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 13, 2026

📝 PRs merging into main branch

Our main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the use of SecureRandom and various ad-hoc ID generation methods with a new IdStringGenerator utility to improve performance and consistency across the SDK. The new utility generates unique IDs by combining a prefix, random padding from a specific character set, and an atomic sequence number. The changes affect multiple components, including Auth, AppCheck, logging, and query management. Feedback suggests that if strict process-wide uniqueness is required, a shared generator or a static sequence number should be considered to further minimize collision risks across multiple instances.

@dconeybe dconeybe requested a review from aashishpatil-g May 13, 2026 19:51
@dconeybe dconeybe merged commit 9f88524 into main May 13, 2026
48 checks passed
@dconeybe dconeybe deleted the dconeybe/dataconnect/InsecureRandom branch May 13, 2026 20:56
@github-actions github-actions Bot mentioned this pull request May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants