Skip to content

Use cryptographically secure random bytes in RandomStringTrait#1176

Merged
steinkel merged 2 commits into16.next-cake5from
fix/random-string-trait
Apr 12, 2026
Merged

Use cryptographically secure random bytes in RandomStringTrait#1176
steinkel merged 2 commits into16.next-cake5from
fix/random-string-trait

Conversation

@steinkel
Copy link
Copy Markdown
Member

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].

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].
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 via random_bytes() and bin2hex().
  • 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.

Comment thread src/Traits/RandomStringTrait.php Outdated
Comment thread src/Traits/RandomStringTrait.php
Comment thread tests/TestCase/Traits/RandomStringTraitTest.php Outdated
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.
@steinkel steinkel merged commit c0cf3c0 into 16.next-cake5 Apr 12, 2026
26 checks passed
@steinkel steinkel deleted the fix/random-string-trait branch April 12, 2026 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants