2018-04-24 01:59:51 +00:00
|
|
|
import unittest
|
2018-04-25 13:05:01 +00:00
|
|
|
import paramiko
|
2018-04-24 01:59:51 +00:00
|
|
|
|
2018-04-24 02:57:25 +00:00
|
|
|
from tornado.httputil import HTTPServerRequest
|
2018-10-18 10:17:42 +00:00
|
|
|
from tornado.options import options
|
2018-08-24 07:11:21 +00:00
|
|
|
from tests.utils import read_file, make_tests_data_path
|
2019-01-15 15:02:14 +00:00
|
|
|
from webssh import handler
|
2018-10-20 09:26:33 +00:00
|
|
|
from webssh.handler import (
|
2019-01-15 15:02:14 +00:00
|
|
|
MixinHandler, IndexHandler, WsockHandler, InvalidValueError
|
2018-10-20 09:26:33 +00:00
|
|
|
)
|
2018-04-24 01:59:51 +00:00
|
|
|
|
2018-10-15 12:13:11 +00:00
|
|
|
try:
|
|
|
|
from unittest.mock import Mock
|
|
|
|
except ImportError:
|
|
|
|
from mock import Mock
|
|
|
|
|
2018-04-24 01:59:51 +00:00
|
|
|
|
|
|
|
class TestMixinHandler(unittest.TestCase):
|
|
|
|
|
2018-10-15 12:13:11 +00:00
|
|
|
def test_is_forbidden(self):
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler = MixinHandler()
|
2019-01-16 14:58:49 +00:00
|
|
|
handler.redirecting = True
|
2018-10-18 10:17:42 +00:00
|
|
|
options.fbidhttp = True
|
2018-10-15 12:13:11 +00:00
|
|
|
|
2018-10-19 10:18:55 +00:00
|
|
|
context = Mock(
|
2018-10-15 12:13:11 +00:00
|
|
|
address=('8.8.8.8', 8888),
|
|
|
|
trusted_downstream=['127.0.0.1'],
|
|
|
|
_orig_protocol='http'
|
|
|
|
)
|
2019-01-16 14:58:49 +00:00
|
|
|
hostname = '4.4.4.4'
|
|
|
|
self.assertTrue(mhandler.is_forbidden(context, hostname))
|
2018-10-15 12:13:11 +00:00
|
|
|
|
2018-10-19 10:18:55 +00:00
|
|
|
context = Mock(
|
2018-10-15 12:13:11 +00:00
|
|
|
address=('8.8.8.8', 8888),
|
|
|
|
trusted_downstream=[],
|
|
|
|
_orig_protocol='http'
|
|
|
|
)
|
2018-10-21 06:07:44 +00:00
|
|
|
hostname = 'www.google.com'
|
2019-01-15 15:02:14 +00:00
|
|
|
self.assertEqual(mhandler.is_forbidden(context, hostname), False)
|
|
|
|
|
2018-10-19 10:18:55 +00:00
|
|
|
context = Mock(
|
2018-10-15 12:13:11 +00:00
|
|
|
address=('8.8.8.8', 8888),
|
|
|
|
trusted_downstream=[],
|
2019-01-16 14:58:49 +00:00
|
|
|
_orig_protocol='http'
|
2018-10-15 12:13:11 +00:00
|
|
|
)
|
2019-01-16 14:58:49 +00:00
|
|
|
hostname = '4.4.4.4'
|
|
|
|
self.assertTrue(mhandler.is_forbidden(context, hostname))
|
2018-10-21 06:07:44 +00:00
|
|
|
|
|
|
|
context = Mock(
|
2019-01-16 14:58:49 +00:00
|
|
|
address=('192.168.1.1', 8888),
|
2018-10-21 06:07:44 +00:00
|
|
|
trusted_downstream=[],
|
|
|
|
_orig_protocol='http'
|
|
|
|
)
|
2019-01-16 14:58:49 +00:00
|
|
|
hostname = 'www.google.com'
|
|
|
|
self.assertIsNone(mhandler.is_forbidden(context, hostname))
|
2019-01-15 15:02:14 +00:00
|
|
|
|
|
|
|
options.fbidhttp = False
|
|
|
|
self.assertIsNone(mhandler.is_forbidden(context, hostname))
|
2019-01-16 14:58:49 +00:00
|
|
|
|
|
|
|
hostname = '4.4.4.4'
|
|
|
|
self.assertIsNone(mhandler.is_forbidden(context, hostname))
|
|
|
|
|
|
|
|
handler.redirecting = False
|
|
|
|
self.assertIsNone(mhandler.is_forbidden(context, hostname))
|
|
|
|
|
|
|
|
context._orig_protocol = 'https'
|
|
|
|
self.assertIsNone(mhandler.is_forbidden(context, hostname))
|
2018-10-21 06:07:44 +00:00
|
|
|
|
|
|
|
def test_get_redirect_url(self):
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler = MixinHandler()
|
2018-10-21 06:07:44 +00:00
|
|
|
hostname = 'www.example.com'
|
|
|
|
uri = '/'
|
|
|
|
port = 443
|
|
|
|
|
2018-10-21 06:45:58 +00:00
|
|
|
self.assertEqual(
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.get_redirect_url(hostname, port, uri=uri),
|
2018-10-21 06:07:44 +00:00
|
|
|
'https://www.example.com/'
|
|
|
|
)
|
|
|
|
|
|
|
|
port = 4433
|
2018-10-21 06:45:58 +00:00
|
|
|
self.assertEqual(
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.get_redirect_url(hostname, port, uri),
|
2018-10-21 06:07:44 +00:00
|
|
|
'https://www.example.com:4433/'
|
|
|
|
)
|
2018-10-15 12:13:11 +00:00
|
|
|
|
2018-10-18 12:25:30 +00:00
|
|
|
def test_get_client_addr(self):
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler = MixinHandler()
|
2018-10-18 12:25:30 +00:00
|
|
|
client_addr = ('8.8.8.8', 8888)
|
|
|
|
context_addr = ('127.0.0.1', 1234)
|
|
|
|
options.xheaders = True
|
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.context = Mock(address=context_addr)
|
|
|
|
mhandler.get_real_client_addr = lambda: None
|
|
|
|
self.assertEqual(mhandler.get_client_addr(), context_addr)
|
2018-10-18 12:25:30 +00:00
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.context = Mock(address=context_addr)
|
|
|
|
mhandler.get_real_client_addr = lambda: client_addr
|
|
|
|
self.assertEqual(mhandler.get_client_addr(), client_addr)
|
2018-10-18 12:25:30 +00:00
|
|
|
|
|
|
|
options.xheaders = False
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.context = Mock(address=context_addr)
|
|
|
|
mhandler.get_real_client_addr = lambda: client_addr
|
|
|
|
self.assertEqual(mhandler.get_client_addr(), context_addr)
|
2018-10-18 12:25:30 +00:00
|
|
|
|
2018-04-25 11:01:54 +00:00
|
|
|
def test_get_real_client_addr(self):
|
2018-10-10 02:51:40 +00:00
|
|
|
x_forwarded_for = '1.1.1.1'
|
|
|
|
x_forwarded_port = 1111
|
|
|
|
x_real_ip = '2.2.2.2'
|
|
|
|
x_real_port = 2222
|
|
|
|
fake_port = 65535
|
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler = MixinHandler()
|
|
|
|
mhandler.request = HTTPServerRequest(uri='/')
|
|
|
|
mhandler.request.remote_ip = x_forwarded_for
|
2018-10-10 02:51:40 +00:00
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
self.assertIsNone(mhandler.get_real_client_addr())
|
2018-04-24 01:59:51 +00:00
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.request.headers.add('X-Forwarded-For', x_forwarded_for)
|
|
|
|
self.assertEqual(mhandler.get_real_client_addr(),
|
2018-10-10 02:51:40 +00:00
|
|
|
(x_forwarded_for, fake_port))
|
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.request.headers.add('X-Forwarded-Port', fake_port + 1)
|
|
|
|
self.assertEqual(mhandler.get_real_client_addr(),
|
2018-10-10 02:51:40 +00:00
|
|
|
(x_forwarded_for, fake_port))
|
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.request.headers['X-Forwarded-Port'] = x_forwarded_port
|
|
|
|
self.assertEqual(mhandler.get_real_client_addr(),
|
2018-10-10 02:51:40 +00:00
|
|
|
(x_forwarded_for, x_forwarded_port))
|
2018-04-24 02:23:45 +00:00
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.request.remote_ip = x_real_ip
|
2018-04-24 02:23:45 +00:00
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.request.headers.add('X-Real-Ip', x_real_ip)
|
|
|
|
self.assertEqual(mhandler.get_real_client_addr(),
|
2018-10-10 02:51:40 +00:00
|
|
|
(x_real_ip, fake_port))
|
2018-04-24 01:59:51 +00:00
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.request.headers.add('X-Real-Port', fake_port + 1)
|
|
|
|
self.assertEqual(mhandler.get_real_client_addr(),
|
2018-10-10 02:51:40 +00:00
|
|
|
(x_real_ip, fake_port))
|
2018-04-25 11:01:54 +00:00
|
|
|
|
2019-01-15 15:02:14 +00:00
|
|
|
mhandler.request.headers['X-Real-Port'] = x_real_port
|
|
|
|
self.assertEqual(mhandler.get_real_client_addr(),
|
2018-10-10 02:51:40 +00:00
|
|
|
(x_real_ip, x_real_port))
|
2018-04-25 13:05:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestIndexHandler(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_get_specific_pkey_with_plain_key(self):
|
|
|
|
fname = 'test_rsa.key'
|
|
|
|
cls = paramiko.RSAKey
|
2018-08-24 07:11:21 +00:00
|
|
|
key = read_file(make_tests_data_path(fname))
|
2018-10-14 08:42:29 +00:00
|
|
|
|
2018-04-25 13:05:01 +00:00
|
|
|
pkey = IndexHandler.get_specific_pkey(cls, key, None)
|
|
|
|
self.assertIsInstance(pkey, cls)
|
2018-10-14 08:42:29 +00:00
|
|
|
|
2018-08-20 10:35:50 +00:00
|
|
|
pkey = IndexHandler.get_specific_pkey(cls, key, 'iginored')
|
2018-04-25 13:05:01 +00:00
|
|
|
self.assertIsInstance(pkey, cls)
|
2018-10-14 08:42:29 +00:00
|
|
|
|
2018-04-25 13:05:01 +00:00
|
|
|
pkey = IndexHandler.get_specific_pkey(cls, 'x'+key, None)
|
|
|
|
self.assertIsNone(pkey)
|
|
|
|
|
|
|
|
def test_get_specific_pkey_with_encrypted_key(self):
|
|
|
|
fname = 'test_rsa_password.key'
|
|
|
|
cls = paramiko.RSAKey
|
2018-08-20 10:35:50 +00:00
|
|
|
password = 'television'
|
2018-04-25 13:05:01 +00:00
|
|
|
|
2018-08-24 07:11:21 +00:00
|
|
|
key = read_file(make_tests_data_path(fname))
|
2018-04-25 13:05:01 +00:00
|
|
|
pkey = IndexHandler.get_specific_pkey(cls, key, password)
|
|
|
|
self.assertIsInstance(pkey, cls)
|
2018-10-14 08:42:29 +00:00
|
|
|
|
2018-04-25 13:05:01 +00:00
|
|
|
pkey = IndexHandler.get_specific_pkey(cls, 'x'+key, None)
|
|
|
|
self.assertIsNone(pkey)
|
|
|
|
|
2018-08-26 07:13:02 +00:00
|
|
|
with self.assertRaises(paramiko.PasswordRequiredException):
|
2018-04-25 13:05:01 +00:00
|
|
|
pkey = IndexHandler.get_specific_pkey(cls, key, None)
|
|
|
|
|
|
|
|
def test_get_pkey_obj_with_plain_key(self):
|
|
|
|
fname = 'test_ed25519.key'
|
|
|
|
cls = paramiko.Ed25519Key
|
2018-08-24 07:11:21 +00:00
|
|
|
key = read_file(make_tests_data_path(fname))
|
2018-10-14 08:42:29 +00:00
|
|
|
|
2018-08-22 09:37:23 +00:00
|
|
|
pkey = IndexHandler.get_pkey_obj(key, None, fname)
|
2018-04-25 13:05:01 +00:00
|
|
|
self.assertIsInstance(pkey, cls)
|
2018-10-14 08:42:29 +00:00
|
|
|
|
2018-08-22 09:37:23 +00:00
|
|
|
pkey = IndexHandler.get_pkey_obj(key, 'iginored', fname)
|
2018-04-25 13:05:01 +00:00
|
|
|
self.assertIsInstance(pkey, cls)
|
2018-10-14 08:42:29 +00:00
|
|
|
|
|
|
|
with self.assertRaises(InvalidValueError) as ctx:
|
2018-08-22 09:37:23 +00:00
|
|
|
pkey = IndexHandler.get_pkey_obj('x'+key, None, fname)
|
2018-10-14 08:42:29 +00:00
|
|
|
self.assertIn('Invalid private key', str(ctx.exception))
|
2018-04-25 13:05:01 +00:00
|
|
|
|
|
|
|
def test_get_pkey_obj_with_encrypted_key(self):
|
|
|
|
fname = 'test_ed25519_password.key'
|
|
|
|
password = 'abc123'
|
|
|
|
cls = paramiko.Ed25519Key
|
2018-08-24 07:11:21 +00:00
|
|
|
key = read_file(make_tests_data_path(fname))
|
2018-10-14 08:42:29 +00:00
|
|
|
|
2018-08-22 09:37:23 +00:00
|
|
|
pkey = IndexHandler.get_pkey_obj(key, password, fname)
|
2018-04-25 13:05:01 +00:00
|
|
|
self.assertIsInstance(pkey, cls)
|
2018-10-14 08:42:29 +00:00
|
|
|
|
|
|
|
with self.assertRaises(InvalidValueError) as ctx:
|
2018-08-22 09:37:23 +00:00
|
|
|
pkey = IndexHandler.get_pkey_obj(key, 'wrongpass', fname)
|
2018-10-14 08:42:29 +00:00
|
|
|
self.assertIn('Wrong password', str(ctx.exception))
|
|
|
|
|
|
|
|
with self.assertRaises(InvalidValueError) as ctx:
|
|
|
|
pkey = IndexHandler.get_pkey_obj('x'+key, '', fname)
|
|
|
|
self.assertIn('Invalid private key', str(ctx.exception))
|
|
|
|
|
2018-09-12 05:07:05 +00:00
|
|
|
with self.assertRaises(paramiko.PasswordRequiredException):
|
|
|
|
pkey = IndexHandler.get_pkey_obj(key, '', fname)
|
2019-01-10 14:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestWsockHandler(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_check_origin(self):
|
|
|
|
request = HTTPServerRequest(uri='/')
|
|
|
|
obj = Mock(spec=WsockHandler, request=request)
|
|
|
|
|
2019-01-19 08:46:25 +00:00
|
|
|
obj.origin_policy = 'same'
|
2019-01-10 14:09:32 +00:00
|
|
|
request.headers['Host'] = 'www.example.com:4433'
|
|
|
|
origin = 'https://www.example.com:4433'
|
|
|
|
self.assertTrue(WsockHandler.check_origin(obj, origin))
|
|
|
|
|
|
|
|
origin = 'https://www.example.com'
|
|
|
|
self.assertFalse(WsockHandler.check_origin(obj, origin))
|
|
|
|
|
2019-01-19 08:46:25 +00:00
|
|
|
obj.origin_policy = 'primary'
|
2019-01-10 14:09:32 +00:00
|
|
|
self.assertTrue(WsockHandler.check_origin(obj, origin))
|
|
|
|
|
|
|
|
origin = 'https://blog.example.com'
|
|
|
|
self.assertTrue(WsockHandler.check_origin(obj, origin))
|
|
|
|
|
|
|
|
origin = 'https://blog.example.org'
|
|
|
|
self.assertFalse(WsockHandler.check_origin(obj, origin))
|
|
|
|
|
2019-01-19 08:46:25 +00:00
|
|
|
origin = 'https://blog.example.org'
|
|
|
|
obj.origin_policy = {'https://blog.example.org'}
|
|
|
|
self.assertTrue(WsockHandler.check_origin(obj, origin))
|
|
|
|
|
|
|
|
origin = 'http://blog.example.org'
|
|
|
|
obj.origin_policy = {'http://blog.example.org'}
|
|
|
|
self.assertTrue(WsockHandler.check_origin(obj, origin))
|
|
|
|
|
|
|
|
origin = 'http://blog.example.org'
|
|
|
|
obj.origin_policy = {'https://blog.example.org'}
|
|
|
|
self.assertFalse(WsockHandler.check_origin(obj, origin))
|
|
|
|
|
|
|
|
obj.origin_policy = '*'
|
|
|
|
origin = 'https://blog.example.org'
|
2019-01-10 14:09:32 +00:00
|
|
|
self.assertTrue(WsockHandler.check_origin(obj, origin))
|