Skip to content

Commit 6ecb91e

Browse files
committed
Add logging for hostname canonicalization feature
1 parent 743966d commit 6ecb91e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

asyncssh/connection.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,27 @@ async def _canonicalize_host(loop: asyncio.AbstractEventLoop,
281281

282282
host = options.host
283283

284-
if not options.canonicalize_hostname or not options.canonical_domains or \
285-
host.count('.') > options.canonicalize_max_dots:
284+
if not options.canonicalize_hostname or not options.canonical_domains:
285+
logger.info('Host canonicalization disabled')
286+
return None
287+
288+
if host.count('.') > options.canonicalize_max_dots:
289+
logger.info('Host canonicalization skipped due to max dots')
286290
return None
287291

288292
try:
289293
ipaddress.ip_address(host)
290294
except ValueError:
291295
pass
292296
else:
297+
logger.info('Hostname canonicalization skipped on IP address')
293298
return None
294299

300+
logger.debug1('Beginning hostname canonicalization')
301+
295302
for domain in options.canonical_domains:
303+
logger.debug1(' Checking domain %s', domain)
304+
296305
canon_host = f'{host}.{domain}'
297306

298307
try:
@@ -303,18 +312,28 @@ async def _canonicalize_host(loop: asyncio.AbstractEventLoop,
303312

304313
cname = addrinfo[0][3]
305314

306-
if cname:
315+
if cname and cname != canon_host:
316+
logger.debug1(' Checking CNAME rules for hostname %s '
317+
'with CNAME %s', canon_host, cname)
318+
307319
for patterns in options.canonicalize_permitted_cnames:
308320
host_pat, cname_pat = map(WildcardPatternList, patterns)
309321

310322
if host_pat.matches(canon_host) and cname_pat.matches(cname):
323+
logger.info('Hostname canonicalization to CNAME '
324+
'applied: %s -> %s', options.host, cname)
311325
return cname
312326

327+
logger.info('Hostname canonicalization applied: %s -> %s',
328+
options.host, canon_host)
329+
313330
return canon_host
314331

315332
if not options.canonicalize_fallback_local:
333+
logger.info('Hostname canonicalization failed (fallback disabled)')
316334
raise OSError(f'Unable to canonicalize hostname "{host}"')
317335

336+
logger.info('Hostname canonicalization failed, using local resolver')
318337
return None
319338

320339

tests/test_connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,16 @@ async def test_canonicalize(self):
26932693
canonical_domains=['test']) as conn:
26942694
self.assertEqual(conn.get_extra_info('host'), 'testhost.test')
26952695

2696+
@asynctest
2697+
async def test_canonicalize_max_dots(self):
2698+
"""Test hostname canonicalization exceeding max_dots"""
2699+
2700+
async with self.connect('testhost.test', known_hosts=None,
2701+
canonicalize_hostname=True,
2702+
canonicalize_max_dots=0,
2703+
canonical_domains=['test']) as conn:
2704+
self.assertEqual(conn.get_extra_info('host'), 'testhost.test')
2705+
26962706
@asynctest
26972707
async def test_canonicalize_ip_address(self):
26982708
"""Test hostname canonicalization with IP address"""

0 commit comments

Comments
 (0)