Skip to content

Commit

Permalink
Merge pull request #2349 from pallets/safe_join-empty
Browse files Browse the repository at this point in the history
`safe_join` keeps `directory=""` as relative path
  • Loading branch information
davidism authored Mar 15, 2022
2 parents be66757 + 7f0c059 commit 365ee7e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Unreleased
- The ``TestResponse.text`` property is a shortcut for
``r.get_data(as_text=True)``, for convenient testing against text
instead of bytes. :pr:`2337`
- ``safe_join`` ensures that the path remains relative if the trusted
directory is the empty string. :pr:`2349`


Version 2.0.3
Expand Down
5 changes: 5 additions & 0 deletions src/werkzeug/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def safe_join(directory: str, *pathnames: str) -> t.Optional[str]:
base directory.
:return: A safe path, otherwise ``None``.
"""
if not directory:
# Ensure we end up with ./path if directory="" is given,
# otherwise the first untrusted part could become trusted.
directory = "."

parts = [directory]

for filename in pathnames:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ def test_safe_join_os_sep():
sec._os_alt_seps = "*"
assert safe_join("foo", "bar/baz*") is None
sec._os_alt_steps = prev_value


def test_safe_join_empty_trusted():
assert safe_join("", "c:test.txt") == "./c:test.txt"

0 comments on commit 365ee7e

Please sign in to comment.