-
Notifications
You must be signed in to change notification settings - Fork 8.1k
ext/sodium: throw ValueError for pwhash argument errors #22388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1429,23 +1429,28 @@ PHP_FUNCTION(sodium_crypto_pwhash) | |||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (hash_len <= 0) { | ||||||
| zend_argument_error(sodium_exception_ce, 1, "must be greater than 0"); | ||||||
| zend_argument_value_error(1, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (hash_len >= 0xffffffff) { | ||||||
| zend_argument_error(sodium_exception_ce, 1, "is too large"); | ||||||
| zend_argument_value_error(1, "is too large"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(or whatever the correct format specifier is) |
||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (passwd_len >= 0xffffffff) { | ||||||
| zend_argument_error(sodium_exception_ce, 2, "is too long"); | ||||||
| zend_argument_value_error(2, "is too long"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (opslimit <= 0) { | ||||||
| zend_argument_error(sodium_exception_ce, 4, "must be greater than 0"); | ||||||
| zend_argument_value_error(4, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (memlimit <= 0 || memlimit > SIZE_MAX) { | ||||||
| zend_argument_error(sodium_exception_ce, 5, "must be greater than 0"); | ||||||
| zend_argument_value_error(5, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (alg != crypto_pwhash_ALG_ARGON2I13 | ||||||
|
|
@@ -1460,15 +1465,18 @@ PHP_FUNCTION(sodium_crypto_pwhash) | |||||
| zend_error(E_WARNING, "empty password"); | ||||||
| } | ||||||
| if (salt_len != crypto_pwhash_SALTBYTES) { | ||||||
| zend_argument_error(sodium_exception_ce, 3, "must be SODIUM_CRYPTO_PWHASH_SALTBYTES bytes long"); | ||||||
| zend_argument_value_error(3, "must be SODIUM_CRYPTO_PWHASH_SALTBYTES bytes long"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (opslimit < crypto_pwhash_OPSLIMIT_MIN) { | ||||||
| zend_argument_error(sodium_exception_ce, 4, "must be greater than or equal to %d", crypto_pwhash_OPSLIMIT_MIN); | ||||||
| zend_argument_value_error(4, "must be greater than or equal to %d", crypto_pwhash_OPSLIMIT_MIN); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (memlimit < crypto_pwhash_MEMLIMIT_MIN) { | ||||||
| zend_argument_error(sodium_exception_ce, 5, "must be greater than or equal to %d", crypto_pwhash_MEMLIMIT_MIN); | ||||||
| zend_argument_value_error(5, "must be greater than or equal to %d", crypto_pwhash_MEMLIMIT_MIN); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| hash = zend_string_alloc((size_t) hash_len, 0); | ||||||
|
|
@@ -1513,26 +1521,31 @@ PHP_FUNCTION(sodium_crypto_pwhash_str) | |||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (opslimit <= 0) { | ||||||
| zend_argument_error(sodium_exception_ce, 2, "must be greater than 0"); | ||||||
| zend_argument_value_error(2, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (memlimit <= 0 || memlimit > SIZE_MAX) { | ||||||
| zend_argument_error(sodium_exception_ce, 3, "must be greater than 0"); | ||||||
| zend_argument_value_error(3, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (passwd_len >= 0xffffffff) { | ||||||
| zend_argument_error(sodium_exception_ce, 1, "is too long"); | ||||||
| zend_argument_value_error(1, "is too long"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (passwd_len <= 0) { | ||||||
| zend_error(E_WARNING, "empty password"); | ||||||
| } | ||||||
| if (opslimit < crypto_pwhash_OPSLIMIT_MIN) { | ||||||
| zend_argument_error(sodium_exception_ce, 2, "must be greater than or equal to %d", crypto_pwhash_OPSLIMIT_MIN); | ||||||
| zend_argument_value_error(2, "must be greater than or equal to %d", crypto_pwhash_OPSLIMIT_MIN); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (memlimit < crypto_pwhash_MEMLIMIT_MIN) { | ||||||
| zend_argument_error(sodium_exception_ce, 3, "must be greater than or equal to %d", crypto_pwhash_MEMLIMIT_MIN); | ||||||
| zend_argument_value_error(3, "must be greater than or equal to %d", crypto_pwhash_MEMLIMIT_MIN); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| hash_str = zend_string_alloc(crypto_pwhash_STRBYTES - 1, 0); | ||||||
|
|
@@ -1619,30 +1632,36 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256) | |||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (hash_len <= 0 || hash_len >= ZSTR_MAX_LEN || hash_len > 0x1fffffffe0ULL) { | ||||||
| zend_argument_error(sodium_exception_ce, 1, "must be greater than 0"); | ||||||
| zend_argument_value_error(1, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (opslimit <= 0) { | ||||||
| zend_argument_error(sodium_exception_ce, 4, "must be greater than 0"); | ||||||
| zend_argument_value_error(4, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (memlimit <= 0 || memlimit > SIZE_MAX) { | ||||||
| zend_argument_error(sodium_exception_ce, 5, "must be greater than 0"); | ||||||
| zend_argument_value_error(5, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (passwd_len <= 0) { | ||||||
| zend_error(E_WARNING, "empty password"); | ||||||
| } | ||||||
| if (salt_len != crypto_pwhash_scryptsalsa208sha256_SALTBYTES) { | ||||||
| zend_argument_error(sodium_exception_ce, 3, "must be SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES bytes long"); | ||||||
| zend_argument_value_error(3, "must be SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES bytes long"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (opslimit < crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE) { | ||||||
| zend_argument_error(sodium_exception_ce, 4, "must be greater than or equal to %d", crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE); | ||||||
| zend_argument_value_error(4, "must be greater than or equal to %d", crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (memlimit < crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE) { | ||||||
| zend_argument_error(sodium_exception_ce, 5, "must be greater than or equal to %d", crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE); | ||||||
| zend_argument_value_error(5, "must be greater than or equal to %d", crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| hash = zend_string_alloc((size_t) hash_len, 0); | ||||||
|
|
@@ -1674,22 +1693,26 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256_str) | |||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (opslimit <= 0) { | ||||||
| zend_argument_error(sodium_exception_ce, 2, "must be greater than 0"); | ||||||
| zend_argument_value_error(2, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (memlimit <= 0 || memlimit > SIZE_MAX) { | ||||||
| zend_argument_error(sodium_exception_ce, 3, "must be greater than 0"); | ||||||
| zend_argument_value_error(3, "must be greater than 0"); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (passwd_len <= 0) { | ||||||
| zend_error(E_WARNING, "empty password"); | ||||||
| } | ||||||
| if (opslimit < crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE) { | ||||||
| zend_argument_error(sodium_exception_ce, 2, "must be greater than or equal to %d", crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE); | ||||||
| zend_argument_value_error(2, "must be greater than or equal to %d", crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| if (memlimit < crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE) { | ||||||
| zend_argument_error(sodium_exception_ce, 3, "must be greater than or equal to %d", crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE); | ||||||
| zend_argument_value_error(3, "must be greater than or equal to %d", crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE); | ||||||
| sodium_remove_param_values_from_backtrace(EG(exception)); | ||||||
| RETURN_THROWS(); | ||||||
| } | ||||||
| hash_str = zend_string_alloc | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --TEST-- | ||
| sodium pwhash argument errors throw ValueError and keep the whole backtrace scrubbed | ||
| --EXTENSIONS-- | ||
| sodium | ||
| --SKIPIF-- | ||
| <?php | ||
| if (!defined('SODIUM_CRYPTO_PWHASH_SALTBYTES')) print "skip libsodium without argon2"; | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| function wrap(string $password): void | ||
| { | ||
| sodium_crypto_pwhash_str($password, SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, 1); | ||
| } | ||
|
|
||
| $secret = "hunter2-secret"; | ||
| wrap($secret); | ||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Uncaught ValueError: sodium_crypto_pwhash_str(): Argument #3 ($memlimit) must be greater than or equal to %d in %s:%d | ||
| Stack trace: | ||
| #0 %s(%d): sodium_crypto_pwhash_str() | ||
| #1 %s(%d): wrap() | ||
| #2 {main} | ||
| thrown in %s on line %d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not empty ValueError API.