Fix GH-21673: password_verify() failed to verify bcrypt passwords containing null bytes#21675
Open
LamentXU123 wants to merge 2 commits intophp:masterfrom
Open
Fix GH-21673: password_verify() failed to verify bcrypt passwords containing null bytes#21675LamentXU123 wants to merge 2 commits intophp:masterfrom
LamentXU123 wants to merge 2 commits intophp:masterfrom
Conversation
devnexen
reviewed
Apr 8, 2026
| int status = 0; | ||
|
|
||
| /* password_hash() already rejects NUL bytes for bcrypt inputs. */ | ||
| if (memchr(ZSTR_VAL(password), '\0', ZSTR_LEN(password))) { |
Member
There was a problem hiding this comment.
nit: zend_str_has_nul_bytes(password)
Member
There was a problem hiding this comment.
or what can be done is modifying password argument handling in password_hash/verify possibly.
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.
Now, since including \0 in
password_hashis not allowed in PASSWORD_BCRYPT. It is reasonable to add checkers that, makes input that includes \0 inpassword_verifydirectly fails.This PR align
password_verify()withpassword_hash()for bcrypt by rejecting passwords containing NUL bytes before verification.Because bcrypt treats passwords as NUL-terminated C strings, this avoids ambiguous verification behavior for inputs such as "foo\0bar". A regression test is included.
Fix #21673
To be short: password encrypted by BYCRYPT should never includes \0. So directly rejecting it is always correct. It also avoids ambiguous verification where something like this happens: