Skip to content

get random in UUID more cryptography secure #135226

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LamentXU123
Copy link

random.getrandbits() is used widely in lib uuid espectially in generating clock_seqs in UUID v1 and v6

if 624*32//14+1 UUIDs are leaked to hackers, they could predict the next UUID generated.

reference: https://github.com/LamentXU123/cve/blob/main/UUID.md

to make it more cryptography secure, secrets.randbits() should be used instead of random.getrandbits()

@python-cla-bot
Copy link

python-cla-bot bot commented Jun 6, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jun 6, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@LamentXU123 LamentXU123 changed the title get random in UUID more cryptography securty get random in UUID more cryptography secure Jun 6, 2025
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

uuid1() already has security warnings, and the rest that were changed here are documented as pseudo-random. I don't think we need to change anything here, apart from maybe adding some extra documentation notes.

@LamentXU123
Copy link
Author

You're right. But I think changing them to a more secure way has no disadvantage. Why not make it more unpredictable with no side effects?

@ZeroIntensity
Copy link
Member

Well, there are clearly side-effects. The UUID tests are totally broken by this change.

@LamentXU123
Copy link
Author

Well it seems like its broken because the test script uses random.getrandbits()

for example:

    def test_uuid1_time(self):
        with mock.patch.object(self.uuid, '_generate_time_safe', None), \
             mock.patch.object(self.uuid, '_last_timestamp', None), \
             mock.patch.object(self.uuid, 'getnode', return_value=93328246233727), \
             mock.patch('time.time_ns', return_value=1545052026752910643), \
             mock.patch('random.getrandbits', return_value=5317): # guaranteed to be random
            u = self.uuid.uuid1()
            self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f'))

this will triger a assertion error because random.getrandbits() was changed to secrets.randbits() so teh script could not control the random number by patching ;)

I'll take a closer look soon

@LamentXU123
Copy link
Author

Well it seems like its broken because the test script uses random.getrandbits()

for example:

    def test_uuid1_time(self):
        with mock.patch.object(self.uuid, '_generate_time_safe', None), \
             mock.patch.object(self.uuid, '_last_timestamp', None), \
             mock.patch.object(self.uuid, 'getnode', return_value=93328246233727), \
             mock.patch('time.time_ns', return_value=1545052026752910643), \
             mock.patch('random.getrandbits', return_value=5317): # guaranteed to be random
            u = self.uuid.uuid1()
            self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f'))

this will triger a assertion error because random.getrandbits() was changed to secrets.randbits() so the script could not control the random number by patching ;)

I'll take a closer look soon

@LamentXU123
Copy link
Author

Maybe I could add a parameter to uuid1() and uuid6() which is False by default, and if its true, using secret.randbits() instead of random.getrandbits() ?

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