Skip to content

Commit 9c666f7

Browse files
committed
CVE-2021-4189 More fixes to get tests passing
Various things that need to be fixed to make things work in Python2 land
1 parent 73bb207 commit 9c666f7

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

Lib/ftplib.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,54 @@ class FTP:
109109
welcome = None
110110
passiveserver = 1
111111
encoding = "latin-1"
112-
# Disables https://bugs.python.org/issue43285 security if set to True.
113-
trust_server_pasv_ipv4_address = False
112+
# # Disables https://bugs.python.org/issue43285 security if set to True.
113+
# trust_server_pasv_ipv4_address = False
114114

115115
# Initialization method (called by class instantiation).
116116
# Initialize host to localhost, port to standard ftp port
117117
# Optional arguments are host (for connect()),
118118
# and user, passwd, acct (for login())
119119
def __init__(self, host='', user='', passwd='', acct='',
120-
timeout=_GLOBAL_DEFAULT_TIMEOUT):
120+
timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None):
121+
self.source_address = source_address
122+
# Disables https://bugs.python.org/issue43285 security if set to True.
123+
self.trust_server_pasv_ipv4_address = False
121124
self.timeout = timeout
122125
if host:
123126
self.connect(host)
124127
if user:
125128
self.login(user, passwd, acct)
126129

127-
def connect(self, host='', port=0, timeout=-999):
130+
def __enter__(self):
131+
return self
132+
133+
# Context management protocol: try to quit() if active
134+
def __exit__(self, *args):
135+
if self.sock is not None:
136+
try:
137+
self.quit()
138+
except (OSError, EOFError):
139+
pass
140+
finally:
141+
if self.sock is not None:
142+
self.close()
143+
144+
def connect(self, host='', port=0, timeout=-999, source_address=None):
128145
'''Connect to host. Arguments are:
129146
- host: hostname to connect to (string, default previous host)
130147
- port: port to connect to (integer, default previous port)
148+
- timeout: the timeout to set against the ftp socket(s)
149+
- source_address: a 2-tuple (host, port) for the socket to bind
150+
to as its source address before connecting.
131151
'''
132152
if host != '':
133153
self.host = host
134154
if port > 0:
135155
self.port = port
136156
if timeout != -999:
137157
self.timeout = timeout
158+
if source_address is not None:
159+
self.source_address = source_address
138160
self.sock = socket.create_connection((self.host, self.port), self.timeout)
139161
self.af = self.sock.family
140162
self.file = self.sock.makefile('rb')

Lib/test/test_ftplib.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
except ImportError:
1616
ssl = None
1717

18+
from contextlib import closing
1819
from unittest import TestCase, SkipTest, skipUnless
1920
from test import test_support
2021
from test.test_support import HOST, HOSTv6
@@ -648,7 +649,7 @@ def is_client_connected():
648649

649650
def test_source_address(self):
650651
self.client.quit()
651-
port = support.find_unused_port()
652+
port = test_support.find_unused_port()
652653
try:
653654
self.client.connect(self.server.host, self.server.port,
654655
source_address=(HOST, port))
@@ -660,10 +661,10 @@ def test_source_address(self):
660661
raise
661662

662663
def test_source_address_passive_connection(self):
663-
port = support.find_unused_port()
664+
port = test_support.find_unused_port()
664665
self.client.source_address = (HOST, port)
665666
try:
666-
with self.client.transfercmd('list') as sock:
667+
with closing(self.client.transfercmd('list')) as sock:
667668
self.assertEqual(sock.getsockname()[1], port)
668669
except OSError as e:
669670
if e.errno == errno.EADDRINUSE:

0 commit comments

Comments
 (0)