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