Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added support for FTP_TLS (Weak Cryptography Issue) #1320

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions storages/backends/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
# Usage:
#
# Add below to settings.py:
# FTP_STORAGE_LOCATION = '[a]ftp://<user>:<pass>@<host>:<port>/[path]'
# FTP_STORAGE_LOCATION = '[a]ftp[s]://<user>:<pass>@<host>:<port>/[path]'
#
# In models.py you can write:
# from FTPStorage import FTPStorage
# fs = FTPStorage()
# For a TLS configuration, you must use 'ftps' protocol
# class FTPTest(models.Model):
# file = models.FileField(upload_to='a/b/c/', storage=fs)

Expand Down Expand Up @@ -57,7 +58,7 @@ def _decode_location(self, location):
splitted_url = urlparse(location)
config = {}

if splitted_url.scheme not in ("ftp", "aftp"):
if splitted_url.scheme not in ("ftp", "aftp", "ftps"):
fazledyn-or marked this conversation as resolved.
Show resolved Hide resolved
raise ImproperlyConfigured("FTPStorage works only with FTP protocol!")
if splitted_url.hostname == "":
raise ImproperlyConfigured("You must at least provide hostname!")
Expand All @@ -66,6 +67,12 @@ def _decode_location(self, location):
config["active"] = True
else:
config["active"] = False

if splitted_url.scheme == "ftps":
config["secure"] = True
else:
config["secure"] = False

config["path"] = splitted_url.path
config["host"] = splitted_url.hostname
config["user"] = splitted_url.username
Expand All @@ -84,11 +91,13 @@ def _start_connection(self):

# Real reconnect
if self._connection is None:
ftp = ftplib.FTP()
ftp = ftplib.FTP_TLS() if self._config["secure"] else ftplib.FTP()
ftp.encoding = self.encoding
try:
ftp.connect(self._config["host"], self._config["port"])
ftp.login(self._config["user"], self._config["passwd"])
if self._config["secure"]:
ftp.prot_p()
if self._config["active"]:
ftp.set_pasv(False)
if self._config["path"] != "":
Expand Down
51 changes: 51 additions & 0 deletions tests/test_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
URL = "ftp://{user}:{passwd}@{host}:{port}/".format(
user=USER, passwd=PASSWORD, host=HOST, port=PORT
)
URL_TLS = "ftps://{user}:{passwd}@{host}:{port}/".format(
user=USER, passwd=PASSWORD, host=HOST, port=PORT
)

LIST_FIXTURE = """drwxr-xr-x 2 ftp nogroup 4096 Jul 27 09:46 dir
-rw-r--r-- 1 ftp nogroup 1024 Jul 27 09:45 fi
Expand Down Expand Up @@ -48,6 +51,7 @@ def test_decode_location(self):
"active": False,
"path": "/",
"port": 2121,
"secure": False
}
self.assertEqual(config, wanted_config)
# Test active FTP
Expand All @@ -59,6 +63,7 @@ def test_decode_location(self):
"active": True,
"path": "/",
"port": 2121,
"secure": False
}
self.assertEqual(config, wanted_config)

Expand Down Expand Up @@ -239,3 +244,49 @@ def test_close(self, mock_ftp, mock_storage):
file_.is_dirty = True
file_.read()
file_.close()


class FTPTLSTest(TestCase):
def setUp(self):
self.storage = ftp.FTPStorage(location=URL_TLS)

def test_init_no_location(self):
jschneier marked this conversation as resolved.
Show resolved Hide resolved
with self.assertRaises(ImproperlyConfigured):
ftp.FTPStorage()

@patch("storages.backends.ftp.setting", return_value=URL_TLS)
jschneier marked this conversation as resolved.
Show resolved Hide resolved
def test_init_location_from_setting(self, mock_setting):
storage = ftp.FTPStorage()
self.assertTrue(mock_setting.called)
self.assertEqual(storage.location, URL_TLS)

def test_decode_location(self):
config = self.storage._decode_location(URL_TLS)
jschneier marked this conversation as resolved.
Show resolved Hide resolved
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": "/",
"port": 2121,
"secure": True
}
self.assertEqual(config, wanted_config)

def test_decode_location_error(self):
jschneier marked this conversation as resolved.
Show resolved Hide resolved
with self.assertRaises(ImproperlyConfigured):
self.storage._decode_location("foo")
with self.assertRaises(ImproperlyConfigured):
self.storage._decode_location("http://foo.pt")
# TODO: Cannot not provide a port
# with self.assertRaises(ImproperlyConfigured):
# self.storage._decode_location('ftp://')

@patch("ftplib.FTP_TLS")
def test_start_connection(self, mock_ftp):
jschneier marked this conversation as resolved.
Show resolved Hide resolved
self.storage._start_connection()
self.assertIsNotNone(self.storage._connection)
# Start active
storage = ftp.FTPStorage(location=URL_TLS)
storage._start_connection()