示例#1
0
 def test_no_compute_rpcapi_with_invalid_token(self, mock_validate):
     """Tests that we don't create a ComputeAPI object until we actually
     need to use it to call the internal compute RPC API after token
     validation succeeds. This way, we will not perform expensive object
     creations when we receive unauthenticated (via token) messages. In the
     past, it was possible for unauthenticated requests such as TCP RST or
     requests with invalid tokens to be used to DOS the console proxy
     service.
     """
     # We will simulate a request with an invalid token and verify it
     # will not trigger a ComputeAPI object creation.
     mock_req = mock.MagicMock()
     mock_req.makefile().readline.side_effect = [
         b'GET /vnc.html?token=123-456-789 HTTP/1.1\r\n', b''
     ]
     client_addr = ('8.8.8.8', 54321)
     mock_server = mock.MagicMock()
     handler = websocketproxy.NovaProxyRequestHandler(
         mock_req, client_addr, mock_server)
     # Internal ComputeAPI reference should be None when the request handler
     # is initially created.
     self.assertIsNone(handler._compute_rpcapi)
     # Set up a token validation to fail when the new_websocket_client
     # is called to handle the request.
     mock_validate.side_effect = exception.InvalidToken(token='123-456-789')
     # We expect InvalidToken to be raised during handling.
     self.assertRaises(exception.InvalidToken, handler.new_websocket_client)
     # And our internal ComputeAPI reference should still be None.
     self.assertIsNone(handler._compute_rpcapi)
示例#2
0
    def setUp(self):
        super(NovaWebsocketSecurityProxyTestCase, self).setUp()

        self.flags(allowed_origins=[
            'allowed-origin-example-1.net', 'allowed-origin-example-2.net'
        ],
                   group='console')

        self.server = websocketproxy.NovaWebSocketProxy(
            security_proxy=mock.MagicMock(spec=base.SecurityProxy))

        with mock.patch('websockify.ProxyRequestHandler'):
            self.wh = websocketproxy.NovaProxyRequestHandler()
        self.wh.server = self.server
        self.wh.path = "http://127.0.0.1/?token=123-456-789"
        self.wh.socket = mock.MagicMock()
        self.wh.msg = mock.MagicMock()
        self.wh.do_proxy = mock.MagicMock()
        self.wh.headers = mock.MagicMock()

        def get_header(header):
            if header == 'cookie':
                return 'token="123-456-789"'
            elif header == 'Origin':
                return 'https://example.net:6080'
            elif header == 'Host':
                return 'example.net:6080'
            else:
                return

        self.wh.headers.get = get_header
示例#3
0
    def test_reject_open_redirect(self):
        # This will test the behavior when an attempt is made to cause an open
        # redirect. It should be rejected.
        mock_req = mock.MagicMock()
        mock_req.makefile().readline.side_effect = [
            b'GET //example.com/%2F.. HTTP/1.1\r\n',
            b''
        ]

        # Collect the response data to verify at the end. The
        # SimpleHTTPRequestHandler writes the response data by calling the
        # request socket sendall() method.
        self.data = b''

        def fake_sendall(data):
            self.data += data

        mock_req.sendall.side_effect = fake_sendall

        client_addr = ('8.8.8.8', 54321)
        mock_server = mock.MagicMock()
        # This specifies that the server will be able to handle requests other
        # than only websockets.
        mock_server.only_upgrade = False

        # Constructing a handler will process the mock_req request passed in.
        websocketproxy.NovaProxyRequestHandler(
            mock_req, client_addr, mock_server)

        # Verify no redirect happens and instead a 400 Bad Request is returned.
        self.data = self.data.decode()
        self.assertIn('Error code: 400', self.data)
        self.assertIn('Message: URI must not start with //', self.data)
示例#4
0
    def test_reject_open_redirect(self):
        # This will test the behavior when an attempt is made to cause an open
        # redirect. It should be rejected.
        mock_req = mock.MagicMock()
        mock_req.makefile().readline.side_effect = [
            b'GET //example.com/%2F.. HTTP/1.1\r\n',
            b''
        ]

        client_addr = ('8.8.8.8', 54321)
        mock_server = mock.MagicMock()
        # This specifies that the server will be able to handle requests other
        # than only websockets.
        mock_server.only_upgrade = False

        # Constructing a handler will process the mock_req request passed in.
        handler = websocketproxy.NovaProxyRequestHandler(
            mock_req, client_addr, mock_server)

        # Collect the response data to verify at the end. The
        # SimpleHTTPRequestHandler writes the response data to a 'wfile'
        # attribute.
        output = io.BytesIO()
        handler.wfile = output
        # Process the mock_req again to do the capture.
        handler.do_GET()
        output.seek(0)
        result = output.readlines()

        # Verify no redirect happens and instead a 400 Bad Request is returned.
        self.assertIn('400 URI must not start with //', result[0].decode())
示例#5
0
    def setUp(self):
        super(NovaProxyRequestHandlerDBTestCase, self).setUp()

        self.flags(console_allowed_origins=['allowed-origin-example-1.net',
                                            'allowed-origin-example-2.net'])
        with mock.patch('websockify.ProxyRequestHandler'):
            self.wh = websocketproxy.NovaProxyRequestHandler()
        self.wh.server = websocketproxy.NovaWebSocketProxy()
        self.wh.socket = mock.MagicMock()
        self.wh.msg = mock.MagicMock()
        self.wh.do_proxy = mock.MagicMock()
        self.wh.headers = mock.MagicMock()
示例#6
0
    def test_address_string_doesnt_do_reverse_dns_lookup(self, getfqdn):
        request_mock = mock.MagicMock()
        request_mock.makefile().readline.side_effect = [
            b'GET /vnc.html?token=123-456-789 HTTP/1.1\r\n', b''
        ]
        server_mock = mock.MagicMock()
        client_address = ('8.8.8.8', 54321)

        handler = websocketproxy.NovaProxyRequestHandler(
            request_mock, client_address, server_mock)
        handler.log_message('log message using client address context info')

        self.assertFalse(getfqdn.called)  # no reverse dns look up
        self.assertEqual(handler.address_string(), '8.8.8.8')  # plain address
示例#7
0
    def setUp(self):
        super(NovaProxyRequestHandlerBaseTestCase, self).setUp()

        self.flags(allowed_origins=[
            'allowed-origin-example-1.net', 'allowed-origin-example-2.net'
        ],
                   group='console')
        self.server = websocketproxy.NovaWebSocketProxy()
        with mock.patch('websockify.ProxyRequestHandler'):
            self.wh = websocketproxy.NovaProxyRequestHandler()
        self.wh.server = self.server
        self.wh.socket = mock.MagicMock()
        self.wh.msg = mock.MagicMock()
        self.wh.do_proxy = mock.MagicMock()
        self.wh.headers = mock.MagicMock()
        self.path = urlparse.urlencode({'path': '?token=123-456-789'})
        self.path_invalid = urlparse.urlencode({'path': '?token=XXX'})