Replace rand() with random_bytes() for SQL placeholder generation#595
Open
flightlesstux wants to merge 1 commit intopassbolt:masterfrom
Open
Replace rand() with random_bytes() for SQL placeholder generation#595flightlesstux wants to merge 1 commit intopassbolt:masterfrom
flightlesstux wants to merge 1 commit intopassbolt:masterfrom
Conversation
getCaseSensitiveValue() and getCaseSensitiveValues() used rand() to generate unique named placeholder suffixes for CakePHP's query binder. rand() is a pseudo-random function seeded from a low-entropy source; under concurrent load or on platforms with a weak PRNG, it can produce collisions that cause the query binder to overwrite a previously bound value, silently corrupting query results. Replace rand() with bin2hex(random_bytes(4)) which uses the OS CSPRNG, produces 8 hex characters of uniform output, and has a collision probability of 1 in 4 billion per query — negligible even under high concurrency.
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
CaseSensitiveCompareValueTraitusesrand()to generate unique named placeholder suffixes when binding values for case-sensitive MySQL/MariaDB comparisons:rand()is a non-cryptographic PRNG. Under concurrent load, or on platforms where the PHP PRNG has low entropy, duplicate values are possible. A collision causes CakePHP's query binder to silently overwrite a previously bound value, producing incorrect query results without raising an error.Fix
Replace
rand()withbin2hex(random_bytes(4))which reads from the OS CSPRNG:This produces 8 hex characters (32 bits of uniform entropy), giving a collision probability of ~1 in 4 billion per invocation — negligible even under high concurrency.
Applied to both
getCaseSensitiveValue()andgetCaseSensitiveValues().Impact
random_bytes()is available in all PHP versions ≥ 7.0