Use cryptographically secure random bytes in RandomStringTrait#1176
Merged
steinkel merged 2 commits into16.next-cake5from Apr 12, 2026
Merged
Use cryptographically secure random bytes in RandomStringTrait#1176steinkel merged 2 commits into16.next-cake5from
steinkel merged 2 commits into16.next-cake5from
Conversation
str_shuffle() relies on mt_rand which is not cryptographically secure. Replace with random_bytes() so placeholder passwords for social-login users cannot be predicted. Output changes from alphanumeric [a-zA-Z0-9] to hex [0-9a-f].
There was a problem hiding this comment.
Pull request overview
This PR replaces str_shuffle()-based random string generation with random_bytes() to ensure placeholder passwords (e.g., for social-login users) are generated using cryptographically secure randomness.
Changes:
- Updated
RandomStringTrait::randomString()to generate strings viarandom_bytes()andbin2hex(). - Expanded the trait’s unit tests to validate length handling and to assert hex-only output.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/Traits/RandomStringTrait.php |
Switches random string generation from str_shuffle() to bin2hex(random_bytes()), changing output charset to hex. |
tests/TestCase/Traits/RandomStringTraitTest.php |
Splits/extends tests to validate defaulting behavior and new hex output expectations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Replace bin2hex(random_bytes()) with random_int() over the full 62-char alphabet to maintain ~59 bits of entropy at default length (vs ~40 bits with hex output). Cast $length to int after validation and add odd-length and length-assertion tests.
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.
str_shuffle() relies on mt_rand which is not cryptographically secure. Replace with random_bytes() so placeholder passwords for social-login users cannot be predicted.
Output changes from alphanumeric [a-zA-Z0-9] to hex [0-9a-f].