Blame SOURCES/00372-CVE-2021-4189.patch

67e492
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
67e492
index 2ff251a..385e432 100644
67e492
--- a/Lib/ftplib.py
67e492
+++ b/Lib/ftplib.py
67e492
@@ -104,6 +104,8 @@ class FTP:
67e492
     welcome = None
67e492
     passiveserver = 1
67e492
     encoding = "latin-1"
67e492
+    # Disables https://bugs.python.org/issue43285 security if set to True.
67e492
+    trust_server_pasv_ipv4_address = False
67e492
 
67e492
     # Initialization method (called by class instantiation).
67e492
     # Initialize host to localhost, port to standard ftp port
67e492
@@ -333,8 +335,13 @@ class FTP:
67e492
         return sock
67e492
 
67e492
     def makepasv(self):
67e492
+        """Internal: Does the PASV or EPSV handshake -> (address, port)"""
67e492
         if self.af == socket.AF_INET:
67e492
-            host, port = parse227(self.sendcmd('PASV'))
67e492
+            untrusted_host, port = parse227(self.sendcmd('PASV'))
67e492
+            if self.trust_server_pasv_ipv4_address:
67e492
+                host = untrusted_host
67e492
+            else:
67e492
+                host = self.sock.getpeername()[0]
67e492
         else:
67e492
             host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
67e492
         return host, port
67e492
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
67e492
index 4ff2f71..3ca7cc1 100644
67e492
--- a/Lib/test/test_ftplib.py
67e492
+++ b/Lib/test/test_ftplib.py
67e492
@@ -94,6 +94,10 @@ class DummyFTPHandler(asynchat.async_chat):
67e492
         self.rest = None
67e492
         self.next_retr_data = RETR_DATA
67e492
         self.push('220 welcome')
67e492
+        # We use this as the string IPv4 address to direct the client
67e492
+        # to in response to a PASV command.  To test security behavior.
67e492
+        # https://bugs.python.org/issue43285/.
67e492
+        self.fake_pasv_server_ip = '252.253.254.255'
67e492
 
67e492
     def collect_incoming_data(self, data):
67e492
         self.in_buffer.append(data)
67e492
@@ -136,7 +140,8 @@ class DummyFTPHandler(asynchat.async_chat):
67e492
             sock.bind((self.socket.getsockname()[0], 0))
67e492
             sock.listen()
67e492
             sock.settimeout(TIMEOUT)
67e492
-            ip, port = sock.getsockname()[:2]
67e492
+            port = sock.getsockname()[1]
67e492
+            ip = self.fake_pasv_server_ip
67e492
             ip = ip.replace('.', ','); p1 = port / 256; p2 = port % 256
67e492
             self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2))
67e492
             conn, addr = sock.accept()
67e492
@@ -694,6 +699,26 @@ class TestFTPClass(TestCase):
67e492
         # IPv4 is in use, just make sure send_epsv has not been used
67e492
         self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv')
67e492
 
67e492
+    def test_makepasv_issue43285_security_disabled(self):
67e492
+        """Test the opt-in to the old vulnerable behavior."""
67e492
+        self.client.trust_server_pasv_ipv4_address = True
67e492
+        bad_host, port = self.client.makepasv()
67e492
+        self.assertEqual(
67e492
+                bad_host, self.server.handler_instance.fake_pasv_server_ip)
67e492
+        # Opening and closing a connection keeps the dummy server happy
67e492
+        # instead of timing out on accept.
67e492
+        socket.create_connection((self.client.sock.getpeername()[0], port),
67e492
+                                 timeout=TIMEOUT).close()
67e492
+
67e492
+    def test_makepasv_issue43285_security_enabled_default(self):
67e492
+        self.assertFalse(self.client.trust_server_pasv_ipv4_address)
67e492
+        trusted_host, port = self.client.makepasv()
67e492
+        self.assertNotEqual(
67e492
+                trusted_host, self.server.handler_instance.fake_pasv_server_ip)
67e492
+        # Opening and closing a connection keeps the dummy server happy
67e492
+        # instead of timing out on accept.
67e492
+        socket.create_connection((trusted_host, port), timeout=TIMEOUT).close()
67e492
+
67e492
     def test_with_statement(self):
67e492
         self.client.quit()
67e492