aa3440
diff --git a/paramiko/common.py b/paramiko/common.py
aa3440
index 0b0cc2a..50355f6 100644
aa3440
--- a/paramiko/common.py
aa3440
+++ b/paramiko/common.py
aa3440
@@ -32,6 +32,7 @@ MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE = range(60, 62)
aa3440
 MSG_USERAUTH_GSSAPI_RESPONSE, MSG_USERAUTH_GSSAPI_TOKEN = range(60, 62)
aa3440
 MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, MSG_USERAUTH_GSSAPI_ERROR,\
aa3440
 MSG_USERAUTH_GSSAPI_ERRTOK, MSG_USERAUTH_GSSAPI_MIC = range(63, 67)
aa3440
+HIGHEST_USERAUTH_MESSAGE_ID = 79
aa3440
 MSG_GLOBAL_REQUEST, MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE = range(80, 83)
aa3440
 MSG_CHANNEL_OPEN, MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, \
aa3440
     MSG_CHANNEL_WINDOW_ADJUST, MSG_CHANNEL_DATA, MSG_CHANNEL_EXTENDED_DATA, \
aa3440
diff --git a/paramiko/transport.py b/paramiko/transport.py
aa3440
index 7906c9f..31df82a 100644
aa3440
--- a/paramiko/transport.py
aa3440
+++ b/paramiko/transport.py
aa3440
@@ -49,7 +49,8 @@ from paramiko.common import xffffffff, cMSG_CHANNEL_OPEN, cMSG_IGNORE, \
aa3440
     MSG_CHANNEL_SUCCESS, MSG_CHANNEL_FAILURE, MSG_CHANNEL_DATA, \
aa3440
     MSG_CHANNEL_EXTENDED_DATA, MSG_CHANNEL_WINDOW_ADJUST, MSG_CHANNEL_REQUEST, \
aa3440
     MSG_CHANNEL_EOF, MSG_CHANNEL_CLOSE, MIN_WINDOW_SIZE, MIN_PACKET_SIZE, \
aa3440
-    MAX_WINDOW_SIZE, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_PACKET_SIZE
aa3440
+    MAX_WINDOW_SIZE, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_PACKET_SIZE, \
aa3440
+    HIGHEST_USERAUTH_MESSAGE_ID
aa3440
 from paramiko.compress import ZlibCompressor, ZlibDecompressor
aa3440
 from paramiko.dsskey import DSSKey
aa3440
 from paramiko.kex_gex import KexGex, KexGexSHA256
aa3440
@@ -1720,6 +1721,43 @@ class Transport (threading.Thread, ClosingContextManager):
aa3440
             max_packet_size = self.default_max_packet_size
aa3440
         return clamp_value(MIN_PACKET_SIZE, max_packet_size, MAX_WINDOW_SIZE)
aa3440
 
aa3440
+    def _ensure_authed(self, ptype, message):
aa3440
+        """
aa3440
+        Checks message type against current auth state.
aa3440
+
aa3440
+        If server mode, and auth has not succeeded, and the message is of a
aa3440
+        post-auth type (channel open or global request) an appropriate error
aa3440
+        response Message is crafted and returned to caller for sending.
aa3440
+
aa3440
+        Otherwise (client mode, authed, or pre-auth message) returns None.
aa3440
+        """
aa3440
+        if (
aa3440
+            not self.server_mode
aa3440
+            or ptype <= HIGHEST_USERAUTH_MESSAGE_ID
aa3440
+            or self.is_authenticated()
aa3440
+        ):
aa3440
+            return None
aa3440
+        # WELP. We must be dealing with someone trying to do non-auth things
aa3440
+        # without being authed. Tell them off, based on message class.
aa3440
+        reply = Message()
aa3440
+        # Global requests have no details, just failure.
aa3440
+        if ptype == MSG_GLOBAL_REQUEST:
aa3440
+            reply.add_byte(cMSG_REQUEST_FAILURE)
aa3440
+        # Channel opens let us reject w/ a specific type + message.
aa3440
+        elif ptype == MSG_CHANNEL_OPEN:
aa3440
+            kind = message.get_text()
aa3440
+            chanid = message.get_int()
aa3440
+            reply.add_byte(cMSG_CHANNEL_OPEN_FAILURE)
aa3440
+            reply.add_int(chanid)
aa3440
+            reply.add_int(OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED)
aa3440
+            reply.add_string('')
aa3440
+            reply.add_string('en')
aa3440
+        # NOTE: Post-open channel messages do not need checking; the above will
aa3440
+        # reject attemps to open channels, meaning that even if a malicious
aa3440
+        # user tries to send a MSG_CHANNEL_REQUEST, it will simply fall under
aa3440
+        # the logic that handles unknown channel IDs (as the channel list will
aa3440
+        # be empty.)
aa3440
+        return reply
aa3440
 
aa3440
     def run(self):
aa3440
         # (use the exposed "run" method, because if we specify a thread target
aa3440
@@ -1779,7 +1817,11 @@ class Transport (threading.Thread, ClosingContextManager):
aa3440
                             continue
aa3440
 
aa3440
                     if ptype in self._handler_table:
aa3440
-                        self._handler_table[ptype](self, m)
aa3440
+                        error_msg = self._ensure_authed(ptype, m)
aa3440
+                        if error_msg:
aa3440
+                            self._send_message(error_msg)
aa3440
+                        else:
aa3440
+                            self._handler_table[ptype](self, m)
aa3440
                     elif ptype in self._channel_handler_table:
aa3440
                         chanid = m.get_int()
aa3440
                         chan = self._channels.get(chanid)
aa3440
diff --git a/tests/test_transport.py b/tests/test_transport.py
aa3440
index d81ad8f..1305cd5 100644
aa3440
--- a/tests/test_transport.py
aa3440
+++ b/tests/test_transport.py
aa3440
@@ -32,7 +32,7 @@ from hashlib import sha1
aa3440
 import unittest
aa3440
 
aa3440
 from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey, \
aa3440
-    SSHException, ChannelException, Packetizer
aa3440
+    SSHException, ChannelException, Packetizer, Channel
aa3440
 from paramiko import AUTH_FAILED, AUTH_SUCCESSFUL
aa3440
 from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
aa3440
 from paramiko.common import MSG_KEXINIT, cMSG_CHANNEL_WINDOW_ADJUST, \
aa3440
@@ -87,7 +87,11 @@ class NullServer (ServerInterface):
aa3440
 
aa3440
     def check_global_request(self, kind, msg):
aa3440
         self._global_request = kind
aa3440
-        return False
aa3440
+        # NOTE: for w/e reason, older impl of this returned False always, even
aa3440
+        # tho that's only supposed to occur if the request cannot be served.
aa3440
+        # For now, leaving that the default unless test supplies specific
aa3440
+        # 'acceptable' request kind
aa3440
+        return kind == 'acceptable'
aa3440
 
aa3440
     def check_channel_x11_request(self, channel, single_connection, auth_protocol, auth_cookie, screen_number):
aa3440
         self._x11_single_connection = single_connection
aa3440
@@ -125,7 +129,9 @@ class TransportTest(unittest.TestCase):
aa3440
         self.socks.close()
aa3440
         self.sockc.close()
aa3440
 
aa3440
-    def setup_test_server(self, client_options=None, server_options=None):
aa3440
+    def setup_test_server(
aa3440
+        self, client_options=None, server_options=None, connect_kwargs=None,
aa3440
+    ):
aa3440
         host_key = RSAKey.from_private_key_file(test_path('test_rsa.key'))
aa3440
         public_host_key = RSAKey(data=host_key.asbytes())
aa3440
         self.ts.add_server_key(host_key)
aa3440
@@ -139,8 +145,13 @@ class TransportTest(unittest.TestCase):
aa3440
         self.server = NullServer()
aa3440
         self.assertTrue(not event.is_set())
aa3440
         self.ts.start_server(event, self.server)
aa3440
-        self.tc.connect(hostkey=public_host_key,
aa3440
-                        username='slowdive', password='pygmalion')
aa3440
+        if connect_kwargs is None:
aa3440
+            connect_kwargs = dict(
aa3440
+                hostkey=public_host_key,
aa3440
+                username='slowdive',
aa3440
+                password='pygmalion',
aa3440
+            )
aa3440
+        self.tc.connect(**connect_kwargs)
aa3440
         event.wait(1.0)
aa3440
         self.assertTrue(event.is_set())
aa3440
         self.assertTrue(self.ts.is_active())
aa3440
@@ -846,3 +857,37 @@ class TransportTest(unittest.TestCase):
aa3440
         self.assertEqual([chan], r)
aa3440
         self.assertEqual([], w)
aa3440
         self.assertEqual([], e)
aa3440
+
aa3440
+    def test_server_rejects_open_channel_without_auth(self):
aa3440
+        try:
aa3440
+            self.setup_test_server(connect_kwargs={})
aa3440
+            self.tc.open_session()
aa3440
+        except ChannelException as e:
aa3440
+            assert e.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
aa3440
+        else:
aa3440
+            assert False, "Did not raise ChannelException!"
aa3440
+
aa3440
+    def test_server_rejects_arbitrary_global_request_without_auth(self):
aa3440
+        self.setup_test_server(connect_kwargs={})
aa3440
+        # NOTE: this dummy global request kind would normally pass muster
aa3440
+        # from the test server.
aa3440
+        self.tc.global_request('acceptable')
aa3440
+        # Global requests never raise exceptions, even on failure (not sure why
aa3440
+        # this was the original design...ugh.) Best we can do to tell failure
aa3440
+        # happened is that the client transport's global_response was set back
aa3440
+        # to None; if it had succeeded, it would be the response Message.
aa3440
+        err = "Unauthed global response incorrectly succeeded!"
aa3440
+        assert self.tc.global_response is None, err
aa3440
+
aa3440
+    def test_server_rejects_port_forward_without_auth(self):
aa3440
+        # NOTE: at protocol level port forward requests are treated same as a
aa3440
+        # regular global request, but Paramiko server implements a special-case
aa3440
+        # method for it, so it gets its own test. (plus, THAT actually raises
aa3440
+        # an exception on the client side, unlike the general case...)
aa3440
+        self.setup_test_server(connect_kwargs={})
aa3440
+        try:
aa3440
+            self.tc.request_port_forward('localhost', 1234)
aa3440
+        except SSHException as e:
aa3440
+            assert "forwarding request denied" in str(e)
aa3440
+        else:
aa3440
+            assert False, "Did not raise SSHException!"