Skip to content

Commit

Permalink
Fix safe_join to allow joining a base path with an empty string (#336)
Browse files Browse the repository at this point in the history
Regression introduced in 895a068
  • Loading branch information
jdufresne authored and jschneier committed Jun 22, 2017
1 parent e89db45 commit 39a2a7a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
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/')

0 comments on commit 39a2a7a

Please sign in to comment.