Skip to content

[BUG] - AttributeError(public_blob) #2462

Closed

Description

@paulf69487623

Are you using paramiko as a client or server?

Client

What feature(s) aren't working right?

Keys/auth

What version(s) of paramiko are you using?

3.5.0

What version(s) of Python are you using?

3.9.6

What operating system and version are you using?

MacOS Sonoma, RHEL 7, RHEL8

If you're connecting as a client, which SSH server are you connecting to?

OpenSSH

If you're using paramiko as part of another tool, which tool/version?

Fabric

Expected/desired behavior

Successful authentication

Actual behavior

An AttributeError: public_blob exception is being thrown by AgentKey.__getattr(self, name).

How to reproduce

import fabric

c = fabric.Connection('hostname')
output = c.run('hostname').stdout.strip()
print(output)

The code here isn't very interesting. This may only happen when using SSH certificates, and this doesn't seem very common.

There are several other bug reports that seem to be hitting this issue for various reasons. Eg, #2320 and #2299.

Anything else?

The primary issue seems to be how the inner_key value of the AgentKey class is being used.

In the __init__ method of AgentKey where self.inner_key is being initialized, there is a comment in the exception handler that inner_key is best-effort and to not explode. That tells me that its allowable for this value to be None.

In the __getattr__ method of AgentKey, the code checks if self.inner_key is None and raises an AttributeError. This matches the expected behavior for getattr as described at https://docs.python.org/3/library/functions.html#getattr.

The issue seems to be happening in the AuthHandler._get_key_type_and_bits method. This is the code in question:

# Use certificate contents, if available, plain pubkey otherwise
if key.public_blob:
    return key.public_blob.key_type, key.public_blob.key_blob
else:
    return key.get_name(), key

This code assumes that the public_blob attribute exists on key but might be None. Behind the scenes, this is calling the AgentKey.__getattr__ method. If inner_key isn't set, this causes the exception.

I've tested two fixes, and they both seem to work.

The first fix is to not throw an exception in AgentKey, and instead return None. That looks like this:

    def __getattr__(self, name):
        """
        Proxy any un-implemented methods/properties to the inner_key.
        """
        if self.inner_key is None:  # nothing to proxy to
            return None
        return getattr(self.inner_key, name)

While this works, it does seem to break the behavior described by the documentation for getattr.

The other fix, which is probably better, is to check for the attribute in AuthHandler. That would look like this:

# Use certificate contents, if available, plain pubkey otherwise
if hasattr(key, "public_blob"):
    return key.public_blob.key_type, key.public_blob.key_blob
else:
    return key.get_name(), key

The hasattr function calls getattr, and checks if it raises the AttributeError. The exception happens, but its handled gracefully and sends it down the else path successfully.

I'm happy to PR either of these fixes, but both are pretty trivial.

Thanks,

Paul

Activity

  1. added a commit that references this issue on Oct 9, 2024
    95d5159
  2. johannes85 commented on Oct 17, 2024

    @johannes85

    +1 for solving this issue, which already has cost me a couple of hours.

    My proposed solution is

    if hasattr(key, "public_blob") and key.public_blob:
        return key.public_blob.key_type, key.public_blob.key_blob
    else:
        return key.get_name(), key

    The hasattr(key, "public_blob") check isn't enough since it will break the case if the attribute exists but is None which will end in an error accessing key.public_blob.key_type.

    OR

    Add self.public_blob = None to the init method of AgentKey (was removed in f012ebc), but I think the first and last solutions are better, since they removes the assumtion public_blob exist in every instance of PKey which isn't the case.

    OR

    Maybe the public_blob member has to be defined in the base class and set to None as default, since every class (excluding AgentKey) extending PKey is setting it to None by default which I think is the cleanest solution.

  3. Sakoes commented on Nov 20, 2024

    @Sakoes

    Same issue on ubuntu. Any updates regarding a fix?

  4. paulf69487623 commented on Nov 20, 2024

    @paulf69487623
    Author

    The original PR hasn't been updated to include the extra fix this needs, so I opened my own PR in #2475. I've added @johannes85 proposed fix, which makes sense to me.

  5. lgxz commented on Jan 24, 2025

    @lgxz

    +1 for solving this issue, which already has cost me a couple of hours.

    My proposed solution is

    if hasattr(key, "public_blob") and key.public_blob:
    return key.public_blob.key_type, key.public_blob.key_blob
    else:
    return key.get_name(), key
    The hasattr(key, "public_blob") check isn't enough since it will break the case if the attribute exists but is None which will end in an error accessing key.public_blob.key_type.

    OR

    Add self.public_blob = None to the init method of AgentKey (was removed in f012ebc), but I think the first and last solutions are better, since they removes the assumtion public_blob exist in every instance of PKey which isn't the case.

    OR

    Maybe the public_blob member has to be defined in the base class and set to None as default, since every class (excluding AgentKey) extending PKey is setting it to None by default which I think is the cleanest solution.

    +1 for solving this issue, which already has cost me a couple of hours.

    My proposed solution is

    if hasattr(key, "public_blob") and key.public_blob:
    return key.public_blob.key_type, key.public_blob.key_blob
    else:
    return key.get_name(), key
    The hasattr(key, "public_blob") check isn't enough since it will break the case if the attribute exists but is None which will end in an error accessing key.public_blob.key_type.

    OR

    Add self.public_blob = None to the init method of AgentKey (was removed in f012ebc), but I think the first and last solutions are better, since they removes the assumtion public_blob exist in every instance of PKey which isn't the case.

    OR

    Maybe the public_blob member has to be defined in the base class and set to None as default, since every class (excluding AgentKey) extending PKey is setting it to None by default which I think is the cleanest solution.

    Thanks! This solution fixed my problem perfectly.

    I can't understand why it hasn't merged. The problem costs me several hours.

  6. macsir commented on Feb 14, 2025

    @macsir

    Agreed, this fix should be merged.

  7. pikeas commented on May 2, 2025

    @pikeas

    Just ran into this as well, the linked PR resolved the issue. Please merge!

  8. dagbdagb commented on May 20, 2025

    @dagbdagb

    For whoever else needs to find this:

    If you rely on paramiko just trying every key in your ~/.ssh/ directory, current paramiko will fail if it finds an -sk key. (Reading the doc, it appears to also fail if your key is named something else than id_* and is not found via an agent.)

    If this applies to you:
    The cheapest workaround for the public blob exception is to specify exactly what private key to use in the .connect() calll.

    try:
                ssh.connect(hostname=ip_address, username=SSH_USER, timeout=TIMEOUT, key_filename=SSH_KEY)
    except.....
    

    ... where SSH_KEY is the full file path to your key.

  9. bitprophet commented on Oct 17, 2026

    @bitprophet
    Member

    Thanks to everyone reporting variations on this & submitting fixes.

    After digging in a bit, I can reproduce by commenting out the key-class-guessing machinery that normally sets up AgentKey.inner_key. (I personally don't have many cert-bearing keys nor unsupported key types floating around.)

    As implied by comments above and in linked tickets, I'd expect the typical symptom to be use of either unsupported keys (Paramiko itself doesn't need to support the actual key type when the agent is laundering signing requests etc) or bugs in supported-key detection. In either case, the chain of events is:

    • AgentKey instance created from agent key material
    • The key material cannot be mapped to a "real" supported key class, so AgentKey.inner_key remains None instead of a class instance
    • The metaprogramming attribute-laundering between AgentKey, its inner key, and accessors like AuthHandler, eventually goes kaplooey during the auth step that handles certificate-bearing keys.
      • Partly because AgentKey, I think correctly, raises AttributeError on any passthru attr access that would target a None .inner_key - one submitted bugfix makes this return None, which would be technically incorrect (eg, AgentKey().rando_garbage would appear to exist and be None, even though PKeys have no such attr.)

    The proximate cause of this specific issue is that the existing key classes all set self.public_blob = None in their constructors, but AgentKey does not; this is part of a long-term architectural flaw in Paramiko where PKey isn't a "proper" abstract class setting up the full "contract" - subclasses have always just happened to do the needful for .public_blob and other attributes/methods. (To be fair to the OG implementers, Paramiko also far predates anything remotely resembling ABC in the stdlib...)

    I don't have the time/energy right now to do the absolute best fix - overhauling the key hierarchy so this + other incidental bits are properly locked in, which would be good but is a little risky - but the next best fix is to have AgentKey initialize .public_blob correctly. I may also incorporate the "next-next-best" fix others have submitted, which is to make the public blob attribute access in AuthHandler a bit more defensive.

    This should go out in the next Paramiko 5.0.x bugfix release.

  10. added a commit that references this issue on Oct 17, 2026
    cb4a2c4
  11. added a commit that references this issue on Oct 18, 2026
    2111304
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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions