-
Notifications
You must be signed in to change notification settings - Fork 86
Use cryptographically secure random for new document ids #1162
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ | |
| import datetime | ||
|
|
||
| _AUTO_ID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | ||
| system_random = random.SystemRandom() | ||
|
|
||
|
|
||
| class BaseCollectionReference(Generic[QueryType]): | ||
|
|
@@ -623,8 +624,11 @@ def _auto_id() -> str: | |
| str: A 20 character string composed of digits, uppercase and | ||
| lowercase and letters. | ||
| """ | ||
|
|
||
| return "".join(random.choice(_AUTO_ID_CHARS) for _ in range(20)) | ||
| 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)) | ||
|
Comment on lines
+627
to
+631
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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))
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is slower There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, this is slower |
||
|
|
||
|
|
||
| def _item_to_document_ref(collection_reference, item): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing unit test
test__auto_idintests/unit/v1/test_base_collection.pymocksrandom.choice. With this change, the test will likely fail as the code now primarily callssystem_random.choice. The tests need to be updated to cover both the primary path (usingsystem_random.choice) and the fallback path (usingrandom.choicewhenNotImplementedErroris raised).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed