Skip to content

Fix GH-21673: password_verify() failed to verify bcrypt passwords containing null bytes#21675

Open
LamentXU123 wants to merge 2 commits intophp:masterfrom
LamentXU123:vul-2
Open

Fix GH-21673: password_verify() failed to verify bcrypt passwords containing null bytes#21675
LamentXU123 wants to merge 2 commits intophp:masterfrom
LamentXU123:vul-2

Conversation

@LamentXU123
Copy link
Copy Markdown
Contributor

@LamentXU123 LamentXU123 commented Apr 8, 2026

Now, since including \0 in password_hash is not allowed in PASSWORD_BCRYPT. It is reasonable to add checkers that, makes input that includes \0 in password_verify directly fails.

This PR align password_verify() with password_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:

<?php
$hash = password_hash("secret", PASSWORD_BCRYPT);
var_dump(password_verify("secret" . chr(0) . "suffix", $hash)); //True, WTF?
?>

int status = 0;

/* password_hash() already rejects NUL bytes for bcrypt inputs. */
if (memchr(ZSTR_VAL(password), '\0', ZSTR_LEN(password))) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: zend_str_has_nul_bytes(password)

Copy link
Copy Markdown
Member

@devnexen devnexen Apr 8, 2026

Choose a reason for hiding this comment

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

or what can be done is modifying password argument handling in password_hash/verify possibly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

password_verify() failed to verify bcrypt passwords containing null bytes

2 participants