From dfea8a16242e43037025de18acbe1218ac002bb9 Mon Sep 17 00:00:00 2001 From: Kyle Larose Date: Thu, 30 Mar 2023 10:48:52 -0400 Subject: [PATCH] handle invalid characeters in encoding When establishing a connection to an SSH server, WebSSH tries to guess at its encoding using a command sent prior to opening the terminal. Unfortunately, sometimes this can return data which does not form proper input for the `encode` function used by `is_valid_encoding` to test whether the encoding is known. In particular, we ran in to a case where the server was returning a string which had a \0 in it when converted to ascii. That raised a `ValueError` (as opposed to `LookupError`), which was uncaught. Handle the `ValueError` case as well, treating it as an unknown encoding so that we fall back on the default. --- tests/test_handler.py | 25 ++++++++++++++++++++++++- webssh/utils.py | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_handler.py b/tests/test_handler.py index 2f30d9d..a9ad924 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -1,3 +1,4 @@ +import io import unittest import paramiko @@ -7,7 +8,7 @@ from tests.utils import read_file, make_tests_data_path from webssh import handler from webssh import worker from webssh.handler import ( - MixinHandler, WsockHandler, PrivateKey, InvalidValueError + IndexHandler, MixinHandler, WsockHandler, PrivateKey, InvalidValueError, SSHClient ) try: @@ -315,3 +316,25 @@ class TestWsockHandler(unittest.TestCase): obj.worker_ref = ref WsockHandler.on_message(obj, b'{"data": "somestuff"}') obj.close.assert_called_with(reason='Worker closed') + +class TestIndexHandler(unittest.TestCase): + def test_null_in_encoding(self): + handler = Mock(spec=IndexHandler) + + # This is a little nasty, but the index handler has a lot of + # dependencies to mock. Mocking out everything but the bits + # we want to test lets us test this case without needing to + # refactor the relevant code out of IndexHandler + def parse_encoding(data): + return IndexHandler.parse_encoding(handler, data) + handler.parse_encoding = parse_encoding + + ssh = Mock(spec=SSHClient) + stdin = io.BytesIO() + stdout = io.BytesIO(initial_bytes=b"UTF-8\0") + stderr = io.BytesIO() + ssh.exec_command.return_value = (stdin, stdout, stderr) + + encoding = IndexHandler.get_default_encoding(handler, ssh) + self.assertEquals("utf-8", encoding) + diff --git a/webssh/utils.py b/webssh/utils.py index 845ca56..454b1cf 100644 --- a/webssh/utils.py +++ b/webssh/utils.py @@ -59,6 +59,8 @@ def is_valid_encoding(encoding): u'test'.encode(encoding) except LookupError: return False + except ValueError: + return False return True