Repository navigation
Description
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
Add check for public_blob attr.
johannes85 commentedon Oct 17, 2024
+1 for solving this issue, which already has cost me a couple of hours.
My proposed solution is
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 accessingkey.public_blob.key_type.OR
Add
self.public_blob = Noneto 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.
Update auth_handler.py because of Windows 11 issues
Sakoes commentedon Nov 20, 2024
Same issue on ubuntu. Any updates regarding a fix?
Fix AttributeError(public_blob) paramiko#2462
paulf69487623 commentedon Nov 20, 2024
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.
lgxz commentedon Jan 24, 2025
Thanks! This solution fixed my problem perfectly.
I can't understand why it hasn't merged. The problem costs me several hours.
macsir commentedon Feb 14, 2025
Agreed, this fix should be merged.
Fix AttributeError(public_blob) paramiko#2462
pikeas commentedon May 2, 2025
Just ran into this as well, the linked PR resolved the issue. Please merge!
dagbdagb commentedon May 20, 2025
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.
... where SSH_KEY is the full file path to your key.
bitprophet commentedon Oct 17, 2026
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:
AgentKeyinstance created from agent key materialAgentKey.inner_keyremainsNoneinstead of a class instanceAgentKey, its inner key, and accessors likeAuthHandler, eventually goes kaplooey during the auth step that handles certificate-bearing keys.AgentKey, I think correctly, raisesAttributeErroron any passthru attr access that would target aNone.inner_key- one submitted bugfix makes this returnNone, which would be technically incorrect (eg,AgentKey().rando_garbagewould appear to exist and beNone, even thoughPKeys have no such attr.)The proximate cause of this specific issue is that the existing key classes all set
self.public_blob = Nonein their constructors, butAgentKeydoes not; this is part of a long-term architectural flaw in Paramiko wherePKeyisn't a "proper" abstract class setting up the full "contract" - subclasses have always just happened to do the needful for.public_bloband 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
AgentKeyinitialize.public_blobcorrectly. I may also incorporate the "next-next-best" fix others have submitted, which is to make the public blob attribute access inAuthHandlera bit more defensive.This should go out in the next Paramiko 5.0.x bugfix release.
Fix some issues preventing use of unusual/buggy keys via agent
Fix some issues preventing use of unusual/buggy keys via agent