Fix CallerReference collision in unique_string() using uuid4#10287
Open
shreyaskommuri wants to merge 1 commit intoaws:developfrom
Open
Fix CallerReference collision in unique_string() using uuid4#10287shreyaskommuri wants to merge 1 commit intoaws:developfrom
shreyaskommuri wants to merge 1 commit intoaws:developfrom
Conversation
unique_string() previously combined a 1-second Unix timestamp with random.randint(1, 1000000). Two invocations within the same second had a 1-in-1,000,000 chance of generating an identical CallerReference, causing CloudFront to silently drop the duplicate create-invalidation or create-distribution request. The Mersenne Twister RNG used by the random module is also not appropriate for generating values that are expected to be unique and unpredictable. Replace the implementation with uuid.uuid4(), which provides 122 bits of cryptographically random entropy and guarantees uniqueness regardless of call frequency. uuid is part of the Python standard library and requires no new dependencies. Fixes: aws#10281
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
unique_string()inawscli/customizations/cloudfront.pygenerates theCallerReferenceforcreate-invalidationandcreate-distributionusingrandom.randint(1, 1000000)combined with a Unix timestamp at 1-secondprecision:
Two invocations within the same second share the same timestamp and have a
1-in-1,000,000 chance of producing an identical
CallerReference. Whenthis occurs CloudFront silently treats the second request as a duplicate and
does not create a new invalidation or distribution.
Additionally, Python's
randommodule uses the Mersenne Twister algorithm andis not cryptographically secure, which is inappropriate for values that are
expected to be unique and unpredictable.
Closes #10281.
Solution
Replace the implementation with
uuid.uuid4(), which provides 122 bits ofcryptographically random entropy. This eliminates both the timestamp dependency
and the collision risk entirely.
uuidis part of the Python standard libraryand requires no new dependencies.
Changes
awscli/customizations/cloudfront.py— replaceimport time/import randomwith
import uuid; rewriteunique_string()to useuuid.uuid4()tests/unit/customizations/test_cloudfront.py— new unit tests verifyingthe UUID4 format, custom prefix support, and uniqueness across 1 000 calls
Testing
Generated by AI tools, and reviewed by shreyaskommuri