Ticket #4428: 4428-bis.patch

File 4428-bis.patch, 4.4 KB (added by alanfranzoni, 6 years ago)
  • twisted/conch/test/test_transport.py

     
    6161        self.unimplementeds = []
    6262        self.debugs = []
    6363        self.ignoreds = []
     64        self.gotUnsupportedVersion = None
    6465
     66    def _unsupportedVersionReceived(self, remoteVersion):
     67        """
     68        Intercept unsupported version call.
    6569
     70        @type remoteVersion: C{str}
     71        """
     72        self.gotUnsupportedVersion = remoteVersion
     73        return transport.SSHTransportBase._unsupportedVersionReceived(self, remoteVersion)
     74
    6675    def receiveError(self, reasonCode, description):
    6776        """
    6877        Store any errors received.
     
    800809        self.assertTrue(proto.gotVersion)
    801810        self.assertEquals(proto.otherVersionString, "SSH-1.99-OpenSSH")
    802811
     812    def test_supportedVersionsAreAllowed(self):
     813        """
     814        Test that versions configured as supported work.
     815        """
     816        proto = MockTransportBase()
     817        proto.supportedVersions = ("9.99", )
     818        proto.makeConnection(proto_helpers.StringTransport())
     819        proto.dataReceived("SSH-9.99-OpenSSH\n")
     820        self.assert_(not proto.gotUnsupportedVersion)
    803821
     822    def test_unsupportedVersionsCallUnsupportedVersionReceived(self):
     823        """
     824        Test that versions not configured as supported don't work.
     825        """
     826        proto = MockTransportBase()
     827        proto.supportedVersions = ("2.0", )
     828        proto.makeConnection(proto_helpers.StringTransport())
     829        proto.dataReceived("SSH-9.99-OpenSSH\n")
     830        self.assertEquals("9.99", proto.gotUnsupportedVersion)
     831
    804832    def test_badPackets(self):
    805833        """
    806834        Test that the transport disconnects with an error when it receives
  • twisted/conch/topfiles/4428.feature

     
     1twisted.conch.ssh.transport.SSHTransportBase now allows supported ssh protocol versions to be overriden.
  • twisted/conch/ssh/transport.py

     
    6767    @ivar supportedLanguages: A list of strings representing languages
    6868        supported, from most-preferred to least.
    6969
     70    @ivar supportedVersions: A container of strings representing supported ssh
     71        protcol version numbers.
     72
    7073    @ivar isClient: A boolean indicating whether this is a client or server.
    7174
    7275    @ivar gotVersion: A boolean indicating whether we have receieved the
     
    149152    supportedPublicKeys = ['ssh-rsa', 'ssh-dss']
    150153    supportedCompressions = ['none', 'zlib']
    151154    supportedLanguages = ()
     155    supportedVersions = ('1.99', '2.0')
    152156    isClient = False
    153157    gotVersion = False
    154158    buf = ''
     
    282286        self.incomingPacketSequence += 1
    283287        return payload
    284288
     289    def _unsupportedVersionReceived(self, remoteVersion):
     290        """
     291        Called when an unsupported version of the ssh protocol is received from
     292            the remote endpoint.
    285293
     294        @param remoteVersion: remote ssh protocol version which is unsupported
     295            by us.
     296        @type remoteVersion: C{str}
     297        """
     298        self.sendDisconnect(DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED,
     299            'bad version ' + remoteVersion)
     300
    286301    def dataReceived(self, data):
    287302        """
    288303        First, check for the version string (SSH-2.0-*).  After that has been
     
    300315                if p.startswith('SSH-'):
    301316                    self.gotVersion = True
    302317                    self.otherVersionString = p.strip()
    303                     if p.split('-')[1] not in ('1.99', '2.0'): # bad version
    304                         self.sendDisconnect(
    305                             DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED,
    306                             'bad version ' + p.split('-')[1])
     318                    remoteVersion = p.split('-')[1]
     319                    if remoteVersion not in self.supportedVersions:
     320                        self._unsupportedVersionReceived(remoteVersion)
    307321                        return
    308322                    i = lines.index(p)
    309323                    self.buf = '\n'.join(lines[i + 1:])