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

Fix safe_join to allow joining a base path with an empty string #336

Merged
merged 1 commit into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
django-storages change log
==========================

1.6.1 (UNRELEASED)
******************

* Fix regression in ``safe_join()`` to allow joining a base path with an empty
string.

1.6 (2017-06-21)
******************

Expand Down Expand Up @@ -363,4 +369,3 @@ since March 2013.

.. _#89: https://bitbucket.org/david/django-storages/issue/89/112-broke-the-mosso-backend
.. _pull request #5: https://bitbucket.org/david/django-storages/pull-request/5/fixed-path-bug-and-added-testcase-for

6 changes: 3 additions & 3 deletions storages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def safe_join(base, *paths):
"""
base_path = force_text(base)
base_path = base_path.rstrip('/')
paths = [force_text(p) for p in paths]
paths = [base_path + '/'] + [force_text(p) for p in paths if p]

final_path = posixpath.normpath(posixpath.join(base_path + '/', *paths))
final_path = posixpath.normpath(posixpath.join(*paths))
# posixpath.normpath() strips the trailing /. Add it back.
if paths[-1].endswith('/'):
final_path += '/'

# Ensure final_path starts with base_path and that the next character after
# the final path is /.
# the base path is /.
base_path_len = len(base_path)
if (not final_path.startswith(base_path) or final_path[base_path_len] != '/'):
raise ValueError('the joined path is located outside of the base path'
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ def test_datetime_isoformat(self):
dt = datetime.datetime(2017, 5, 19, 14, 45, 37, 123456)
path = utils.safe_join('base_url', dt.isoformat())
self.assertEqual(path, 'base_url/2017-05-19T14:45:37.123456')

def test_join_empty_string(self):
path = utils.safe_join('base_url', '')
self.assertEqual(path, 'base_url/')