Handling broken entries in known_hosts #490

Closed
martintopholm opened this Issue Feb 23, 2015 · 2 comments

Projects

None yet

2 participants

@martintopholm
Contributor

When broken entries exists in known_hosts, paramiko raises SSHException
with "Invalid key".

echo "example.net ssh-rsa AAAA" >>known_hosts
python2
>>> import paramiko
>>> hostkeys = paramiko.HostKeys()
>>> hostkeys.load('known_hosts')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mph/.local/lib/python2.7/site-packages/paramiko/hostkeys.py", line 99, in load
    e = HostKeyEntry.from_line(line, lineno)
  File "/home/mph/.local/lib/python2.7/site-packages/paramiko/hostkeys.py", line 327, in from_line
    key = RSAKey(data=decodebytes(key))
  File "/home/mph/.local/lib/python2.7/site-packages/paramiko/rsakey.py", line 65, in __init__
    raise SSHException('Invalid key')
paramiko.ssh_exception.SSHException: Invalid key

It would be nice if paramiko would ignore unparsable lines in known_hosts
or provide the file and line in the error message.

@monrad monrad referenced this issue in Juniper/py-junos-eznc Feb 23, 2015
Closed

Invalid host key error not caught #349

@bitprophet
Member

Fair point. Also, OpenSSH's implementation certainly doesn't seem to be as picky, and we generally use them as a guide for behavior. I'd merge a PR that implements this. EDIT: should also see if any have been submitted previously first...

@martintopholm
Contributor

On Fri, 27 Feb 2015, Jeff Forcier wrote:

[...] EDIT: should also see if any have been submitted previously first...

Related pull requests include #404 (Add informative BadHostKeyException).

@bitprophet bitprophet added this to the 1.13.4 / 1.14.3 / 1.15.3 milestone Mar 17, 2015
@bitprophet bitprophet added a commit that closed this issue Sep 30, 2015
@martintopholm @bitprophet martintopholm + bitprophet Silently ignore invalid keys in HostKeys.load()
When broken entries exists in known_hosts, paramiko raises SSHException
with "Invalid key". This patch catches the exception during
HostKeys.load() and continues to next line.

This should fix #490.
669ecbd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment