Use cryptographically secure random for new document ids#1162
Use cryptographically secure random for new document ids#1162probicheaux wants to merge 1 commit intogoogleapis:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @probicheaux, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the security posture of automatically generated Firestore document IDs by switching to a cryptographically secure random number generator. This change ensures that document IDs are more robust against prediction and potential exploits, while also maintaining compatibility with diverse operating environments through a thoughtful fallback mechanism. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request improves security by using a cryptographically secure random number generator (random.SystemRandom) for creating new document IDs. The change includes a fallback to the standard random module for compatibility with older systems.
However, the unit tests for _auto_id have not been updated to reflect this change, which is a critical issue that needs to be addressed. I've also included a suggestion to refactor the _auto_id function to reduce code duplication and improve maintainability.
| try: | ||
| return "".join(system_random.choice(_AUTO_ID_CHARS) for _ in range(20)) | ||
| # Very old Unix systems don't have os.urandom (/dev/urandom), in which case use random.choice | ||
| except NotImplementedError: | ||
| return "".join(random.choice(_AUTO_ID_CHARS) for _ in range(20)) |
There was a problem hiding this comment.
The existing unit test test__auto_id in tests/unit/v1/test_base_collection.py mocks random.choice. With this change, the test will likely fail as the code now primarily calls system_random.choice. The tests need to be updated to cover both the primary path (using system_random.choice) and the fallback path (using random.choice when NotImplementedError is raised).
| try: | ||
| return "".join(system_random.choice(_AUTO_ID_CHARS) for _ in range(20)) | ||
| # Very old Unix systems don't have os.urandom (/dev/urandom), in which case use random.choice | ||
| except NotImplementedError: | ||
| return "".join(random.choice(_AUTO_ID_CHARS) for _ in range(20)) |
There was a problem hiding this comment.
While the current implementation is correct, it contains duplicated code for generating the random string. To improve maintainability, you could refactor this to select the appropriate random function first, and then use it to generate the string.
try:
# Use a cryptographically secure random number generator if available.
# The .random() method will raise NotImplementedError on systems without os.urandom().
system_random.random()
choice_func = system_random.choice
except NotImplementedError:
# Fallback to the default pseudo-random generator on very old systems.
choice_func = random.choice
return "".join(choice_func(_AUTO_ID_CHARS) for _ in range(20))There was a problem hiding this comment.
The change looks good to me, but I think the coverage check will likely fail, because the new except case isn't covered by unit tests. So you'll likely have to add a new one, with random.SystemRandom mocked to raise an exception
I triggered the CI tests, so we'll see what comes up
Thanks for submitting this
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #1161