Skip to content

Commit

Permalink
Fix handling of "UserKnownHostsFile none" in config
Browse files Browse the repository at this point in the history
This commit fixes the handling of "none" in the UserKnownHostsFile
directive in config. Previously, setting this value to none would
usually end up causing the default known host files to be used rather
than disabling known host checking.

Thanks go to GitHub user bigpick for reporting this issue!
  • Loading branch information
ronf committed Oct 13, 2024
1 parent b77bf85 commit 2fa354d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 11 additions & 3 deletions asyncssh/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7911,9 +7911,17 @@ def prepare(self, # type: ignore
rekey_seconds, connect_timeout, login_timeout,
keepalive_interval, keepalive_count_max)

self.known_hosts = known_hosts if known_hosts != () else \
(cast(List[str], config.get('UserKnownHostsFile', [])) +
cast(List[str], config.get('GlobalKnownHostsFile', []))) or ()
if known_hosts != ():
self.known_hosts = known_hosts
else:
user_known_hosts = \
cast(List[str], config.get('UserKnownHostsFile', ()))

if user_known_hosts == []:
self.known_hosts = None
else:
self.known_hosts = list(user_known_hosts) + \
cast(List[str], config.get('GlobalKnownHostsFile', []))

self.host_key_alias = \
cast(Optional[str], host_key_alias if host_key_alias != () else
Expand Down
10 changes: 10 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,16 @@ async def test_known_hosts_none(self):
async with self.connect(known_hosts=None) as conn:
self.assertEqual(conn.get_server_host_key_algs(), default_algs)

@asynctest
async def test_known_hosts_none_in_config(self):
"""Test connecting with known hosts checking disabled in config file"""

with open('config', 'w') as f:
f.write('UserKnownHostsFile none')

async with self.connect(config='config'):
pass

@asynctest
async def test_known_hosts_none_without_x509(self):
"""Test connecting with known hosts checking and X.509 disabled"""
Expand Down

0 comments on commit 2fa354d

Please sign in to comment.