Skip to content

Tweak PKey.get_fingerprint to use string-ish hexdigest, not bytes-ish digest. #726

Open

Description

@johnthagen

If the following code using paramiko 0.16.0:

import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('127.0.0.1', username='user', password='password')

Is run in Python 3.5.1 with BytesWarnings enabled (-bb flag), an error is triggered:

$ python3 -bb para.py
Traceback (most recent call last):
  File "para.py", line 5, in <module>
    client.connect('127.0.0.1', username='user', password='ironmanmachine')
  File "/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 348, in connect
    server_key)
  File "/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 623, in missing_host_key
    (key.get_name(), hostname, hexlify(key.get_fingerprint())))
BytesWarning: str() on a bytes instance

Activity

  1. bitprophet commented on Apr 25, 2016

    @bitprophet
    Member

    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.

  2. bitprophet commented on Apr 25, 2016

    @bitprophet
    Member

    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).

  3. added this to the milestone on Apr 25, 2016
  4. johnthagen commented on Apr 25, 2016

    @johnthagen
    Author

    @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.

  5. bitprophet commented on Apr 26, 2016

    @bitprophet
    Member

    Good to know - thanks!

  6. 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
  7. bitprophet commented on Feb 21, 2017

    @bitprophet
    Member

    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.)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions