You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tweak PKey.get_fingerprint to use string-ish hexdigest, not bytes-ish digest. #726
Is this causing an actual, real-world problem for you somewhere? I don't normally see people running with BytesWarning enabled.
I dug briefly out of curiosity and did find that e.g. RSAKey.__str__ returns self.asbytes() and so a simple print(key) spits out a nice TypeError: __str__ returned non-string (type bytes).
But that may be orthogonal since we're never actually str'ing the key itself, only the results of .get_name (returns a str obj), hostname (hopefully a str) and hexlify (oh hey! this, and the result of PKey.get_fingerprint that it operates on, are bytes. Huh.)
Docstring for get_fingerprint implies it returns a string, but the entire method's blame output shows it untouched by the big Python 3 upgrade, so I don't think anybody has yet had to confront whether this should legitimately be spitting out a string or bytes object. (Of course, for debugging purposes, we still need some string representation of it.)
get_fingerprint is implemented as simply md5(self.asbytes()).digest(). Under Python 2, md5(xxx).digest() prints a string representation of bytes; under Python 3, it's a bytes object.
Problem is it's not bytes representing encoded text, so the usual "turn a bytes into a unicode" suspects will all get pissy with us. Asking for a repr instead is kinda fugly because it doubles up all the backslashes, etc (vs what a Python 2 user sees natively).
At this point my eyes glaze over and I wonder how important it is to most users that get_fingerprint returns the result of .digest() and not, say, .hexdigest() - which is much more human-friendly. In fact - the logging line is already doing basically that by calling binascii.hexlify. This may thus just be due to ancient code prior to changes letting us use hashlib.
Actually - the final piece seems to be that hexlify on Python 3 takes a bytes-in-bytes-out approach, leaving us with a bytes object (albeit one that does nicely .decode() into a string/unicode object). Sure enough, if I replace hexlify(key.get_fingerprint()) with md5(key.asbytes()).hexdigest(), the BytesWarning goes away (well, until the next log line that does similar crap.)
Still unsure whether we should adapt get_fingerprint to return hexdigest or if there are users out there expecting the "raw" byte form. Tempted to simply add a second method e.g. get_hex_fingerprint. Will poke some, even though I've already wasted too much time on this.
Ugh, yea. There are literally no mentions of get_fingerprint in our own codebase that are not wrapped with hexlify. Copypasta is the worst :(
Given the propensity for this hurting some downstream users (e.g. even a search of our own tracker finds code from another lib, see #513 (comment)) I'll stick it in the 3.0 milestone which will contain API breaking changes. Will then update get_fingerprint to return hexdigest() instead of digest() (and remove all the friggin hexlify).
@bitprophet To answer your first question, it is not causing an actual problem. We always test our code with -bb because we want to catch BytesWarning's early as they are often a symptom of another problem. So the real-world affect on us is we need to catch the BytesWarning's generated from paramiko. Not a huge deal, but wanted to share as I expected (and seems to be the case) it pointed to a larger issue.
Thanks for taking the time to look into this, and 3.0 milestone sounds like a great plan.
changed the title [-]BytesWarning triggered in SSHClient.connect()[/-][+]Tweak `PKey.get_fingerprint` to use string-ish hexdigest, not bytes-ish digest.[/+]on Apr 26, 2016
Also, note to self, when this goes in it'd be a great time to make sure that __str__ returns a fingerprint (or the private key text, or something along those lines) instead of "my bytes, which are basically useless printed to a human raw". And also that said fingerprint is of the ab:cd:ef:12:34 form that every other freakin' SSH tool uses. Because ugh, why isn't that in yet. (In my experience, even the hexlify stuff doesn't quite get us there.)
Activity
bitprophet commentedon Apr 25, 2016
Is this causing an actual, real-world problem for you somewhere? I don't normally see people running with
BytesWarningenabled.I dug briefly out of curiosity and did find that e.g.
RSAKey.__str__returnsself.asbytes()and so a simpleprint(key)spits out a niceTypeError: __str__ returned non-string (type bytes).But that may be orthogonal since we're never actually
str'ing the key itself, only the results of.get_name(returns a str obj),hostname(hopefully a str) andhexlify(oh hey! this, and the result ofPKey.get_fingerprintthat it operates on, are bytes. Huh.)Docstring for
get_fingerprintimplies it returns a string, but the entire method's blame output shows it untouched by the big Python 3 upgrade, so I don't think anybody has yet had to confront whether this should legitimately be spitting out a string or bytes object. (Of course, for debugging purposes, we still need some string representation of it.)get_fingerprintis implemented as simplymd5(self.asbytes()).digest(). Under Python 2,md5(xxx).digest()prints a string representation of bytes; under Python 3, it's a bytes object.Problem is it's not bytes representing encoded text, so the usual "turn a
bytesinto aunicode" suspects will all get pissy with us. Asking for areprinstead is kinda fugly because it doubles up all the backslashes, etc (vs what a Python 2 user sees natively).At this point my eyes glaze over and I wonder how important it is to most users that
get_fingerprintreturns the result of.digest()and not, say,.hexdigest()- which is much more human-friendly. In fact - the logging line is already doing basically that by callingbinascii.hexlify. This may thus just be due to ancient code prior to changes letting us usehashlib.Actually - the final piece seems to be that
hexlifyon Python 3 takes a bytes-in-bytes-out approach, leaving us with a bytes object (albeit one that does nicely.decode()into a string/unicode object). Sure enough, if I replacehexlify(key.get_fingerprint())withmd5(key.asbytes()).hexdigest(), theBytesWarninggoes away (well, until the next log line that does similar crap.)Still unsure whether we should adapt
get_fingerprintto returnhexdigestor if there are users out there expecting the "raw" byte form. Tempted to simply add a second method e.g.get_hex_fingerprint. Will poke some, even though I've already wasted too much time on this.bitprophet commentedon Apr 25, 2016
Ugh, yea. There are literally no mentions of
get_fingerprintin our own codebase that are not wrapped withhexlify. Copypasta is the worst :(Given the propensity for this hurting some downstream users (e.g. even a search of our own tracker finds code from another lib, see #513 (comment)) I'll stick it in the 3.0 milestone which will contain API breaking changes. Will then update
get_fingerprintto returnhexdigest()instead ofdigest()(and remove all the frigginhexlify).johnthagen commentedon Apr 25, 2016
@bitprophet To answer your first question, it is not causing an actual problem. We always test our code with
-bbbecause we want to catchBytesWarning's early as they are often a symptom of another problem. So the real-world affect on us is we need to catch theBytesWarning's generated fromparamiko. Not a huge deal, but wanted to share as I expected (and seems to be the case) it pointed to a larger issue.Thanks for taking the time to look into this, and 3.0 milestone sounds like a great plan.
bitprophet commentedon Apr 26, 2016
Good to know - thanks!
[-]BytesWarning triggered in SSHClient.connect()[/-][+]Tweak `PKey.get_fingerprint` to use string-ish hexdigest, not bytes-ish digest.[/+]bitprophet commentedon Feb 21, 2017
Also, note to self, when this goes in it'd be a great time to make sure that
__str__returns a fingerprint (or the private key text, or something along those lines) instead of "my bytes, which are basically useless printed to a human raw". And also that said fingerprint is of theab:cd:ef:12:34form that every other freakin' SSH tool uses. Because ugh, why isn't that in yet. (In my experience, even thehexlifystuff doesn't quite get us there.)