Skip to content

fix: reject hashes containing NUL bytes in compare#1225

Open
abhu85 wants to merge 1 commit intokelektiv:masterfrom
abhu85:fix/1224-nul-byte-validation
Open

fix: reject hashes containing NUL bytes in compare#1225
abhu85 wants to merge 1 commit intokelektiv:masterfrom
abhu85:fix/1224-nul-byte-validation

Conversation

@abhu85
Copy link
Copy Markdown

@abhu85 abhu85 commented Apr 29, 2026

Summary

Reject bcrypt hashes containing NUL bytes (\x00) in compare() and compareSync() to prevent non-canonical hash validation.

Problem

When a hash string contains a NUL byte, the native C++ binding truncates at the first \x00 (via c_str()), causing non-canonical hashes like validHash + "\x00extra" to validate against the password. This is a security concern as it allows hash manipulation.

Solution

Add input validation in compare() and compareSync() to reject hash strings containing NUL bytes before passing to the native binding.

Test Plan

  • compare(pw, hash + "\x00extra") rejects
  • compareSync(pw, hash + "\x00extra") rejects
  • Normal compare/compareSync still works
  • Existing test suite passes (81/81)

Fixes #1224

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bcrypt.compare accepts NUL-byte suffixed hashes (non-canonical input)

1 participant