mirror of https://github.com/huashengdun/webssh
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.pull/340/head
parent
04a9bd5eff
commit
dfea8a1624
|
@ -1,3 +1,4 @@
|
||||||
|
import io
|
||||||
import unittest
|
import unittest
|
||||||
import paramiko
|
import paramiko
|
||||||
|
|
||||||
|
@ -7,7 +8,7 @@ from tests.utils import read_file, make_tests_data_path
|
||||||
from webssh import handler
|
from webssh import handler
|
||||||
from webssh import worker
|
from webssh import worker
|
||||||
from webssh.handler import (
|
from webssh.handler import (
|
||||||
MixinHandler, WsockHandler, PrivateKey, InvalidValueError
|
IndexHandler, MixinHandler, WsockHandler, PrivateKey, InvalidValueError, SSHClient
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -315,3 +316,25 @@ class TestWsockHandler(unittest.TestCase):
|
||||||
obj.worker_ref = ref
|
obj.worker_ref = ref
|
||||||
WsockHandler.on_message(obj, b'{"data": "somestuff"}')
|
WsockHandler.on_message(obj, b'{"data": "somestuff"}')
|
||||||
obj.close.assert_called_with(reason='Worker closed')
|
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)
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,8 @@ def is_valid_encoding(encoding):
|
||||||
u'test'.encode(encoding)
|
u'test'.encode(encoding)
|
||||||
except LookupError:
|
except LookupError:
|
||||||
return False
|
return False
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue