Skip to content

Commit 2fa354d

Browse files
committed
Fix handling of "UserKnownHostsFile none" in config
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!
1 parent b77bf85 commit 2fa354d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

asyncssh/connection.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7911,9 +7911,17 @@ def prepare(self, # type: ignore
79117911
rekey_seconds, connect_timeout, login_timeout,
79127912
keepalive_interval, keepalive_count_max)
79137913

7914-
self.known_hosts = known_hosts if known_hosts != () else \
7915-
(cast(List[str], config.get('UserKnownHostsFile', [])) +
7916-
cast(List[str], config.get('GlobalKnownHostsFile', []))) or ()
7914+
if known_hosts != ():
7915+
self.known_hosts = known_hosts
7916+
else:
7917+
user_known_hosts = \
7918+
cast(List[str], config.get('UserKnownHostsFile', ()))
7919+
7920+
if user_known_hosts == []:
7921+
self.known_hosts = None
7922+
else:
7923+
self.known_hosts = list(user_known_hosts) + \
7924+
cast(List[str], config.get('GlobalKnownHostsFile', []))
79177925

79187926
self.host_key_alias = \
79197927
cast(Optional[str], host_key_alias if host_key_alias != () else

tests/test_connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,16 @@ async def test_known_hosts_none(self):
706706
async with self.connect(known_hosts=None) as conn:
707707
self.assertEqual(conn.get_server_host_key_algs(), default_algs)
708708

709+
@asynctest
710+
async def test_known_hosts_none_in_config(self):
711+
"""Test connecting with known hosts checking disabled in config file"""
712+
713+
with open('config', 'w') as f:
714+
f.write('UserKnownHostsFile none')
715+
716+
async with self.connect(config='config'):
717+
pass
718+
709719
@asynctest
710720
async def test_known_hosts_none_without_x509(self):
711721
"""Test connecting with known hosts checking and X.509 disabled"""

0 commit comments

Comments
 (0)