Skip to content

Commit c59e075

Browse files
authored
password: Use php_random_bytes_throw in php_password_make_salt (#10393)
The CSPRNG failing should be rare nowadays, but it *might* happen and without this patch it's hard for the user to find out why the salt generation failed: The error message is not actionable. This patch will automatically set the CSPRNG exception to the `$previous` exception of the ValueError that is thrown, allowing the developer to determine the cause of the salt generation failure. Before: Fatal error: Uncaught ValueError: Unable to generate salt in php-src/test3.php:3 Stack trace: #0 php-src/test3.php(3): password_hash(Object(SensitiveParameterValue), '2y') #1 {main} thrown in php-src/test3.php on line 3 After: Fatal error: Uncaught Random\RandomException: Cannot open /dev/urandom: No such file or directory in php-src/test3.php:3 Stack trace: #0 php-src/test3.php(3): password_hash(Object(SensitiveParameterValue), '2y') #1 {main} Next ValueError: Unable to generate salt in php-src/test3.php:3 Stack trace: #0 php-src/test3.php(3): password_hash(Object(SensitiveParameterValue), '2y') #1 {main} thrown in php-src/test3.php on line 3
1 parent a7998fd commit c59e075

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ PHP NEWS
101101
. Make array_pad's $length warning less confusing. (nielsdos)
102102
. E_WARNING emitted by strtok in the caase both arguments are not provided when
103103
starting tokenisation. (David Carlier)
104+
. password_hash() will now chain the original RandomException to the ValueError
105+
on salt generation failure. (timwolla)
104106

105107
- Streams:
106108
. Fixed bug #51056: blocking fread() will block even if data is available.

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ PHP 8.3 UPGRADE NOTES
6969
can have. Before, it was only possible to add at most 1048576 elements at a
7070
time.
7171
. strtok() raises a warning in the case token is not provided when starting tokenization.
72+
. password_hash() will now chain the underlying Random\RandomException
73+
as the ValueError’s $previous Exception when salt generation fails.
7274

7375
========================================
7476
6. New Functions

ext/standard/password.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static zend_string* php_password_make_salt(size_t length) /* {{{ */
8383
}
8484

8585
buffer = zend_string_alloc(length * 3 / 4 + 1, 0);
86-
if (FAILURE == php_random_bytes_silent(ZSTR_VAL(buffer), ZSTR_LEN(buffer))) {
86+
if (FAILURE == php_random_bytes_throw(ZSTR_VAL(buffer), ZSTR_LEN(buffer))) {
8787
zend_value_error("Unable to generate salt");
8888
zend_string_release_ex(buffer, 0);
8989
return NULL;

0 commit comments

Comments
 (0)