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

45541d
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
45541d
index 6644554..0550f0a 100644
45541d
--- a/Lib/ftplib.py
45541d
+++ b/Lib/ftplib.py
45541d
@@ -108,6 +108,8 @@ class FTP:
45541d
     file = None
45541d
     welcome = None
45541d
     passiveserver = 1
45541d
+    # Disables https://bugs.python.org/issue43285 security if set to True.
45541d
+    trust_server_pasv_ipv4_address = False
45541d
 
45541d
     # Initialization method (called by class instantiation).
45541d
     # Initialize host to localhost, port to standard ftp port
45541d
@@ -310,8 +312,13 @@ class FTP:
45541d
         return sock
45541d
 
45541d
     def makepasv(self):
45541d
+        """Internal: Does the PASV or EPSV handshake -> (address, port)"""
45541d
         if self.af == socket.AF_INET:
45541d
-            host, port = parse227(self.sendcmd('PASV'))
45541d
+            untrusted_host, port = parse227(self.sendcmd('PASV'))
45541d
+            if self.trust_server_pasv_ipv4_address:
45541d
+                host = untrusted_host
45541d
+            else:
45541d
+                host = self.sock.getpeername()[0]
45541d
         else:
45541d
             host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
45541d
         return host, port
45541d
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
45541d
index 8a3eb06..62a3f5e 100644
45541d
--- a/Lib/test/test_ftplib.py
45541d
+++ b/Lib/test/test_ftplib.py
45541d
@@ -67,6 +67,10 @@ class DummyFTPHandler(asynchat.async_chat):
45541d
         self.rest = None
45541d
         self.next_retr_data = RETR_DATA
45541d
         self.push('220 welcome')
45541d
+        # We use this as the string IPv4 address to direct the client
45541d
+        # to in response to a PASV command.  To test security behavior.
45541d
+        # https://bugs.python.org/issue43285/.
45541d
+        self.fake_pasv_server_ip = '252.253.254.255'
45541d
 
45541d
     def collect_incoming_data(self, data):
45541d
         self.in_buffer.append(data)
45541d
@@ -109,7 +113,8 @@ class DummyFTPHandler(asynchat.async_chat):
45541d
         sock.bind((self.socket.getsockname()[0], 0))
45541d
         sock.listen(5)
45541d
         sock.settimeout(10)
45541d
-        ip, port = sock.getsockname()[:2]
45541d
+        port = sock.getsockname()[1]
45541d
+        ip = self.fake_pasv_server_ip
45541d
         ip = ip.replace('.', ',')
45541d
         p1, p2 = divmod(port, 256)
45541d
         self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2))
45541d
@@ -577,6 +582,26 @@ class TestFTPClass(TestCase):
45541d
         # IPv4 is in use, just make sure send_epsv has not been used
45541d
         self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv')
45541d
 
45541d
+    def test_makepasv_issue43285_security_disabled(self):
45541d
+        """Test the opt-in to the old vulnerable behavior."""
45541d
+        self.client.trust_server_pasv_ipv4_address = True
45541d
+        bad_host, port = self.client.makepasv()
45541d
+        self.assertEqual(
45541d
+                bad_host, self.server.handler_instance.fake_pasv_server_ip)
45541d
+        # Opening and closing a connection keeps the dummy server happy
45541d
+        # instead of timing out on accept.
45541d
+        socket.create_connection((self.client.sock.getpeername()[0], port),
45541d
+                                 timeout=TIMEOUT).close()
45541d
+
45541d
+    def test_makepasv_issue43285_security_enabled_default(self):
45541d
+        self.assertFalse(self.client.trust_server_pasv_ipv4_address)
45541d
+        trusted_host, port = self.client.makepasv()
45541d
+        self.assertNotEqual(
45541d
+                trusted_host, self.server.handler_instance.fake_pasv_server_ip)
45541d
+        # Opening and closing a connection keeps the dummy server happy
45541d
+        # instead of timing out on accept.
45541d
+        socket.create_connection((trusted_host, port), timeout=TIMEOUT).close()
45541d
+
45541d
     def test_line_too_long(self):
45541d
         self.assertRaises(ftplib.Error, self.client.sendcmd,
45541d
                           'x' * self.client.maxline * 2)