Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions passlib/handlers/bcrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,25 @@ def detect_wrap_bug(ident):
NOTE: if in future we need to deliberately create hashes which have this bug,
can use something like 'hashpw(repeat_string(secret[:((1+secret) % 256) or 1]), 72)'
"""
# check if it exhibits wraparound bug
secret = (b"0123456789" * 26)[:255]
bug_hash = (
ident.encode("ascii")
+ b"04$R1lJ2gkNaoPGdafE.H.16.nVyh2niHsGJhayOHLMiXlI45o8/DU.6"
)
if verify(secret, bug_hash):
return True

# if it doesn't have wraparound bug, make sure it *does* handle things
# correctly -- or we're in some weird third case.
# Secret which will trip the wraparound bug, if present
secret = (b"0123456789"*26)[:255]

# Python bcrypt >= 5.0.0 will raise an exception on passwords greater than 72 characters,
# whereas earlier versions without the wraparound bug silently truncated the input to 72
# characters. See if the exception is generated.
try:
bug_hash = ident.encode("ascii") + b"04$R1lJ2gkNaoPGdafE.H.16.nVyh2niHsGJhayOHLMiXlI45o8/DU.6"

# If we get here, the backend auto-truncates, test for wraparound bug
if verify(secret, bug_hash):
return True
except ValueError:
# Backend explicitly will not auto-truncate, truncate the password to 72 characters
secret = secret[:72]

# Check to make sure that the backend still hashes correctly; if not, we're in a failure case
# not related to the original wraparound bug or bcrypt >= 5.0.0 input length restriction.
correct_hash = (
ident.encode("ascii")
+ b"04$R1lJ2gkNaoPGdafE.H.16.1MKHPvmKwryeulRe225LKProWYwt9Oi"
Expand Down Expand Up @@ -607,11 +615,20 @@ def _load_backend_mixin(mixin_cls, name, dryrun):
import bcrypt as _bcrypt
except ImportError: # pragma: no cover
return False

# Attempt to get bcrypt backend version
version = '<unknown>'

try:
version = metadata.version("bcrypt")
except Exception:
logger.warning("(trapped) error reading bcrypt version", exc_info=True)
version = "<unknown>"
# "New style" (793bef 2023-11-23) version
version = _bcrypt.__version__
except:
try:
# Old style verion
version = _bcrypt.__about__.__version__
except:
# Can't find version, leave it as '<unknown>'
log.warning("(trapped) error reading bcrypt version", exc_info=True)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Do we have to do this instead of using packaging.metadata.version? 🤔

P.S. The updated hash check looks fine though

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I only did the check that way as metadata.version() failed on a random old install I tested on. The above works in both cases.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

On what version specifically does it fail? 🤔 From what I know metadata.version/distribution should get the info from .dist-info, maybe that specific version is missing some metadata or isn't packaged "correctly".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Old Slackware 14.2 i486 install, I'd have to start the machine up to check passlib/bcrypt versions. If it's guaranteed functionality/just some packaging aberration switch it back to packaging.metadata.version (or I can do it and amend the PR!)


logger.debug("detected 'bcrypt' backend, version %r", version)
return mixin_cls._finalize_backend_mixin(name, dryrun)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ argon2 = [
"argon2-cffi>=18.2.0",
]
bcrypt = [
"bcrypt<5.0.0",
"bcrypt>=3.1.0",
]
totp = [
"cryptography>=43.0.1",
Expand Down
Loading