1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
|
# Copyright (c) 2016-2022 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
# distribution and is available at:
#
# http://www.eclipse.org/legal/epl-2.0/
#
# This program may also be made available under the following secondary
# licenses when the conditions for such availability set forth in the
# Eclipse Public License v2.0 are satisfied:
#
# GNU General Public License, Version 2.0, or any later versions of
# that license
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
#
# Contributors:
# Ron Frederick - initial implementation, API, and documentation
"""Unit tests for AsyncSSH X11 forwarding"""
import asyncio
import os
import socket
from unittest.mock import patch
import asyncssh
from asyncssh.misc import maybe_wait_closed
from asyncssh.packet import Boolean, String, UInt32
from asyncssh.x11 import XAUTH_FAMILY_IPV4, XAUTH_FAMILY_DECNET
from asyncssh.x11 import XAUTH_FAMILY_IPV6, XAUTH_FAMILY_HOSTNAME
from asyncssh.x11 import XAUTH_FAMILY_WILD, XAUTH_PROTO_COOKIE
from asyncssh.x11 import XAUTH_COOKIE_LEN, X11_BASE_PORT, X11_LISTEN_HOST
from asyncssh.x11 import SSHXAuthorityEntry, SSHX11ClientListener
from asyncssh.x11 import walk_xauth, lookup_xauth, update_xauth
from .server import Server, ServerTestCase
from .util import asynctest
def _failing_bind(self, address):
"""Raise OSError to simulate a socket bind failure"""
# pylint: disable=unused-argument
raise OSError
async def _create_x11_process(conn, command=None, x11_forwarding=True,
x11_display='test:0', **kwargs):
"""Create a client process with X11 forwarding enabled"""
return await conn.create_process(command, x11_forwarding=x11_forwarding,
x11_display=x11_display, **kwargs)
class _X11Peer:
"""Peer representing X server to forward connections to"""
expected_auth = b''
@classmethod
async def connect(cls, session_factory, host, port):
"""Simulate connecting to an X server"""
# pylint: disable=unused-argument
if port == X11_BASE_PORT:
return None, cls()
else:
raise OSError('Connection refused')
def __init__(self):
self._peer = None
self._check_auth = True
def set_peer(self, peer):
"""Set the peer forwarder to exchange data with"""
self._peer = peer
def write(self, data):
"""Consume data from the peer"""
if self._check_auth:
match = data[32:48] == self.expected_auth
self._peer.write(b'\x01' if match else b'\xff')
self._check_auth = False
else:
self._peer.write(data)
def write_eof(self):
"""Consume EOF from the peer"""
def was_eof_received(self):
"""Report that an incoming EOF has not been reeceived"""
# pylint: disable=no-self-use
return False # pragma: no cover
def pause_reading(self):
"""Ignore flow control requests"""
def resume_reading(self):
"""Ignore flow control requests"""
def close(self):
"""Consume close request"""
class _X11ClientListener(SSHX11ClientListener):
"""Unit test X server to forward connections to"""
async def forward_connection(self):
"""Forward a connection to this server"""
self._connect_coro = _X11Peer.connect
return await super().forward_connection()
class _X11ClientChannel(asyncssh.SSHClientChannel):
"""Patched X11 client channel for unit testing"""
async def make_x11_forwarding_request(self, proto, data, screen):
"""Make a request to enable X11 forwarding"""
return await self._make_request(b'x11-req', Boolean(False),
String(proto), String(data),
UInt32(screen))
class _X11ServerConnection(asyncssh.SSHServerConnection):
"""Unit test X11 forwarding server connection"""
async def attach_x11_listener(self, chan, auth_proto, auth_data, screen):
"""Attach a channel to a remote X11 display"""
if screen == 9:
return False
_X11Server.auth_proto = auth_proto
_X11Server.auth_data = auth_data
return await super().attach_x11_listener(chan, auth_proto,
auth_data, screen)
class _X11Server(Server):
"""Server for testing AsyncSSH X11 forwarding"""
auth_proto = b''
auth_data = b''
@staticmethod
def _uint16(value, endian):
"""Encode a 16-bit value using the specified endianness"""
if endian == 'B':
return bytes((value >> 8, value & 255))
else:
return bytes((value & 255, value >> 8))
@staticmethod
def _pad(data):
"""Pad a string to a multiple of 4 bytes"""
length = len(data) % 4
return data + ((4 - length) * b'\00' if length else b'')
async def _open_x11(self, chan, endian, bad):
"""Open an X11 connection back to the client"""
display = chan.get_x11_display()
if display:
dpynum = int(display.rsplit(':')[-1].split('.')[0])
else:
return 2
reader, writer = await asyncio.open_connection(
X11_LISTEN_HOST, X11_BASE_PORT + dpynum)
auth_data = bytearray(self.auth_data)
if bad:
auth_data[-1] ^= 0xff
request = b''.join((endian.encode('ascii'), b'\x00',
self._uint16(11, endian),
self._uint16(0, endian),
self._uint16(len(self.auth_proto), endian),
self._uint16(len(self.auth_data), endian),
b'\x00\x00', self._pad(self.auth_proto),
self._pad(auth_data)))
writer.write(request[:24])
await asyncio.sleep(0.1)
writer.write(request[24:])
result = await reader.read(1)
if result == b'':
result = b'\x02'
if result == b'\x01':
writer.write(b'\x00')
writer.close()
await maybe_wait_closed(writer)
return result[0]
async def _begin_session(self, stdin, _stdout, _stderr):
"""Begin processing a new session"""
action = stdin.channel.get_command()
if action:
if action.startswith('connect '):
endian = action[8:9]
bad = bool(action[9:] == 'X')
result = await self._open_x11(stdin.channel, endian, bad)
stdin.channel.exit(result)
elif action == 'attach':
with patch('socket.socket.bind', _failing_bind):
result = await self._conn.attach_x11_listener(
None, b'', b'', 0)
stdin.channel.exit(bool(result))
elif action == 'open':
try:
result = await self._conn.create_x11_connection(None)
except asyncssh.ChannelOpenError:
result = None
stdin.channel.exit(bool(result))
elif action == 'invalid':
try:
result = await self._conn.create_x11_connection(
None, b'\xff')
except asyncssh.ChannelOpenError:
pass
elif action == 'sleep':
await asyncio.sleep(0.1)
else:
stdin.channel.exit(255)
stdin.channel.close()
await stdin.channel.wait_closed()
def session_requested(self):
return self._begin_session
@patch('asyncssh.connection.SSHServerConnection', _X11ServerConnection)
@patch('asyncssh.x11.SSHX11ClientListener', _X11ClientListener)
class _TestX11(ServerTestCase):
"""Unit tests for AsyncSSH X11 forwarding"""
@classmethod
def setUpClass(cls):
"""Create Xauthority file needed for test"""
super().setUpClass()
auth_data = os.urandom(XAUTH_COOKIE_LEN)
with open('.Xauthority', 'wb') as auth_file:
auth_file.write(bytes(SSHXAuthorityEntry(
XAUTH_FAMILY_HOSTNAME, b'test', b'1',
XAUTH_PROTO_COOKIE, auth_data)))
auth_file.write(bytes(SSHXAuthorityEntry(
XAUTH_FAMILY_HOSTNAME, b'test', b'0',
XAUTH_PROTO_COOKIE, auth_data)))
auth_file.write(bytes(SSHXAuthorityEntry(
XAUTH_FAMILY_IPV4, socket.inet_pton(socket.AF_INET,
'127.0.0.2'), b'0', XAUTH_PROTO_COOKIE, auth_data)))
auth_file.write(bytes(SSHXAuthorityEntry(
XAUTH_FAMILY_IPV4, socket.inet_pton(socket.AF_INET,
'127.0.0.1'), b'0', XAUTH_PROTO_COOKIE, auth_data)))
auth_file.write(bytes(SSHXAuthorityEntry(
XAUTH_FAMILY_IPV6, socket.inet_pton(socket.AF_INET6, '::2'),
b'0', XAUTH_PROTO_COOKIE, auth_data)))
auth_file.write(bytes(SSHXAuthorityEntry(
XAUTH_FAMILY_IPV6, socket.inet_pton(socket.AF_INET6, '::1'),
b'0', XAUTH_PROTO_COOKIE, auth_data)))
# Added to cover case where we don't match on address family
auth_file.write(bytes(SSHXAuthorityEntry(
XAUTH_FAMILY_DECNET, b'test', b'0',
XAUTH_PROTO_COOKIE, auth_data)))
# Wildcard address family match
auth_file.write(bytes(SSHXAuthorityEntry(
XAUTH_FAMILY_WILD, b'', b'0',
XAUTH_PROTO_COOKIE, auth_data)))
with open('.Xauthority-empty', 'wb'):
pass
with open('.Xauthority-corrupted', 'wb') as auth_file:
auth_file.write(b'\x00\x00\x00')
_X11Peer.expected_auth = auth_data
@classmethod
async def start_server(cls):
"""Start an SSH server for the tests to use"""
return (await cls.create_server(
_X11Server, x11_forwarding=True,
authorized_client_keys='authorized_keys'))
async def _check_x11(self, command=None, *, exc=None,
exit_status=None, **kwargs):
"""Check requesting X11 forwarding"""
async with self.connect() as conn:
if exc:
with self.assertRaises(exc):
await _create_x11_process(conn, command, **kwargs)
else:
proc = await _create_x11_process(conn, command, **kwargs)
await proc.wait()
self.assertEqual(proc.exit_status, exit_status)
@asynctest
async def test_xauth_lookup(self):
"""Test writing an xauth entry and looking it back up"""
await update_xauth('xauth', 'test', '0', b'', b'\x00')
_, auth_data = await lookup_xauth(asyncio.get_event_loop(),
'xauth', 'test', '0')
os.unlink('xauth')
self.assertEqual(auth_data, b'\x00')
@asynctest
async def test_xauth_dead_lock(self):
"""Test removal of dead Xauthority lock"""
with open('xauth-c', 'w'):
pass
await asyncio.sleep(6)
await update_xauth('xauth', 'test', '0', b'', b'\x00')
_, auth_data = await lookup_xauth(asyncio.get_event_loop(),
'xauth', 'test', '0')
os.unlink('xauth')
self.assertEqual(auth_data, b'\x00')
@asynctest
async def test_xauth_update(self):
"""Test overwriting an xauth entry"""
await update_xauth('xauth', 'test', '0', b'', b'\x00')
await update_xauth('xauth', 'test', '0', b'', b'\x01')
self.assertEqual(len(list(walk_xauth('xauth'))), 1)
_, auth_data = await lookup_xauth(asyncio.get_event_loop(),
'xauth', 'test', '0')
os.unlink('xauth')
self.assertEqual(auth_data, b'\x01')
@asynctest
async def test_forward_big(self):
"""Test requesting X11 forwarding with big-endian connect"""
await self._check_x11('connect B', exit_status=1,
x11_display='test:0.0',
x11_single_connection=True)
@asynctest
async def test_forward_little(self):
"""Test requesting X11 forwarding with little-endian connect"""
await self._check_x11('connect l', exit_status=1)
@asynctest
async def test_connection_refused_big(self):
"""Test the X server refusing connection with big-endian connect"""
await self._check_x11('connect B', exit_status=2, x11_display='test:1')
@asynctest
async def test_connection_refused_little(self):
"""Test the X server refusing connection with little-endian connect"""
await self._check_x11('connect l', exit_status=2, x11_display='test:1')
@asynctest
async def test_bad_auth_big(self):
"""Test sending bad auth data with big-endian connect"""
await self._check_x11('connect BX', exit_status=0)
@asynctest
async def test_bad_auth_little(self):
"""Test sending bad auth data with little-endian connect"""
await self._check_x11('connect lX', exit_status=0)
@asynctest
async def test_ipv4_address(self):
"""Test matching against an IPv4 address"""
await self._check_x11(x11_display='127.0.0.1:0')
@asynctest
async def test_ipv6_address(self):
"""Test matching against an IPv6 address"""
await self._check_x11(x11_display='[::1]:0')
@asynctest
async def test_wildcard_address(self):
"""Test matching against a wildcard host entry"""
await self._check_x11(x11_display='wild:0')
@asynctest
async def test_local_server(self):
"""Test matching against a local X server"""
await self._check_x11(x11_display=':0')
@asynctest
async def test_domain_socket(self):
"""Test matching against an explicit domain socket"""
await self._check_x11(x11_display='/test:0')
@asynctest
async def test_display_environment(self):
"""Test getting X11 display from the environment"""
os.environ['DISPLAY'] = 'test:0'
await self._check_x11(x11_display=None)
del os.environ['DISPLAY']
@asynctest
async def test_display_not_set(self):
"""Test requesting X11 forwarding with no display set"""
await self._check_x11(exc=asyncssh.ChannelOpenError, x11_display=None)
@asynctest
async def test_forwarding_denied(self):
"""Test SSH server denying X11 forwarding"""
await self._check_x11(exc=asyncssh.ChannelOpenError,
x11_display='test:0.9')
@asynctest
async def test_xauth_environment(self):
"""Test getting Xauthority path from the environment"""
os.environ['XAUTHORITY'] = '.Xauthority'
await self._check_x11()
del os.environ['XAUTHORITY']
@asynctest
async def test_no_xauth_match(self):
"""Test no xauth match"""
await self._check_x11(x11_display='no_match:1')
@asynctest
async def test_invalid_display(self):
"""Test invalid X11 display value"""
await self._check_x11(exc=asyncssh.ChannelOpenError, x11_display='test')
@asynctest
async def test_xauth_missing(self):
"""Test missing .Xauthority file"""
await self._check_x11(x11_auth_path='.Xauthority-missing')
@asynctest
async def test_xauth_empty(self):
"""Test empty .Xauthority file"""
await self._check_x11(x11_auth_path='.Xauthority-empty')
@asynctest
async def test_xauth_corrupted(self):
"""Test .Xauthority file with corrupted entry"""
await self._check_x11(exc=asyncssh.ChannelOpenError,
x11_auth_path='.Xauthority-corrupted')
@asynctest
async def test_selective_forwarding(self):
"""Test requesting X11 forwarding from one session and not another"""
async with self.connect() as conn:
await conn.create_process('sleep')
await _create_x11_process(conn, 'sleep', x11_display='test:0')
@asynctest
async def test_from_connect(self):
"""Test requesting X11 forwarding on connection"""
async with self.connect(x11_forwarding=True,
x11_display='text:0') as conn:
await conn.create_process('sleep')
@asynctest
async def test_multiple_sessions(self):
"""Test requesting X11 forwarding from two different sessions"""
async with self.connect() as conn:
await _create_x11_process(conn)
await _create_x11_process(conn)
@asynctest
async def test_simultaneous_sessions(self):
"""Test X11 forwarding from multiple sessions simultaneously"""
async with self.connect() as conn:
await _create_x11_process(conn, 'sleep')
await _create_x11_process(conn, 'sleep', x11_display='test:0.1')
@asynctest
async def test_consecutive_different_servers(self):
"""Test X11 forwarding to different X servers consecutively"""
async with self.connect() as conn:
proc = await _create_x11_process(conn)
await proc.wait()
await _create_x11_process(conn, x11_display='test1:0')
@asynctest
async def test_simultaneous_different_servers(self):
"""Test X11 forwarding to different X servers simultaneously"""
async with self.connect() as conn:
await _create_x11_process(conn, 'sleep')
with self.assertRaises(asyncssh.ChannelOpenError):
await _create_x11_process(conn, x11_display='test1:0')
@asynctest
async def test_forwarding_disabled(self):
"""Test X11 request when forwarding was never enabled"""
async with self.connect() as conn:
result = await conn.run('connect l')
self.assertEqual(result.exit_status, 2)
@asynctest
async def test_attach_failure(self):
"""Test X11 listener attach when forwarding was never enabled"""
async with self.connect() as conn:
result = await conn.run('attach')
self.assertEqual(result.exit_status, 0)
@asynctest
async def test_attach_lock_failure(self):
"""Test X11 listener attach when Xauthority can't be locked"""
with open('.Xauthority-c', 'w'):
pass
await self._check_x11('connect l', exc=asyncssh.ChannelOpenError)
os.unlink('.Xauthority-c')
@asynctest
async def test_open_failure(self):
"""Test opening X11 connection when forwarding was never enabled"""
async with self.connect() as conn:
result = await conn.run('open')
self.assertEqual(result.exit_status, 0)
@asynctest
async def test_open_invalid_unicode(self):
"""Test opening X11 connection with invalid unicode in original host"""
async with self.connect() as conn:
result = await conn.run('invalid')
self.assertEqual(result.exit_status, None)
@asynctest
async def test_forwarding_not_allowed(self):
"""Test an X11 request from a non-authorized user"""
ckey = asyncssh.read_private_key('ckey')
cert = ckey.generate_user_certificate(ckey, 'name', principals=['ckey'],
permit_x11_forwarding=False)
async with self.connect(username='ckey', client_keys=[(ckey, cert)],
agent_path=None) as conn:
with self.assertRaises(asyncssh.ChannelOpenError):
await _create_x11_process(conn, 'connect l')
@asynctest
async def test_forwarding_ignore_failure(self):
"""Test ignoring failure on an X11 forwarding request"""
ckey = asyncssh.read_private_key('ckey')
cert = ckey.generate_user_certificate(ckey, 'name', principals=['ckey'],
permit_x11_forwarding=False)
async with self.connect(username='ckey', client_keys=[(ckey, cert)],
agent_path=None) as conn:
proc = await _create_x11_process(
conn, x11_forwarding='ignore_failure', x11_display='test')
await proc.wait()
proc = await _create_x11_process(
conn, x11_forwarding='ignore_failure')
await proc.wait()
@asynctest
async def test_invalid_x11_forwarding_request(self):
"""Test an invalid X11 forwarding request"""
with patch('asyncssh.connection.SSHClientChannel', _X11ClientChannel):
async with self.connect() as conn:
stdin, _, _ = await conn.open_session('sleep')
result = await stdin.channel.make_x11_forwarding_request(
'', 'xx', 0)
self.assertFalse(result)
@asynctest
async def test_unknown_action(self):
"""Test unknown action"""
async with self.connect() as conn:
result = await conn.run('unknown')
self.assertEqual(result.exit_status, 255)
|